-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlint.mjs
93 lines (88 loc) · 2.33 KB
/
lint.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
import os from 'node:os';
import path from 'node:path';
import url from 'node:url';
import process from 'node:process';
import childProcess from 'node:child_process';
const projectPath = path.dirname(
path.dirname(url.fileURLToPath(import.meta.url)),
);
const platform = os.platform();
/* eslint-disable no-console */
async function main(argv = process.argv) {
argv = argv.slice(2);
let fix = false;
const restArgs = [];
while (argv.length > 0) {
const option = argv.shift();
if (option === '--fix') {
fix = true;
argv.shift();
} else {
restArgs.push(option);
}
}
// Linting code
const eslintArgs = [
'{src,pages,blog,docs,server,scripts}/**/*.{js,mjs,ts,mts,jsx,tsx,json}',
'docusaurus.config.ts',
];
if (fix) {
eslintArgs.push('--fix');
}
console.error('Running eslint:');
console.error(['eslint', ...eslintArgs].join(' '));
childProcess.execFileSync('eslint', eslintArgs, {
stdio: ['inherit', 'inherit', 'inherit'],
windowsHide: true,
encoding: 'utf-8',
shell: platform === 'win32' ? true : false,
cwd: projectPath,
});
// Linting shell scripts (this does not have auto-fixing)
const shellCheckArgs = [
'./src',
'./scripts',
'-type',
'f',
'-regextype',
'posix-extended',
'-regex',
'.*\\.(sh)',
'-exec',
'shellcheck',
'{}',
'+',
];
console.error('Running shellcheck:');
console.error(['find', ...shellCheckArgs].join(' '));
childProcess.execFileSync('find', shellCheckArgs, {
stdio: ['inherit', 'inherit', 'inherit'],
windowsHide: true,
encoding: 'utf-8',
shell: platform === 'win32' ? true : false,
cwd: projectPath,
});
// Linting markdown
const prettierArgs = [
!fix ? '--check' : '--write',
'./README.md',
'{pages,blog,docs}/**/*.{md,mdx}',
];
console.error('Running prettier:');
console.error(['prettier', ...prettierArgs].join(' '));
childProcess.execFileSync('prettier', prettierArgs, {
stdio: ['inherit', 'inherit', 'inherit'],
windowsHide: true,
encoding: 'utf-8',
shell: platform === 'win32' ? true : false,
cwd: projectPath,
});
}
/* eslint-enable no-console */
if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
void main();
}
}