@naturalcycles/dev-lib 19.29.0 → 19.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lint.util.d.ts +6 -2
- package/dist/lint.util.js +68 -70
- package/package.json +1 -1
package/dist/lint.util.d.ts
CHANGED
|
@@ -11,8 +11,12 @@ interface EslintAllOptions {
|
|
|
11
11
|
* Runs `eslint` command for all predefined paths (e.g /src, /scripts, etc).
|
|
12
12
|
*/
|
|
13
13
|
export declare function eslintAll(opt?: EslintAllOptions): Promise<void>;
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
interface RunPrettierOptions {
|
|
15
|
+
experimentalCli?: boolean;
|
|
16
|
+
fix?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare function runPrettier(opt?: RunPrettierOptions): void;
|
|
19
|
+
export declare function stylelintAll(fix?: boolean): void;
|
|
16
20
|
export declare function lintStagedCommand(): Promise<void>;
|
|
17
21
|
export declare function runCommitlintCommand(): void;
|
|
18
22
|
export declare function requireActionlintVersion(): void;
|
package/dist/lint.util.js
CHANGED
|
@@ -7,7 +7,6 @@ import { _since } from '@naturalcycles/js-lib/datetime/time.util.js';
|
|
|
7
7
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js';
|
|
8
8
|
import { _filterFalsyValues } from '@naturalcycles/js-lib/object/object.util.js';
|
|
9
9
|
import { semver2 } from '@naturalcycles/js-lib/semver';
|
|
10
|
-
import { _truncate } from '@naturalcycles/js-lib/string/string.util.js';
|
|
11
10
|
import { git2 } from '@naturalcycles/nodejs-lib';
|
|
12
11
|
import { boldGrey, dimGrey, grey } from '@naturalcycles/nodejs-lib/colors';
|
|
13
12
|
import { exec2 } from '@naturalcycles/nodejs-lib/exec2';
|
|
@@ -21,52 +20,60 @@ const { CI, ESLINT_CONCURRENCY } = process.env;
|
|
|
21
20
|
*/
|
|
22
21
|
export async function lintAllCommand() {
|
|
23
22
|
const started = Date.now();
|
|
24
|
-
const { commitOnChanges, failOnChanges } = _yargs().options({
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}).argv
|
|
34
|
-
const needToTrackChanges = commitOnChanges || failOnChanges
|
|
35
|
-
const gitStatusAtStart = gitStatus()
|
|
36
|
-
if (needToTrackChanges && gitStatusAtStart) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
23
|
+
// const { commitOnChanges, failOnChanges } = _yargs().options({
|
|
24
|
+
// commitOnChanges: {
|
|
25
|
+
// type: 'boolean',
|
|
26
|
+
// default: false,
|
|
27
|
+
// },
|
|
28
|
+
// failOnChanges: {
|
|
29
|
+
// type: 'boolean',
|
|
30
|
+
// default: false,
|
|
31
|
+
// },
|
|
32
|
+
// }).argv
|
|
33
|
+
// const needToTrackChanges = commitOnChanges || failOnChanges
|
|
34
|
+
// const gitStatusAtStart = gitStatus()
|
|
35
|
+
// if (needToTrackChanges && gitStatusAtStart) {
|
|
36
|
+
// console.log('lint-all: git shows changes before run:')
|
|
37
|
+
// console.log(gitStatusAtStart)
|
|
38
|
+
// }
|
|
39
|
+
// In CI, Github Actions doesn't allow us to push and then run CI on changes again
|
|
40
|
+
// (known limitation / "infinite run loop" prevention)
|
|
41
|
+
// That's why we run in "no-fix" mode in CI, and "fix" mode locally
|
|
42
|
+
const fix = !CI;
|
|
40
43
|
// Fast linters (that run in <1 second) go first
|
|
41
44
|
runActionLint();
|
|
42
|
-
runBiome();
|
|
45
|
+
runBiome(fix);
|
|
43
46
|
// From this point we start the "slow" linters, with ESLint leading the way
|
|
44
47
|
// We run eslint BEFORE Prettier, because eslint can delete e.g unused imports.
|
|
45
|
-
await eslintAll(
|
|
48
|
+
await eslintAll({
|
|
49
|
+
fix,
|
|
50
|
+
});
|
|
46
51
|
if (existsSync(`node_modules/stylelint`) &&
|
|
47
52
|
existsSync(`node_modules/stylelint-config-standard-scss`)) {
|
|
48
|
-
stylelintAll();
|
|
53
|
+
stylelintAll(fix);
|
|
49
54
|
}
|
|
50
|
-
runPrettier();
|
|
51
|
-
await runKTLint();
|
|
55
|
+
runPrettier({ fix });
|
|
56
|
+
await runKTLint(fix);
|
|
52
57
|
console.log(`${boldGrey('lint-all')} ${dimGrey(`took ` + _since(started))}`);
|
|
53
|
-
if (needToTrackChanges) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
58
|
+
// if (needToTrackChanges) {
|
|
59
|
+
// const gitStatusAfter = gitStatus()
|
|
60
|
+
// const hasChanges = gitStatusAfter !== gitStatusAtStart
|
|
61
|
+
// if (!hasChanges) return
|
|
62
|
+
// const msg =
|
|
63
|
+
// 'style(ci): ' + _truncate(git2.commitMessageToTitleMessage(git2.getLastGitCommitMsg()), 60)
|
|
64
|
+
//
|
|
65
|
+
// // pull, commit, push changes
|
|
66
|
+
// git2.pull()
|
|
67
|
+
// git2.commitAll(msg)
|
|
68
|
+
// git2.push()
|
|
69
|
+
//
|
|
70
|
+
// // fail on changes
|
|
71
|
+
// if (failOnChanges) {
|
|
72
|
+
// console.log(gitStatusAfter)
|
|
73
|
+
// console.log('lint-all failOnChanges: exiting with status 1')
|
|
74
|
+
// process.exitCode = 1
|
|
75
|
+
// }
|
|
76
|
+
// }
|
|
70
77
|
}
|
|
71
78
|
/**
|
|
72
79
|
* Runs `eslint` command for all predefined paths (e.g /src, /scripts, etc).
|
|
@@ -80,7 +87,7 @@ export async function eslintAll(opt) {
|
|
|
80
87
|
},
|
|
81
88
|
fix: {
|
|
82
89
|
type: 'boolean',
|
|
83
|
-
default: true
|
|
90
|
+
default: !CI, // defaults to false in CI, true otherwise
|
|
84
91
|
},
|
|
85
92
|
});
|
|
86
93
|
const { ext, fix } = {
|
|
@@ -88,23 +95,19 @@ export async function eslintAll(opt) {
|
|
|
88
95
|
...opt,
|
|
89
96
|
};
|
|
90
97
|
const extensions = ext.split(',');
|
|
91
|
-
|
|
98
|
+
// The only time we don't parallelize is when run locally with no-fix,
|
|
99
|
+
// so errors can be seen properly
|
|
100
|
+
const runInParallel = fix || !!CI;
|
|
101
|
+
if (runInParallel) {
|
|
92
102
|
await Promise.all([
|
|
93
|
-
// /src
|
|
94
103
|
runESLint(`src`, extensions, fix),
|
|
95
|
-
// /scripts
|
|
96
104
|
runESLint(`scripts`, extensions, fix),
|
|
97
|
-
// /e2e
|
|
98
105
|
runESLint(`e2e`, extensions, fix),
|
|
99
106
|
]);
|
|
100
107
|
}
|
|
101
108
|
else {
|
|
102
|
-
// with no-fix - let's run serially
|
|
103
|
-
// /src
|
|
104
109
|
await runESLint(`src`, extensions, fix);
|
|
105
|
-
// /scripts
|
|
106
110
|
await runESLint(`scripts`, extensions, fix);
|
|
107
|
-
// /e2e
|
|
108
111
|
await runESLint(`e2e`, extensions, fix);
|
|
109
112
|
}
|
|
110
113
|
console.log(`${boldGrey('eslint-all')} ${dimGrey(`took ` + _since(started))}`);
|
|
@@ -138,7 +141,7 @@ async function runESLint(dir, extensions = eslintExtensions.split(','), fix = tr
|
|
|
138
141
|
cacheLocation,
|
|
139
142
|
`--no-error-on-unmatched-pattern`,
|
|
140
143
|
`--report-unused-disable-directives`, // todo: unnecessary with flat, as it's defined in the config
|
|
141
|
-
fix ? `--fix` : '',
|
|
144
|
+
fix ? `--fix` : '--no-fix',
|
|
142
145
|
].filter(_isTruthy),
|
|
143
146
|
shell: false,
|
|
144
147
|
env: _filterFalsyValues({
|
|
@@ -155,7 +158,8 @@ const prettierPaths = [
|
|
|
155
158
|
// Exclude
|
|
156
159
|
...lintExclude.map((s) => `!${s}`),
|
|
157
160
|
];
|
|
158
|
-
export function runPrettier(
|
|
161
|
+
export function runPrettier(opt = {}) {
|
|
162
|
+
const { experimentalCli = true, fix = true } = opt;
|
|
159
163
|
const prettierConfigPath = [`./prettier.config.js`].find(f => existsSync(f));
|
|
160
164
|
if (!prettierConfigPath)
|
|
161
165
|
return;
|
|
@@ -166,7 +170,7 @@ export function runPrettier(experimentalCli = true) {
|
|
|
166
170
|
// prettier --write 'src/**/*.{js,ts,css,scss,graphql}'
|
|
167
171
|
exec2.spawn(prettierPath, {
|
|
168
172
|
args: [
|
|
169
|
-
`--write
|
|
173
|
+
fix ? `--write` : '--check',
|
|
170
174
|
`--log-level=warn`,
|
|
171
175
|
'--cache-location',
|
|
172
176
|
cacheLocation,
|
|
@@ -184,13 +188,14 @@ const stylelintPaths = [
|
|
|
184
188
|
// Exclude
|
|
185
189
|
...lintExclude.map((s) => `!${s}`),
|
|
186
190
|
];
|
|
187
|
-
export function stylelintAll() {
|
|
188
|
-
const
|
|
191
|
+
export function stylelintAll(fix) {
|
|
192
|
+
const argv = _yargs().options({
|
|
189
193
|
fix: {
|
|
190
194
|
type: 'boolean',
|
|
191
|
-
default: true
|
|
195
|
+
default: !CI, // defaults to false in CI, true otherwise
|
|
192
196
|
},
|
|
193
197
|
}).argv;
|
|
198
|
+
fix ??= argv.fix;
|
|
194
199
|
const config = [`./stylelint.config.js`].find(f => existsSync(f));
|
|
195
200
|
if (!config)
|
|
196
201
|
return;
|
|
@@ -229,12 +234,12 @@ export function runCommitlintCommand() {
|
|
|
229
234
|
log: false,
|
|
230
235
|
});
|
|
231
236
|
}
|
|
232
|
-
async function runKTLint() {
|
|
237
|
+
async function runKTLint(fix = true) {
|
|
233
238
|
if (!existsSync(`node_modules/@naturalcycles/ktlint`))
|
|
234
239
|
return;
|
|
235
240
|
// @ts-expect-error ktlint is not installed (due to size in node_modules), but it's ok
|
|
236
241
|
const { ktlintAll } = await import('@naturalcycles/ktlint');
|
|
237
|
-
await ktlintAll();
|
|
242
|
+
await ktlintAll(fix ? ['-F'] : []);
|
|
238
243
|
}
|
|
239
244
|
function runActionLint() {
|
|
240
245
|
// Only run if there is a folder of `.github/workflows`, otherwise actionlint will fail
|
|
@@ -261,12 +266,6 @@ export function getActionLintVersion() {
|
|
|
261
266
|
return exec2.exec('actionlint --version').split('\n')[0];
|
|
262
267
|
}
|
|
263
268
|
export function runBiome(fix = true) {
|
|
264
|
-
// if (!fs.existsSync(`node_modules/@biomejs/biome`)) {
|
|
265
|
-
// if (verbose) {
|
|
266
|
-
// console.log(`biome is not installed (checked in node_modules/@biomejs/biome), skipping`)
|
|
267
|
-
// }
|
|
268
|
-
// return
|
|
269
|
-
// }
|
|
270
269
|
const configPath = `biome.jsonc`;
|
|
271
270
|
if (!existsSync(configPath)) {
|
|
272
271
|
console.log(`biome is skipped, because ./biome.jsonc is not present`);
|
|
@@ -289,14 +288,13 @@ function canRunBinary(name) {
|
|
|
289
288
|
return false;
|
|
290
289
|
}
|
|
291
290
|
}
|
|
292
|
-
function gitStatus() {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}
|
|
291
|
+
// function gitStatus(): string | undefined {
|
|
292
|
+
// try {
|
|
293
|
+
// return execSync('git status -s', {
|
|
294
|
+
// encoding: 'utf8',
|
|
295
|
+
// })
|
|
296
|
+
// } catch {}
|
|
297
|
+
// }
|
|
300
298
|
const require = createRequire(import.meta.url);
|
|
301
299
|
export function findPackageBinPath(pkg, cmd) {
|
|
302
300
|
const packageJsonPath = require.resolve(`${pkg}/package.json`);
|