@naturalcycles/dev-lib 20.73.1 → 20.74.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/cfg/_cnst.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- export declare const prettierDirs: string[]
2
- export declare const prettierExtensionsAll: string
3
- export declare const eslintExtensions: string
1
+ export declare const eslintDirs: string[]
2
+ export declare const stylelintDirs: string[]
4
3
  export declare const stylelintExtensions: string
5
4
  export declare const lintExclude: string[]
6
5
  export declare const minActionlintVersion: string
package/cfg/_cnst.js CHANGED
@@ -1,17 +1,13 @@
1
- const prettierDirs = ['src,scripts,e2e,docs,cfg,resources,.github,public,static']
2
- // everything that prettier supports:
3
- const prettierExtensionsAll =
4
- 'ts,tsx,cts,mts,css,scss,js,jsx,cjs,mjs,json,jsonc,md,graphql,yml,yaml,html,vue'
5
- const eslintExtensions = 'ts,tsx,html'
1
+ // ESLint config decides which file types to lint inside these dirs.
2
+ // The dirs are needed, because the shared ESLint config enables type-aware parsing only for them.
3
+ const eslintDirs = ['src', 'scripts', 'e2e']
4
+ // stylelint has no file discovery of its own, and it doesn't read .gitignore files.
5
+ // `--ignore-path .gitignore` is not a replacement: it reads only the root .gitignore (not nested ones),
6
+ // and stylelint still walks the ignored dirs (e.g android/ios build output), which is slow.
7
+ // That's why stylelint gets the source dirs explicitly.
8
+ const stylelintDirs = ['src', 'scripts', 'e2e', 'resources', 'public', 'static']
6
9
  const stylelintExtensions = 'css,scss'
7
10
  const lintExclude = ['**/__exclude/**']
8
11
  const minActionlintVersion = '1.7.12'
9
12
 
10
- export {
11
- prettierDirs,
12
- prettierExtensionsAll,
13
- eslintExtensions,
14
- stylelintExtensions,
15
- lintExclude,
16
- minActionlintVersion,
17
- }
13
+ export { eslintDirs, stylelintDirs, stylelintExtensions, lintExclude, minActionlintVersion }
@@ -3,6 +3,21 @@
3
3
  Extendable.
4
4
  */
5
5
 
