Skip to content

Commit

Permalink
Feat doctor add timeout (#660)
Browse files Browse the repository at this point in the history
* feat: add timeout

* feat: add scan time
  • Loading branch information
fyangstudio authored Nov 20, 2020
1 parent d3eaa6a commit aee6813
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/doctor/bin/iceworks-doctor
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ if (args.scan) {
maintainability: result.maintainability.score,
repeatability: result.repeatability.score,
});
console.log(`Scan time: ${result.scanTime} s.`);
}).catch((err) => {
console.log('Error!');
console.error(err);
Expand Down
3 changes: 2 additions & 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 react/rax projects, troubleshooting and automatically fixing errors",
"version": "0.2.2",
"version": "0.2.3",
"keywords": [
"doctor",
"analysis",
Expand Down Expand Up @@ -34,6 +34,7 @@
"glob": "^7.1.6",
"ignore": "^5.1.8",
"jscpd": "^3.3.3",
"moment": "^2.29.1",
"stylelint": "^13.7.0",
"typescript": "^3.0.0",
"typhonjs-escomplex": "^0.1.0"
Expand Down
10 changes: 7 additions & 3 deletions packages/doctor/src/Scanner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from 'path';
import Timer from './Timer';
import { IScannerOptions, IScanOptions, IFileInfo, IScannerReports } from './types/Scanner';
import getCustomESLintConfig from './getCustomESLintConfig';
import getEslintReports from './getEslintReports';
Expand All @@ -7,7 +8,6 @@ import getRepeatabilityReports from './getRepeatabilityReports';
import getFiles from './getFiles';
import getFinalScore from './getFinalScore';


export default class Scanner {
public options: IScannerOptions;

Expand All @@ -17,6 +17,7 @@ export default class Scanner {

// Entry
public async scan(directory: string, options?: IScanOptions): Promise<IScannerReports> {
const timer = new Timer(options.timeout);
const reports = {} as IScannerReports;

const files: IFileInfo[] = getFiles(directory, this.options.supportExts, this.options.ignore);
Expand All @@ -37,12 +38,12 @@ export default class Scanner {
if (!options || options.disableESLint !== true) {
// Example: react react-ts rax rax-ts
const ruleKey = `${options.framework || 'react'}${options.languageType === 'ts' ? '-ts' : ''}`;
reports.ESLint = getEslintReports(files, ruleKey, getCustomESLintConfig(directory), options && options.fix);
reports.ESLint = getEslintReports(timer, files, ruleKey, getCustomESLintConfig(directory), options && options.fix);
}

// Calculate maintainability
if (!options || options.disableMaintainability !== true) {
reports.maintainability = getMaintainabilityReports(files);
reports.maintainability = getMaintainabilityReports(timer,files);
}

// Calculate repeatability
Expand All @@ -59,6 +60,9 @@ export default class Scanner {
].filter((score) => !isNaN(score)),
);

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

return reports;
}
}
23 changes: 23 additions & 0 deletions packages/doctor/src/Timer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const moment = require('moment');

export default class Timer {
protected startTime: number;
protected endTime: undefined | number;

constructor(timeout?: number) {
this.startTime = Date.now();
if (timeout) {
this.endTime = this.startTime + timeout;
}
}

public checkTimeout(): void {
if (this.endTime && Date.now() > this.endTime) {
throw new Error('@iceworks/doctor time out!');
}
}

public duration(): number {
return moment.duration(Date.now() - this.startTime).asSeconds();
}
}
5 changes: 4 additions & 1 deletion packages/doctor/src/getEslintReports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CLIEngine } from 'eslint';
import { deepmerge, getESLintConfig } from '@iceworks/spec';
import Scorer from './Scorer';
import Timer from './Timer';
import { IFileInfo, IEslintReports } from './types/Scanner';

// level waring minus 1 point
Expand All @@ -10,7 +11,7 @@ const ERROR_WEIGHT = 3;
// bonus add 2 point
const BONUS_WEIGHT = 2;

export default function getEslintReports(files: IFileInfo[], ruleKey: string, customConfig?: any, fix?: boolean): IEslintReports {
export default function getEslintReports(timer: Timer, files: IFileInfo[], ruleKey: string, customConfig?: any, fix?: boolean): IEslintReports {
let warningScore = 0;
let warningCount = 0;

Expand Down Expand Up @@ -48,6 +49,8 @@ export default function getEslintReports(files: IFileInfo[], ruleKey: string, cu
...result,
filePath: file.path,
});

timer.checkTimeout();
});
});

Expand Down
5 changes: 4 additions & 1 deletion packages/doctor/src/getMaintainabilityReports.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as escomplex from 'typhonjs-escomplex';
import Scorer from './Scorer';
import Timer from './Timer';
import { IFileInfo, IMaintainabilityReport, IMaintainabilityReports } from './types/Scanner';

// https://www.npmjs.com/package/typhonjs-escomplex
export default function getMaintainabilityReports(files: IFileInfo[]): IMaintainabilityReports {
export default function getMaintainabilityReports(timer: Timer, files: IFileInfo[]): IMaintainabilityReports {
const reports = [];

files.forEach((file) => {
Expand All @@ -19,6 +20,8 @@ export default function getMaintainabilityReports(files: IFileInfo[]): IMaintain
} catch (e) {
// ignore
}

timer.checkTimeout();
});

return {
Expand Down
2 changes: 2 additions & 0 deletions packages/doctor/src/types/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface IScanOptions {
framework?: string;
languageType?: 'js' | 'ts';
tempFileDir?: string;
timeout?: number;
disableESLint?: boolean;
disableMaintainability?: boolean;
disableRepeatability?: boolean;
Expand Down Expand Up @@ -63,6 +64,7 @@ export interface IScannerReports {
lines: number;
};
score?: number;
scanTime?: number;
ESLint?: IEslintReports;
maintainability?: IMaintainabilityReports;
repeatability?: IRepeatabilityReports;
Expand Down

0 comments on commit aee6813

Please sign in to comment.