Skip to content

Commit deec345

Browse files
fix(pencil): fix lint and add pre-commit restr
1 parent fa1a58a commit deec345

40 files changed

+1208
-352
lines changed

.eslintrc.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = {
2+
env: {
3+
node: true,
4+
commonjs: true,
5+
es2021: true,
6+
"jest/globals": true
7+
},
8+
extends: [
9+
'airbnb-base',
10+
"prettier",
11+
"plugin:node/recommended"
12+
],
13+
plugins: [
14+
"eslint-plugin-prettier",
15+
"jest"
16+
],
17+
parserOptions: {
18+
"ecmaVersion": 2020
19+
},
20+
rules: {
21+
"no-param-reassign": "warn",
22+
'prettier/prettier': 'warn',
23+
"no-console": 0,
24+
eqeqeq: 0,
25+
"max-len": [2, {"code": 168, "ignoreUrls": true}],
26+
"no-await-in-loop": 0,
27+
"no-mixed-operators": 0,
28+
"no-plusplus": ["error", { "allowForLoopAfterthoughts": true }],
29+
"no-continue": 0,
30+
"radix": 0,
31+
"no-underscore-dangle": 0,
32+
"no-restricted-syntax": "warn",
33+
"consistent-return": "warn",
34+
"prefer-destructuring": 0,
35+
},
36+
};

.gitmodules

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[submodule "test_data"]
2-
path = test_data
1+
[submodule "testdata"]
2+
path = testdata
33
url = https://github.com/OHIF/viewer-testdata

.husky/pre-commit

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
. "$(dirname "$0")/_/husky.sh"
33

44
yarn run lint:fix
5+
git add $(git diff --name-only --cached)

package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@
77
"yarn": ">=1.22.4"
88
},
99
"devDependencies": {
10+
"eslint": "^8.9.0",
11+
"eslint-config-airbnb-base": "^15.0.0",
12+
"eslint-config-prettier": "^8.3.0",
13+
"eslint-plugin-import": "^2.25.4",
14+
"eslint-plugin-jest": "^26.1.0",
15+
"eslint-plugin-node": "^11.1.0",
16+
"eslint-plugin-prettier": "^4.0.0",
1017
"jest": "^27.5.1",
1118
"lerna": "^4.0.0",
1219
"must": "^0.13.4",
13-
"prettier": "^2.5.1"
20+
"prettier": "^2.5.1",
21+
"prettier-eslint": "^13.0.0"
1422
},
1523
"dependencies": {
16-
"commander": "^9.0.0",
1724
"husky": "^7.0.4"
1825
},
1926
"workspaces": [
@@ -23,7 +30,8 @@
2330
"test": "lerna run test --parallel --stream",
2431
"build": "lerna run build --stream",
2532
"link:exec": "lerna run link:exec --stream",
26-
"lint": "lerna run lint --parallel --stream -- ./lib/**/*.js ./tests/**/*.js",
27-
"lint:fix": "lerna run lint:fix --parallel --stream -- ./lib/**/*.js ./tests/**/*.js"
33+
"lint": "lerna run lint --parallel --stream -- --color ./lib/**/*.js ./tests/**/*.js",
34+
"lint:fix": "lerna run lint:fix --parallel --stream -- --color ./lib/**/*.js ./tests/**/*.js",
35+
"postinstall": "git config core.hooksPath '.husky' && echo 'Pre-commit configured'"
2836
}
2937
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
3+
extends: [
4+
'../../.eslintrc.js'
5+
],
6+
globals: {
7+
"TEST_DATA_PATH": true,
8+
"OUTPUT_TEMP_PATH": true
9+
},
10+
};

