@nera-static/stan 1.0.1 → 1.1.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/CHANGELOG.md +16 -0
- package/README.md +147 -15
- package/canonical/editorconfig +12 -0
- package/canonical/github/FUNDING.yml +13 -0
- package/canonical/github/dependabot.yml +20 -0
- package/canonical/github/workflows/publish.yml +64 -0
- package/canonical/github/workflows/test.yml +37 -0
- package/canonical/husky/pre-push +1 -0
- package/canonical/variants/chokidar-major/github/dependabot.yml +25 -0
- package/canonical/variants/desktop-release/github/workflows/publish.yml +104 -0
- package/canonical/variants/dist-check/github/workflows/test.yml +43 -0
- package/canonical/variants/image-cache/github/workflows/publish.yml +73 -0
- package/canonical/variants/image-cache/github/workflows/test.yml +46 -0
- package/canonical/variants/validate-templates/github/workflows/publish.yml +64 -0
- package/canonical/variants/validate-templates/github/workflows/test.yml +40 -0
- package/canonical/variants/windows-smoke/github/workflows/test.yml +57 -0
- package/canonical/vitest.config.js +3 -0
- package/package.json +24 -12
- package/src/bem.js +35 -0
- package/src/checks/canonical.js +368 -0
- package/src/checks/css.js +131 -0
- package/src/checks/docs.js +405 -0
- package/src/checks/package.js +352 -0
- package/src/checks/pug.js +369 -0
- package/src/checks/tree.js +544 -0
- package/src/cli.js +49 -39
- package/src/eslint/check.js +64 -0
- package/src/eslint/config.js +45 -28
- package/src/eslint/files.js +37 -0
- package/src/markdown.js +60 -0
- package/src/repo-files.js +62 -1
- package/src/settings.js +136 -0
- package/src/stylelint/config.js +122 -0
- package/src/stylelint/index.js +15 -0
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
|
|
5
|
+
import { satisfiesFloor } from '@nera-static/plugin-utils/guard'
|
|
6
|
+
import { isPlainObject } from '@nera-static/plugin-utils/values'
|
|
7
|
+
import * as espree from 'espree'
|
|
8
|
+
|
|
9
|
+
import { PLUGIN_UTILS } from '../helper-names.js'
|
|
10
|
+
import { readPackage } from '../kind.js'
|
|
11
|
+
import { OUTSIDE_REPO, isPublished, publishedPatterns, writeInside } from '../repo-files.js'
|
|
12
|
+
|
|
13
|
+
/** The prefix of every structure rule id (D4). */
|
|
14
|
+
export const STRUCTURE_PREFIX = 'structure/'
|
|
15
|
+
|
|
16
|
+
/** The rule ids the `package.json` check reports. */
|
|
17
|
+
export const PACKAGE_RULES = Object.freeze({
|
|
18
|
+
keyOrder: `${STRUCTURE_PREFIX}package-key-order`,
|
|
19
|
+
engines: `${STRUCTURE_PREFIX}engines`,
|
|
20
|
+
pluginUtilsRange: `${STRUCTURE_PREFIX}plugin-utils-range`,
|
|
21
|
+
changelogInFiles: `${STRUCTURE_PREFIX}changelog-in-files`,
|
|
22
|
+
npmPkgFix: `${STRUCTURE_PREFIX}npm-pkg-fix`,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The order of the top-level `package.json` keys, one for every repo kind: the skeleton
|
|
27
|
+
* of 2-plugin-anatomy §4.4, extended by `nera` (after `engines`, as the themes have it)
|
|
28
|
+
* and `optionalDependencies` (after `dependencies`). Any other key follows these, in
|
|
29
|
+
* the order the file has it.
|
|
30
|
+
*/
|
|
31
|
+
export const KEY_ORDER = Object.freeze([
|
|
32
|
+
'name',
|
|
33
|
+
'version',
|
|
34
|
+
'private',
|
|
35
|
+
'description',
|
|
36
|
+
'main',
|
|
37
|
+
'type',
|
|
38
|
+
'exports',
|
|
39
|
+
'bin',
|
|
40
|
+
'scripts',
|
|
41
|
+
'files',
|
|
42
|
+
'keywords',
|
|
43
|
+
'author',
|
|
44
|
+
'license',
|
|
45
|
+
'repository',
|
|
46
|
+
'bugs',
|
|
47
|
+
'homepage',
|
|
48
|
+
'publishConfig',
|
|
49
|
+
'sideEffects',
|
|
50
|
+
'engines',
|
|
51
|
+
'nera',
|
|
52
|
+
'peerDependencies',
|
|
53
|
+
'dependencies',
|
|
54
|
+
'optionalDependencies',
|
|
55
|
+
'devDependencies',
|
|
56
|
+
])
|
|
57
|
+
|
|
58
|
+
/** The allowed `engines.node` values: the core floor, and the floor ESLint 10 and stylelint 17 need. */
|
|
59
|
+
export const ENGINES = Object.freeze(['>=22.12.0', '^22.13.0 || >=24'])
|
|
60
|
+
|
|
61
|
+
/** The fields that can name a `@nera-static/plugin-utils` range. */
|
|
62
|
+
const DEPENDENCY_FIELDS = Object.freeze(['dependencies', 'peerDependencies', 'optionalDependencies', 'devDependencies'])
|
|
63
|
+
|
|
64
|
+
const FILE = 'package.json'
|
|
65
|
+
const CHANGELOG = 'CHANGELOG.md'
|
|
66
|
+
const CARET_RANGE = /^\^(\d+)\.\d+\.\d+$/
|
|
67
|
+
const RELATIVE_PREFIX = /^(?:\.\/)+/
|
|
68
|
+
/** A `repository.url` scheme `npm pkg fix` turns into its `git+` form. */
|
|
69
|
+
const GIT_PLUS_SCHEMES = /^(?:https?|ssh):\/\//
|
|
70
|
+
const FIX_HINT = 'run nera-stan --fix'
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The `package.json` check: key order, `engines.node`, the plugin-utils range, and, for a
|
|
74
|
+
* package that is published (no `"private": true`), `CHANGELOG.md` in `files` and the forms
|
|
75
|
+
* `npm pkg fix` writes for `bin` and `repository.url`. `--fix` (D7) reorders the keys, adds
|
|
76
|
+
* `CHANGELOG.md` to `files` and writes the `npm pkg fix` forms; it keeps the file's indent,
|
|
77
|
+
* line endings and final newline and changes no value, and it never writes a `package.json`
|
|
78
|
+
* that leads out of the repo through a symlink.
|
|
79
|
+
*/
|
|
80
|
+
export const packageCheck = Object.freeze({
|
|
81
|
+
name: 'package',
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Every rule id this check can report.
|
|
85
|
+
*
|
|
86
|
+
* @returns {string[]}
|
|
87
|
+
*/
|
|
88
|
+
rules() {
|
|
89
|
+
return Object.values(PACKAGE_RULES)
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Check the repo's `package.json`.
|
|
94
|
+
*
|
|
95
|
+
* A rule under `nera-stan.skip` neither reports nor fixes.
|
|
96
|
+
*
|
|
97
|
+
* @param {{ root: string, pkg: object, settings: { skip: Record<string, string> }, fix: boolean }} context
|
|
98
|
+
* @returns {{ findings: { file: string, line: number, rule: string, message: string,
|
|
99
|
+
* fixable: boolean }[] }}
|
|
100
|
+
*/
|
|
101
|
+
run({ root, pkg, settings, fix }) {
|
|
102
|
+
const file = path.join(root, FILE)
|
|
103
|
+
const text = readFileSync(file, 'utf8')
|
|
104
|
+
const findings = checkPackage(pkg, text)
|
|
105
|
+
const active = (rule) => !Object.hasOwn(settings.skip, rule)
|
|
106
|
+
// Only a fixable finding rewrites the file, so a manifest with nothing to fix keeps its formatting.
|
|
107
|
+
if (!fix || !findings.some((finding) => finding.fixable && active(finding.rule))) {
|
|
108
|
+
return { findings }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const fixed = fixPackage(pkg, active)
|
|
112
|
+
const output = serialize(fixed, text)
|
|
113
|
+
if (!writeInside(root, FILE, output)) {
|
|
114
|
+
return { findings: findings.map((finding) => finding.fixable && active(finding.rule)
|
|
115
|
+
? { ...finding, message: `${finding.message}, but ${OUTSIDE_REPO}` }
|
|
116
|
+
: finding) }
|
|
117
|
+
}
|
|
118
|
+
return { findings: checkPackage(fixed, output) }
|
|
119
|
+
},
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The findings for one parsed `package.json` and its text, which gives the lines.
|
|
124
|
+
*
|
|
125
|
+
* @param {object} pkg - The parsed manifest.
|
|
126
|
+
* @param {string} text - The manifest's text.
|
|
127
|
+
* @param {object} [options]
|
|
128
|
+
* @param {string} [options.pluginUtilsRange] - The range a repo must not go below;
|
|
129
|
+
* default nera-stan's own `@nera-static/plugin-utils` dependency.
|
|
130
|
+
* @returns {{ file: string, line: number, rule: string, message: string, fixable: boolean }[]}
|
|
131
|
+
*/
|
|
132
|
+
export function checkPackage(pkg, text, { pluginUtilsRange = ownPluginUtilsRange() } = {}) {
|
|
133
|
+
const lineOf = keyLines(text)
|
|
134
|
+
const published = pkg.private !== true
|
|
135
|
+
return [
|
|
136
|
+
...keyOrder(pkg, lineOf),
|
|
137
|
+
...engines(pkg, lineOf),
|
|
138
|
+
...pluginUtils(pkg, lineOf, pluginUtilsRange),
|
|
139
|
+
...(published ? [...changelogInFiles(pkg, lineOf), ...npmPkgFix(pkg, lineOf)] : []),
|
|
140
|
+
].map((finding) => ({ file: FILE, ...finding }))
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The `@nera-static/plugin-utils` range nera-stan itself depends on.
|
|
145
|
+
*
|
|
146
|
+
* @returns {string}
|
|
147
|
+
*/
|
|
148
|
+
export function ownPluginUtilsRange() {
|
|
149
|
+
return readPackage(fileURLToPath(new URL('../..', import.meta.url))).dependencies[PLUGIN_UTILS]
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function keyOrder(pkg, lineOf) {
|
|
153
|
+
const keys = Object.keys(pkg)
|
|
154
|
+
const expected = orderedKeys(keys)
|
|
155
|
+
const index = keys.findIndex((key, i) => key !== expected[i])
|
|
156
|
+
if (index === -1) {
|
|
157
|
+
return []
|
|
158
|
+
}
|
|
159
|
+
const found = keys[index]
|
|
160
|
+
const where = KEY_ORDER.includes(found)
|
|
161
|
+
? `"${expected[index]}" belongs here, before "${found}"`
|
|
162
|
+
: `"${found}" is no skeleton key and belongs after every skeleton key`
|
|
163
|
+
return [{
|
|
164
|
+
line: lineOf(found),
|
|
165
|
+
rule: PACKAGE_RULES.keyOrder,
|
|
166
|
+
message: `key "${found}" is out of order: ${where}; the order is ${KEY_ORDER.join(', ')}, ` +
|
|
167
|
+
`then any other key: ${FIX_HINT} to reorder`,
|
|
168
|
+
fixable: true,
|
|
169
|
+
}]
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function engines(pkg, lineOf) {
|
|
173
|
+
const node = isPlainObject(pkg.engines) ? pkg.engines.node : undefined
|
|
174
|
+
if (ENGINES.includes(node)) {
|
|
175
|
+
return []
|
|
176
|
+
}
|
|
177
|
+
const allowed = ENGINES.map((value) => `"${value}"`).join(' or ')
|
|
178
|
+
const problem = node === undefined ? '"engines.node" is missing' : `"engines.node" is ${JSON.stringify(node)}`
|
|
179
|
+
return [{
|
|
180
|
+
line: lineOf('engines', 'node'),
|
|
181
|
+
rule: PACKAGE_RULES.engines,
|
|
182
|
+
message: `${problem}: set it to ${allowed} (the second only where a dependency needs Node 22.13)`,
|
|
183
|
+
fixable: false,
|
|
184
|
+
}]
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function pluginUtils(pkg, lineOf, ownRange) {
|
|
188
|
+
const [, major] = CARET_RANGE.exec(ownRange)
|
|
189
|
+
const floor = ownRange.slice(1)
|
|
190
|
+
return DEPENDENCY_FIELDS
|
|
191
|
+
.filter((field) => isPlainObject(pkg[field]) && Object.hasOwn(pkg[field], PLUGIN_UTILS))
|
|
192
|
+
.flatMap((field) => {
|
|
193
|
+
const range = pkg[field][PLUGIN_UTILS]
|
|
194
|
+
const match = typeof range === 'string' ? CARET_RANGE.exec(range) : null
|
|
195
|
+
if (match && match[1] === major && satisfiesFloor(range.slice(1), floor)) {
|
|
196
|
+
return []
|
|
197
|
+
}
|
|
198
|
+
return [{
|
|
199
|
+
line: lineOf(field, PLUGIN_UTILS),
|
|
200
|
+
rule: PACKAGE_RULES.pluginUtilsRange,
|
|
201
|
+
message: `"${field}.${PLUGIN_UTILS}" is ${JSON.stringify(range)}: use a caret range of major ` +
|
|
202
|
+
`${major} no lower than "${ownRange}", e.g. "${ownRange}", then run npm install`,
|
|
203
|
+
fixable: false,
|
|
204
|
+
}]
|
|
205
|
+
})
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function changelogInFiles(pkg, lineOf) {
|
|
209
|
+
if (!Array.isArray(pkg.files) || isPublished(publishedPatterns(pkg), CHANGELOG)) {
|
|
210
|
+
return []
|
|
211
|
+
}
|
|
212
|
+
const fixed = { ...pkg, files: [...pkg.files, CHANGELOG] }
|
|
213
|
+
return [{
|
|
214
|
+
line: lineOf('files'),
|
|
215
|
+
rule: PACKAGE_RULES.changelogInFiles,
|
|
216
|
+
message: `"files" does not ship ${CHANGELOG}: add "${CHANGELOG}" to "files" (${FIX_HINT})`,
|
|
217
|
+
fixable: isPublished(publishedPatterns(fixed), CHANGELOG),
|
|
218
|
+
}]
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function npmPkgFix(pkg, lineOf) {
|
|
222
|
+
const findings = binEntries(pkg.bin)
|
|
223
|
+
.filter(({ target }) => RELATIVE_PREFIX.test(target))
|
|
224
|
+
.map(({ keys, target }) => ({
|
|
225
|
+
line: lineOf(...keys),
|
|
226
|
+
rule: PACKAGE_RULES.npmPkgFix,
|
|
227
|
+
message: `"${keys.join('.')}" is ${JSON.stringify(target)}: write it without the leading "./", ` +
|
|
228
|
+
`as npm pkg fix does: "${stripRelative(target)}" (${FIX_HINT})`,
|
|
229
|
+
fixable: true,
|
|
230
|
+
}))
|
|
231
|
+
|
|
232
|
+
const url = isPlainObject(pkg.repository) ? pkg.repository.url : undefined
|
|
233
|
+
if (typeof url === 'string' && !url.startsWith('git+')) {
|
|
234
|
+
const fixable = GIT_PLUS_SCHEMES.test(url)
|
|
235
|
+
findings.push({
|
|
236
|
+
line: lineOf('repository', 'url'),
|
|
237
|
+
rule: PACKAGE_RULES.npmPkgFix,
|
|
238
|
+
message: `"repository.url" is ${JSON.stringify(url)}: write it with the "git+" prefix, as npm pkg fix ` +
|
|
239
|
+
(fixable ? `does: "git+${url}" (${FIX_HINT})` : 'does, e.g. "git+https://github.com/<owner>/<repo>.git"'),
|
|
240
|
+
fixable,
|
|
241
|
+
})
|
|
242
|
+
}
|
|
243
|
+
return findings
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* The manifest with the fixes of D7 applied for every rule `active` accepts: the `npm pkg fix`
|
|
248
|
+
* forms, `CHANGELOG.md` in `files`, then the key order. Values are copied, never changed otherwise.
|
|
249
|
+
*/
|
|
250
|
+
function fixPackage(pkg, active) {
|
|
251
|
+
const fixed = { ...pkg }
|
|
252
|
+
const published = pkg.private !== true
|
|
253
|
+
if (published && active(PACKAGE_RULES.npmPkgFix)) {
|
|
254
|
+
if (typeof fixed.bin === 'string') {
|
|
255
|
+
fixed.bin = stripRelative(fixed.bin)
|
|
256
|
+
} else if (isPlainObject(fixed.bin)) {
|
|
257
|
+
fixed.bin = Object.fromEntries(Object.entries(fixed.bin)
|
|
258
|
+
.map(([name, target]) => [name, typeof target === 'string' ? stripRelative(target) : target]))
|
|
259
|
+
}
|
|
260
|
+
const url = isPlainObject(fixed.repository) ? fixed.repository.url : undefined
|
|
261
|
+
if (typeof url === 'string' && GIT_PLUS_SCHEMES.test(url)) {
|
|
262
|
+
fixed.repository = { ...fixed.repository, url: `git+${url}` }
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (published && active(PACKAGE_RULES.changelogInFiles) &&
|
|
266
|
+
changelogInFiles(fixed, () => 1).some((finding) => finding.fixable)) {
|
|
267
|
+
fixed.files = [...fixed.files, CHANGELOG]
|
|
268
|
+
}
|
|
269
|
+
if (!active(PACKAGE_RULES.keyOrder)) {
|
|
270
|
+
return fixed
|
|
271
|
+
}
|
|
272
|
+
return Object.fromEntries(orderedKeys(Object.keys(fixed)).map((key) => [key, fixed[key]]))
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function stripRelative(target) {
|
|
276
|
+
return target.replace(RELATIVE_PREFIX, '')
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The skeleton keys in `KEY_ORDER`, then every other key in the order given. */
|
|
280
|
+
function orderedKeys(keys) {
|
|
281
|
+
const known = KEY_ORDER.filter((key) => keys.includes(key))
|
|
282
|
+
return [...known, ...keys.filter((key) => !KEY_ORDER.includes(key))]
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function binEntries(bin) {
|
|
286
|
+
if (typeof bin === 'string') {
|
|
287
|
+
return [{ keys: ['bin'], target: bin }]
|
|
288
|
+
}
|
|
289
|
+
if (isPlainObject(bin)) {
|
|
290
|
+
return Object.entries(bin)
|
|
291
|
+
.filter(([, target]) => typeof target === 'string')
|
|
292
|
+
.map(([name, target]) => ({ keys: ['bin', name], target }))
|
|
293
|
+
}
|
|
294
|
+
return []
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* The manifest as JSON in the style of `text`: its indent (default 4 spaces), its line
|
|
299
|
+
* ending, and a final newline when it had one.
|
|
300
|
+
*
|
|
301
|
+
* @param {object} pkg - The manifest to write.
|
|
302
|
+
* @param {string} text - The manifest's current text, whose style is kept.
|
|
303
|
+
* @returns {string}
|
|
304
|
+
*/
|
|
305
|
+
export function serialize(pkg, text) {
|
|
306
|
+
const indent = /^([ \t]+)"/m.exec(text)?.[1] ?? ' '
|
|
307
|
+
const eol = text.includes('\r\n') ? '\r\n' : '\n'
|
|
308
|
+
const json = JSON.stringify(pkg, null, indent).replaceAll('\n', eol)
|
|
309
|
+
return /\r?\n$/.test(text) ? `${json}${eol}` : json
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* The line of a key path in the manifest's text, e.g. `lineOf('engines', 'node')`. A path
|
|
314
|
+
* that is not in the text falls back to its nearest parent's line, the root to line 1.
|
|
315
|
+
*
|
|
316
|
+
* @param {string} text - The manifest's text.
|
|
317
|
+
* @returns {(...keys: string[]) => number}
|
|
318
|
+
*/
|
|
319
|
+
export function keyLines(text) {
|
|
320
|
+
const lines = new Map()
|
|
321
|
+
let ast
|
|
322
|
+
try {
|
|
323
|
+
// The parentheses make the JSON an expression; they add no line.
|
|
324
|
+
ast = espree.parse(`(${text}\n)`, { ecmaVersion: 'latest', loc: true })
|
|
325
|
+
} catch {
|
|
326
|
+
ast = null
|
|
327
|
+
}
|
|
328
|
+
const visit = (node, keys) => {
|
|
329
|
+
for (const property of node.properties) {
|
|
330
|
+
const key = property.key.type === 'Literal' ? String(property.key.value) : property.key.name
|
|
331
|
+
const at = [...keys, key]
|
|
332
|
+
lines.set(at.join('\0'), property.key.loc.start.line)
|
|
333
|
+
if (property.value.type === 'ObjectExpression') {
|
|
334
|
+
visit(property.value, at)
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const root = ast?.body[0]?.expression
|
|
339
|
+
if (root?.type === 'ObjectExpression') {
|
|
340
|
+
visit(root, [])
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return (...keys) => {
|
|
344
|
+
for (let length = keys.length; length > 0; length--) {
|
|
345
|
+
const line = lines.get(keys.slice(0, length).join('\0'))
|
|
346
|
+
if (line !== undefined) {
|
|
347
|
+
return line
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return 1
|
|
351
|
+
}
|
|
352
|
+
}
|