Skip to content

Commit

Permalink
chore: merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinhui committed Mar 3, 2021
2 parents fa8615b + 15b47af commit 5e51720
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
4 changes: 4 additions & 0 deletions packages/doctor/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# changelog

## 0.4.6
- fix can't catch timeout error
2 changes: 1 addition & 1 deletion packages/doctor/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@iceworks/doctor",
"description": "Analyse and running codemods over react/rax projects, troubleshooting and automatically fixing errors",
"version": "0.4.5",
"version": "0.4.6",
"keywords": [
"doctor",
"analysis",
Expand Down
42 changes: 25 additions & 17 deletions packages/doctor/src/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class Scanner {

// Entry
public async scan(directory: string, options?: IScanOptions): Promise<IScannerReports> {
const timer = new Timer(options?.timeout);
const timer = new Timer();
const reports = {} as IScannerReports;
const tempFileDir = options?.tempFileDir || tempDir;
const subprocessList: any[] = [];
Expand Down Expand Up @@ -67,22 +67,30 @@ export default class Scanner {
});
}

// Check
await Promise.all(subprocessList);
// Set result
await Promise.all(processReportList.map(async (fn) => { await fn(); }));

// Calculate total score
reports.score = getFinalScore(
[
(reports.ESLint || {}).score,
(reports.maintainability || {}).score,
(reports.repeatability || {}).score,
].filter((score) => !isNaN(score)),
);

// Duration seconds
reports.scanTime = timer.duration();
async function process() {
// Check
await Promise.all(subprocessList);
// Set result
await Promise.all(processReportList.map(async (fn) => { await fn(); }));

// Calculate total score
reports.score = getFinalScore(
[
(reports.ESLint || {}).score,
(reports.maintainability || {}).score,
(reports.repeatability || {}).score,
].filter((score) => !isNaN(score)),
);

// Duration seconds
reports.scanTime = timer.duration();
}

if (options.timeout) {
await Promise.race([timer.raceTimeout(options.timeout), process()]);
} else {
await process();
}

return reports;
}
Expand Down
14 changes: 7 additions & 7 deletions packages/doctor/src/Timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ export default class Timer {

protected endTime: undefined | number;

constructor(timeout?: number) {
this.timer = null;
constructor() {
this.startTime = Date.now();
}

if (timeout) {
this.timer = setTimeout(() => {
throw new Error('@iceworks/doctor time out!');
}, timeout);
}
public async raceTimeout(ms: number) {
await new Promise(resolve => {
this.timer = setTimeout(resolve, ms);
});
throw new Error('@iceworks/doctor time out!');
}

public duration(): number {
Expand Down

0 comments on commit 5e51720

Please sign in to comment.