@widgetic/dev-tools 0.3.10 → 0.3.12

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/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@widgetic/dev-tools",
3
- "version": "0.3.10",
3
+ "version": "0.3.12",
4
4
  "type": "module",
5
- "description": "Build-time checks and dev tooling shared by the published @widgetic packages, released once so the repositories that consume them cannot drift apart. Currently: check-runtime-deps (with the fixture suite that proves it fails), the Vite sibling-$lib remap, the canonical Frontend sibling layout that builds every consumer's dev-alias tables, and check-links — the link-doctor that fails when a sibling symlink dangles, a promised dist/ file is missing, or a dead checkout name lingers, check-design-system-peers — which fails when the two design-system peers' lockfiles drift apart, and comment-console-logs — which comments out the debugging console calls of a codebase and keeps every one of them in the code.",
5
+ "description": "Build-time checks and dev tooling shared by the published @widgetic packages, released once so the repositories that consume them cannot drift apart. Currently: check-runtime-deps (with the fixture suite that proves it fails), the Vite sibling-$lib remap, the canonical Frontend sibling layout that builds every consumer's dev-alias tables, and check-links — the link-doctor that fails when a sibling symlink dangles, a promised dist/ file is missing, or a dead checkout name lingers, check-design-system-peers — which fails when the two design-system peers' lockfiles drift apart, check-packed-imports — which fails when a file a package would publish imports a file the package does not hold, comment-console-logs — which comments out the debugging console calls of a codebase and keeps every one of them in the code, and sync-frontend-checkouts — which fast-forwards every Frontend git checkout and drops Vite dep caches so a laptop is not serving yesterday's sibling source.",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://gitlab.com/widgeticai/Frontend/dev-tools.git"
@@ -23,8 +23,10 @@
23
23
  "check-readme-contracts-fixtures": "bin/check-readme-contracts-fixtures.mjs",
24
24
  "check-layout-fixtures": "bin/check-layout-fixtures.mjs",
25
25
  "check-design-system-peers": "bin/check-design-system-peers.mjs",
26
+ "check-packed-imports": "bin/check-packed-imports.mjs",
26
27
  "comment-console-logs": "bin/comment-console-logs.mjs",
27
- "publish-all-to-npm": "bin/publish-all-to-npm.mjs"
28
+ "publish-all-to-npm": "bin/publish-all-to-npm.mjs",
29
+ "sync-frontend-checkouts": "bin/sync-frontend-checkouts.mjs"
28
30
  },
