@dcaldeira/tooling 0.1.0 → 0.2.1
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/README.md +34 -34
- package/checks/a11y.mjs +206 -206
- package/checks/architecture.mjs +137 -137
- package/checks/audit.mjs +121 -121
- package/checks/bundle.mjs +128 -126
- package/checks/dependencies.mjs +277 -277
- package/checks/duplication.mjs +135 -0
- package/checks/lint-scss.mjs +363 -349
- package/checks/package.mjs +30 -30
- package/checks/playwright-summary.mjs +53 -53
- package/checks/sass.mjs +67 -67
- package/checks/scan-secrets.mjs +58 -58
- package/checks/shared.mjs +86 -86
- package/checks/translations.mjs +55 -55
- package/checks/visual-compare.mjs +148 -148
- package/checks/visual-report-html.mjs +470 -470
- package/checks/visual-report.mjs +13 -13
- package/config.schema.json +999 -927
- package/configs/eslint.mjs +27 -27
- package/configs/knip.json +11 -11
- package/configs/playwright.mjs +15 -15
- package/configs/vitest.mjs +16 -16
- package/package.json +105 -105
- package/release/finalize.mjs +65 -65
- package/release/github.mjs +64 -64
- package/release/prepare.mjs +30 -30
- package/runner/browsers.mjs +73 -73
- package/runner/cache.mjs +121 -121
- package/runner/ci-plan.mjs +54 -54
- package/runner/cli.mjs +250 -248
- package/runner/config.mjs +148 -148
- package/runner/docs-install.mjs +93 -93
- package/runner/execute.mjs +298 -286
- package/runner/generate.mjs +122 -122
- package/runner/git.mjs +122 -122
- package/runner/graph.mjs +250 -243
- package/runner/insights.mjs +313 -313
- package/runner/provision.mjs +53 -53
- package/runner/repo-standard.mjs +104 -0
- package/runner/report.mjs +35 -35
- package/runner/serve-static.mjs +76 -76
- package/runner/setup.mjs +241 -237
- package/runner/summary.mjs +112 -112
- package/runner/watch.mjs +82 -82
- package/templates/ci.yml +25 -25
- package/templates/dependabot-merge.yml +13 -13
- package/templates/dependabot.yml +26 -26
- package/templates/pre-push +13 -13
- package/templates/release.yml +23 -23
package/runner/generate.mjs
CHANGED
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, realpathSync, writeFileSync } from 'node:fs';
|
|
2
|
-
import { dirname, isAbsolute, relative, resolve, sep } from 'node:path';
|
|
3
|
-
import { inside, loadConfig } from './config.mjs';
|
|
4
|
-
|
|
5
|
-
const kebab = (name) =>
|
|
6
|
-
name
|
|
7
|
-
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
8
|
-
.replace(/([A-Z])([A-Z][a-z])/g, '$1-$2')
|
|
9
|
-
.toLowerCase();
|
|
10
|
-
|
|
11
|
-
export function generateFiles(root, kind, { name, path, designSystem = false } = {}, config = {}) {
|
|
12
|
-
if (!['component', 'hook'].includes(kind)) throw new Error('Generator must be component or hook');
|
|
13
|
-
if (
|
|
14
|
-
!name ||
|
|
15
|
-
(kind === 'component'
|
|
16
|
-
? !/^[A-Z][A-Za-z0-9]*$/.test(name)
|
|
17
|
-
: !/^use[A-Z][A-Za-z0-9]*$/.test(name))
|
|
18
|
-
)
|
|
19
|
-
throw new Error(
|
|
20
|
-
kind === 'component'
|
|
21
|
-
? 'Use a PascalCase component name'
|
|
22
|
-
: 'Hook names must start with use followed by PascalCase',
|
|
23
|
-
);
|
|
24
|
-
const settings = config.generators ?? {};
|
|
25
|
-
const directory =
|
|
26
|
-
path ??
|
|
27
|
-
(kind === 'component'
|
|
28
|
-
? (settings.components ?? 'src/components')
|
|
29
|
-
: (settings.hooks ?? 'src/hooks'));
|
|
30
|
-
inside(root, directory);
|
|
31
|
-
const files = new Map();
|
|
32
|
-
if (kind === 'component') {
|
|
33
|
-
const block =
|
|
34
|
-
config.styles?.preset === 'rookie' || config.styles?.rules?.componentName
|
|
35
|
-
? name.replace(/(?<!^)(?=[A-Z])/g, '-').toLowerCase()
|
|
36
|
-
: kebab(name),
|
|
37
|
-
base = `${directory}/${name}`;
|
|
38
|
-
let source;
|
|
39
|
-
if (designSystem) {
|
|
40
|
-
const ds = settings.designSystem;
|
|
41
|
-
if (!ds?.package || !ds.component)
|
|
42
|
-
throw new Error(
|
|
43
|
-
'Configure generators.designSystem.package and component before using --design-system',
|
|
44
|
-
);
|
|
45
|
-
if (
|
|
46
|
-
!/^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/.test(ds.package) ||
|
|
47
|
-
!/^[A-Z][A-Za-z0-9]*$/.test(ds.component)
|
|
48
|
-
)
|
|
49
|
-
throw new Error('Invalid design-system import');
|
|
50
|
-
source = `import type { ComponentProps } from 'react';\nimport { ${ds.component} as Primitive } from '${ds.package}';\nimport styles from './${name}.module.scss';\n\nexport type ${name}Props = ComponentProps<typeof Primitive>;\n\nexport function ${name}({ className, ...props }: ${name}Props) {\n return <Primitive className={[styles['${block}'], className].filter(Boolean).join(' ')} {...props} />;\n}\n`;
|
|
51
|
-
} else {
|
|
52
|
-
source = `import type { HTMLAttributes } from 'react';\nimport styles from './${name}.module.scss';\n\nexport type ${name}Props = HTMLAttributes<HTMLDivElement>;\n\nexport function ${name}({ className, ...props }: ${name}Props) {\n return <div className={[styles['${block}'], className].filter(Boolean).join(' ')} {...props} />;\n}\n`;
|
|
53
|
-
}
|
|
54
|
-
files.set(`${base}/${name}.tsx`, source);
|
|
55
|
-
files.set(`${base}/${name}.module.scss`, `.${block} {\n display: block;\n}\n`);
|
|
56
|
-
files.set(
|
|
57
|
-
`${base}/${name}.test.tsx`,
|
|
58
|
-
`// @vitest-environment jsdom\nimport { cleanup, render, screen } from '@testing-library/react';\nimport { afterEach, describe, expect, it } from 'vitest';\nimport { ${name} } from './${name}';\n\nafterEach(cleanup);\n\ndescribe('${name}', () => {\n it('renders its content and forwards DOM attributes', () => {\n render(<${name} data-testid="subject" className="custom">Content</${name}>);\n const element = screen.getByTestId('subject');\n expect(element.textContent).toBe('Content');\n expect(element.classList.contains('custom')).toBe(true);\n });\n});\n`,
|
|
59
|
-
);
|
|
60
|
-
files.set(
|
|
61
|
-
`${base}/index.ts`,
|
|
62
|
-
`export { ${name} } from './${name}';\nexport type { ${name}Props } from './${name}';\n`,
|
|
63
|
-
);
|
|
64
|
-
} else {
|
|
65
|
-
files.set(
|
|
66
|
-
`${directory}/${name}.ts`,
|
|
67
|
-
`import { useState } from 'react';\n\n// Replace this initial state scaffold with the hook's domain behaviour.\nexport function ${name}<T>(initialValue: T) {\n return useState(initialValue);\n}\n`,
|
|
68
|
-
);
|
|
69
|
-
files.set(
|
|
70
|
-
`${directory}/${name}.test.ts`,
|
|
71
|
-
`// @vitest-environment jsdom\nimport { act, cleanup, renderHook } from '@testing-library/react';\nimport { afterEach, describe, expect, it } from 'vitest';\nimport { ${name} } from './${name}';\n\nafterEach(cleanup);\n\ndescribe('${name}', () => {\n it('initializes state and accepts updates', () => {\n const { result } = renderHook(() => ${name}('initial'));\n expect(result.current[0]).toBe('initial');\n act(() => result.current[1]('updated'));\n expect(result.current[0]).toBe('updated');\n });\n});\n`,
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
return files;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function writeGenerated(root, files, { dryRun = false } = {}) {
|
|
78
|
-
const canonicalRoot = realpathSync(root);
|
|
79
|
-
for (const path of files.keys()) {
|
|
80
|
-
const target = inside(root, path);
|
|
81
|
-
if (existsSync(target)) throw new Error(`Refusing to overwrite ${path}`);
|
|
82
|
-
let parent = dirname(target);
|
|
83
|
-
while (!existsSync(parent)) parent = dirname(parent);
|
|
84
|
-
const rel = relative(canonicalRoot, realpathSync(parent));
|
|
85
|
-
if (
|
|
86
|
-
isAbsolute(rel) ||
|
|
87
|
-
rel === '..' ||
|
|
88
|
-
rel.startsWith(`..${sep}`) ||
|
|
89
|
-
resolve(canonicalRoot, rel) !== realpathSync(parent)
|
|
90
|
-
)
|
|
91
|
-
throw new Error(`Generator destination escapes repository: ${path}`);
|
|
92
|
-
}
|
|
93
|
-
if (dryRun) return;
|
|
94
|
-
for (const [path, content] of files) {
|
|
95
|
-
const target = inside(root, path);
|
|
96
|
-
mkdirSync(dirname(target), { recursive: true });
|
|
97
|
-
writeFileSync(target, content, { flag: 'wx' });
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function generateCommand(root, kind, options) {
|
|
102
|
-
let config;
|
|
103
|
-
try {
|
|
104
|
-
config = loadConfig(root, options.config).config;
|
|
105
|
-
} catch (error) {
|
|
106
|
-
if (options.config || error.code !== 'ENOENT') throw error;
|
|
107
|
-
config = {};
|
|
108
|
-
}
|
|
109
|
-
const files = generateFiles(
|
|
110
|
-
root,
|
|
111
|
-
kind,
|
|
112
|
-
{ name: options.name, path: options.path, designSystem: options['design-system'] },
|
|
113
|
-
config,
|
|
114
|
-
);
|
|
115
|
-
writeGenerated(root, files, { dryRun: options['dry-run'] });
|
|
116
|
-
console.log(
|
|
117
|
-
options['dry-run']
|
|
118
|
-
? JSON.stringify({ files: [...files].map(([path, content]) => ({ path, content })) }, null, 2)
|
|
119
|
-
: `Generated ${[...files.keys()].join(', ')}`,
|
|
120
|
-
);
|
|
121
|
-
return 0;
|
|
122
|
-
}
|
|
1
|
+
import { existsSync, mkdirSync, realpathSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, isAbsolute, relative, resolve, sep } from 'node:path';
|
|
3
|
+
import { inside, loadConfig } from './config.mjs';
|
|
4
|
+
|
|
5
|
+
const kebab = (name) =>
|
|
6
|
+
name
|
|
7
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
8
|
+
.replace(/([A-Z])([A-Z][a-z])/g, '$1-$2')
|
|
9
|
+
.toLowerCase();
|
|
10
|
+
|
|
11
|
+
export function generateFiles(root, kind, { name, path, designSystem = false } = {}, config = {}) {
|
|
12
|
+
if (!['component', 'hook'].includes(kind)) throw new Error('Generator must be component or hook');
|
|
13
|
+
if (
|
|
14
|
+
!name ||
|
|
15
|
+
(kind === 'component'
|
|
16
|
+
? !/^[A-Z][A-Za-z0-9]*$/.test(name)
|
|
17
|
+
: !/^use[A-Z][A-Za-z0-9]*$/.test(name))
|
|
18
|
+
)
|
|
19
|
+
throw new Error(
|
|
20
|
+
kind === 'component'
|
|
21
|
+
? 'Use a PascalCase component name'
|
|
22
|
+
: 'Hook names must start with use followed by PascalCase',
|
|
23
|
+
);
|
|
24
|
+
const settings = config.generators ?? {};
|
|
25
|
+
const directory =
|
|
26
|
+
path ??
|
|
27
|
+
(kind === 'component'
|
|
28
|
+
? (settings.components ?? 'src/components')
|
|
29
|
+
: (settings.hooks ?? 'src/hooks'));
|
|
30
|
+
inside(root, directory);
|
|
31
|
+
const files = new Map();
|
|
32
|
+
if (kind === 'component') {
|
|
33
|
+
const block =
|
|
34
|
+
config.styles?.preset === 'rookie' || config.styles?.rules?.componentName
|
|
35
|
+
? name.replace(/(?<!^)(?=[A-Z])/g, '-').toLowerCase()
|
|
36
|
+
: kebab(name),
|
|
37
|
+
base = `${directory}/${name}`;
|
|
38
|
+
let source;
|
|
39
|
+
if (designSystem) {
|
|
40
|
+
const ds = settings.designSystem;
|
|
41
|
+
if (!ds?.package || !ds.component)
|
|
42
|
+
throw new Error(
|
|
43
|
+
'Configure generators.designSystem.package and component before using --design-system',
|
|
44
|
+
);
|
|
45
|
+
if (
|
|
46
|
+
!/^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/.test(ds.package) ||
|
|
47
|
+
!/^[A-Z][A-Za-z0-9]*$/.test(ds.component)
|
|
48
|
+
)
|
|
49
|
+
throw new Error('Invalid design-system import');
|
|
50
|
+
source = `import type { ComponentProps } from 'react';\nimport { ${ds.component} as Primitive } from '${ds.package}';\nimport styles from './${name}.module.scss';\n\nexport type ${name}Props = ComponentProps<typeof Primitive>;\n\nexport function ${name}({ className, ...props }: ${name}Props) {\n return <Primitive className={[styles['${block}'], className].filter(Boolean).join(' ')} {...props} />;\n}\n`;
|
|
51
|
+
} else {
|
|
52
|
+
source = `import type { HTMLAttributes } from 'react';\nimport styles from './${name}.module.scss';\n\nexport type ${name}Props = HTMLAttributes<HTMLDivElement>;\n\nexport function ${name}({ className, ...props }: ${name}Props) {\n return <div className={[styles['${block}'], className].filter(Boolean).join(' ')} {...props} />;\n}\n`;
|
|
53
|
+
}
|
|
54
|
+
files.set(`${base}/${name}.tsx`, source);
|
|
55
|
+
files.set(`${base}/${name}.module.scss`, `.${block} {\n display: block;\n}\n`);
|
|
56
|
+
files.set(
|
|
57
|
+
`${base}/${name}.test.tsx`,
|
|
58
|
+
`// @vitest-environment jsdom\nimport { cleanup, render, screen } from '@testing-library/react';\nimport { afterEach, describe, expect, it } from 'vitest';\nimport { ${name} } from './${name}';\n\nafterEach(cleanup);\n\ndescribe('${name}', () => {\n it('renders its content and forwards DOM attributes', () => {\n render(<${name} data-testid="subject" className="custom">Content</${name}>);\n const element = screen.getByTestId('subject');\n expect(element.textContent).toBe('Content');\n expect(element.classList.contains('custom')).toBe(true);\n });\n});\n`,
|
|
59
|
+
);
|
|
60
|
+
files.set(
|
|
61
|
+
`${base}/index.ts`,
|
|
62
|
+
`export { ${name} } from './${name}';\nexport type { ${name}Props } from './${name}';\n`,
|
|
63
|
+
);
|
|
64
|
+
} else {
|
|
65
|
+
files.set(
|
|
66
|
+
`${directory}/${name}.ts`,
|
|
67
|
+
`import { useState } from 'react';\n\n// Replace this initial state scaffold with the hook's domain behaviour.\nexport function ${name}<T>(initialValue: T) {\n return useState(initialValue);\n}\n`,
|
|
68
|
+
);
|
|
69
|
+
files.set(
|
|
70
|
+
`${directory}/${name}.test.ts`,
|
|
71
|
+
`// @vitest-environment jsdom\nimport { act, cleanup, renderHook } from '@testing-library/react';\nimport { afterEach, describe, expect, it } from 'vitest';\nimport { ${name} } from './${name}';\n\nafterEach(cleanup);\n\ndescribe('${name}', () => {\n it('initializes state and accepts updates', () => {\n const { result } = renderHook(() => ${name}('initial'));\n expect(result.current[0]).toBe('initial');\n act(() => result.current[1]('updated'));\n expect(result.current[0]).toBe('updated');\n });\n});\n`,
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return files;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function writeGenerated(root, files, { dryRun = false } = {}) {
|
|
78
|
+
const canonicalRoot = realpathSync(root);
|
|
79
|
+
for (const path of files.keys()) {
|
|
80
|
+
const target = inside(root, path);
|
|
81
|
+
if (existsSync(target)) throw new Error(`Refusing to overwrite ${path}`);
|
|
82
|
+
let parent = dirname(target);
|
|
83
|
+
while (!existsSync(parent)) parent = dirname(parent);
|
|
84
|
+
const rel = relative(canonicalRoot, realpathSync(parent));
|
|
85
|
+
if (
|
|
86
|
+
isAbsolute(rel) ||
|
|
87
|
+
rel === '..' ||
|
|
88
|
+
rel.startsWith(`..${sep}`) ||
|
|
89
|
+
resolve(canonicalRoot, rel) !== realpathSync(parent)
|
|
90
|
+
)
|
|
91
|
+
throw new Error(`Generator destination escapes repository: ${path}`);
|
|
92
|
+
}
|
|
93
|
+
if (dryRun) return;
|
|
94
|
+
for (const [path, content] of files) {
|
|
95
|
+
const target = inside(root, path);
|
|
96
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
97
|
+
writeFileSync(target, content, { flag: 'wx' });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function generateCommand(root, kind, options) {
|
|
102
|
+
let config;
|
|
103
|
+
try {
|
|
104
|
+
config = loadConfig(root, options.config).config;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (options.config || error.code !== 'ENOENT') throw error;
|
|
107
|
+
config = {};
|
|
108
|
+
}
|
|
109
|
+
const files = generateFiles(
|
|
110
|
+
root,
|
|
111
|
+
kind,
|
|
112
|
+
{ name: options.name, path: options.path, designSystem: options['design-system'] },
|
|
113
|
+
config,
|
|
114
|
+
);
|
|
115
|
+
writeGenerated(root, files, { dryRun: options['dry-run'] });
|
|
116
|
+
console.log(
|
|
117
|
+
options['dry-run']
|
|
118
|
+
? JSON.stringify({ files: [...files].map(([path, content]) => ({ path, content })) }, null, 2)
|
|
119
|
+
: `Generated ${[...files.keys()].join(', ')}`,
|
|
120
|
+
);
|
|
121
|
+
return 0;
|
|
122
|
+
}
|
package/runner/git.mjs
CHANGED
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
import { execFileSync } from 'node:child_process';
|
|
2
|
-
import { createHash } from 'node:crypto';
|
|
3
|
-
import { lstatSync, readFileSync, readlinkSync } from 'node:fs';
|
|
4
|
-
import { dirname, join, resolve } from 'node:path';
|
|
5
|
-
import { executable } from './execute.mjs';
|
|
6
|
-
|
|
7
|
-
export const git = (root, ...args) =>
|
|
8
|
-
execFileSync('git', args, {
|
|
9
|
-
cwd: root,
|
|
10
|
-
encoding: 'utf8',
|
|
11
|
-
maxBuffer: 64 * 1024 * 1024,
|
|
12
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
13
|
-
}).trimEnd();
|
|
14
|
-
const nul = (value) => value.split('\0').filter(Boolean);
|
|
15
|
-
|
|
16
|
-
export function changedFiles(root, base, { committed = false, head = 'HEAD' } = {}) {
|
|
17
|
-
const ancestor = git(root, 'merge-base', base, head);
|
|
18
|
-
const changes = nul(git(root, 'diff', '--name-only', '--no-renames', '-z', ancestor, head));
|
|
19
|
-
if (!committed)
|
|
20
|
-
changes.push(
|
|
21
|
-
...nul(git(root, 'diff', '--name-only', '--no-renames', '-z', 'HEAD')),
|
|
22
|
-
...nul(git(root, 'ls-files', '--others', '--exclude-standard', '-z')),
|
|
23
|
-
);
|
|
24
|
-
return [...new Set(changes)].sort();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function toolIdentity(root, tool) {
|
|
28
|
-
const name = typeof tool === 'string' ? tool : tool.name;
|
|
29
|
-
const cwd = typeof tool === 'string' ? root : resolve(root, tool.cwd ?? '.');
|
|
30
|
-
const command = executable(cwd, name);
|
|
31
|
-
if (!command) return { name, cwd, command: null };
|
|
32
|
-
if (name === 'node') return { name, cwd, command, version: process.version };
|
|
33
|
-
if (command[0] === process.execPath && command[1]) {
|
|
34
|
-
for (let directory = dirname(command[1]); ; directory = dirname(directory)) {
|
|
35
|
-
try {
|
|
36
|
-
const manifest = readFileSync(join(directory, 'package.json'), 'utf8');
|
|
37
|
-
const entry = createHash('sha256').update(readFileSync(command[1])).digest('hex');
|
|
38
|
-
return { name, cwd, command, manifest, entry };
|
|
39
|
-
} catch (error) {
|
|
40
|
-
if (error.code !== 'ENOENT') throw error;
|
|
41
|
-
}
|
|
42
|
-
if (dirname(directory) === directory) break;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
let version;
|
|
46
|
-
try {
|
|
47
|
-
version = execFileSync(command[0], [...command.slice(1), '--version'], {
|
|
48
|
-
cwd,
|
|
49
|
-
encoding: 'utf8',
|
|
50
|
-
timeout: 5000,
|
|
51
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
52
|
-
});
|
|
53
|
-
} catch {
|
|
54
|
-
version = 'version-unavailable';
|
|
55
|
-
}
|
|
56
|
-
return { name, cwd, command, version };
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function captureSnapshot(
|
|
60
|
-
root,
|
|
61
|
-
{ base, version, config, environment = [], tools = [], inputs = [] },
|
|
62
|
-
) {
|
|
63
|
-
const hash = createHash('sha256');
|
|
64
|
-
const add = (value) => {
|
|
65
|
-
const text = String(value);
|
|
66
|
-
hash.update(`${Buffer.byteLength(text)}:`);
|
|
67
|
-
hash.update(text);
|
|
68
|
-
};
|
|
69
|
-
add(version);
|
|
70
|
-
add(process.version);
|
|
71
|
-
add(process.platform);
|
|
72
|
-
add(config);
|
|
73
|
-
for (const file of ['node_modules/.package-lock.json', ...inputs]) {
|
|
74
|
-
add(file);
|
|
75
|
-
try {
|
|
76
|
-
const bytes = readFileSync(resolve(root, file));
|
|
77
|
-
add(bytes.length);
|
|
78
|
-
hash.update(bytes);
|
|
79
|
-
} catch (error) {
|
|
80
|
-
if (error.code !== 'ENOENT') throw error;
|
|
81
|
-
add('absent');
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
for (const key of environment) {
|
|
85
|
-
add(key);
|
|
86
|
-
add(process.env[key] ?? '');
|
|
87
|
-
}
|
|
88
|
-
const context = hash.copy().digest('hex');
|
|
89
|
-
const identities = tools.map((tool) => toolIdentity(root, tool));
|
|
90
|
-
for (const identity of identities) add(JSON.stringify(identity));
|
|
91
|
-
add(git(root, 'rev-parse', 'HEAD'));
|
|
92
|
-
add(git(root, 'rev-parse', `${base}^{commit}`));
|
|
93
|
-
const files = new Set(
|
|
94
|
-
nul(git(root, 'ls-files', '--cached', '--others', '--exclude-standard', '-z')),
|
|
95
|
-
);
|
|
96
|
-
const digests = {};
|
|
97
|
-
for (const file of [...files].sort()) {
|
|
98
|
-
const content = createHash('sha256');
|
|
99
|
-
const part = (value) => content.update(JSON.stringify(value));
|
|
100
|
-
try {
|
|
101
|
-
const stat = lstatSync(resolve(root, file));
|
|
102
|
-
part(stat.mode & 0o111);
|
|
103
|
-
if (stat.isSymbolicLink()) part(readlinkSync(resolve(root, file)));
|
|
104
|
-
else if (stat.isFile()) {
|
|
105
|
-
const bytes = readFileSync(resolve(root, file));
|
|
106
|
-
part(bytes.length);
|
|
107
|
-
content.update(bytes);
|
|
108
|
-
} else part('directory');
|
|
109
|
-
} catch (error) {
|
|
110
|
-
if (error.code !== 'ENOENT') throw error;
|
|
111
|
-
part('deleted');
|
|
112
|
-
}
|
|
113
|
-
digests[file] = content.digest('hex');
|
|
114
|
-
add(file);
|
|
115
|
-
add(digests[file]);
|
|
116
|
-
}
|
|
117
|
-
return { fingerprint: hash.digest('hex'), context, files: digests, tools: identities };
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export const snapshot = (root, context) => captureSnapshot(root, context).fingerprint;
|
|
121
|
-
|
|
122
|
-
export const clean = (root) => !git(root, 'status', '--porcelain', '--untracked-files=all');
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { lstatSync, readFileSync, readlinkSync } from 'node:fs';
|
|
4
|
+
import { dirname, join, resolve } from 'node:path';
|
|
5
|
+
import { executable } from './execute.mjs';
|
|
6
|
+
|
|
7
|
+
export const git = (root, ...args) =>
|
|
8
|
+
execFileSync('git', args, {
|
|
9
|
+
cwd: root,
|
|
10
|
+
encoding: 'utf8',
|
|
11
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
12
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
13
|
+
}).trimEnd();
|
|
14
|
+
const nul = (value) => value.split('\0').filter(Boolean);
|
|
15
|
+
|
|
16
|
+
export function changedFiles(root, base, { committed = false, head = 'HEAD' } = {}) {
|
|
17
|
+
const ancestor = git(root, 'merge-base', base, head);
|
|
18
|
+
const changes = nul(git(root, 'diff', '--name-only', '--no-renames', '-z', ancestor, head));
|
|
19
|
+
if (!committed)
|
|
20
|
+
changes.push(
|
|
21
|
+
...nul(git(root, 'diff', '--name-only', '--no-renames', '-z', 'HEAD')),
|
|
22
|
+
...nul(git(root, 'ls-files', '--others', '--exclude-standard', '-z')),
|
|
23
|
+
);
|
|
24
|
+
return [...new Set(changes)].sort();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function toolIdentity(root, tool) {
|
|
28
|
+
const name = typeof tool === 'string' ? tool : tool.name;
|
|
29
|
+
const cwd = typeof tool === 'string' ? root : resolve(root, tool.cwd ?? '.');
|
|
30
|
+
const command = executable(cwd, name);
|
|
31
|
+
if (!command) return { name, cwd, command: null };
|
|
32
|
+
if (name === 'node') return { name, cwd, command, version: process.version };
|
|
33
|
+
if (command[0] === process.execPath && command[1]) {
|
|
34
|
+
for (let directory = dirname(command[1]); ; directory = dirname(directory)) {
|
|
35
|
+
try {
|
|
36
|
+
const manifest = readFileSync(join(directory, 'package.json'), 'utf8');
|
|
37
|
+
const entry = createHash('sha256').update(readFileSync(command[1])).digest('hex');
|
|
38
|
+
return { name, cwd, command, manifest, entry };
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (error.code !== 'ENOENT') throw error;
|
|
41
|
+
}
|
|
42
|
+
if (dirname(directory) === directory) break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
let version;
|
|
46
|
+
try {
|
|
47
|
+
version = execFileSync(command[0], [...command.slice(1), '--version'], {
|
|
48
|
+
cwd,
|
|
49
|
+
encoding: 'utf8',
|
|
50
|
+
timeout: 5000,
|
|
51
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
52
|
+
});
|
|
53
|
+
} catch {
|
|
54
|
+
version = 'version-unavailable';
|
|
55
|
+
}
|
|
56
|
+
return { name, cwd, command, version };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function captureSnapshot(
|
|
60
|
+
root,
|
|
61
|
+
{ base, version, config, environment = [], tools = [], inputs = [] },
|
|
62
|
+
) {
|
|
63
|
+
const hash = createHash('sha256');
|
|
64
|
+
const add = (value) => {
|
|
65
|
+
const text = String(value);
|
|
66
|
+
hash.update(`${Buffer.byteLength(text)}:`);
|
|
67
|
+
hash.update(text);
|
|
68
|
+
};
|
|
69
|
+
add(version);
|
|
70
|
+
add(process.version);
|
|
71
|
+
add(process.platform);
|
|
72
|
+
add(config);
|
|
73
|
+
for (const file of ['node_modules/.package-lock.json', ...inputs]) {
|
|
74
|
+
add(file);
|
|
75
|
+
try {
|
|
76
|
+
const bytes = readFileSync(resolve(root, file));
|
|
77
|
+
add(bytes.length);
|
|
78
|
+
hash.update(bytes);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error.code !== 'ENOENT') throw error;
|
|
81
|
+
add('absent');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
for (const key of environment) {
|
|
85
|
+
add(key);
|
|
86
|
+
add(process.env[key] ?? '');
|
|
87
|
+
}
|
|
88
|
+
const context = hash.copy().digest('hex');
|
|
89
|
+
const identities = tools.map((tool) => toolIdentity(root, tool));
|
|
90
|
+
for (const identity of identities) add(JSON.stringify(identity));
|
|
91
|
+
add(git(root, 'rev-parse', 'HEAD'));
|
|
92
|
+
add(git(root, 'rev-parse', `${base}^{commit}`));
|
|
93
|
+
const files = new Set(
|
|
94
|
+
nul(git(root, 'ls-files', '--cached', '--others', '--exclude-standard', '-z')),
|
|
95
|
+
);
|
|
96
|
+
const digests = {};
|
|
97
|
+
for (const file of [...files].sort()) {
|
|
98
|
+
const content = createHash('sha256');
|
|
99
|
+
const part = (value) => content.update(JSON.stringify(value));
|
|
100
|
+
try {
|
|
101
|
+
const stat = lstatSync(resolve(root, file));
|
|
102
|
+
part(stat.mode & 0o111);
|
|
103
|
+
if (stat.isSymbolicLink()) part(readlinkSync(resolve(root, file)));
|
|
104
|
+
else if (stat.isFile()) {
|
|
105
|
+
const bytes = readFileSync(resolve(root, file));
|
|
106
|
+
part(bytes.length);
|
|
107
|
+
content.update(bytes);
|
|
108
|
+
} else part('directory');
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (error.code !== 'ENOENT') throw error;
|
|
111
|
+
part('deleted');
|
|
112
|
+
}
|
|
113
|
+
digests[file] = content.digest('hex');
|
|
114
|
+
add(file);
|
|
115
|
+
add(digests[file]);
|
|
116
|
+
}
|
|
117
|
+
return { fingerprint: hash.digest('hex'), context, files: digests, tools: identities };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export const snapshot = (root, context) => captureSnapshot(root, context).fingerprint;
|
|
121
|
+
|
|
122
|
+
export const clean = (root) => !git(root, 'status', '--porcelain', '--untracked-files=all');
|