packages/static-wado-creator/lib/index.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
"use strict";
21
const dicomCodec = require("@cornerstonejs/dicom-codec");
32
const { program, Stats } = require("@ohif/static-wado-util");
43
const dicomParser = require("dicom-parser");
4+
const fs = require("fs");
5+
const path = require("path");
6+
const homedir = require("os").homedir();
57
const asyncIterableToBuffer = require("./operation/adapter/asyncIterableToBuffer");
68
const getDataSet = require("./operation/getDataSet");
7-
const JSONWriter = require("./writer/JSONWriter");
89
const InstanceDeduplicate = require("./operation/InstanceDeduplicate");
910
const DeduplicateWriter = require("./writer/DeduplicateWriter");
1011
const ImageFrameWriter = require("./writer/ImageFrameWriter");
1112
const CompleteStudyWriter = require("./writer/CompleteStudyWriter");
1213
const IdCreator = require("./util/IdCreator");
13-
const fs = require("fs");
1414
const dirScanner = require("./reader/dirScanner");
1515
const ScanStudy = require("./operation/ScanStudy");
1616
const HashDataWriter = require("./writer/HashDataWriter");
17-
const JSONReader = require("./reader/JSONReader");
18-
const path = require("path");
19-
const homedir = require("os").homedir();
2017
const VideoWriter = require("./writer/VideoWriter");
2118
const {
2219
transcodeImageFrame,
@@ -150,8 +147,11 @@ class StaticWado {
150147
let bulkDataIndex = 0;
151148
let imageFrameIndex = 0;
152149
const generator = {
153-
bulkdata: async (bulkData) =>
154-
this.callback.bulkdata(targetId, bulkDataIndex++, bulkData),
150+
bulkdata: async (bulkData) => {
151+
const _bulkDataIndex = bulkDataIndex;
152+
bulkDataIndex += 1;
153+
this.callback.bulkdata(targetId, _bulkDataIndex, bulkData);
154+
},
155155
imageFrame: async (originalImageFrame) => {
156156
const { imageFrame, id: transcodedId } = await transcodeImageFrame(
157157
id,
@@ -161,9 +161,12 @@ class StaticWado {
161161
this.options
162162
);
163163

164+
const currentImageFrameIndex = imageFrameIndex;
165+
imageFrameIndex += 1;
166+
164167
return this.callback.imageFrame(
165168
transcodedId,
166-
imageFrameIndex++,
169+
currentImageFrameIndex,
167170
imageFrame
168171
);
169172
},

packages/static-wado-creator/lib/model/TagLists.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const hashFactory = require("node-object-hash");
2+
const Tags = require("../dictionary/Tags");
3+
24
const hasher = hashFactory();
3-
const Tags = require("../../lib/dictionary/Tags");
45

56
const { PatientID, PatientName, IssuerOfPatientID } = Tags;
67
const {

packages/static-wado-creator/lib/operation/InstanceDeduplicate.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
const JSONWriter = require("../../lib/writer/JSONWriter");
2-
const TagLists = require("../../lib/model/TagLists");
3-
const Tags = require("../../lib/dictionary/Tags");
1+
// TODO review returning type of some methods
2+
const JSONWriter = require("../writer/JSONWriter");
3+
const TagLists = require("../model/TagLists");
4+
const Tags = require("../dictionary/Tags");
5+
46
const extractors = {
57
patient: TagLists.PatientQuery,
68
study: TagLists.StudyQuery,
@@ -15,18 +17,18 @@ const extractors = {
1517
* Whenever the study UID changes, a new event is fired to indicate the deduplicated data is ready.
1618
*/
1719
async function deduplicateSingleInstance(id, imageFrame) {
18-
if (!imageFrame) return;
20+
if (!imageFrame) return {};
1921
const studyData = await this.completeStudy.getCurrentStudyData(this, id);
2022
const seriesUID = imageFrame[Tags.SeriesInstanceUID];
2123
const sopUID = imageFrame[Tags.SOPInstanceUID];
2224
if (!sopUID) {
2325
console.warn("No sop instance UID in", imageFrame);
24-
return;
26+
return {};
2527
}
2628
if (studyData.sopExists(sopUID)) {
2729
// console.log('SOP Instance UID', sopUID.Value[0], 'already exists, skipping');
2830
// TODO - allow replace as an option
29-
return;
31+
return {};
3032
}
3133
const deduplicated = { ...imageFrame };
3234

@@ -59,6 +61,7 @@ const canonicalize = (json) => {
5961
Object.keys(json).forEach((tag) => {
6062
if (!tag || tag === "undefined") {
6163
// console.error('Got an undefined tag:', tag, typeof(tag));
64+
// TODO check returning value
6265
return;
6366
}
6467
const val = json[tag];
@@ -71,23 +74,28 @@ const canonicalize = (json) => {
7174
};
7275

7376
const InstanceDeduplicate = (options) =>
74-
async function InstanceDeduplicate(id, imageFrame) {
77+
async function run(id, sourceImageFrame) {
7578
// Notify the existing listeners, if any
76-
imageFrame = canonicalize(imageFrame);
79+
const imageFrame = canonicalize(sourceImageFrame);
7780
if (options.isInstanceMetadata) {
7881
await JSONWriter(id.sopInstanceRootPath, "metadata", imageFrame);
7982
}
8083
if (!options.isDeduplicate && !options.isGroup) {
8184
return;
8285
}
83-
if (!this.deduplicateSingleInstance)
86+
87+
if (!this.deduplicateSingleInstance) {
8488
this.deduplicateSingleInstance = deduplicateSingleInstance;
89+
}
90+
8591
const deduppedInstance = await this.deduplicateSingleInstance(
8692
id,
8793
imageFrame
8894
);
89-
if (!deduppedInstance) return;
90-
await this.deduplicated(id, deduppedInstance);
95+
if (deduppedInstance) {
96+
// this refers to callee
97+
await this.deduplicated(id, deduppedInstance);
98+
}
9199
};
92100

93101
module.exports = InstanceDeduplicate;

packages/static-wado-creator/lib/operation/ScanStudy.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
const path = require("path");
2-
const StudyData = require("./StudyData");
32

43
function ScanStudy(options) {
54
const { directoryName, deduplicatedRoot, deduplicatedInstancesRoot } =
65
options;
76

8-
return async function (dir, studyInstanceUid) {
7+
return async function run(dir, studyInstanceUid) {
98
const studyPath = path.join(directoryName, "studies", studyInstanceUid);
109
const deduplicatedPath = path.join(deduplicatedRoot, studyInstanceUid);
1110
const deduplicatedInstancesPath = path.join(

packages/static-wado-creator/lib/operation/StudyData.js

+28-26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const Tags = require("../../lib/dictionary/Tags");
2-
const TagLists = require("../../lib/model/TagLists");
31
const path = require("path");
42
const fs = require("fs");
5-
const JSONReader = require("../../lib/reader/JSONReader");
6-
const JSONWriter = require("../../lib/writer/JSONWriter");
73
const hashFactory = require("node-object-hash");
4+
const Tags = require("../dictionary/Tags");
5+
const TagLists = require("../model/TagLists");
6+
const JSONReader = require("../reader/JSONReader");
7+
const JSONWriter = require("../writer/JSONWriter");
8+
89
const hasher = hashFactory();
910

1011
const getSeriesInstanceUid = (seriesInstance) =>
@@ -23,19 +24,6 @@ const getSeriesInstanceUid = (seriesInstance) =>
2324
* level data multiple times when it already exists.
2425
*/
2526
class StudyData {
26-
deduplicated = [];
27-
extractData = {};
28-
// The list of already existing files read in to create this object
29-
existingFiles = [];
30-
// The read hashes is the hashes of the files that are read, both as groups and internally
31-
readHashes = {};
32-
// The deduplicated hashes are just the hashes for individual items
33-
deduplicatedHashes = {};
34-
sopInstances = {};
35-
36-
// Used to track if new instances have been added.
37-
newInstancesAdded = 0;
38-
3927
constructor(
4028
{
4129
studyInstanceUid,
@@ -45,6 +33,18 @@ class StudyData {
4533
},
4634
{ isGroup }
4735
) {
36+
this.deduplicated = [];
37+
this.extractData = {};
38+
// The list of already existing files read in to create this object
39+
this.existingFiles = [];
40+
// The read hashes is the hashes of the files that are read, both as groups and internally
41+
this.readHashes = {};
42+
// The deduplicated hashes are just the hashes for individual items
43+
this.deduplicatedHashes = {};
44+
this.sopInstances = {};
45+
46+
// Used to track if new instances have been added.
47+
this.newInstancesAdded = 0;
4848
this.studyInstanceUid = studyInstanceUid;
4949
this.studyPath = studyPath;
5050
this.isGroup = isGroup;
@@ -138,7 +138,7 @@ class StudyData {
138138
console.log("No refs for", deduplicated);
139139
return deduplicated;
140140
}
141-
const ret = Object.assign({}, deduplicated);
141+
const ret = { ...deduplicated };
142142
for (const hashKey of refs.Value) {
143143
const item = await this.getOrLoadExtract(hashKey);
144144
Object.assign(ret, item);
@@ -161,23 +161,23 @@ class StudyData {
161161
if (!this.isGroup) return;
162162
// TODO - check the hash code on the added data, if it has already been seen then ignore this item.
163163
if (this.internalAddDeduplicated(data)) {
164-
this.newInstancesAdded++;
164+
this.newInstancesAdded += 1;
165165
}
166166
}
167167

168168
/** Add the instance if not already present */
169169
internalAddDeduplicated(data, fileName = "internal") {
170170
const hashValue = TagLists.addHash(data, Tags.InstanceType);
171-
if (this.deduplicatedHashes[hashValue]) {
171+
if (!hashValue || this.deduplicatedHashes[hashValue]) {
172172
// console.log('Not adding', hashValue, 'because the hash exists');
173-
return;
173+
return false;
174174
}
175175
const sopUID = data[Tags.SOPInstanceUID];
176176
const sopValue = (sopUID && sopUID.Value && sopUID.Value[0]) || sopUID;
177177
const sopIndex = this.sopInstances[sopValue];
178178
if (!sopValue) {
179179
console.warn("No sop value in ", data);
180-
return;
180+
return false;
181181
}
182182
this.deduplicatedHashes[hashValue] = data;
183183
this.readHashes[hashValue] = fileName;
@@ -188,7 +188,8 @@ class StudyData {
188188
this.sopInstances[sopValue] = this.deduplicated.length;
189189
this.deduplicated.push(data);
190190
}
191-
return hashValue;
191+
192+
return !!hashValue;
192193
}
193194

194195
async listJsonFiles(dir) {
@@ -259,7 +260,7 @@ class StudyData {
259260
const refs = item[Tags.DeduppedRef];
260261
if (refs && refs.Value) {
261262
refs.Value.forEach((hashValue) => {
262-
this.readHashes[hashValue] = hashValue + ".gz";
263+
this.readHashes[hashValue] = `${hashValue}.gz`;
263264
});
264265
}
265266
} else {
@@ -327,7 +328,7 @@ class StudyData {
327328
Value: [instances.length],
328329
};
329330
numberOfInstances += instances.length;
330-
numberOfSeries++;
331+
numberOfSeries += 1;
331332
seriesList.push(seriesQuery);
332333
const modality = seriesQuery[Tags.Modality].Value[0];
333334
if (modalitiesInStudy.indexOf(modality) == -1)
@@ -391,6 +392,7 @@ class StudyData {
391392
return data;
392393
}
393394

395+
/* eslint-disable-next-line class-methods-use-this */
394396
removeGz(name) {
395397
const gzIndex = name.indexOf(".gz");
396398
return (gzIndex > 0 && name.substring(0, gzIndex)) || name;
@@ -410,7 +412,7 @@ class StudyData {
410412
data[Tags.DeduppedRef] = {
411413
vr: "CS",
412414
Value: Object.keys(this.readHashes).filter(
413-
(hash) => this.deduplicatedHashes[hashValue] == undefined
415+
() => this.deduplicatedHashes[hashValue] == undefined
414416
),
415417
};
416418
await JSONWriter(

0 commit comments

Comments
 (0)