29
31
  "files": [
30
32
  "bin",
@@ -41,13 +43,17 @@
41
43
  "check:links": "node bin/check-links.mjs",
42
44
  "check:readme": "node bin/check-readme-contracts.mjs",
43
45
  "check:peers": "node bin/check-design-system-peers.mjs",
46
+ "check:packed": "node bin/check-packed-imports.mjs",
44
47
  "test:links": "node bin/check-links-fixtures.mjs",
45
48
  "test:readme": "node --test test/readme-contract.test.mjs",
46
49
  "test:peers": "node --test test/lockfile-peers.test.mjs",
50
+ "test:packed": "node --test test/packed-imports.test.mjs",
47
51
  "test:publish": "node --test test/publish-to-npm.test.mjs",
48
52
  "test:lists": "node --test test/package-lists.test.mjs",
49
53
  "test:comment-logs": "node --test test/comment-console-logs.test.mjs",
50
- "test": "npm run check:deps && npm run test:deps && npm run test:vite && npm run test:layout && npm run test:links && npm run test:readme && npm run test:peers && npm run test:publish && npm run test:lists && npm run test:comment-logs",
54
+ "test:sync-frontend": "node --test test/sync-frontend-checkouts.test.mjs",
55
+ "test": "npm run check:deps && npm run test:deps && npm run test:vite && npm run test:layout && npm run test:links && npm run test:readme && npm run test:peers && npm run test:packed && npm run test:publish && npm run test:lists && npm run test:comment-logs && npm run test:sync-frontend",
56
+ "sync-frontend": "node bin/sync-frontend-checkouts.mjs",
51
57
  "publish-to-npm": "node bin/publish-package.mjs",
52
58
  "publish-all-to-npm": "node bin/publish-all-to-npm.mjs",
53
59
  "prepublishOnly": "npm test"
@@ -24,10 +24,13 @@ test('the design-system table expands to every subpath plus the index', () => {
24
24
  '@widgetic/design-system/fonts',
25
25
  '@widgetic/design-system/i18n',
26
26
  '@widgetic/design-system/icons',
27
+ '@widgetic/design-system/inline-completion',
28
+ '@widgetic/design-system/prompt-references',
27
29
  '@widgetic/design-system/rtl',
28
30
  '@widgetic/design-system/shad-editor',
29
31
  '@widgetic/design-system/stores',
30
- '@widgetic/design-system/styles'
32
+ '@widgetic/design-system/styles',
33
+ '@widgetic/design-system/textarea-overlay'
31
34
  ].sort()
32
35
  );
33
36
  assert.equal(aliases['@widgetic/design-system/icons'], `${FRONTEND}/design-system/src/lib/icons`);
@@ -35,6 +38,20 @@ test('the design-system table expands to every subpath plus the index', () => {
35
38
  assert.equal(aliases['@widgetic/design-system/i18n'], `${FRONTEND}/design-system/src/lib/i18n`);
36
39
  // the right to left kit: a directory as well, and the test-only `rtl/audit` resolves under it as a prefix
37
40
  assert.equal(aliases['@widgetic/design-system/rtl'], `${FRONTEND}/design-system/src/lib/rtl`);
41
+ // ghost text and the mirror layer it draws on: directories with an `index.ts`, so a bare import and a deeper one both resolve
42
+ assert.equal(
43
+ aliases['@widgetic/design-system/inline-completion'],
44
+ `${FRONTEND}/design-system/src/lib/inline-completion`
45
+ );
46
+ assert.equal(
47
+ aliases['@widgetic/design-system/textarea-overlay'],
48
+ `${FRONTEND}/design-system/src/lib/textarea-overlay`
49
+ );
50
+ // references to attached files in a prompt box: a directory with an `index.ts` as well
51
+ assert.equal(
52
+ aliases['@widgetic/design-system/prompt-references'],
53
+ `${FRONTEND}/design-system/src/lib/prompt-references`
54
+ );
38
55
  // styles is a file, not a directory — the one entry that ever differed
39
56
  assert.equal(
40
57
  aliases['@widgetic/design-system/styles'],
@@ -0,0 +1,220 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
4
+ import { tmpdir } from 'node:os';
5
+ import { dirname, join } from 'node:path';
6
+ import {
7
+ checkPackedImports,
8
+ describePackedImportProblems,
9
+ findPackedImportProblems,
10
+ findSpecifiers,
11
+ listPackedPaths,
12
+ maskSource,
13
+ resolvePackedTarget,
14
+ svelteScriptText
15
+ } from '../lib/packed-imports.mjs';
16
+
17
+ test('maskSource takes comments out, keeps every string as a placeholder, and blanks templates and regular expressions', () => {
18
+ const { code, strings } = maskSource(
19
+ [
20
+ "// import a from '../comment-line.js';",
21
+ "/* import b from '../comment-block.js'; */",
22
+ "/**\n * @example\n * import c from '../doc-comment.js';\n */",
23
+ "const url = 'http://example.test/x'; // not a comment start inside the string",
24
+ 'const source = `import d from "../template.js"; ${`nested ${1}`} end`;',
25
+ "const pattern = /['\"]\\/\\//g;",
26
+ "const half = a / b / c;",
27
+ "import e from './real.js';"
28
+ ].join('\n')
29
+ );
30
+ assert.doesNotMatch(code, /comment-line|comment-block|doc-comment|template\.js/);
31
+ // (\x27 is a quote: check-runtime-deps reads a quote after `from` in this file as an import of a package.)
32
+ assert.match(code, /import e from \x27§\d+§\x27;/);
33
+ assert.deepEqual(strings.includes('./real.js'), true);
34
+ assert.deepEqual(strings.includes('http://example.test/x'), true);
35
+ // A slash that divides is still code.
36
+ assert.match(code, /a \/ b \/ c/);
37
+ });
38
+
39
+ test('findSpecifiers sees every form of import and nothing that only looks like one', () => {
40
+ const specifiers = findSpecifiers(
41
+ [
42
+ "import defaultOne from './default.js';",
43
+ "import { a, b as c } from './named.js';",
44
+ "import * as ns from './namespace.js';",
45
+ "import './side-effect.js';",
46
+ "import type { T } from './type.js';",
47
+ "export * from './export-star.js';",
48
+ "export { x } from './export-named.js';",
49
+ "export type { Y } from './export-type.js';",
50
+ 'import {\n\tmulti,\n\tline\n} from "./multi-line.js";',
51
+ "const lazy = await import('./dynamic.js');",
52
+ "const old = require('./required.cjs');",
53
+ "import fs = require('./ts-require.js');",
54
+ "export const notAnImport = './plain-string.js';",
55
+ "export default './default-string.js';",
56
+ "const table = { import: './object-key.js', export: './object-key-2.js' };",
57
+ "// import hidden from './hidden.js';",
58
+ "const text = \"import inString from './in-string.js'\";",
59
+ 'const template = `import inTemplate from "./in-template.js"`;',
60
+ 'const meta = import.meta.url;',
61
+ '/// <reference path="./reference.d.ts" />'
62
+ ].join('\n')
63
+ );
64
+ assert.deepEqual(
65
+ specifiers.sort(),
66
+ [
67
+ './default.js',
68
+ './dynamic.js',
69
+ './export-named.js',
70
+ './export-star.js',
71
+ './export-type.js',
72
+ './multi-line.js',
73
+ './named.js',
74
+ './namespace.js',
75
+ './reference.d.ts',
76
+ './required.cjs',
77
+ './side-effect.js',
78
+ './ts-require.js',
79
+ './type.js'
80
+ ].sort()
81
+ );
82
+ });
83
+
84
+ test('a .svelte file is read in its script blocks only', () => {
85
+ const text = [
86
+ '<script lang="ts">',
87
+ "\timport Child from './Child.svelte';",
88
+ '</script>',
89
+ '<script module>',
90
+ "\texport { shared } from './shared.js';",
91
+ '</script>',
92
+ '<p>',
93
+ "\timport Fake from './fake.js';",
94
+ '</p>'
95
+ ].join('\n');
96
+ assert.deepEqual(findSpecifiers(svelteScriptText(text)).sort(), ['./Child.svelte', './shared.js']);
97
+ });
98
+
99
+ test('resolvePackedTarget finds a packed file the way a bundler would, and says what is wrong when there is none', () => {
100
+ const packed = new Set(['dist/a.js', 'dist/b.js', 'dist/b.d.ts', 'dist/util/index.js', 'dist/data.json', 'dist/Part.svelte', 'dist/types.d.ts']);
101
+ assert.deepEqual(resolvePackedTarget('dist/a.js', './b.js', packed), { status: 'ok', target: 'dist/b.js' });
102
+ assert.deepEqual(resolvePackedTarget('dist/a.js', './b', packed), { status: 'ok', target: 'dist/b.js' });
103
+ assert.deepEqual(resolvePackedTarget('dist/a.js', './util', packed), { status: 'ok', target: 'dist/util/index.js' });
104
+ assert.deepEqual(resolvePackedTarget('dist/a.js', './util/', packed), { status: 'ok', target: 'dist/util/index.js' });
105
+ assert.deepEqual(resolvePackedTarget('dist/a.js', './data.json', packed), { status: 'ok', target: 'dist/data.json' });
106
+ assert.deepEqual(resolvePackedTarget('dist/a.js', './Part.svelte', packed), { status: 'ok', target: 'dist/Part.svelte' });
107
+ // A declaration file names `./types.js`, or `./types.ts` as TypeScript 5 writes it, and TypeScript reads both as `./types.d.ts`.
108
+ assert.deepEqual(resolvePackedTarget('dist/a.d.ts', './types.js', packed), { status: 'ok', target: 'dist/types.d.ts' });
109
+ assert.deepEqual(resolvePackedTarget('dist/a.d.ts', './types.ts', packed), { status: 'ok', target: 'dist/types.d.ts' });
110
+ assert.deepEqual(resolvePackedTarget('dist/a.d.ts', './types', packed), { status: 'ok', target: 'dist/types.d.ts' });
111
+ // A file that runs cannot load a declaration file: a `./types.js` that is only `./types.d.ts` in the pack is a break.
112
+ assert.deepEqual(resolvePackedTarget('dist/a.js', './types.js', packed), { status: 'missing', target: 'dist/types.js' });
113
+ assert.deepEqual(resolvePackedTarget('dist/a.js', './types', packed), { status: 'missing', target: 'dist/types' });
114
+ assert.deepEqual(resolvePackedTarget('dist/Part.svelte', './types.js', packed), { status: 'missing', target: 'dist/types.js' });
115
+ // A TypeScript source file in the pack names its sibling `./y.ts` as `./y.js`.
116
+ const withSource = new Set([...packed, 'src/x.ts', 'src/y.ts']);
117
+ assert.deepEqual(resolvePackedTarget('src/x.ts', './y.js', withSource), { status: 'ok', target: 'src/y.ts' });
118
+ assert.deepEqual(resolvePackedTarget('src/x.ts', './y', withSource), { status: 'ok', target: 'src/y.ts' });
119
+ assert.deepEqual(resolvePackedTarget('dist/a.js', './gone.js', packed), { status: 'missing', target: 'dist/gone.js' });
120
+ assert.deepEqual(resolvePackedTarget('dist/a.js', '../other/x.js', packed), { status: 'missing', target: 'other/x.js' });
121
+ assert.deepEqual(resolvePackedTarget('dist/a.js', '../../static/x.json', packed), { status: 'outside', target: '../static/x.json' });
122
+ assert.deepEqual(resolvePackedTarget('index.js', '../x.js', packed), { status: 'outside', target: '../x.js' });
123
+ });
124
+
125
+ test('findPackedImportProblems reports an import out of the package, a file the build did not write, and an alias of the source tree', () => {
126
+ const files = new Map([
127
+ ['dist/ok.js', "import './inside.js';\nimport data from './data.json';\nimport logo from './logo.svg?url';\nimport 'node:fs';\nimport 'svelte';"],
128
+ ['dist/inside.js', "// import example from '../../outside/in-a-comment.js';\nexport const inside = 1;"],
129
+ ['dist/out.js', "import schema from '../../static/schema.json';"],
130
+ ['dist/missing.js', "export { gone } from './gone.js';"],
131
+ ['dist/alias.js', "import { helper } from '$lib/utils/helper';"],
132
+ ['dist/Part.svelte', '<script>\n\timport { x } from "./nowhere";\n</script>\n<p>import y from "../../markup.js"</p>']
133
+ ]);
134
+ const problems = findPackedImportProblems(files, ['dist/data.json', 'dist/logo.svg']);
135
+ assert.deepEqual(problems, [
136
+ { file: 'dist/out.js', specifier: '../../static/schema.json', reason: 'outside', target: '../static/schema.json' },
137
+ { file: 'dist/missing.js', specifier: './gone.js', reason: 'missing', target: 'dist/gone.js' },
138
+ { file: 'dist/alias.js', specifier: '$lib/utils/helper', reason: 'alias' },
139
+ { file: 'dist/Part.svelte', specifier: './nowhere', reason: 'missing', target: 'dist/nowhere' }
140
+ ]);
141
+ assert.deepEqual(describePackedImportProblems(problems), [
142
+ "dist/out.js imports '../../static/schema.json', a file outside the package (../static/schema.json)",
143
+ "dist/missing.js imports './gone.js', which is not in the package (dist/gone.js)",
144
+ "dist/alias.js imports '$lib/utils/helper', an alias that only exists in the source tree",
145
+ "dist/Part.svelte imports './nowhere', which is not in the package (dist/nowhere)"
146
+ ]);
147
+ });
148
+
149
+ /**
150
+ * Makes a package folder with the files given, runs `body` with its path and removes it again.
151
+ * @param {Record<string, string>} files
152
+ * @param {(packageDir: string) => void} body
153
+ */
154
+ function withPackageDir(files, body) {
155
+ const packageDir = mkdtempSync(join(tmpdir(), 'packed-imports-'));
156
+ try {
157
+ for (const [path, text] of Object.entries(files)) {
158
+ mkdirSync(dirname(join(packageDir, path)), { recursive: true });
159
+ writeFileSync(join(packageDir, path), text);
160
+ }
161
+ body(packageDir);
162
+ } finally {
163
+ rmSync(packageDir, { recursive: true, force: true });
164
+ }
165
+ }
166
+
167
+ test('listPackedPaths reads what the real npm would pack, and only that', () => {
168
+ withPackageDir(
169
+ {
170
+ 'package.json': JSON.stringify({ name: 'pack-fixture', version: '1.0.0', files: ['dist'] }),
171
+ 'dist/index.js': "import './part.js';\n",
172
+ 'dist/part.js': 'export {};\n',
173
+ 'src/not-packed.js': "import '../../outside.js';\n"
174
+ },
175
+ (packageDir) => {
176
+ assert.deepEqual(listPackedPaths(packageDir).sort(), ['dist/index.js', 'dist/part.js', 'package.json']);
177
+ }
178
+ );
179
+ });
180
+
181
+ test('checkPackedImports finds the break of editor 4.1.19: a packed file that imports a file outside the package', () => {
182
+ withPackageDir(
183
+ {
184
+ 'package.json': JSON.stringify({ name: 'pack-fixture', version: '1.0.0', files: ['dist'] }),
185
+ 'dist/embed/parse.js': "import schema from '../../../static/embed-schema.json';\nexport const parse = () => schema;\n",
186
+ 'dist/index.js': "export { parse } from './embed/parse.js';\n"
187
+ },
188
+ (packageDir) => {
189
+ const { problems, packedCount, scannedCount } = checkPackedImports(packageDir);
190
+ assert.equal(packedCount, 3);
191
+ assert.equal(scannedCount, 2);
192
+ assert.deepEqual(describePackedImportProblems(problems), [
193
+ "dist/embed/parse.js imports '../../../static/embed-schema.json', a file outside the package (../static/embed-schema.json)"
194
+ ]);
195
+ }
196
+ );
197
+ });
198
+
199
+ test('checkPackedImports finds nothing in a package whose imports all land in it', () => {
200
+ withPackageDir(
201
+ {
202
+ 'package.json': JSON.stringify({ name: 'pack-fixture', version: '1.0.0', files: ['dist'] }),
203
+ 'dist/index.js': "// import example from '../../example/only-in-a-comment.js';\nexport { parse } from './parse.js';\n",
204
+ 'dist/parse.js': "import schema from './schema.json';\nexport const parse = () => schema;\n",
205
+ 'dist/schema.json': '{}'
206
+ },
207
+ (packageDir) => {
208
+ assert.deepEqual(checkPackedImports(packageDir).problems, []);
209
+ }
210
+ );
211
+ });
212
+
213
+ test('checkPackedImports stops, and does not skip, when a listed file cannot be read', () => {
214
+ withPackageDir({ 'package.json': '{}' }, (packageDir) => {
215
+ assert.throws(
216
+ () => checkPackedImports(packageDir, { listPaths: () => ['package.json', 'dist/gone.js'] }),
217
+ /dist\/gone\.js is in the pack but cannot be read/
218
+ );
219
+ });
220
+ });
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
3
3
  import { spawnSync } from 'node:child_process';
4
4
  import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
5
5
  import { tmpdir } from 'node:os';
6
- import { join, resolve } from 'node:path';
6
+ import { dirname, join, resolve } from 'node:path';
7
7
  import { fileURLToPath } from 'node:url';
8
8
  import { PUBLISHABLE_PACKAGES } from '../lib/publish-to-npm.mjs';
9
9
 
@@ -17,6 +17,7 @@ test('the walk calls publish-to-npm in each npm package', () => {
17
17
  'cache-layer:publish-to-npm',
18
18
  'design-system:publish-to-npm',
19
19
  'dev-tools:publish-to-npm',
20
+ 'brand:publish-to-npm',
20
21
  'canvas:publish-to-npm',
21
22
  'chat:publish-to-npm',
22
23
  'editor:publish-to-npm',
@@ -90,10 +91,18 @@ test('the scripts the publisher runs before it raises a version exist in the pac
90
91
  * Runs the real publisher in a throwaway checkout of a package called `packageName`, with a stand-in `npm` and `node`
91
92
  * first on the PATH that only write down their arguments: nothing is built, published or sent anywhere, and no Doppler
92
93
  * is asked. `fails` is a piece of a command line that makes the stand-ins exit 1.
94
+ *
95
+ * The stand-in `npm pack` prints the files in `packList` (the real one is asked what the package would take; the
96
+ * publisher reads those files for the imports they make), and the files in `files` are committed in the checkout for it
97
+ * to read. With `bump`, the stand-in `npm version patch` really changes the version, 0.0.1 to 0.0.2, so a publish that
98
+ * stops afterwards can be seen to put it back.
93
99
  * @param {string} packageName
94
- * @param {{ fails?: string }} [options]
100
+ * @param {{ fails?: string, files?: Record<string, string>, packList?: string[], bump?: boolean }} [options]
95
101
  */
96
- function runPublisherIn(packageName, { fails = 'no command line has this in it' } = {}) {
102
+ function runPublisherIn(
103
+ packageName,
104
+ { fails = 'no command line has this in it', files = {}, packList = ['package.json'], bump = false } = {}
105
+ ) {
97
106
  const sandbox = mkdtempSync(join(tmpdir(), 'publish-package-'));
98
107
  try {
99
108
  const origin = join(sandbox, 'origin.git');
@@ -104,7 +113,17 @@ function runPublisherIn(packageName, { fails = 'no command line has this in it'
104
113
  for (const tool of ['npm', 'node']) {
105
114
  writeFileSync(
106
115
  join(bin, tool),
107
- `#!/bin/sh\necho "${tool} $*" >> "$STAND_IN_CALLS"\ncase "${tool} $*" in *"$STAND_IN_FAILS"*) exit 1 ;; esac\nexit 0\n`,
116
+ [
117
+ '#!/bin/sh',
118
+ `echo "${tool} $*" >> "$STAND_IN_CALLS"`,
119
+ `case "${tool} $*" in *"$STAND_IN_FAILS"*) exit 1 ;; esac`,
120
+ `case "${tool} $*" in`,
121
+ ' "npm pack "*) echo "$STAND_IN_PACK" ;;',
122
+ ' "npm version patch"*) if [ -n "$STAND_IN_BUMP" ]; then sed -i.bak \'s/"version": "0.0.1"/"version": "0.0.2"/\' package.json; rm -f package.json.bak; fi ;;',
123
+ 'esac',
124
+ 'exit 0',
125
+ ''
126
+ ].join('\n'),
108
127
  { mode: 0o755 }
109
128
  );
110
129
  }
@@ -120,6 +139,10 @@ function runPublisherIn(packageName, { fails = 'no command line has this in it'
120
139
  git(checkout, 'remote', 'add', 'origin', origin);
121
140
  writeFileSync(join(checkout, 'package.json'), JSON.stringify({ name: packageName, version: '0.0.1' }, null, 2) + '\n');
122
141
  writeFileSync(join(checkout, 'src', 'sdk', 'version.ts'), "export const SDK_VERSION = '0.0.1';\n");
142
+ for (const [path, text] of Object.entries(files)) {
143
+ mkdirSync(dirname(join(checkout, path)), { recursive: true });
144
+ writeFileSync(join(checkout, path), text);
145
+ }
123
146
  git(checkout, 'add', '.');
124
147
  git(checkout, 'commit', '-m', 'chore: publish 0.0.1');
125
148
  writeFileSync(join(checkout, 'change.txt'), 'something that is not published yet\n');
@@ -131,7 +154,14 @@ function runPublisherIn(packageName, { fails = 'no command line has this in it'
131
154
  const result = spawnSync(process.execPath, [PUBLISHER], {
132
155
  cwd: checkout,
133
156
  encoding: 'utf8',
134
- env: { ...process.env, PATH: `${bin}:${process.env.PATH}`, STAND_IN_CALLS: callsFile, STAND_IN_FAILS: fails }
157
+ env: {
158
+ ...process.env,
159
+ PATH: `${bin}:${process.env.PATH}`,
160
+ STAND_IN_CALLS: callsFile,
161
+ STAND_IN_FAILS: fails,
162
+ STAND_IN_PACK: JSON.stringify([{ files: packList.map((path) => ({ path })) }]),
163
+ STAND_IN_BUMP: bump ? '1' : ''
164
+ }
135
165
  });
136
166
  const calls = existsSync(callsFile) ? readFileSync(callsFile, 'utf8').split('\n').filter(Boolean) : [];
137
167
  return {
@@ -178,6 +208,7 @@ test('embed-sdk checks the CDN settings first, then raises the version, builds,
178
208
  'npm version patch --no-git-tag-version',
179
209
  'npm run build',
180
210
  'npm run check:no-secrets',
211
+ 'npm pack --dry-run --json --ignore-scripts',
181
212
  'npm publish --ignore-scripts --access public',
182
213
  'npm run publish:cdn -- --yes --purge --allow-dirty'
183
214
  ]);
@@ -223,10 +254,72 @@ test('the cli checks, gates, raises the version, builds and publishes to npm, in
223
254
  'npm run check:release',
224
255
  'npm version patch --no-git-tag-version',
225
256
  'npm run build',
257
+ 'npm pack --dry-run --json --ignore-scripts',
226
258
  'npm publish --ignore-scripts --access public'
227
259
  ]);
228
260
  });
229
261
 
262
+ test('every package reads what npm would pack, after its build and right before npm publish', () => {
263
+ const publisher = readFileSync(PUBLISHER, 'utf8');
264
+ // One place for all of them: `publishToNpm` is what every branch of the publisher calls to publish.
265
+ assert.equal(publisher.split('verifyPackedImports();').length - 1, 1, 'the check is called once, from publishToNpm');
266
+ assert.match(publisher, /function publishToNpm\(\) \{\n\tverifyPackedImports\(\);\n/);
267
+ assert.equal(publisher.split("run('npm', ['publish'").length - 1, 1, 'npm publish is called once');
268
+ assert.equal(publisher.indexOf('verifyPackedImports();') < publisher.indexOf("run('npm', ['publish'"), true);
269
+ for (const packageName of ['canvas', 'api-sdk', 'ui-sdk', 'embed-sdk', 'cli', 'dev-tools']) {
270
+ const run = runPublisherIn(packageName);
271
+ const packCall = run.calls.indexOf('npm pack --dry-run --json --ignore-scripts');
272
+ const publishCall = run.calls.findIndex((call) => call.startsWith('npm publish'));
273
+ assert.equal(packCall > -1, true, `${packageName} does not read the pack`);
274
+ assert.equal(publishCall, packCall + 1, `${packageName}: npm publish does not come right after the pack was read`);
275
+ const buildCall = run.calls.findIndex((call) => /^npm run (build|build:publish|package)$/.test(call));
276
+ if (buildCall > -1) assert.equal(buildCall < packCall, true, `${packageName} reads the pack before it is built`);
277
+ }
278
+ });
279
+
280
+ test('a package whose packed files import a file it does not hold stops before npm publish, and puts the version back', () => {
281
+ const run = runPublisherIn('canvas', {
282
+ bump: true,
283
+ files: {
284
+ 'dist/embed/parse.js': "import schema from '../../../static/embed-schema.json';\nexport const parse = () => schema;\n",
285
+ 'dist/index.js': "export { parse } from './embed/parse.js';\n"
286
+ },
287
+ packList: ['package.json', 'dist/index.js', 'dist/embed/parse.js']
288
+ });
289
+ assert.equal(run.status, 1, run.output);
290
+ assert.deepEqual(run.calls, [
291
+ 'npm version patch --no-git-tag-version',
292
+ 'npm run package',
293
+ 'npm pack --dry-run --json --ignore-scripts'
294
+ ]);
295
+ assert.equal(run.versionInPackageJson, '0.0.1', 'the version the stand-in raised is put back');
296
+ assert.equal(run.commitsAdded, 0);
297
+ assert.match(run.output, /canvas: 1 import\(s\) of the packed files cannot be loaded from the installed package\. Nothing was published\./);
298
+ assert.match(run.output, /dist\/embed\/parse\.js imports '\.\.\/\.\.\/\.\.\/static\/embed-schema\.json', a file outside the package/);
299
+ });
300
+
301
+ test('a package whose packed files only import files it holds goes on to npm publish', () => {
302
+ const run = runPublisherIn('canvas', {
303
+ files: {
304
+ 'dist/index.js': "// import example from '../../not-an-import.js';\nexport { parse } from './parse.js';\n",
305
+ 'dist/parse.js': "import schema from './schema.json';\nexport const parse = () => schema;\n",
306
+ 'dist/schema.json': '{}'
307
+ },
308
+ packList: ['package.json', 'dist/index.js', 'dist/parse.js', 'dist/schema.json']
309
+ });
310
+ assert.equal(run.calls.some((call) => call.startsWith('npm publish')), true, run.output);
311
+ assert.match(run.output, /canvas: 2 of 4 packed files read, no import leaves the package/);
312
+ });
313
+
314
+ test('when npm cannot say what it would pack, the publish stops before npm publish and puts the version back', () => {
315
+ const run = runPublisherIn('canvas', { bump: true, fails: 'pack --dry-run' });
316
+ assert.equal(run.status, 1, run.output);
317
+ assert.equal(run.calls.some((call) => call.startsWith('npm publish')), false);
318
+ assert.equal(run.versionInPackageJson, '0.0.1');
319
+ assert.equal(run.commitsAdded, 0);
320
+ assert.match(run.output, /canvas: could not read what npm would pack/);
321
+ });
322
+
230
323
  test('another package does not run the release gate of the cli', () => {
231
324
  const run = runPublisherIn('canvas');
232
325
  assert.equal(run.calls.some((call) => call.includes('check:release')), false);
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Does the Frontend sync walk every git checkout, skip a dirty tree, and
3
+ * only delete Vite caches that exist?
4
+ *
5
+ * The throwaway tree is a fake Frontend/: named directories, a `.git` marker
6
+ * (a file is enough — git itself is never called against it), and optional
7
+ * `node_modules/.vite` folders. `git` is a stub that records the args and
8
+ * returns whatever the fixture asked for, so a real `git pull` cannot run.
9
+ */
10
+
11
+ import { test } from 'node:test';
12
+ import assert from 'node:assert/strict';
13
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync, existsSync } from 'node:fs';
14
+ import { tmpdir } from 'node:os';
15
+ import { join } from 'node:path';
16
+
17
+ import {
18
+ existingViteCacheDirectories,
19
+ listGitCheckouts,
20
+ syncFrontendCheckouts
21
+ } from '../lib/sync-frontend-checkouts.mjs';
22
+
23
+ /** @type {string[]} */
24
+ const created = [];
25
+
26
+ function frontendTree(names) {
27
+ const root = mkdtempSync(join(tmpdir(), 'sync-frontend-'));
28
+ created.push(root);
29
+ for (const name of names) {
30
+ const dir = join(root, name);
31
+ mkdirSync(dir, { recursive: true });
32
+ writeFileSync(join(dir, '.git'), 'gitdir: fake\n');
33
+ }
34
+ return root;
35
+ }
36
+
37
+ test('listGitCheckouts only returns immediate children that have .git', () => {
38
+ const root = frontendTree(['site', 'editor']);
39
+ mkdirSync(join(root, 'not-a-repo'));
40
+ writeFileSync(join(root, 'README.md'), 'no');
41
+ const listed = listGitCheckouts(root).map((path) => path.slice(root.length + 1));
42
+ assert.deepEqual(listed, ['editor', 'site']);
43
+ });
44
+
45
+ test('existingViteCacheDirectories only names folders that are on disk', () => {
46
+ const root = frontendTree(['site']);
47
+ const site = join(root, 'site');
48
+ assert.deepEqual(existingViteCacheDirectories(site), []);
49
+ mkdirSync(join(site, 'node_modules', '.vite'), { recursive: true });
50
+ assert.equal(existingViteCacheDirectories(site).length, 1);
51
+ assert.ok(existingViteCacheDirectories(site)[0].endsWith('node_modules/.vite'));
52
+ });
53
+
54
+ test('a dirty checkout is committed then pulled; its Vite cache is still removed', () => {
55
+ const root = frontendTree(['editor']);
56
+ const editor = join(root, 'editor');
57
+ mkdirSync(join(editor, 'node_modules', '.vite'), { recursive: true });
58
+ /** @type {string[][]} */
59
+ const calls = [];
60
+ let dirty = true;
61
+ const git = (_cwd, args) => {
62
+ calls.push(args);
63
+ if (args[0] === 'status') return { code: 0, stdout: dirty ? ' M src/lib/Foo.svelte\n' : '', stderr: '' };
64
+ if (args[0] === 'add') return { code: 0, stdout: '', stderr: '' };
65
+ if (args[0] === 'commit') {
66
+ dirty = false;
67
+ return { code: 0, stdout: '[main abc] chore(editor): commit local changes before sync pull\n', stderr: '' };
68
+ }
69
+ if (args[0] === 'rev-parse') return { code: 0, stdout: 'origin/main\n', stderr: '' };
70
+ if (args[0] === 'pull') return { code: 0, stdout: 'Already up to date.\n', stderr: '' };
71
+ throw new Error(`unexpected git ${args.join(' ')}`);
72
+ };
73
+ const removed = [];
74
+ const results = syncFrontendCheckouts({
75
+ frontendRoot: root,
76
+ git,
77
+ removeDirectory: (path) => removed.push(path)
78
+ });
79
+ assert.equal(results.length, 1);
80
+ assert.equal(results[0].committed, true);
81
+ assert.equal(results[0].pull, 'already-up-to-date');
82
+ assert.equal(calls.some((args) => args[0] === 'commit'), true);
83
+ assert.equal(calls.some((args) => args[0] === 'pull'), true);
84
+ assert.equal(removed.length, 1);
85
+ assert.ok(removed[0].endsWith('node_modules/.vite'));
86
+ });
87
+
88
+ test('--no-commit-dirty leaves a dirty checkout unpulled', () => {
89
+ const root = frontendTree(['editor']);
90
+ const git = (_cwd, args) => {
91
+ if (args[0] === 'status') return { code: 0, stdout: ' M src/lib/Foo.svelte\n', stderr: '' };
92
+ if (args[0] === 'rev-parse') return { code: 0, stdout: 'origin/main\n', stderr: '' };
93
+ throw new Error(`unexpected git ${args.join(' ')}`);
94
+ };
95
+ const results = syncFrontendCheckouts({
96
+ frontendRoot: root,
97
+ git,
98
+ commitDirty: false,
99
+ cleanVite: false
100
+ });
101
+ assert.equal(results[0].pull, 'skipped-dirty');
102
+ });
103
+
104
+ test('a clean checkout with an upstream is pulled with --ff-only', () => {
105
+ const root = frontendTree(['chat']);
106
+ const git = (_cwd, args) => {
107
+ if (args[0] === 'status') return { code: 0, stdout: '', stderr: '' };
108
+ if (args[0] === 'rev-parse') return { code: 0, stdout: 'origin/main\n', stderr: '' };
109
+ if (args[0] === 'pull') {
110
+ assert.deepEqual(args, ['pull', '--ff-only']);
111
+ return { code: 0, stdout: 'Already up to date.\n', stderr: '' };
112
+ }
113
+ throw new Error(`unexpected git ${args.join(' ')}`);
114
+ };
115
+ const results = syncFrontendCheckouts({ frontendRoot: root, git, cleanVite: false });
116
+ assert.equal(results[0].pull, 'already-up-to-date');
117
+ });
118
+
119
+ test('no upstream skips the pull instead of failing', () => {
120
+ const root = frontendTree(['blog']);
121
+ const git = (_cwd, args) => {
122
+ if (args[0] === 'status') return { code: 0, stdout: '', stderr: '' };
123
+ if (args[0] === 'rev-parse') return { code: 128, stdout: '', stderr: 'no upstream' };
124
+ throw new Error(`unexpected git ${args.join(' ')}`);
125
+ };
126
+ const results = syncFrontendCheckouts({ frontendRoot: root, git, cleanVite: false });
127
+ assert.equal(results[0].pull, 'skipped-no-upstream');
128
+ });
129
+
130
+ test('dry-run reports caches and does not delete them', () => {
131
+ const root = frontendTree(['site']);
132
+ const site = join(root, 'site');
133
+ mkdirSync(join(site, 'node_modules', '.vite'), { recursive: true });
134
+ const results = syncFrontendCheckouts({
135
+ frontendRoot: root,
136
+ dryRun: true,
137
+ removeDirectory: () => {
138
+ throw new Error('dry-run must not delete');
139
+ }
140
+ });
141
+ assert.equal(results[0].pull, 'dry-run');
142
+ assert.equal(results[0].cachesRemoved.length, 0);
143
+ assert.equal(results[0].cachesWouldRemove.length, 1);
144
+ assert.equal(existsSync(join(site, 'node_modules', '.vite')), true);
145
+ });
146
+
147
+ test.after(() => {
148
+ for (const root of created) rmSync(root, { recursive: true, force: true });
149
+ });