6
+ import { execSync } from 'node:child_process'
7
+ import fs from 'node:fs'
8
+ import path from 'node:path'
9
+ import { _assert } from '@naturalcycles/js-lib/error/assert.js'
10
+ import { semver2 } from '@naturalcycles/js-lib/semver'
11
+ import { exec2 } from '@naturalcycles/nodejs-lib/exec2'
12
+ import picomatch from 'picomatch'
13
+ import {
14
+ eslintDirs,
15
+ lintExclude,
16
+ minActionlintVersion,
17
+ stylelintDirs,
18
+ stylelintExtensions,
19
+ } from './_cnst.js'
20
+
6
21
  const {
7
22
  platform,
8
23
  arch,
@@ -11,13 +26,7 @@ const {
11
26
 
12
27
  console.log(`lint-staged.config.js runs on node ${node} ${platform} ${arch}`)
13
28
 
14
- import { execSync } from 'node:child_process'
15
- import fs from 'node:fs'
16
- import { _assert } from '@naturalcycles/js-lib/error/assert.js'
17
- import { semver2 } from '@naturalcycles/js-lib/semver'
18
- import { exec2 } from '@naturalcycles/nodejs-lib/exec2'
19
- import picomatch from 'picomatch'
20
- import { prettierExtensionsAll, lintExclude, minActionlintVersion } from './_cnst.js'
29
+ const cwd = process.cwd()
21
30
 
22
31
  const oxfmtConfigPath = ['oxfmt.config.ts'].find(fs.existsSync)
23
32
 
@@ -42,6 +51,7 @@ if (eslintConfigPath) {
42
51
  `--config ${eslintConfigPath}`,
43
52
  '--cache',
44
53
  '--cache-location node_modules/.cache/eslint',
54
+ '--no-warn-ignored',
45
55
  ]
46
56
  .filter(Boolean)
47
57
  .join(' ')
@@ -68,39 +78,36 @@ const stylelintExists =
68
78
  fs.existsSync('node_modules/stylelint-config-standard-scss')
69
79
  const stylelintCmd = stylelintExists ? `stylelint --fix --config ${stylelintConfigPath}` : undefined
70
80
 
81
+ // Same dirs as `dev-lib check` passes to eslint. ESLint config decides the file types.
82
+ const isEslintFile = picomatch(eslintDirs.map(dir => `${dir}/**`))
83
+ // Same files as `dev-lib check` passes to stylelint
84
+ const isStylelintFile = picomatch(`{${stylelintDirs.join(',')}}/**/*.{${stylelintExtensions}}`, {
85
+ dot: true,
86
+ ignore: lintExclude,
87
+ })
88
+ const isActionlintFile = picomatch('.github/**/*.{yml,yaml}')
89
+
90
+ // actionlint, oxlint, eslint, stylelint, oxfmt
91
+ // Run sequentially, in this order: eslint can remove code (e.g unused imports), oxfmt formats last.
92
+ // actionlint runs first, same as in `dev-lib check`. It fails fast, and its error line numbers
93
+ // match the files, which lint-staged restores to the original state on failure (before oxfmt).
94
+ // Sequential order also means actionlint doesn't read a workflow file while oxfmt rewrites it.
95
+ // oxlint and oxfmt receive all staged files. They skip the file types they don't support,
96
+ // and apply ignorePatterns from their own config, same as `dev-lib check` does.
97
+ // This way, lint-staged doesn't need its own list of file extensions, which can get out of sync.
98
+ // Don't run them without a file list (on the whole repo): lint-staged stages every tracked file
99
+ // that tasks have modified, so unstaged changes in other files would end up in the commit.
71
100
  const linters = {
72
- // oxlint, eslint, stylelint, oxfmt
73
- [`./**/*.{${prettierExtensionsAll}}`]: match => runOxlintEslintStylelintOxfmt(match),
101
+ '*': [
102
+ runActionlint,
103
+ oxlintCmd,
104
+ eslintCmd && runEslint,
105
+ stylelintCmd && runStylelint,
106
+ oxfmtCmd,
107
+ ].filter(Boolean),
74
108
 
75
109
  // ktlint
76
110
  '**/*.{kt,kts}': runKtlint,
77
-
78
- './.github/**/*.{yml,yaml}': runActionlint,
79
- }
80
-
81
- export function runOxlintEslintStylelintOxfmt(match) {
82
- const filesList = getFilesList(match)
83
- if (!filesList) return []
84
- const eslintFilesList = eslintCmd && getEslintFilesList(match)
85
-
86
- return [
87
- oxlintCmd && `${oxlintCmd} ${filesList}`,
88
- eslintFilesList && `${eslintCmd} ${eslintFilesList}`,
89
- stylelintCmd && `${stylelintCmd} ${filesList}`,
90
- oxfmtCmd && `${oxfmtCmd} ${filesList}`,
91
- ].filter(Boolean)
92
- }
93
-
94
- export function runOxlintOxfmt(match) {
95
- const filesList = getFilesList(match)
96
- if (!filesList) return []
97
- return [oxlintCmd, oxfmtCmd].filter(Boolean).map(s => `${s} ${filesList}`)
98
- }
99
-
100
- export function runOxfmt(match) {
101
- const filesList = getFilesList(match)
102
- if (!filesList || !oxfmtCmd) return []
103
- return [oxfmtCmd].map(s => `${s} ${filesList}`)
104
111
  }
105
112
 
106
113
  export function runKtlint(match) {
@@ -116,8 +123,12 @@ export function runKtlint(match) {
116
123
  return [`${dir}/resources/ktlint -F ${filesList}`]
117
124
  }
118
125
 
126
+ /**
127
+ * Runs only if a file under .github is staged.
128
+ * actionlint runs without a file list: it checks all workflows, and it never modifies files.
129
+ */
119
130
  export function runActionlint(match) {
120
- if (!match.length) return []
131
+ if (!filterFiles(match, isActionlintFile).length) return []
121
132
 
122
133
  if (!canRunBinary('actionlint')) {
123
134
  console.log(
@@ -130,26 +141,23 @@ export function runActionlint(match) {
130
141
  return ['actionlint']
131
142
  }
132
143
 
133
- export function runActionlintOxfmt(match) {
134
- if (!match.length) return []
135
-
136
- const tools = []
137
-
138
- if (canRunBinary('actionlint')) {
139
- requireActionlintVersion()
140
- // run actionlint on all files at once, as it's fast anyway
141
- tools.push('actionlint')
142
- } else {
143
- console.log(
144
- `actionlint is not installed and won't be run.\nThis is how to install it: https://github.com/rhysd/actionlint/blob/main/docs/install.md`,
145
- )
146
- }
144
+ function runEslint(match) {
145
+ const files = filterFiles(match, isEslintFile)
146
+ if (!files.length) return []
147
+ return [`${eslintCmd} ${files.join(' ')}`]
148
+ }
147
149
 
148
- if (oxfmtCmd) {
149
- tools.push(oxfmtCmd)
150
- }
150
+ function runStylelint(match) {
151
+ const files = filterFiles(match, isStylelintFile)
152
+ if (!files.length) return []
153
+ return [`${stylelintCmd} ${files.join(' ')}`]
154
+ }
151
155
 
152
- return tools
156
+ /**
157
+ * lint-staged passes absolute paths, the matcher expects paths relative to cwd.
158
+ */
159
+ function filterFiles(match, isMatch) {
160
+ return match.filter(f => isMatch(path.relative(cwd, f)))
153
161
  }
154
162
 
155
163
  function getFilesList(match) {
@@ -157,11 +165,6 @@ function getFilesList(match) {
157
165
  return match.filter(f => !isExcluded(f)).join(' ')
158
166
  }
159
167
 
160
- function getEslintFilesList(match) {
161
- const isIncluded = picomatch('./{src,scripts,e2e}/**/*.{ts,tsx,html}')
162
- return match.filter(f => isIncluded(f)).join(' ')
163
- }
164
-
165
168
  function canRunBinary(name) {
166
169
  try {
167
170
  execSync(`which ${name}`)
@@ -20,11 +20,11 @@ export interface CheckOptions {
20
20
  */
21
21
  export declare function runCheck(opt?: CheckOptions): Promise<void>;
22
22
  interface EslintAllOptions {
23
- ext?: string;
24
23
  fix?: boolean;
25
24
  }
26
25
  /**
27
26
  * Runs `eslint` command for all predefined paths (e.g /src, /scripts, etc).
27
+ * File types are decided by the `files` patterns in the ESLint config.
28
28
  *
29
29
  * Returns true if it ran
30
30
  */
@@ -14,7 +14,7 @@ import { dimGrey, white } from '@naturalcycles/nodejs-lib/colors';
14
14
  import { exec2 } from '@naturalcycles/nodejs-lib/exec2';
15
15
  import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
16
16
  import { kpySync } from '@naturalcycles/nodejs-lib/kpy';
17
- import { eslintExtensions, lintExclude, minActionlintVersion, prettierDirs, stylelintExtensions, } from '../cfg/_cnst.js';
17
+ import { eslintDirs, lintExclude, minActionlintVersion, stylelintDirs, stylelintExtensions, } from '../cfg/_cnst.js';
18
18
  import { cfgDir } from './paths.js';
19
19
  const { CI } = process.env;
20
20
  /**
@@ -123,31 +123,27 @@ export async function runCheck(opt = {}) {
123
123
  }
124
124
  /**
125
125
  * Runs `eslint` command for all predefined paths (e.g /src, /scripts, etc).
126
+ * File types are decided by the `files` patterns in the ESLint config.
126
127
  *
127
128
  * Returns true if it ran
128
129
  */
129
130
  export function eslintAll(opt) {
130
131
  const argv = _parseArgs({
131
- ext: {
132
- type: 'string',
133
- default: eslintExtensions,
134
- },
135
132
  fix: {
136
133
  type: 'boolean',
137
134
  default: !CI, // defaults to false in CI, true otherwise
138
135
  },
139
136
  });
140
- const { ext, fix } = {
137
+ const { fix } = {
141
138
  ...argv,
142
139
  ...opt,
143
140
  };
144
- const extensions = ext.split(',');
145
- return runESLint(extensions, fix);
141
+ return runESLint(fix);
146
142
  }
147
143
  /**
148
144
  * Returns true if it ran.
149
145
  */
150
- function runESLint(extensions = eslintExtensions.split(','), fix = true) {
146
+ function runESLint(fix = true) {
151
147
  const eslintConfigPath = `eslint.config.js`;
152
148
  if (!existsSync(eslintConfigPath)) {
153
149
  // faster to bail-out like this
@@ -163,7 +159,7 @@ function runESLint(extensions = eslintExtensions.split(','), fix = true) {
163
159
  args: [
164
160
  '--config',
165
161
  eslintConfigPath,
166
- `{src,scripts,e2e}/**/*.{${extensions.join(',')}}`,
162
+ ...eslintDirs,
167
163
  // `--parser-options=project:${tsconfigPath}`,
168
164
  // The next line fixes the `typescript-eslint` 8.37 bug of resolving tsconfig.json
169
165
  // `--parser-options=tsconfigRootDir:${tsconfigRootDir}`,
@@ -231,7 +227,7 @@ export function hasOxfmtConfig() {
231
227
  }
232
228
  const stylelintPaths = [
233
229
  // Everything inside these folders
234
- `./{${prettierDirs.join(',')}}/**/*.{${stylelintExtensions}}`,
230
+ `./{${stylelintDirs.join(',')}}/**/*.{${stylelintExtensions}}`,
235
231
  // Exclude
236
232
  ...lintExclude.map((s) => `!${s}`),
237
233
  ];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/dev-lib",
3
3
  "type": "module",
4
- "version": "20.73.1",
4
+ "version": "20.74.0",
5
5
  "dependencies": {
6
6
  "prompts": "^2",
7
7
  "@naturalcycles/js-lib": "^15",
package/readme.md CHANGED
@@ -143,9 +143,6 @@ Pass `--no-fix` (or `--fix=false`) to disable the default `--fix` flag on linter
143
143
  linter, or when linter behaves badly and corrupts your files (just happened to me with
144
144
  `eslint-plugin-vue`).
145
145
 
146
- Pass `--ext` (e.g `--ext ts,html`) to override the list of ESLint extensions (default is
147
- `ts,tsx,vue` right now).
148
-
149
146
  For Stylelint to be run, you need to manually install it in the target project:
150
147
 
151
148
  `yarn add -D stylelint stylelint-config-standard-scss`