Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit c8f4f6a

Browse files
committed
Add eslint in packing process
1 parent 84991f7 commit c8f4f6a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

scripts/pack.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ optParser.addOption('b', 'binary', 'boolean', 'Pack binary');
2828
optParser.addOption('np', 'no-pseudo', 'boolean', 'Whether use pseudo library');
2929
optParser.addOption('wf', 'with-ffmpeg', 'boolean', 'Whether pack ffmpeg library');
3030
optParser.addOption('h', 'help', 'boolean', 'Show help');
31+
optParser.addOption('li', 'lint', 'boolean', 'Whether lint code with eslint');
3132

3233
const options = optParser.parseArgs(process.argv);
3334

@@ -58,6 +59,26 @@ if (options.help || Object.keys(options).length === 0) {
5859
process.exit(0);
5960
}
6061

62+
if (options.lint) {
63+
// Check lint deps
64+
const lintDeps = ['eslint'];
65+
console.log('Checking lint dependencies...');
66+
const npmRoot = execSync(`npm root -g`).toString().trim();
67+
const missingLintDeps = lintDeps.filter((dep) => {
68+
return !fs.existsSync(path.join(npmRoot, dep));
69+
});
70+
71+
if (missingLintDeps.length === 0) {
72+
console.log('Lint dependencies OK.');
73+
} else {
74+
for (const dep of missingLintDeps) {
75+
console.log('Installing eslint');
76+
execSync(`npm install eslint --global --save-dev`);
77+
execSync('npm init --yes');
78+
}
79+
}
80+
}
81+
6182
var npmInstallOption = '';
6283
if (process.getuid && process.getuid() === 0) {
6384
// Running as root
@@ -237,9 +258,30 @@ function packCommon(target) {
237258
}
238259
}
239260
if (common.files) {
261+
let eslint;
262+
if (options.lint) {
263+
const { ESLint } = require('eslint');
264+
eslint = new ESLint({ overrideConfigFile: `${rootDir}/source/.eslintrc.json` });
265+
}
240266
// Copy common files
241267
for (const file of common.files) {
242268
const filePath = path.join(packSrc, file);
269+
const extname = path.extname(filePath);
270+
if (options.lint && extname === '.js') {
271+
eslint.lintFiles(filePath)
272+
.then((results) => {
273+
eslint.loadFormatter('stylish')
274+
.then((formatter) => {
275+
console.log(formatter.format(results));
276+
})
277+
.catch((err) => {
278+
console.log(err);
279+
})
280+
})
281+
.catch((err) => {
282+
console.log(err);
283+
})
284+
}
243285
execSync(`cp -a ${filePath} ${packDist}`);
244286
}
245287
}

source/.eslintrc.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es2021": true
5+
},
6+
"extends": "eslint:recommended",
7+
"parserOptions": {
8+
"ecmaVersion": 12,
9+
"sourceType": "module"
10+
},
11+
"rules": {
12+
}
13+
}

0 commit comments

Comments
 (0)