@burnsred/entity-sync 0.0.0-stage → 3.0.0-rc.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/.claude-plugin/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +11 -0
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +240 -2
- package/agents/entity-editor.md +16 -0
- package/bin/entity-sync.mjs +36 -0
- package/dist/cli.d.mts +20 -0
- package/dist/commands/asserts.d.mts +4 -0
- package/dist/commands/check.d.mts +6 -0
- package/dist/commands/inspect.d.mts +30 -0
- package/dist/commands/plan.d.mts +28 -0
- package/dist/commands/schema.d.mts +39 -0
- package/dist/config.d.mts +175 -0
- package/dist/index.d.mts +5 -0
- package/dist/lib/cache.d.mts +120 -0
- package/dist/lib/diff.d.mts +228 -0
- package/dist/lib/field-map.d.mts +93 -0
- package/dist/lib/gen-asserts.d.mts +70 -0
- package/dist/lib/graph.d.mts +90 -0
- package/dist/lib/parse-entities.d.mts +257 -0
- package/dist/lib/parse-schema.d.mts +98 -0
- package/dist/lib/resolve.d.mts +135 -0
- package/dist/project.d.mts +60 -0
- package/hooks/editor-guard.mjs +86 -0
- package/hooks/hooks.json +15 -0
- package/package.json +54 -4
- package/skills/entity/SKILL.md +105 -0
- package/skills/entity/reference/create.md +74 -0
- package/skills/entity/reference/editor.md +80 -0
- package/skills/entity/reference/enums.md +89 -0
- package/skills/entity/reference/fields.md +101 -0
- package/skills/entity/reference/sync.md +191 -0
- package/src/cli.mjs +110 -0
- package/src/commands/asserts.mjs +52 -0
- package/src/commands/check.mjs +88 -0
- package/src/commands/inspect.mjs +290 -0
- package/src/commands/plan.mjs +542 -0
- package/src/commands/schema.mjs +206 -0
- package/src/config.mjs +205 -0
- package/src/index.mjs +19 -0
- package/src/lib/cache.mjs +323 -0
- package/src/lib/diff.mjs +706 -0
- package/src/lib/field-map.mjs +438 -0
- package/src/lib/gen-asserts.mjs +478 -0
- package/src/lib/graph.mjs +284 -0
- package/src/lib/parse-entities.mjs +935 -0
- package/src/lib/parse-schema.mjs +667 -0
- package/src/lib/resolve.mjs +455 -0
- package/src/project.mjs +115 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# Task: sync
|
|
2
|
+
|
|
3
|
+
**Do not read the generated schema types (`schema.dts` in
|
|
4
|
+
`entity-sync.config.mjs`, default `src/api/schema.d.ts`) to find drift.** On a
|
|
5
|
+
real project it is hundreds of KB. The `entity-sync` CLI does that mechanically
|
|
6
|
+
and hands you a report of a few KB. Read the report.
|
|
7
|
+
|
|
8
|
+
Paths below use the defaults; if the config overrides `stateDir`,
|
|
9
|
+
`entitiesDir` or `assertsFile`, use its values instead.
|
|
10
|
+
|
|
11
|
+
## The procedure
|
|
12
|
+
|
|
13
|
+
Note - to avoid permission requests, run each step as a single bare
|
|
14
|
+
`npm run <script>` command. Do not chain with `&&`, and do not pipe: every step
|
|
15
|
+
below is already a whole script, so the one thing you invoke matches the
|
|
16
|
+
`Bash(npm run *)` rule on its own.
|
|
17
|
+
|
|
18
|
+
The scripts do **not** call `nvm use` - they cannot. npm runs scripts through
|
|
19
|
+
`/bin/sh`, where `nvm` does not exist, because it is a shell function rather than
|
|
20
|
+
a binary. If the project pins a node version, it is enforced via `engines` and
|
|
21
|
+
`engine-strict`, not by these scripts.
|
|
22
|
+
|
|
23
|
+
1. **`npm run entity:sync`** - fetches the schema from the broker (or reads
|
|
24
|
+
`schema.file`), regenerates the schema types, and writes the drift report.
|
|
25
|
+
Broker credentials come from the env vars and `envFiles` named in the config
|
|
26
|
+
(default `API_SUPERUSER_USERNAME` / `API_SUPERUSER_PASSWORD`, from
|
|
27
|
+
`env/.env` or `env/.env.local`), or from the shell; it fails with a clear
|
|
28
|
+
message if neither is present. A "wrong API" error means another service is
|
|
29
|
+
answering on the schema URL - check `schema.expectedTitle` and which broker
|
|
30
|
+
is running before anything else. Do not pipe to `tail`.
|
|
31
|
+
|
|
32
|
+
2. **Read `.entity-sync/report.md`** (the run also prints it). It contains only
|
|
33
|
+
what needs attention: unresolved mappings, ambiguous ones, per-entity edits
|
|
34
|
+
grouped into dependency waves, and anything needing a human decision.
|
|
35
|
+
Entities already in sync do not appear at all.
|
|
36
|
+
|
|
37
|
+
3. **Resolve any unresolved or ambiguous mappings first**, before editing
|
|
38
|
+
fields. Add the annotation that records the decision (see "Annotations"),
|
|
39
|
+
then re-run `npm run entity:plan`. Ask the user when the choice is genuinely
|
|
40
|
+
ambiguous rather than guessing.
|
|
41
|
+
|
|
42
|
+
4. **Work the waves in order, children before parents.** A parent's field type
|
|
43
|
+
names its child's `Fields` type, so editing a parent first means revisiting
|
|
44
|
+
it. Entities within a wave are independent of each other.
|
|
45
|
+
|
|
46
|
+
**Delegate each entity to an entity editor subagent** (see "Delegation" in
|
|
47
|
+
`SKILL.md` for which one). Within a wave the
|
|
48
|
+
entities are independent, so dispatch them all in a single message and let
|
|
49
|
+
them run concurrently. Give each one:
|
|
50
|
+
|
|
51
|
+
- the entity name and the component it maps to
|
|
52
|
+
- that entity's `####` section from the report, pasted verbatim
|
|
53
|
+
- nothing else - the editor reads `fields.md` and `enums.md` itself
|
|
54
|
+
|
|
55
|
+
Do not read or edit the entity files yourself. The point of the delegation is
|
|
56
|
+
that the file contents and the edit reasoning stay out of this context; only
|
|
57
|
+
each agent's summary comes back. See "Why delegate" below.
|
|
58
|
+
|
|
59
|
+
Wait for the whole wave before starting the next one, and do not run
|
|
60
|
+
`entity:check` between entities - it is project-wide, so it is a per-wave
|
|
61
|
+
step at most.
|
|
62
|
+
|
|
63
|
+
5. **`npm run entity:check`** regenerates the compile-time assertions and runs
|
|
64
|
+
`tsc`. This - not the report - is the authority on whether an entity is
|
|
65
|
+
correct. To read the failures grouped by entity:
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
npm run entity:check:errors
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
That script is `entity-sync check --grouped`: it runs the same check and
|
|
72
|
+
groups tsc's output by entity, so the command stays a single bare `npm run`.
|
|
73
|
+
It also lists tsc errors outside the assertions file separately - those are
|
|
74
|
+
not entity drift, but the build still fails on them.
|
|
75
|
+
|
|
76
|
+
Each failure names the entity, the field, and both types, e.g.
|
|
77
|
+
`{ expected: LineEntityFields[]; got: List<LineEntityRecord> }`.
|
|
78
|
+
Repeat until green.
|
|
79
|
+
|
|
80
|
+
Because the output is already grouped by entity, it is a ready-made
|
|
81
|
+
dispatcher: hand each entity's group to an entity editor and re-check.
|
|
82
|
+
Keep the grouped headers in this context, not the fixes.
|
|
83
|
+
|
|
84
|
+
6. **`npx eslint --fix <files>`** on everything you touched. Not a project
|
|
85
|
+
`lint:fix` script with the files appended: such scripts usually already name
|
|
86
|
+
a directory, so the arguments append to it and the whole tree is linted too.
|
|
87
|
+
|
|
88
|
+
The subagents lint their own files, so this is a backstop for anything you
|
|
89
|
+
edited directly - typically the annotations from step 3.
|
|
90
|
+
|
|
91
|
+
## Why delegate
|
|
92
|
+
|
|
93
|
+
A sync touching five entities generates ten file reads, a dozen edits and
|
|
94
|
+
several rounds of tsc output. All of it stays resident in whichever context
|
|
95
|
+
produced it and is re-sent on every later request. A subagent's context is
|
|
96
|
+
discarded when it returns, so the orchestrator pays only for the summary.
|
|
97
|
+
|
|
98
|
+
What stays here: the report, the wave ordering, the annotations, the
|
|
99
|
+
`entity:check` verdict, and the decisions that need the user. What goes to a
|
|
100
|
+
subagent: anything involving the contents of an entity file.
|
|
101
|
+
|
|
102
|
+
`entity:check` is deliberately **not** available to the editor (the plugin hook
|
|
103
|
+
blocks it). It is a project-wide `tsc`, so a subagent running it would spend
|
|
104
|
+
30s+ and then see failures from files it is not responsible for. Convergence is a global property
|
|
105
|
+
and belongs to the orchestrator.
|
|
106
|
+
|
|
107
|
+
## Reading the report
|
|
108
|
+
|
|
109
|
+
- **Unresolved** - no component to compare against. Needs a schema or noSchema
|
|
110
|
+
annotation.
|
|
111
|
+
- **Ambiguous mappings** - two serializers fit comparably well; the report shows
|
|
112
|
+
each candidate's field-name overlap score. Confirm, then annotate so it is
|
|
113
|
+
never re-derived.
|
|
114
|
+
- **Needs a decision** - usually a nested component with no entity modelling it.
|
|
115
|
+
Creating one is `/entity create` work; say so rather than inventing a shape.
|
|
116
|
+
- **Advisory** - `Field.readOnly` hints and `enum-untyped`. Worth adding while
|
|
117
|
+
you are in the file, not worth a pass of their own. Fold them into the
|
|
118
|
+
subagent dispatch for an entity you are already editing.
|
|
119
|
+
- **Cycles** - mutually recursive entities, with no valid internal ordering.
|
|
120
|
+
Edit the group together: one subagent for the whole cycle, not one per entity.
|
|
121
|
+
- **Enums** - choices dicts whose values disagree with their component, plus
|
|
122
|
+
untagged dicts that already match one. See `enums.md`.
|
|
123
|
+
- **Suppressions** - every schemaIgnore and schemaSubset, with a status saying
|
|
124
|
+
whether it is still doing work. Not collapsed on a repeat run: it is an
|
|
125
|
+
inventory of standing decisions, not news.
|
|
126
|
+
|
|
127
|
+
A repeat run collapses drift you have already seen to one line. `npm run
|
|
128
|
+
entity:plan -- --all` shows it again.
|
|
129
|
+
|
|
130
|
+
Known noise: if the config sets `knownNoise`, that file lists ops the report
|
|
131
|
+
raises that are not real drift, with the reason and the deferred fix (the plan
|
|
132
|
+
output prints its path). Check it before acting on a `type-text` op that
|
|
133
|
+
`entity:check` does not also fail on.
|
|
134
|
+
|
|
135
|
+
## Annotations
|
|
136
|
+
|
|
137
|
+
These live on the entity class - except `@schemaEnum`, which lives on a choices
|
|
138
|
+
dict - and are how a decision is recorded once instead of being re-derived every
|
|
139
|
+
sync. All six are read from JSDoc, so they must be single-word tags -
|
|
140
|
+
TypeScript parses `@schema-none` as the tag `schema`.
|
|
141
|
+
|
|
142
|
+
The same parsing cuts the other way: **never write a literal schema / noSchema /
|
|
143
|
+
schemaIgnore / schemaEnum tag in JSDoc prose.** TypeScript treats an `@word`
|
|
144
|
+
anywhere in a comment as a real tag, not just at line starts. A comment on
|
|
145
|
+
LineEntity that mentioned TotalEntity's noSchema tag in passing annotated
|
|
146
|
+
LineEntity itself as noSchema, silently excluding it from the sync - and the
|
|
147
|
+
failure surfaced two entities away, as a record-type assertion failure on the
|
|
148
|
+
parent OrderEntityRecord (its `lines` substitution was never generated). Refer
|
|
149
|
+
to tags by name instead ("the noSchema note on TotalEntity"). If an entity
|
|
150
|
+
mysteriously disappears from `schema-asserts.ts` after a comment edit, check its
|
|
151
|
+
JSDoc for stray `@` tokens.
|
|
152
|
+
|
|
153
|
+
| tag | meaning |
|
|
154
|
+
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
155
|
+
| `@schema <Component>` | This entity models that component. Overrides derivation; use when `apiBase` yields no candidate or the wrong one. |
|
|
156
|
+
| `@noSchema <reason>` | No API counterpart at all. Screen state, or a synthetic shape the broker has no serializer for. |
|
|
157
|
+
| `@schemaSubset <reason>` | A deliberate _projection_: every field it declares must exist upstream, but it need not declare all of them. For row/table views and the whoami view. |
|
|
158
|
+
| `@schemaIgnore <field> <reason>` | The **schema** is wrong for this field, not the entity. Drops the field from every check, in the report and the assertions alike. |
|
|
159
|
+
| `@schemaAlso <Component>` | This entity _also_ models another component, typically a narrower nested serializer (`NestedCustomer` for `Customer`), so fields embedding it are checked against this entity. Never changes what the entity itself resolves to. |
|
|
160
|
+
| `@schemaEnum <Component>` | On a choices dict, not a class: this dict models that `*Enum` component. See `enums.md`. |
|
|
161
|
+
|
|
162
|
+
The schemaIgnore tag exists mainly for `SerializerMethodField` and un-annotated
|
|
163
|
+
`ReadOnlyField`: drf-spectacular types both as bare `string` unless the broker
|
|
164
|
+
adds `@extend_schema_field`, even when the Python returns a bool, an int, or a
|
|
165
|
+
list of objects. Always give the reason - it is the only thing distinguishing a
|
|
166
|
+
recorded finding from a silenced one, and it is printed beside the field in the
|
|
167
|
+
report's Suppressions section and in `schema-asserts.ts`.
|
|
168
|
+
|
|
169
|
+
Every suppression is listed on every run under `## Suppressions`, with a status:
|
|
170
|
+
|
|
171
|
+
- **needed** - the field still disagrees; the ops it is hiding are listed under
|
|
172
|
+
it, so the reason can be re-judged against what the broker sends today.
|
|
173
|
+
- **redundant** - the field produced no ops. The broker has caught up, and the
|
|
174
|
+
annotation now excludes a field from `entity:check` for nothing. Delete it.
|
|
175
|
+
- **orphaned** - the named field is in neither the component nor the Fields
|
|
176
|
+
type. Usually a typo, or a leftover from a field since deleted.
|
|
177
|
+
|
|
178
|
+
Subset entities are listed there too, with the fields the projection declines to
|
|
179
|
+
compare.
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
/**
|
|
183
|
+
* @schemaIgnore tags A SerializerMethodField, typed as a bare `string` by
|
|
184
|
+
* drf-spectacular. It really sends the customer's tags as objects - the list
|
|
185
|
+
* view renders each one's `icon_key` and `name`.
|
|
186
|
+
*/
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Prefer a broker fix (`@extend_schema_field`) over a schemaIgnore when you can
|
|
190
|
+
make it: it types the field for every consumer, and the report will flag the
|
|
191
|
+
suppression as redundant once the broker catches up.
|
package/src/cli.mjs
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Argument parsing and dispatch for the `entity-sync` bin. Kept out of the bin
|
|
4
|
+
* so it can be exercised in tests without spawning a process.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { loadConfig } from './config.mjs';
|
|
8
|
+
|
|
9
|
+
export const HELP = `entity-sync <command> [--config <path>]
|
|
10
|
+
|
|
11
|
+
sync schema, then plan
|
|
12
|
+
schema fetch/read the OpenAPI schema, regenerate types
|
|
13
|
+
plan [--all] write the drift report to the state dir
|
|
14
|
+
asserts regenerate the compile-time assertions
|
|
15
|
+
check [--grouped] [-- <tsc args>]
|
|
16
|
+
asserts, then the project's tsc
|
|
17
|
+
inspect <cmd> component <Name> | components | path <substr> |
|
|
18
|
+
entity <Name> | entities | enums | errors
|
|
19
|
+
errors group tsc output (stdin) by entity
|
|
20
|
+
--version print the version`;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Split out `--config <path>` from the rest.
|
|
24
|
+
*
|
|
25
|
+
* @param {string[]} argv
|
|
26
|
+
*/
|
|
27
|
+
export function parseArgs(argv) {
|
|
28
|
+
/** @type {string | undefined} */
|
|
29
|
+
let configPath;
|
|
30
|
+
/** @type {string[]} */
|
|
31
|
+
const rest = [];
|
|
32
|
+
|
|
33
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
34
|
+
const arg = argv[i];
|
|
35
|
+
if (arg === '--') {
|
|
36
|
+
rest.push(...argv.slice(i));
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
if (arg === '--config') {
|
|
40
|
+
configPath = argv[i + 1];
|
|
41
|
+
i += 1;
|
|
42
|
+
} else if (arg.startsWith('--config=')) {
|
|
43
|
+
configPath = arg.slice('--config='.length);
|
|
44
|
+
} else {
|
|
45
|
+
rest.push(arg);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const [command, ...args] = rest;
|
|
50
|
+
return { command, args, configPath };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {string[]} argv
|
|
55
|
+
* @param {{ version: string, cwd?: string }} options
|
|
56
|
+
* @returns {Promise<number>} exit code
|
|
57
|
+
*/
|
|
58
|
+
export async function main(argv, { version, cwd }) {
|
|
59
|
+
const { command, args, configPath } = parseArgs(argv);
|
|
60
|
+
|
|
61
|
+
if (command === '--version' || command === '-v') {
|
|
62
|
+
console.log(version);
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
65
|
+
if (
|
|
66
|
+
!command ||
|
|
67
|
+
command === '--help' ||
|
|
68
|
+
command === '-h' ||
|
|
69
|
+
command === 'help'
|
|
70
|
+
) {
|
|
71
|
+
console.log(HELP);
|
|
72
|
+
return command ? 0 : 1;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const config = await loadConfig({ cwd, configPath });
|
|
76
|
+
|
|
77
|
+
switch (command) {
|
|
78
|
+
case 'schema':
|
|
79
|
+
await (await import('./commands/schema.mjs')).run(config);
|
|
80
|
+
return 0;
|
|
81
|
+
|
|
82
|
+
case 'plan':
|
|
83
|
+
(await import('./commands/plan.mjs')).run(config, args);
|
|
84
|
+
return 0;
|
|
85
|
+
|
|
86
|
+
case 'sync':
|
|
87
|
+
await (await import('./commands/schema.mjs')).run(config);
|
|
88
|
+
(await import('./commands/plan.mjs')).run(config, args);
|
|
89
|
+
return 0;
|
|
90
|
+
|
|
91
|
+
case 'asserts':
|
|
92
|
+
await (await import('./commands/asserts.mjs')).run(config);
|
|
93
|
+
return 0;
|
|
94
|
+
|
|
95
|
+
case 'check':
|
|
96
|
+
return (await import('./commands/check.mjs')).run(config, args);
|
|
97
|
+
|
|
98
|
+
case 'inspect':
|
|
99
|
+
(await import('./commands/inspect.mjs')).run(config, args);
|
|
100
|
+
return 0;
|
|
101
|
+
|
|
102
|
+
case 'errors':
|
|
103
|
+
(await import('./commands/inspect.mjs')).run(config, ['errors']);
|
|
104
|
+
return 0;
|
|
105
|
+
|
|
106
|
+
default:
|
|
107
|
+
console.error(`unknown command: ${command}\n\n${HELP}`);
|
|
108
|
+
return 1;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Emit the compile-time assertions file (`assertsFile` in the config).
|
|
4
|
+
*
|
|
5
|
+
* Run by `entity-sync check`, which then hands the file to `tsc`. That is what
|
|
6
|
+
* turns "are these entities correct?" from a judgement into a build step.
|
|
7
|
+
*
|
|
8
|
+
* Usage: `entity-sync asserts`
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { dirname } from 'node:path';
|
|
13
|
+
|
|
14
|
+
import { display } from '../config.mjs';
|
|
15
|
+
import { generateAsserts } from '../lib/gen-asserts.mjs';
|
|
16
|
+
import { formatForProject, loadProject } from '../project.mjs';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {import('../config.mjs').ResolvedConfig} config
|
|
20
|
+
*/
|
|
21
|
+
export async function run(config) {
|
|
22
|
+
const project = loadProject(config);
|
|
23
|
+
|
|
24
|
+
const { source, skipped, entityCount, assertionCount } = generateAsserts({
|
|
25
|
+
entities: project.entities,
|
|
26
|
+
resolutions: project.resolutions,
|
|
27
|
+
schema: project.schema,
|
|
28
|
+
entityByComponent: project.entityByComponent,
|
|
29
|
+
enumResolutions: project.enumResolutions,
|
|
30
|
+
enumByComponent: project.enumByComponent,
|
|
31
|
+
assertsDir: display(config, dirname(config.assertsFile)),
|
|
32
|
+
schemaSpecifier: config.schema.importSpecifier,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
mkdirSync(dirname(config.assertsFile), { recursive: true });
|
|
36
|
+
writeFileSync(
|
|
37
|
+
config.assertsFile,
|
|
38
|
+
await formatForProject(config, config.assertsFile, source),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
console.log(
|
|
42
|
+
`wrote ${display(config, config.assertsFile)}: ` +
|
|
43
|
+
`${assertionCount} assertions across ${entityCount} entities`,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
if (skipped.length) {
|
|
47
|
+
console.log(`\nnot asserted (${skipped.length}):`);
|
|
48
|
+
for (const entry of skipped) {
|
|
49
|
+
console.log(` ${entry.className}: ${entry.reason}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Regenerate the assertions, then type-check the project with its own `tsc`.
|
|
4
|
+
*
|
|
5
|
+
* `tsc` - not the report - is the authority on whether an entity is correct.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* entity-sync check [--grouped] [-- <tsc args>]
|
|
9
|
+
*
|
|
10
|
+
* `--grouped` captures tsc's output and prints the assertion failures grouped
|
|
11
|
+
* by entity, replacing the `check 2>&1 | errors` pipeline with one command. tsc
|
|
12
|
+
* args default to `--noEmit`.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { spawnSync } from 'node:child_process';
|
|
16
|
+
import { readFileSync } from 'node:fs';
|
|
17
|
+
import { createRequire } from 'node:module';
|
|
18
|
+
import { basename, join } from 'node:path';
|
|
19
|
+
|
|
20
|
+
import { run as runAsserts } from './asserts.mjs';
|
|
21
|
+
import { groupAssertionErrors, printGroupedErrors } from './inspect.mjs';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {string} root
|
|
25
|
+
*/
|
|
26
|
+
function resolveTsc(root) {
|
|
27
|
+
try {
|
|
28
|
+
return createRequire(join(root, 'package.json')).resolve(
|
|
29
|
+
'typescript/bin/tsc',
|
|
30
|
+
);
|
|
31
|
+
} catch {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`typescript is not installed in ${root} (it is a peer dependency of @burnsred/entity-sync)`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {import('../config.mjs').ResolvedConfig} config
|
|
40
|
+
* @param {string[]} [args]
|
|
41
|
+
* @returns {Promise<number>} the tsc exit status
|
|
42
|
+
*/
|
|
43
|
+
export async function run(config, args = []) {
|
|
44
|
+
const separator = args.indexOf('--');
|
|
45
|
+
const own = separator === -1 ? args : args.slice(0, separator);
|
|
46
|
+
const tscArgs = separator === -1 ? ['--noEmit'] : args.slice(separator + 1);
|
|
47
|
+
const grouped = own.includes('--grouped');
|
|
48
|
+
|
|
49
|
+
await runAsserts(config);
|
|
50
|
+
|
|
51
|
+
const tsc = resolveTsc(config.root);
|
|
52
|
+
console.log(`\ntsc ${tscArgs.join(' ')}`);
|
|
53
|
+
|
|
54
|
+
if (!grouped) {
|
|
55
|
+
const result = spawnSync(process.execPath, [tsc, ...tscArgs], {
|
|
56
|
+
cwd: config.root,
|
|
57
|
+
stdio: 'inherit',
|
|
58
|
+
});
|
|
59
|
+
return result.status ?? 1;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const result = spawnSync(process.execPath, [tsc, ...tscArgs], {
|
|
63
|
+
cwd: config.root,
|
|
64
|
+
encoding: 'utf8',
|
|
65
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
66
|
+
});
|
|
67
|
+
const output = `${result.stdout ?? ''}${result.stderr ?? ''}`;
|
|
68
|
+
const errors = groupAssertionErrors(
|
|
69
|
+
output,
|
|
70
|
+
readFileSync(config.assertsFile, 'utf8'),
|
|
71
|
+
basename(config.assertsFile),
|
|
72
|
+
);
|
|
73
|
+
printGroupedErrors(errors);
|
|
74
|
+
|
|
75
|
+
// Failures outside the assertions file are not entity drift, but they still
|
|
76
|
+
// fail the build; say so rather than letting a green-looking summary hide them.
|
|
77
|
+
const assertsName = basename(config.assertsFile);
|
|
78
|
+
const other = output
|
|
79
|
+
.split('\n')
|
|
80
|
+
.filter((line) => /error TS\d+/.test(line) && !line.includes(assertsName));
|
|
81
|
+
if (other.length) {
|
|
82
|
+
console.log(`\n${other.length} other tsc errors (not entity assertions):`);
|
|
83
|
+
for (const line of other.slice(0, 20)) console.log(` ${line}`);
|
|
84
|
+
if (other.length > 20) console.log(` ... and ${other.length - 20} more`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return result.status ?? 1;
|
|
88
|
+
}
|