@widgetic/dev-tools 0.3.8 → 0.3.10

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.
@@ -45,13 +45,14 @@ test('the design-system table expands to every subpath plus the index', () => {
45
45
 
46
46
  test('entry packages alias to their entry file, lib packages to their tree', () => {
47
47
  const aliases = buildSiblingAliases(FRONTEND, {
48
- packages: ['api-sdk', 'cache-layer', 'chat', 'file-browser', 'workflows']
48
+ packages: ['api-sdk', 'cache-layer', 'chat', 'file-browser', 'workflows', 'workflows-engine']
49
49
  });
50
50
  assert.equal(aliases['@widgetic/api-sdk'], `${FRONTEND}/api-sdk/src/index.ts`);
51
51
  assert.equal(aliases['@widgetic/cache-layer'], `${FRONTEND}/cache-layer/src/index.ts`);
52
52
  assert.equal(aliases['@widgetic/chat'], `${FRONTEND}/chat/src/lib`);
53
53
  assert.equal(aliases['@widgetic/file-browser'], `${FRONTEND}/file-browser/src/lib`);
54
54
  assert.equal(aliases['@widgetic/workflows'], `${FRONTEND}/workflows/src/lib`);
55
+ assert.equal(aliases['@widgetic/workflows-engine'], `${FRONTEND}/workflows-engine/src/index.ts`);
55
56
  });
56
57
 
57
58
  test('a selection without design-system emits no design-system aliases', () => {
@@ -1,7 +1,9 @@
1
1
  import { test } from 'node:test';
2
2
  import assert from 'node:assert/strict';
3
- import { readFileSync } from 'node:fs';
4
- import { resolve } from 'node:path';
3
+ import { spawnSync } from 'node:child_process';
4
+ import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
5
+ import { tmpdir } from 'node:os';
6
+ import { join, resolve } from 'node:path';
5
7
  import { fileURLToPath } from 'node:url';
6
8
  import { PUBLISHABLE_PACKAGES } from '../lib/publish-to-npm.mjs';
7
9
 
@@ -20,9 +22,11 @@ test('the walk calls publish-to-npm in each npm package', () => {
20
22
  'editor:publish-to-npm',
21
23
  'file-browser:publish-to-npm',
22
24
  'widget-creator:publish-to-npm',
25
+ 'workflows-engine:publish-to-npm',
23
26
  'workflows:publish-to-npm',
24
27
  'embed-sdk:publish-to-npm',
25
- 'ui-sdk:publish-to-npm'
28
+ 'ui-sdk:publish-to-npm',
29
+ 'cli:publish-to-npm'
26
30
  ]
27
31
  );
28
32
  assert.equal(PUBLISHABLE_PACKAGES.some((pkg) => pkg.dir === 'site'), false);
@@ -46,3 +50,190 @@ test('each package script calls the one publisher, and the walk is separate', ()
46
50
  const devTools = JSON.parse(readFileSync(resolve(FRONTEND, 'dev-tools/package.json'), 'utf8'));
47
51
  assert.equal(devTools.scripts['publish-all-to-npm'], 'node bin/publish-all-to-npm.mjs');
48
52
  });
53
+
54
+ // The publisher beside this test (the one above is found through the sibling layout of the Frontend folder).
55
+ const PUBLISHER = fileURLToPath(new URL('../bin/publish-package.mjs', import.meta.url));
56
+
57
+ test('ui-sdk and embed-sdk check the CDN settings before they raise the version, and no other package does', () => {
58
+ const publisher = readFileSync(PUBLISHER, 'utf8');
59
+ const uiSdkPreflight = "run('npm', ['run', 'release:settings'], { cwd: join(packageDir, 'modules') });";
60
+ const embedSdkPreflight = "run('npm', ['run', 'publish:cdn:settings', '--', '--purge']);";
61
+ assert.equal(publisher.split('release:settings').length - 1, 1, 'release:settings is called once');
62
+ assert.equal(publisher.split('publish:cdn:settings').length - 1, 1, 'publish:cdn:settings is called once');
63
+ assert.equal(publisher.includes(uiSdkPreflight), true);
64
+ assert.equal(publisher.includes(embedSdkPreflight), true);
65
+ // Each call sits alone in the branch of its own package, and the two branches come right before the version is raised.
66
+ assert.match(
67
+ publisher,
68
+ /if \(packageName === 'ui-sdk'\) \{\n(?:\t\/\/[^\n]*\n)*\trun\('npm', \['run', 'release:settings'\], \{ cwd: join\(packageDir, 'modules'\) \}\);\n\}\n\nif \(packageName === 'embed-sdk'\) \{\n(?:\t\/\/[^\n]*\n)*\trun\('npm', \['run', 'publish:cdn:settings', '--', '--purge'\]\);\n\}\n\nrun\('npm', \['version', 'patch', '--no-git-tag-version'\]\);/
69
+ );
70
+ assert.equal(publisher.indexOf('release:settings') < publisher.indexOf('publishToNpm();'), true);
71
+ assert.equal(publisher.indexOf('publish:cdn:settings') < publisher.indexOf('publishToNpm();'), true);
72
+ });
73
+
74
+ test('the embed-sdk upload names no settings file: its settings come from Doppler, every time', () => {
75
+ const publisher = readFileSync(PUBLISHER, 'utf8');
76
+ assert.equal(publisher.includes('--env-file'), false);
77
+ assert.equal(publisher.includes('.env.cdn'), false);
78
+ assert.equal(publisher.includes("run('npm', ['run', 'publish:cdn', '--', '--yes', '--purge', '--allow-dirty']);"), true);
79
+ });
80
+
81
+ test('the scripts the publisher runs before it raises a version exist in the package that is checked', () => {
82
+ // A preflight that calls a script which is not there stops every publish of that package, before anything changes.
83
+ const scriptsOf = (/** @type {string} */ relativePath) =>
84
+ JSON.parse(readFileSync(resolve(FRONTEND, relativePath), 'utf8')).scripts ?? {};
85
+ assert.equal(typeof scriptsOf('ui-sdk/modules/package.json')['release:settings'], 'string');
86
+ assert.equal(typeof scriptsOf('embed-sdk/package.json')['publish:cdn:settings'], 'string');
87
+ });
88
+
89
+ /**
90
+ * Runs the real publisher in a throwaway checkout of a package called `packageName`, with a stand-in `npm` and `node`
91
+ * first on the PATH that only write down their arguments: nothing is built, published or sent anywhere, and no Doppler
92
+ * is asked. `fails` is a piece of a command line that makes the stand-ins exit 1.
93
+ * @param {string} packageName
94
+ * @param {{ fails?: string }} [options]
95
+ */
96
+ function runPublisherIn(packageName, { fails = 'no command line has this in it' } = {}) {
97
+ const sandbox = mkdtempSync(join(tmpdir(), 'publish-package-'));
98
+ try {
99
+ const origin = join(sandbox, 'origin.git');
100
+ const checkout = join(sandbox, packageName);
101
+ const bin = join(sandbox, 'bin');
102
+ const callsFile = join(sandbox, 'calls.log');
103
+ mkdirSync(bin);
104
+ for (const tool of ['npm', 'node']) {
105
+ writeFileSync(
106
+ join(bin, tool),
107
+ `#!/bin/sh\necho "${tool} $*" >> "$STAND_IN_CALLS"\ncase "${tool} $*" in *"$STAND_IN_FAILS"*) exit 1 ;; esac\nexit 0\n`,
108
+ { mode: 0o755 }
109
+ );
110
+ }
111
+ const git = (/** @type {string} */ cwd, /** @type {string[]} */ ...args) => {
112
+ const result = spawnSync('git', ['-c', 'user.name=Test', '-c', 'user.email=test@example.test', ...args], { cwd, encoding: 'utf8' });
113
+ assert.equal(result.status, 0, `git ${args.join(' ')}: ${result.stderr}`);
114
+ return result.stdout.trim();
115
+ };
116
+ git(sandbox, 'init', '--bare', '-b', 'main', origin);
117
+ mkdirSync(join(checkout, 'src', 'sdk'), { recursive: true });
118
+ mkdirSync(join(checkout, 'modules'));
119
+ git(checkout, 'init', '-b', 'main');
120
+ git(checkout, 'remote', 'add', 'origin', origin);
121
+ writeFileSync(join(checkout, 'package.json'), JSON.stringify({ name: packageName, version: '0.0.1' }, null, 2) + '\n');
122
+ writeFileSync(join(checkout, 'src', 'sdk', 'version.ts'), "export const SDK_VERSION = '0.0.1';\n");
123
+ git(checkout, 'add', '.');
124
+ git(checkout, 'commit', '-m', 'chore: publish 0.0.1');
125
+ writeFileSync(join(checkout, 'change.txt'), 'something that is not published yet\n');
126
+ git(checkout, 'add', '.');
127
+ git(checkout, 'commit', '-m', 'feat: a change since the last publish');
128
+ git(checkout, 'push', '-u', 'origin', 'main');
129
+ const commitsBefore = git(checkout, 'rev-list', '--count', 'HEAD');
130
+
131
+ const result = spawnSync(process.execPath, [PUBLISHER], {
132
+ cwd: checkout,
133
+ encoding: 'utf8',
134
+ env: { ...process.env, PATH: `${bin}:${process.env.PATH}`, STAND_IN_CALLS: callsFile, STAND_IN_FAILS: fails }
135
+ });
136
+ const calls = existsSync(callsFile) ? readFileSync(callsFile, 'utf8').split('\n').filter(Boolean) : [];
137
+ return {
138
+ status: result.status,
139
+ output: `${result.stdout}${result.stderr}`,
140
+ calls,
141
+ versionInPackageJson: JSON.parse(readFileSync(join(checkout, 'package.json'), 'utf8')).version,
142
+ commitsAdded: Number(git(checkout, 'rev-list', '--count', 'HEAD')) - Number(commitsBefore)
143
+ };
144
+ } finally {
145
+ rmSync(sandbox, { recursive: true, force: true });
146
+ }
147
+ }
148
+
149
+ test('ui-sdk stops before it raises the version, builds or publishes anything, when the CDN settings cannot be read', () => {
150
+ const run = runPublisherIn('ui-sdk', { fails: 'run release:settings' });
151
+ assert.equal(run.status, 1, run.output);
152
+ assert.deepEqual(run.calls, ['npm run release:settings']);
153
+ assert.equal(run.versionInPackageJson, '0.0.1');
154
+ assert.equal(run.commitsAdded, 0);
155
+ assert.match(run.output, /ui-sdk: npm run release:settings exited 1/);
156
+ });
157
+
158
+ test('ui-sdk checks the CDN settings first, then raises the version, then builds and publishes', () => {
159
+ const run = runPublisherIn('ui-sdk');
160
+ assert.deepEqual(run.calls.slice(0, 3), ['npm run release:settings', 'npm version patch --no-git-tag-version', 'npm run build:publish']);
161
+ assert.equal(run.calls.indexOf('npm run release:settings') < run.calls.findIndex((call) => call.startsWith('npm publish')), true);
162
+ });
163
+
164
+ test('embed-sdk stops before it raises the version, builds or publishes anything, when the CDN settings cannot be read', () => {
165
+ const run = runPublisherIn('embed-sdk', { fails: 'run publish:cdn:settings' });
166
+ assert.equal(run.status, 1, run.output);
167
+ assert.deepEqual(run.calls, ['npm run publish:cdn:settings -- --purge']);
168
+ assert.equal(run.versionInPackageJson, '0.0.1');
169
+ assert.equal(run.commitsAdded, 0);
170
+ assert.match(run.output, /embed-sdk: npm run publish:cdn:settings -- --purge exited 1/);
171
+ });
172
+
173
+ test('embed-sdk checks the CDN settings first, then raises the version, builds, publishes to npm and uploads with no settings file', () => {
174
+ const run = runPublisherIn('embed-sdk');
175
+ // The stand-in `npm` changes no version, so the commit that follows has nothing in it: only the calls of npm and node are held here.
176
+ assert.deepEqual(run.calls, [
177
+ 'npm run publish:cdn:settings -- --purge',
178
+ 'npm version patch --no-git-tag-version',
179
+ 'npm run build',
180
+ 'npm run check:no-secrets',
181
+ 'npm publish --ignore-scripts --access public',
182
+ 'npm run publish:cdn -- --yes --purge --allow-dirty'
183
+ ]);
184
+ for (const call of run.calls) assert.doesNotMatch(call, /--env-file|\.env\.cdn/);
185
+ });
186
+
187
+ test('the cli runs its checks and its release gate before the version is raised, and no other package runs the gate', () => {
188
+ const publisher = readFileSync(PUBLISHER, 'utf8');
189
+ const gateCall = "run('npm', ['run', 'check:release']);";
190
+ assert.equal(publisher.split(gateCall).length - 1, 1, 'check:release is called once');
191
+ assert.match(
192
+ publisher,
193
+ /if \(packageName === 'cli'\) \{\n(?:\t\/\/[^\n]*\n)*\trun\('npm', \['run', 'check'\]\);\n\trun\('npm', \['run', 'check:release'\]\);\n\}\n\nif \(packageName === 'ui-sdk'\) \{/
194
+ );
195
+ // The gate is not `prepublishOnly`: the publish passes --ignore-scripts, so a script of the package would be skipped.
196
+ assert.equal(publisher.indexOf(gateCall) < publisher.indexOf("run('npm', ['version', 'patch'"), true);
197
+ assert.equal(publisher.indexOf(gateCall) < publisher.indexOf('publishToNpm();'), true);
198
+ });
199
+
200
+ test('the cli stops before it raises the version, builds or publishes anything, when its own checks fail', () => {
201
+ const run = runPublisherIn('cli', { fails: 'run check' });
202
+ assert.equal(run.status, 1, run.output);
203
+ assert.deepEqual(run.calls, ['npm run check']);
204
+ assert.equal(run.versionInPackageJson, '0.0.1');
205
+ assert.equal(run.commitsAdded, 0);
206
+ assert.match(run.output, /cli: npm run check exited 1/);
207
+ });
208
+
209
+ test('the cli stops before it raises the version, builds or publishes anything, when the release gate fails', () => {
210
+ const run = runPublisherIn('cli', { fails: 'run check:release' });
211
+ assert.equal(run.status, 1, run.output);
212
+ assert.deepEqual(run.calls, ['npm run check', 'npm run check:release']);
213
+ assert.equal(run.versionInPackageJson, '0.0.1');
214
+ assert.equal(run.commitsAdded, 0);
215
+ assert.match(run.output, /cli: npm run check:release exited 1/);
216
+ });
217
+
218
+ test('the cli checks, gates, raises the version, builds and publishes to npm, in that order and nothing else', () => {
219
+ const run = runPublisherIn('cli');
220
+ // The stand-in `npm` changes no version, so the commit that follows has nothing in it: only the calls of npm are held here.
221
+ assert.deepEqual(run.calls, [
222
+ 'npm run check',
223
+ 'npm run check:release',
224
+ 'npm version patch --no-git-tag-version',
225
+ 'npm run build',
226
+ 'npm publish --ignore-scripts --access public'
227
+ ]);
228
+ });
229
+
230
+ test('another package does not run the release gate of the cli', () => {
231
+ const run = runPublisherIn('canvas');
232
+ assert.equal(run.calls.some((call) => call.includes('check:release')), false);
233
+ });
234
+
235
+ test('another package does not check the CDN settings', () => {
236
+ const run = runPublisherIn('canvas');
237
+ assert.equal(run.calls.some((call) => call.includes('release:settings') || call.includes('publish:cdn:settings')), false);
238
+ assert.equal(run.calls[0], 'npm version patch --no-git-tag-version');
239
+ });