@kurvball/rehearse 1.9.0 → 1.10.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/README.md +3 -2
- package/dist/commands/init.js +52 -7
- package/dist/detect/detect.d.ts +6 -0
- package/dist/detect/detect.js +48 -0
- package/dist/detect/finding.d.ts +18 -0
- package/dist/detect/finding.js +4 -0
- package/dist/detect/globs.d.ts +10 -0
- package/dist/detect/globs.js +43 -0
- package/dist/detect/index.d.ts +2 -0
- package/dist/detect/index.js +2 -0
- package/dist/detect/lockfiles.d.ts +15 -0
- package/dist/detect/lockfiles.js +64 -0
- package/dist/detect/paths.d.ts +5 -0
- package/dist/detect/paths.js +39 -0
- package/dist/detect/pieces.d.ts +14 -0
- package/dist/detect/pieces.js +119 -0
- package/dist/detect/prepare.d.ts +14 -0
- package/dist/detect/prepare.js +78 -0
- package/dist/detect/repository.d.ts +33 -0
- package/dist/detect/repository.js +166 -0
- package/dist/detect/scenarios.d.ts +26 -0
- package/dist/detect/scenarios.js +170 -0
- package/dist/detect/scripts.d.ts +46 -0
- package/dist/detect/scripts.js +233 -0
- package/dist/detect/services.d.ts +16 -0
- package/dist/detect/services.js +171 -0
- package/dist/detect/write.d.ts +18 -0
- package/dist/detect/write.js +115 -0
- package/dist/manifest/index.d.ts +1 -1
- package/dist/manifest/index.js +1 -1
- package/dist/manifest/init.d.ts +47 -1
- package/dist/manifest/init.js +19 -16
- package/dist/scenarios/glob.d.ts +1 -0
- package/dist/scenarios/glob.js +10 -3
- package/dist/scenarios/index.d.ts +1 -1
- package/dist/scenarios/index.js +1 -1
- package/llms.txt +1 -1
- package/package.json +1 -1
- package/schema/v1/init.schema.json +69 -1
- package/schema/v1/samples/init.json +46 -1
- package/skill/rehearse/SKILL.md +5 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parse } from 'yaml';
|
|
4
|
+
import { gitOr, topOf } from '../git/index.js';
|
|
5
|
+
import { findFiles, globToRegExp, NEVER_WALKED, toPosix } from '../scenarios/index.js';
|
|
6
|
+
import { lockIn, managerNamed } from './lockfiles.js';
|
|
7
|
+
import { joined, readText, within } from './paths.js';
|
|
8
|
+
export { isFile, joined, readText, within } from './paths.js';
|
|
9
|
+
const NOT_AN_APP = new Set(['test', 'tests', '__tests__', 'fixtures', 'examples', 'e2e']);
|
|
10
|
+
const FILE_LIMIT = 100_000;
|
|
11
|
+
const DEPENDENCY_FIELDS = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];
|
|
12
|
+
function readJson(file) {
|
|
13
|
+
const text = readText(file);
|
|
14
|
+
if (text === null)
|
|
15
|
+
return null;
|
|
16
|
+
try {
|
|
17
|
+
const value = JSON.parse(text.replace(/^\uFEFF/, ''));
|
|
18
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value : null;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const stringsOf = (value) => value && typeof value === 'object' && !Array.isArray(value)
|
|
25
|
+
? Object.fromEntries(Object.entries(value).filter((entry) => typeof entry[1] === 'string'))
|
|
26
|
+
: {};
|
|
27
|
+
export const dependenciesOf = (json) => DEPENDENCY_FIELDS.flatMap((field) => Object.keys(stringsOf(json[field])));
|
|
28
|
+
function listFiles(root, git) {
|
|
29
|
+
const listed = git ? gitOr(root, ['ls-files', '-z', '--cached', '--others', '--exclude-standard']) : null;
|
|
30
|
+
const files = listed === null ? findFiles(root, ['**/*'], FILE_LIMIT) : listed.split('\0');
|
|
31
|
+
return [...new Set(files.filter(Boolean))]
|
|
32
|
+
.filter((file) => !file.split('/').some((part) => NEVER_WALKED.has(part)))
|
|
33
|
+
.sort();
|
|
34
|
+
}
|
|
35
|
+
function workspaceGlobs(root, json) {
|
|
36
|
+
const declared = json?.workspaces;
|
|
37
|
+
const listed = Array.isArray(declared)
|
|
38
|
+
? declared
|
|
39
|
+
: declared && typeof declared === 'object'
|
|
40
|
+
? declared.packages
|
|
41
|
+
: undefined;
|
|
42
|
+
const fromYaml = () => {
|
|
43
|
+
const text = readText(path.join(root, 'pnpm-workspace.yaml'));
|
|
44
|
+
if (text === null)
|
|
45
|
+
return undefined;
|
|
46
|
+
try {
|
|
47
|
+
return parse(text)?.packages;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const globs = Array.isArray(listed) ? listed : fromYaml();
|
|
54
|
+
if (!Array.isArray(globs))
|
|
55
|
+
return null;
|
|
56
|
+
const strings = globs.filter((g) => typeof g === 'string');
|
|
57
|
+
return strings.length ? strings : null;
|
|
58
|
+
}
|
|
59
|
+
export function packageAt(root, dir) {
|
|
60
|
+
const file = joined(dir, 'package.json');
|
|
61
|
+
const json = readJson(path.join(root, file));
|
|
62
|
+
if (!json)
|
|
63
|
+
return null;
|
|
64
|
+
return {
|
|
65
|
+
dir,
|
|
66
|
+
file,
|
|
67
|
+
name: typeof json.name === 'string' && json.name ? json.name : null,
|
|
68
|
+
json,
|
|
69
|
+
scripts: stringsOf(json.scripts),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function packagesOf(root, files, globs) {
|
|
73
|
+
const manifests = files.filter((f) => f === 'package.json' || f.endsWith('/package.json'));
|
|
74
|
+
const chosen = globs
|
|
75
|
+
? manifests.filter((f) => {
|
|
76
|
+
const matches = (g) => globToRegExp(joined(g.replace(/^!/, ''), 'package.json')).test(f);
|
|
77
|
+
return (f === 'package.json' ||
|
|
78
|
+
(globs.some((g) => !g.startsWith('!') && matches(g)) &&
|
|
79
|
+
!globs.some((g) => g.startsWith('!') && matches(g))));
|
|
80
|
+
})
|
|
81
|
+
: manifests.filter((f) => {
|
|
82
|
+
const parts = f.split('/');
|
|
83
|
+
return parts.length === 1 || (parts.length === 2 && !NOT_AN_APP.has(parts[0]));
|
|
84
|
+
});
|
|
85
|
+
return [...new Set(chosen.map((f) => joined(path.posix.dirname(f))))]
|
|
86
|
+
.map((dir) => packageAt(root, dir))
|
|
87
|
+
.filter((p) => p !== null);
|
|
88
|
+
}
|
|
89
|
+
function lockingOf(root, prefix, packages, workspaces) {
|
|
90
|
+
const declaredAt = (dir) => managerNamed(packageAt(root, dir)?.json.packageManager);
|
|
91
|
+
const rootDeclared = declaredAt('');
|
|
92
|
+
const above = prefix
|
|
93
|
+
? prefix.split('/').map((_, i) => Array.from({ length: i + 1 }, () => '..').join('/'))
|
|
94
|
+
: [];
|
|
95
|
+
const nearest = ['', ...above]
|
|
96
|
+
.map((dir) => ({ dir, locked: lockIn(root, dir, rootDeclared) }))
|
|
97
|
+
.find((l) => l.locked.manager !== null);
|
|
98
|
+
const own = workspaces
|
|
99
|
+
? []
|
|
100
|
+
: packages
|
|
101
|
+
.filter((p) => p.dir)
|
|
102
|
+
.map((p) => ({ dir: p.dir, locked: lockIn(root, p.dir, declaredAt(p.dir) ?? rootDeclared) }))
|
|
103
|
+
.filter((l) => l.locked.manager !== null);
|
|
104
|
+
const all = [...(nearest ? [nearest] : []), ...own];
|
|
105
|
+
return {
|
|
106
|
+
lockfiles: all.flatMap((l) => (l.locked.lockfile ? [l.locked.lockfile] : [])),
|
|
107
|
+
managers: all.map((l) => ({ dir: l.dir, manager: l.locked.manager })),
|
|
108
|
+
findings: all.flatMap((l) => (l.locked.finding ? [l.locked.finding] : [])),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
export function readRepository(root) {
|
|
112
|
+
const top = topOf(root);
|
|
113
|
+
const git = top !== null;
|
|
114
|
+
const prefix = git ? toPosix(path.relative(top, root)) : '';
|
|
115
|
+
const files = listFiles(root, git);
|
|
116
|
+
const rootJson = readJson(path.join(root, 'package.json'));
|
|
117
|
+
const globs = workspaceGlobs(root, rootJson);
|
|
118
|
+
const packages = packagesOf(root, files, globs);
|
|
119
|
+
const workspaces = globs !== null;
|
|
120
|
+
const locking = lockingOf(root, prefix, packages, workspaces);
|
|
121
|
+
const fallback = managerNamed(rootJson?.packageManager) ?? 'npm';
|
|
122
|
+
return {
|
|
123
|
+
root,
|
|
124
|
+
prefix,
|
|
125
|
+
git,
|
|
126
|
+
files,
|
|
127
|
+
packages,
|
|
128
|
+
workspaces,
|
|
129
|
+
lockfiles: locking.lockfiles,
|
|
130
|
+
locking: locking.findings,
|
|
131
|
+
managerOf: (dir) => locking.managers.filter((l) => l.dir.startsWith('..') || within(l.dir, joined(dir, 'x'))).at(-1)
|
|
132
|
+
?.manager ?? fallback,
|
|
133
|
+
ignored: (file) => git && gitOr(root, ['check-ignore', '-q', file]) !== null,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
export function declares(repo, dir, name) {
|
|
137
|
+
for (let at = dir;; at = joined(at, '..')) {
|
|
138
|
+
const json = readJson(path.join(repo.root, at, 'package.json'));
|
|
139
|
+
if (json && dependenciesOf(json).includes(name))
|
|
140
|
+
return true;
|
|
141
|
+
if (at === '' || at.startsWith('..'))
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
createRequire(path.join(repo.root, dir, 'package.json')).resolve(`${name}/package.json`);
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export function defaultBranch(root) {
|
|
153
|
+
const head = gitOr(root, ['symbolic-ref', '--quiet', '--short', 'refs/remotes/origin/HEAD']);
|
|
154
|
+
const local = (branch) => gitOr(root, ['rev-parse', '--verify', '--quiet', `refs/heads/${branch}`]) !== null;
|
|
155
|
+
if (head?.startsWith('origin/')) {
|
|
156
|
+
const branch = head.slice('origin/'.length);
|
|
157
|
+
return { branch, from: 'origin/HEAD', local: local(branch) };
|
|
158
|
+
}
|
|
159
|
+
for (const branch of ['main', 'master']) {
|
|
160
|
+
if (local(branch))
|
|
161
|
+
return { branch, from: `refs/heads/${branch}`, local: true };
|
|
162
|
+
if (gitOr(root, ['rev-parse', '--verify', '--quiet', `refs/remotes/origin/${branch}`]) !== null)
|
|
163
|
+
return { branch, from: `refs/remotes/origin/${branch}`, local: false };
|
|
164
|
+
}
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Finding } from './finding.ts';
|
|
2
|
+
import { type PiecesBlock } from './pieces.ts';
|
|
3
|
+
import { type Repository } from './repository.ts';
|
|
4
|
+
import type { Service } from './services.ts';
|
|
5
|
+
export interface Blocks {
|
|
6
|
+
pieces: PiecesBlock | null;
|
|
7
|
+
flows: {
|
|
8
|
+
paths: string[];
|
|
9
|
+
service: string | null;
|
|
10
|
+
count: number;
|
|
11
|
+
} | null;
|
|
12
|
+
calls: {
|
|
13
|
+
paths: string[];
|
|
14
|
+
service: string | null;
|
|
15
|
+
count: number;
|
|
16
|
+
} | null;
|
|
17
|
+
files: {
|
|
18
|
+
paths: string[];
|
|
19
|
+
count: number;
|
|
20
|
+
} | null;
|
|
21
|
+
}
|
|
22
|
+
export declare function scenariosOf(repo: Repository, services: readonly Service[]): {
|
|
23
|
+
blocks: Blocks;
|
|
24
|
+
findings: Finding[];
|
|
25
|
+
pieceDirs: string[];
|
|
26
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { counted, GUIDE } from './finding.js';
|
|
3
|
+
import { either, extensionsOf, globOf, unreadable } from './globs.js';
|
|
4
|
+
import { PIECE, piecesOf } from './pieces.js';
|
|
5
|
+
import { readText, within } from './repository.js';
|
|
6
|
+
const CALL = /\.hurl$/;
|
|
7
|
+
const FILE_SCENARIO = /\.rehearse\.yaml$/;
|
|
8
|
+
const FLOW = /\.flow\.(ya?ml)$/;
|
|
9
|
+
const REACH = /(?:^|\/)(?:reach|[^/]+\.reach)\.(ts|mts|js|mjs)$/;
|
|
10
|
+
const OPENAPI = /(?:^|\/)(?:openapi|swagger)\.(?:ya?ml|json)$/;
|
|
11
|
+
const PLAYWRIGHT = /(?:^|\/)playwright\.config\.[cm]?[jt]s$/;
|
|
12
|
+
const SENT_AS = /^[ \t]*Authorization[ \t]*:[^\n]*?\{\{\s*([A-Za-z_][\w-]*)\s*\}\}/gim;
|
|
13
|
+
function servedBy(services, files, node) {
|
|
14
|
+
const ranked = services
|
|
15
|
+
.map((s) => ({ s, holds: files.filter((f) => within(s.pkg.dir, f)).length }))
|
|
16
|
+
.filter((r) => r.holds > 0)
|
|
17
|
+
.sort((a, b) => b.holds - a.holds || b.s.pkg.dir.length - a.s.pkg.dir.length);
|
|
18
|
+
return (ranked[0]?.s ?? services.find((s) => (s.server === 'node') === node) ?? services[0])?.name ?? null;
|
|
19
|
+
}
|
|
20
|
+
function sentAs(texts) {
|
|
21
|
+
const people = new Set();
|
|
22
|
+
for (const text of texts)
|
|
23
|
+
for (const m of text.matchAll(SENT_AS))
|
|
24
|
+
people.add(m[1]);
|
|
25
|
+
return [...people].sort();
|
|
26
|
+
}
|
|
27
|
+
const shortList = (items) => `${items.slice(0, 3).join(', ')}${items.length > 3 ? ` and ${items.length - 3} more` : ''}`;
|
|
28
|
+
function calls(repo, files, services, fallback) {
|
|
29
|
+
const texts = files.map((f) => readText(path.join(repo.root, f)) ?? '');
|
|
30
|
+
const glob = globOf(files, '*.hurl');
|
|
31
|
+
const askApi = texts.some((t) => /\{\{\s*api\s*\}\}/.test(t));
|
|
32
|
+
const many = counted(files.length, '.hurl file', '.hurl files');
|
|
33
|
+
const one = files.length === 1;
|
|
34
|
+
if (askApi && services.length === 0)
|
|
35
|
+
return {
|
|
36
|
+
block: null,
|
|
37
|
+
finding: {
|
|
38
|
+
kind: 'calls',
|
|
39
|
+
from: glob,
|
|
40
|
+
count: files.length,
|
|
41
|
+
written: false,
|
|
42
|
+
why: `${many} ${one ? 'sends' : 'send'} to {{api}}, and no service was found to answer ${one ? 'it' : 'them'}.`,
|
|
43
|
+
next: `Write the service that answers them under services, then add calls: ['${glob}'].`,
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
const chosen = askApi ? servedBy(services, files, true) : null;
|
|
47
|
+
const people = sentAs(texts);
|
|
48
|
+
return {
|
|
49
|
+
block: { paths: [glob], service: chosen !== fallback ? chosen : null, count: files.length },
|
|
50
|
+
finding: {
|
|
51
|
+
kind: 'calls',
|
|
52
|
+
from: glob,
|
|
53
|
+
count: files.length,
|
|
54
|
+
written: true,
|
|
55
|
+
why: `${many}${chosen ? `, sent to ${chosen}` : ''}`,
|
|
56
|
+
...(people.length > 0 && {
|
|
57
|
+
next: `They use ${shortList(people.map((p) => `{{${p}}}`))}: say who signs in under people and signIn: ${GUIDE}/concepts/blocks/#people`,
|
|
58
|
+
}),
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function flows(repo, files, services, fallback) {
|
|
63
|
+
const findings = [];
|
|
64
|
+
const yaml = files.filter((f) => FLOW.test(f));
|
|
65
|
+
const broken = yaml.length ? unreadable(repo, yaml, 'flow') : null;
|
|
66
|
+
const yamlGlob = yaml.length ? globOf(yaml, `*.flow.${either(extensionsOf(yaml, FLOW))}`) : null;
|
|
67
|
+
if (broken && yamlGlob)
|
|
68
|
+
findings.push({
|
|
69
|
+
kind: 'flows',
|
|
70
|
+
from: broken.file,
|
|
71
|
+
written: false,
|
|
72
|
+
why: `${broken.says} It doesn't read as a flow, so ${yamlGlob} would fail rehearse check.`,
|
|
73
|
+
next: `Fix it to the flow format (${GUIDE}/reference/scenario-files/), then add flows: ['${yamlGlob}'].`,
|
|
74
|
+
});
|
|
75
|
+
const reach = files.filter((f) => {
|
|
76
|
+
if (!REACH.test(f))
|
|
77
|
+
return false;
|
|
78
|
+
const text = readText(path.join(repo.root, f)) ?? '';
|
|
79
|
+
return /\bexport\s+default\b/.test(text) && /\bsteps\s*:/.test(text);
|
|
80
|
+
});
|
|
81
|
+
const plain = reach.some((f) => path.posix.basename(f).startsWith('reach.'));
|
|
82
|
+
const named = reach.some((f) => !path.posix.basename(f).startsWith('reach.'));
|
|
83
|
+
const stem = plain && named ? '{reach,*.reach}' : plain ? 'reach' : '*.reach';
|
|
84
|
+
const read = [...(broken ? [] : yaml), ...reach];
|
|
85
|
+
const paths = [
|
|
86
|
+
...(yamlGlob && !broken ? [yamlGlob] : []),
|
|
87
|
+
...(reach.length ? [globOf(reach, `${stem}.${either(extensionsOf(reach, REACH))}`)] : []),
|
|
88
|
+
];
|
|
89
|
+
if (paths.length === 0)
|
|
90
|
+
return { block: null, findings };
|
|
91
|
+
const chosen = servedBy(services, read, false);
|
|
92
|
+
findings.unshift({
|
|
93
|
+
kind: 'flows',
|
|
94
|
+
from: paths.join(', '),
|
|
95
|
+
count: read.length,
|
|
96
|
+
written: true,
|
|
97
|
+
why: `${counted(read.length, 'flow file', 'flow files')}${chosen ? `, opening ${chosen}` : ''}`,
|
|
98
|
+
});
|
|
99
|
+
return {
|
|
100
|
+
block: { paths, service: chosen !== fallback ? chosen : null, count: read.length },
|
|
101
|
+
findings,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function fileScenarios(repo, files) {
|
|
105
|
+
const glob = globOf(files, '*.rehearse.yaml');
|
|
106
|
+
const broken = unreadable(repo, files, 'file-scenario');
|
|
107
|
+
if (broken)
|
|
108
|
+
return {
|
|
109
|
+
block: null,
|
|
110
|
+
finding: {
|
|
111
|
+
kind: 'files',
|
|
112
|
+
from: broken.file,
|
|
113
|
+
written: false,
|
|
114
|
+
why: `${broken.says} It doesn't read as a file scenario, so ${glob} would fail rehearse check.`,
|
|
115
|
+
next: `Fix it to the file scenario format (${GUIDE}/reference/scenario-files/), then add files: ['${glob}'].`,
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
return {
|
|
119
|
+
block: { paths: [glob], count: files.length },
|
|
120
|
+
finding: {
|
|
121
|
+
kind: 'files',
|
|
122
|
+
from: glob,
|
|
123
|
+
count: files.length,
|
|
124
|
+
written: true,
|
|
125
|
+
why: counted(files.length, 'file scenario', 'file scenarios'),
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
export function scenariosOf(repo, services) {
|
|
130
|
+
const of = (re) => repo.files.filter((f) => re.test(f));
|
|
131
|
+
const fallback = services.some((s) => s.name === 'api') ? 'api' : (services[0]?.name ?? null);
|
|
132
|
+
const pieces = piecesOf(repo, of(PIECE));
|
|
133
|
+
const flow = flows(repo, of(FLOW).concat(of(REACH)), services, fallback);
|
|
134
|
+
const hurl = of(CALL);
|
|
135
|
+
const call = hurl.length ? calls(repo, hurl, services, fallback) : null;
|
|
136
|
+
const scenarioFiles = of(FILE_SCENARIO);
|
|
137
|
+
const file = scenarioFiles.length ? fileScenarios(repo, scenarioFiles) : null;
|
|
138
|
+
const findings = [
|
|
139
|
+
...pieces.findings,
|
|
140
|
+
...flow.findings,
|
|
141
|
+
...(call ? [call.finding] : []),
|
|
142
|
+
...(file ? [file.finding] : []),
|
|
143
|
+
];
|
|
144
|
+
for (const from of of(OPENAPI))
|
|
145
|
+
findings.push({
|
|
146
|
+
kind: 'openapi',
|
|
147
|
+
from,
|
|
148
|
+
written: false,
|
|
149
|
+
why: "Rehearse compares calls written in Hurl, and doesn't read an OpenAPI description.",
|
|
150
|
+
next: `Write a .hurl file beside the handlers, one call for each answer that matters: ${GUIDE}/guides/calls/`,
|
|
151
|
+
});
|
|
152
|
+
for (const from of of(PLAYWRIGHT))
|
|
153
|
+
findings.push({
|
|
154
|
+
kind: 'playwright',
|
|
155
|
+
from,
|
|
156
|
+
written: false,
|
|
157
|
+
why: "Rehearse doesn't run Playwright tests: it compares flows, the steps a person takes, by accessible name.",
|
|
158
|
+
next: `Write a *.flow.yaml for each path that matters: ${GUIDE}/guides/flows/. A future rehearse record will write one from a session you drive.`,
|
|
159
|
+
});
|
|
160
|
+
return {
|
|
161
|
+
blocks: {
|
|
162
|
+
pieces: pieces.block,
|
|
163
|
+
flows: flow.block,
|
|
164
|
+
calls: call?.block ?? null,
|
|
165
|
+
files: file?.block ?? null,
|
|
166
|
+
},
|
|
167
|
+
findings,
|
|
168
|
+
pieceDirs: pieces.dirs,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export type Framework = 'vite' | 'next' | 'astro';
|
|
2
|
+
export type Read = {
|
|
3
|
+
as: 'framework';
|
|
4
|
+
framework: Framework;
|
|
5
|
+
script: string;
|
|
6
|
+
words: string[];
|
|
7
|
+
} | {
|
|
8
|
+
as: 'node';
|
|
9
|
+
script: string;
|
|
10
|
+
entry: string | null;
|
|
11
|
+
tool: string;
|
|
12
|
+
} | {
|
|
13
|
+
as: 'tool';
|
|
14
|
+
} | {
|
|
15
|
+
as: 'others';
|
|
16
|
+
script: string;
|
|
17
|
+
tool: string;
|
|
18
|
+
} | {
|
|
19
|
+
as: 'unknown';
|
|
20
|
+
script: string;
|
|
21
|
+
tool: string;
|
|
22
|
+
} | {
|
|
23
|
+
as: 'fixed';
|
|
24
|
+
script: string;
|
|
25
|
+
port: string;
|
|
26
|
+
} | {
|
|
27
|
+
as: 'together';
|
|
28
|
+
script: string;
|
|
29
|
+
how: '&' | '|';
|
|
30
|
+
} | {
|
|
31
|
+
as: 'handover';
|
|
32
|
+
script: string;
|
|
33
|
+
via: string;
|
|
34
|
+
};
|
|
35
|
+
export declare const FILE_LIKE: RegExp;
|
|
36
|
+
export declare function wordsOf(segment: string): string[];
|
|
37
|
+
export declare function unwrap(words: readonly string[]): {
|
|
38
|
+
words: string[];
|
|
39
|
+
env: string[];
|
|
40
|
+
};
|
|
41
|
+
export declare function segmentsOf(text: string): {
|
|
42
|
+
segments: string[];
|
|
43
|
+
together: '&' | '|' | null;
|
|
44
|
+
};
|
|
45
|
+
export declare const readScript: (scripts: Readonly<Record<string, string>>, script: string) => Read;
|
|
46
|
+
export declare const quoted: (word: string) => string;
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
2
|
+
const MANAGERS = new Set(['npm', 'pnpm', 'yarn', 'bun']);
|
|
3
|
+
const RUNS_OTHERS = new Set([
|
|
4
|
+
'turbo',
|
|
5
|
+
'nx',
|
|
6
|
+
'lerna',
|
|
7
|
+
'concurrently',
|
|
8
|
+
'npm-run-all',
|
|
9
|
+
'npm-run-all2',
|
|
10
|
+
'run-p',
|
|
11
|
+
'run-s',
|
|
12
|
+
'wireit',
|
|
13
|
+
'ultra',
|
|
14
|
+
]);
|
|
15
|
+
const WORKSPACE_FLAGS = /^(?:-r|--recursive|-F|--filter(?:=.*)?|-w|--workspace(?:=.*)?|--workspaces|-ws|--parallel)$/;
|
|
16
|
+
const NOT_SERVERS = new Set([
|
|
17
|
+
'tsc',
|
|
18
|
+
'tsup',
|
|
19
|
+
'rollup',
|
|
20
|
+
'esbuild',
|
|
21
|
+
'webpack',
|
|
22
|
+
'babel',
|
|
23
|
+
'swc',
|
|
24
|
+
'unbuild',
|
|
25
|
+
'storybook',
|
|
26
|
+
'start-storybook',
|
|
27
|
+
'build-storybook',
|
|
28
|
+
'vitest',
|
|
29
|
+
'jest',
|
|
30
|
+
'mocha',
|
|
31
|
+
'eslint',
|
|
32
|
+
'oxlint',
|
|
33
|
+
'prettier',
|
|
34
|
+
'tailwindcss',
|
|
35
|
+
'postcss',
|
|
36
|
+
'sass',
|
|
37
|
+
'chokidar',
|
|
38
|
+
]);
|
|
39
|
+
const NODE_RUNNERS = new Set(['node', 'tsx', 'ts-node', 'ts-node-dev', 'nodemon', 'node-dev', 'bun']);
|
|
40
|
+
const TAKES_A_VALUE = new Set([
|
|
41
|
+
'-r',
|
|
42
|
+
'--require',
|
|
43
|
+
'--import',
|
|
44
|
+
'--loader',
|
|
45
|
+
'--experimental-loader',
|
|
46
|
+
'-C',
|
|
47
|
+
'--conditions',
|
|
48
|
+
'--env-file',
|
|
49
|
+
'--watch-path',
|
|
50
|
+
'--tsconfig',
|
|
51
|
+
'-P',
|
|
52
|
+
'--project',
|
|
53
|
+
'--compiler',
|
|
54
|
+
'-O',
|
|
55
|
+
'--compiler-options',
|
|
56
|
+
'--title',
|
|
57
|
+
]);
|
|
58
|
+
const NODEMON_TAKES_A_VALUE = new Set([
|
|
59
|
+
'-w',
|
|
60
|
+
'--watch',
|
|
61
|
+
'-e',
|
|
62
|
+
'--ext',
|
|
63
|
+
'-i',
|
|
64
|
+
'--ignore',
|
|
65
|
+
'-x',
|
|
66
|
+
'--exec',
|
|
67
|
+
'--delay',
|
|
68
|
+
'-d',
|
|
69
|
+
'--signal',
|
|
70
|
+
'-s',
|
|
71
|
+
]);
|
|
72
|
+
export const FILE_LIKE = /\.(?:[cm]?[jt]s|tsx|jsx)$/;
|
|
73
|
+
export function wordsOf(segment) {
|
|
74
|
+
const words = [];
|
|
75
|
+
const word = /"((?:[^"\\]|\\.)*)"|'([^']*)'|(\S+)/g;
|
|
76
|
+
for (let m = word.exec(segment); m; m = word.exec(segment))
|
|
77
|
+
words.push(m[1] ?? m[2] ?? m[3]);
|
|
78
|
+
return words;
|
|
79
|
+
}
|
|
80
|
+
export function unwrap(words) {
|
|
81
|
+
const env = [];
|
|
82
|
+
let i = 0;
|
|
83
|
+
for (;;) {
|
|
84
|
+
while (i < words.length && ASSIGNMENT.test(words[i]))
|
|
85
|
+
env.push(words[i++]);
|
|
86
|
+
const word = words[i];
|
|
87
|
+
if (word === 'cross-env' ||
|
|
88
|
+
word === 'cross-env-shell' ||
|
|
89
|
+
word === 'npx' ||
|
|
90
|
+
word === 'pnpx' ||
|
|
91
|
+
word === 'bunx') {
|
|
92
|
+
i += 1;
|
|
93
|
+
while (i < words.length && words[i].startsWith('-'))
|
|
94
|
+
i += 1;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (word === 'dotenv' || word === 'env-cmd') {
|
|
98
|
+
const end = words.indexOf('--', i);
|
|
99
|
+
if (end !== -1)
|
|
100
|
+
i = end + 1;
|
|
101
|
+
else
|
|
102
|
+
for (i += 1; i < words.length && words[i].startsWith('-');)
|
|
103
|
+
i += 2;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (word !== undefined && MANAGERS.has(word) && words[i + 1] === 'exec') {
|
|
107
|
+
i += words[i + 2] === '--' ? 3 : 2;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
return { words: words.slice(i), env };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function entryOf(tool, rest) {
|
|
114
|
+
const valued = tool === 'nodemon' ? NODEMON_TAKES_A_VALUE : TAKES_A_VALUE;
|
|
115
|
+
for (let i = 0; i < rest.length; i += 1) {
|
|
116
|
+
const word = rest[i];
|
|
117
|
+
if (word.startsWith('-')) {
|
|
118
|
+
if (valued.has(word))
|
|
119
|
+
i += 1;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if ((tool === 'tsx' && word === 'watch') || (tool === 'bun' && word === 'run'))
|
|
123
|
+
continue;
|
|
124
|
+
return word;
|
|
125
|
+
}
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
export function segmentsOf(text) {
|
|
129
|
+
const segments = [];
|
|
130
|
+
let current = '';
|
|
131
|
+
let quote = null;
|
|
132
|
+
let together = null;
|
|
133
|
+
const cut = () => {
|
|
134
|
+
segments.push(current.trim());
|
|
135
|
+
current = '';
|
|
136
|
+
};
|
|
137
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
138
|
+
const c = text[i];
|
|
139
|
+
if (c === '\\' && quote !== "'" && i + 1 < text.length) {
|
|
140
|
+
current += c + text[(i += 1)];
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (quote) {
|
|
144
|
+
if (c === quote)
|
|
145
|
+
quote = null;
|
|
146
|
+
current += c;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (c === "'" || c === '"') {
|
|
150
|
+
quote = c;
|
|
151
|
+
current += c;
|
|
152
|
+
}
|
|
153
|
+
else if (text.startsWith('&&', i) || text.startsWith('||', i)) {
|
|
154
|
+
cut();
|
|
155
|
+
i += 1;
|
|
156
|
+
}
|
|
157
|
+
else if (c === ';')
|
|
158
|
+
cut();
|
|
159
|
+
else if (c === '&' && text[i - 1] !== '>' && text[i + 1] !== '>') {
|
|
160
|
+
together ??= '&';
|
|
161
|
+
cut();
|
|
162
|
+
}
|
|
163
|
+
else if (c === '|') {
|
|
164
|
+
together ??= '|';
|
|
165
|
+
cut();
|
|
166
|
+
}
|
|
167
|
+
else
|
|
168
|
+
current += c;
|
|
169
|
+
}
|
|
170
|
+
cut();
|
|
171
|
+
return { segments: segments.filter(Boolean), together };
|
|
172
|
+
}
|
|
173
|
+
const PORT_FLAGS = {
|
|
174
|
+
vite: ['--port'],
|
|
175
|
+
astro: ['--port'],
|
|
176
|
+
next: ['-p', '--port'],
|
|
177
|
+
};
|
|
178
|
+
function portIn(framework, words) {
|
|
179
|
+
const at = words.findIndex((w) => PORT_FLAGS[framework].some((f) => w === f || w.startsWith(`${f}=`)));
|
|
180
|
+
if (at === -1)
|
|
181
|
+
return null;
|
|
182
|
+
return words[at].includes('=') ? words[at] : `${words[at]} ${words[at + 1] ?? ''}`.trim();
|
|
183
|
+
}
|
|
184
|
+
function readFrom(scripts, script, top, depth) {
|
|
185
|
+
const { segments, together } = segmentsOf(scripts[script] ?? '');
|
|
186
|
+
if (together)
|
|
187
|
+
return { as: 'together', script: top, how: together };
|
|
188
|
+
const { words, env } = unwrap(wordsOf(segments.at(-1) ?? ''));
|
|
189
|
+
const [tool = '', ...rest] = words;
|
|
190
|
+
const first = rest[0];
|
|
191
|
+
if (MANAGERS.has(tool)) {
|
|
192
|
+
if (rest.some((w) => WORKSPACE_FLAGS.test(w)) || first === 'workspaces' || first === 'workspace')
|
|
193
|
+
return { as: 'others', script: top, tool: words.join(' ') };
|
|
194
|
+
const [named, after] = rest.filter((w) => !w.startsWith('-'));
|
|
195
|
+
const target = named === 'run' || named === 'run-script' ? after : named;
|
|
196
|
+
if (target && target !== script && target in scripts && depth < 3)
|
|
197
|
+
return tool === 'npm' && !rest.includes('--')
|
|
198
|
+
? { as: 'handover', script: top, via: words.join(' ') }
|
|
199
|
+
: readFrom(scripts, target, top, depth + 1);
|
|
200
|
+
if (tool === 'bun' && target && FILE_LIKE.test(target))
|
|
201
|
+
return { as: 'node', script: top, entry: target, tool };
|
|
202
|
+
return { as: 'unknown', script: top, tool: words.join(' ') };
|
|
203
|
+
}
|
|
204
|
+
if (RUNS_OTHERS.has(tool))
|
|
205
|
+
return { as: 'others', script: top, tool };
|
|
206
|
+
const framework = tool === 'vite' && first !== 'build' && first !== 'optimize' && first !== 'preview'
|
|
207
|
+
? 'vite'
|
|
208
|
+
: (tool === 'next' && (first === 'dev' || first === undefined || first.startsWith('-'))) ||
|
|
209
|
+
(tool === 'astro' && first === 'dev')
|
|
210
|
+
? tool
|
|
211
|
+
: null;
|
|
212
|
+
if (framework) {
|
|
213
|
+
const port = portIn(framework, words);
|
|
214
|
+
return port ? { as: 'fixed', script: top, port } : { as: 'framework', framework, script: top, words };
|
|
215
|
+
}
|
|
216
|
+
if (tool === 'vite' && first === 'preview')
|
|
217
|
+
return { as: 'unknown', script: top, tool: 'vite preview' };
|
|
218
|
+
if (tool === 'webpack' && first === 'serve')
|
|
219
|
+
return { as: 'unknown', script: top, tool: 'webpack serve' };
|
|
220
|
+
if ((tool === 'next' || tool === 'astro') && first !== 'build' && first !== 'check' && first !== 'sync')
|
|
221
|
+
return { as: 'unknown', script: top, tool: `${tool}${first ? ` ${first}` : ''}` };
|
|
222
|
+
if (NODE_RUNNERS.has(tool)) {
|
|
223
|
+
const port = env.find((e) => e.startsWith('PORT='));
|
|
224
|
+
return port
|
|
225
|
+
? { as: 'fixed', script: top, port }
|
|
226
|
+
: { as: 'node', script: top, entry: entryOf(tool, rest), tool };
|
|
227
|
+
}
|
|
228
|
+
if (NOT_SERVERS.has(tool) || tool === '' || tool === 'vite' || tool === 'next' || tool === 'astro')
|
|
229
|
+
return { as: 'tool' };
|
|
230
|
+
return { as: 'unknown', script: top, tool };
|
|
231
|
+
}
|
|
232
|
+
export const readScript = (scripts, script) => readFrom(scripts, script, script, 0);
|
|
233
|
+
export const quoted = (word) => /^[\w@%+=:./-]+$/.test(word) ? word : `'${word.replace(/'/g, `'\\''`)}'`;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Finding } from './finding.ts';
|
|
2
|
+
import { type Package, type Repository } from './repository.ts';
|
|
3
|
+
import { type Framework } from './scripts.ts';
|
|
4
|
+
export interface Service {
|
|
5
|
+
name: string;
|
|
6
|
+
pkg: Package;
|
|
7
|
+
server: Framework | 'node';
|
|
8
|
+
up: string;
|
|
9
|
+
env: Record<string, string>;
|
|
10
|
+
ready: string;
|
|
11
|
+
built: string | null;
|
|
12
|
+
}
|
|
13
|
+
export declare function servicesOf(repo: Repository): {
|
|
14
|
+
services: Service[];
|
|
15
|
+
findings: Finding[];
|
|
16
|
+
};
|