@xemahq/repo-build-tooling 0.9.1 → 0.9.2

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@xemahq/repo-build-tooling",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "description": "Dev-time build tooling shared by every Xema repository. Ships as plain ESM with zero dependencies so the published artifact is the reviewed source.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Neuralchowder Inc. <developer@xema.dev> (https://xema.dev)",
@@ -40,6 +40,6 @@
40
40
  "test": "node src/scrub-swagger-plugin-paths.mjs --self-test && node --test src/check-workspace-range-matches-local.test.mjs && node --test src/check-no-vendored-deps.test.mjs && node --test src/readiness.test.mjs && node --test src/composed-image.test.mjs && node --test src/ghcr-manifest.test.mjs && node --test src/declaration-refusal.test.mjs && node --test src/smoke-refusal.test.mjs && node --test src/load-image.test.mjs && node --test src/composed-image-coordinate.test.mjs && node --test src/composed-image-dockerfile.test.mjs"
41
41
  },
42
42
  "xemaRelease": {
43
- "sourceDigest": "sha256:b9de4f5bf70260506dc8fc6c2e2bad30aba714198303f5440888071880721d98"
43
+ "sourceDigest": "sha256:a1775245f7e0b751aeab7d53393103ddc32079c97043755a22b9fa261fa05134"
44
44
  }
45
45
  }
@@ -32,19 +32,33 @@ function required(env, name) {
32
32
  return value;
33
33
  }
34
34
 
35
+ /**
36
+ * The consumer's own `package.json` of the hash package, found through Node's own
37
+ * `node_modules` lookup paths for the consumer, NOT through `require.resolve`: the
38
+ * package's `exports` map need not expose `./package.json`. Only an absent file
39
+ * means "not installed"; any other read error is rethrown as it is.
40
+ */
41
+ function findHashManifest(root) {
42
+ for (const modulesDir of createRequire(join(root, 'package.json')).resolve.paths(HASH_PACKAGE) ?? []) {
43
+ const candidate = join(modulesDir, HASH_PACKAGE, 'package.json');
44
+ try {
45
+ readFileSync(candidate, 'utf8');
46
+ return candidate;
47
+ } catch (error) {
48
+ if (error?.code !== 'ENOENT' && error?.code !== 'ENOTDIR') throw error;
49
+ }
50
+ }
51
+ return undefined;
52
+ }
53
+
35
54
  /** The consumer's own `xema-compute-image-tag` bin path; refuses when it is not installed there. */
36
55
  export function resolveHashBin(root) {
37
- let manifestPath;
38
- try {
39
- manifestPath = createRequire(join(root, 'package.json')).resolve(`${HASH_PACKAGE}/package.json`);
40
- } catch (error) {
41
- if (error?.code === 'MODULE_NOT_FOUND' || error?.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
42
- throw new Error(
43
- `${HASH_PACKAGE} is not installed at ${root} — the content tag is computed only by its ${HASH_BIN} bin; ` +
44
- 'add it as a devDependency of this repository and install',
45
- );
46
- }
47
- throw error;
56
+ const manifestPath = findHashManifest(root);
57
+ if (manifestPath === undefined) {
58
+ throw new Error(
59
+ `${HASH_PACKAGE} is not installed at ${root} — the content tag is computed only by its ${HASH_BIN} bin; ` +
60
+ 'add it as a devDependency of this repository and install',
61
+ );
48
62
  }
49
63
  const bin = JSON.parse(readFileSync(manifestPath, 'utf8')).bin?.[HASH_BIN];
50
64
  if (typeof bin !== 'string') throw new Error(`${HASH_PACKAGE} at ${manifestPath} ships no "${HASH_BIN}" bin`);
@@ -26,7 +26,7 @@ function registry(manifestStatus) {
26
26
  }
27
27
 
28
28
  /** A consumer root whose own install carries a stand-in `xema-compute-image-tag` that echoes its arguments into a file. */
29
- function consumer({ withHash = true } = {}) {
29
+ function consumer({ withHash = true, exportsMap } = {}) {
30
30
  const root = fs.mkdtempSync(path.join(os.tmpdir(), 'xema-coordinate-'));
31
31
  fs.writeFileSync(path.join(root, 'package.json'), '{"name":"consumer"}');
32
32
  if (withHash) {
@@ -34,7 +34,7 @@ function consumer({ withHash = true } = {}) {
34
34
  fs.mkdirSync(path.join(pkg, 'dist/bin'), { recursive: true });
35
35
  fs.writeFileSync(
36
36
  path.join(pkg, 'package.json'),
37
- JSON.stringify({ name: '@xemahq/distribution-source-hash', version: '1.0.0', bin: { 'xema-compute-image-tag': 'dist/bin/compute-image-tag.js' } }),
37
+ JSON.stringify({ name: '@xemahq/distribution-source-hash', version: '1.0.0', ...(exportsMap ? { exports: exportsMap } : {}), bin: { 'xema-compute-image-tag': 'dist/bin/compute-image-tag.js' } }),
38
38
  );
39
39
  fs.writeFileSync(
40
40
  path.join(pkg, 'dist/bin/compute-image-tag.js'),
@@ -106,3 +106,18 @@ test('emitCoordinate prints every output and appends them to GITHUB_OUTPUT', ()
106
106
  assert.equal(fs.readFileSync(file, 'utf8'), `image=i\ncontent_tag=${HASH}\nexists=false\nentry={}\n`);
107
107
  assert.match(lines.at(-1), /is missing — build/);
108
108
  });
109
+
110
+ test('a package whose exports map lacks ./package.json still resolves, as published 0.2.2 does', () => {
111
+ const root = consumer({ exportsMap: { '.': './dist/index.js' } });
112
+ assert.equal(
113
+ resolveHashBin(root),
114
+ path.join(root, 'node_modules/@xemahq/distribution-source-hash', 'dist/bin/compute-image-tag.js'),
115
+ );
116
+ });
117
+
118
+ test('a read error other than absence is rethrown with its real message, not mapped to not installed', () => {
119
+ const root = consumer({ withHash: false });
120
+ // package.json is a directory: EISDIR, which is not "absent"
121
+ fs.mkdirSync(path.join(root, 'node_modules/@xemahq/distribution-source-hash/package.json'), { recursive: true });
122
+ assert.throws(() => resolveHashBin(root), (error) => error.code === 'EISDIR' && !/not installed/.test(error.message));
123
+ });
@@ -13,11 +13,12 @@
13
13
  // builder as the `node` user with NODE_ENV=production, after the production
14
14
  // install, which is the user and environment the image runs with.
15
15
 
16
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
17
- import { join, posix } from 'node:path';
16
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
17
+ import { dirname, join, posix } from 'node:path';
18
18
 
19
19
  import { COMPOSED_IMAGE_DIR } from './composed-image-paths.mjs';
20
20
 
21
+ const TOKEN_REFERENCE = /_authToken\b|\$\{(?:NPM_TOKEN|GH_PACKAGES_TOKEN)\}/;
21
22
  const VERSION = /^\d+\.\d+\.\d+$/;
22
23
  const DIRECTORY = /^[A-Za-z0-9._-]+(\/[A-Za-z0-9._-]+)*\/?$/;
23
24
  const CACHE_ID = /^[a-z0-9][a-z0-9-]*$/;
@@ -39,8 +40,14 @@ function validate({ toolingVersion, copy, cacheId, buildSource }) {
39
40
  if (!REPOSITORY.test(buildSource ?? '')) throw new Error(`--build-source must name a repository, got "${buildSource}"`);
40
41
  }
41
42
 
42
- /** The Dockerfile text. `imageDir` is where the repository keeps the image (and so where this is written). */
43
- export function renderComposedDockerfile({ toolingVersion, copy, cacheId, buildSource, imageDir = COMPOSED_IMAGE_DIR }) {
43
+ /** Whether the repository's `.npmrc` authenticates with a token (`_authToken`, `${NPM_TOKEN}`); a public-registry `.npmrc` needs none. */
44
+ export function npmrcUsesToken(npmrcText) {
45
+ return TOKEN_REFERENCE.test(npmrcText);
46
+ }
47
+
48
+ /** The Dockerfile text. `usesToken` mounts the `npm_token` build secret; derive it with `npmrcUsesToken`. `imageDir` is where the repository keeps the image (and so where this is written). */
49
+ export function renderComposedDockerfile({ toolingVersion, copy, cacheId, buildSource, usesToken, imageDir = COMPOSED_IMAGE_DIR }) {
50
+ if (typeof usesToken !== 'boolean') throw new Error('usesToken must be a boolean derived from .npmrc');
44
51
  validate({ toolingVersion, copy, cacheId, buildSource });
45
52
  const dirFlag = imageDir === COMPOSED_IMAGE_DIR ? '' : ` --image-dir ${imageDir}`;
46
53
  const tool = (command) => `xema-composed-image ${command}${dirFlag}`;
@@ -48,6 +55,11 @@ export function renderComposedDockerfile({ toolingVersion, copy, cacheId, buildS
48
55
  .map((d) => `COPY ${d} ./${d}`)
49
56
  .join('\n');
50
57
  const store = `--mount=type=cache,id=pnpm-store-${cacheId},target=/root/.pnpm-store`;
58
+ const mount = usesToken ? ' --mount=type=secret,id=npm_token,required=true \\\\\n' : '';
59
+ const tokenEnv = usesToken ? 'NPM_TOKEN="$(cat /run/secrets/npm_token)" GH_PACKAGES_TOKEN="$NPM_TOKEN" \\\\\n ' : '';
60
+ const tokenExport = usesToken
61
+ ? 'export NPM_TOKEN="$(cat /run/secrets/npm_token)" && export GH_PACKAGES_TOKEN="$NPM_TOKEN" && \\\\\n '
62
+ : '';
51
63
  const metadata = `--mount=type=cache,id=pnpm-metadata-${cacheId},target=/root/.cache/pnpm`;
52
64
  return `# @generated by \`xema-composed-image dockerfile --write\` (@xemahq/repo-build-tooling@${toolingVersion}) — DO NOT EDIT.
53
65
  # Re-render with the same options; \`--check\` refuses a stale file.
@@ -90,13 +102,9 @@ RUN ${tool('filters')} > /tmp/member-filters && \\
90
102
  test -s /tmp/member-filters
91
103
 
92
104
  RUN ${store} \\
93
- --mount=type=secret,id=npm_token,required=true \\
94
- NPM_TOKEN="$(cat /run/secrets/npm_token)" GH_PACKAGES_TOKEN="$NPM_TOKEN" \\
95
- pnpm fetch $(cat /tmp/member-filters) --frozen-lockfile
105
+ ${mount} ${tokenEnv}pnpm fetch $(cat /tmp/member-filters) --frozen-lockfile
96
106
  RUN ${store} \\
97
- --mount=type=secret,id=npm_token,required=true \\
98
- NPM_TOKEN="$(cat /run/secrets/npm_token)" GH_PACKAGES_TOKEN="$NPM_TOKEN" \\
99
- pnpm install --prefer-offline $(cat /tmp/member-filters) --frozen-lockfile
107
+ ${mount} ${tokenEnv}pnpm install --prefer-offline $(cat /tmp/member-filters) --frozen-lockfile
100
108
 
101
109
  # Every member that owns a Prisma schema generates its OWN client into its own
102
110
  # \`node_modules/.prisma/<service>\` (the schema's \`output\`), before it builds.
@@ -106,9 +114,7 @@ RUN for schema in $(${tool('dirs')}); do \\
106
114
  fi; \\
107
115
  done
108
116
 
109
- RUN --mount=type=secret,id=npm_token,required=true \\
110
- NPM_TOKEN="$(cat /run/secrets/npm_token)" GH_PACKAGES_TOKEN="$NPM_TOKEN" \\
111
- pnpm $(cat /tmp/member-filters) run build
117
+ RUN ${mount ? `${mount.trimStart()} ` : ''}${tokenEnv}pnpm $(cat /tmp/member-filters) run build
112
118
 
113
119
  # Production install, with the members' WORKSPACE packages INJECTED — the same
114
120
  # resolution the per-service image gets from \`pnpm deploy\` (legacy deploy
@@ -134,9 +140,7 @@ RUN --mount=type=secret,id=npm_token,required=true \\
134
140
  # install built, and the members' \`postinstall\` is the Prisma loop below.
135
141
  RUN ${store} \\
136
142
  ${metadata} \\
137
- --mount=type=secret,id=npm_token,required=true \\
138
- export NPM_TOKEN="$(cat /run/secrets/npm_token)" && export GH_PACKAGES_TOKEN="$NPM_TOKEN" && \\
139
- INJECT="--config.inject-workspace-packages=true --config.dedupe-injected-deps=false" && \\
143
+ ${mount} ${tokenExport}INJECT="--config.inject-workspace-packages=true --config.dedupe-injected-deps=false" && \\
140
144
  cp pnpm-lock.yaml /tmp/pnpm-lock.committed.yaml && \\
141
145
  pnpm install --lockfile-only --prefer-offline \${INJECT} $(cat /tmp/member-filters) && \\
142
146
  ${tool('pins')} /tmp/pnpm-lock.committed.yaml && \\
@@ -170,10 +174,16 @@ CMD ["node", "/monorepo/xema-composed-process"]
170
174
  /** `--write` writes the rendered Dockerfile; `--check` throws when the committed one differs. */
171
175
  export function syncComposedDockerfile({ mode, root = process.cwd(), imageDir = COMPOSED_IMAGE_DIR, ...options }) {
172
176
  if (mode !== 'write' && mode !== 'check') throw new Error(`mode must be write or check, got "${mode}"`);
173
- const rendered = renderComposedDockerfile({ ...options, imageDir });
177
+ validate(options);
178
+ const npmrc = join(root, '.npmrc');
179
+ if (!existsSync(npmrc)) throw new Error(`${npmrc} is absent — the Dockerfile copies it and its token use decides the build secret`);
180
+ const rendered = renderComposedDockerfile({ ...options, usesToken: npmrcUsesToken(readFileSync(npmrc, 'utf8')), imageDir });
174
181
  const relativeTarget = posix.join(imageDir, 'Dockerfile');
175
182
  const target = join(root, relativeTarget);
176
- if (mode === 'write') writeFileSync(target, rendered);
183
+ if (mode === 'write') {
184
+ mkdirSync(dirname(target), { recursive: true });
185
+ writeFileSync(target, rendered);
186
+ }
177
187
  else if ((existsSync(target) ? readFileSync(target, 'utf8') : '') !== rendered) {
178
188
  throw new Error(`${relativeTarget} is stale. Run: xema-composed-image dockerfile --write (with the same options)`);
179
189
  }
@@ -4,13 +4,14 @@ import os from 'node:os';
4
4
  import path from 'node:path';
5
5
  import test from 'node:test';
6
6
 
7
- import { renderComposedDockerfile, syncComposedDockerfile } from './composed-image-dockerfile.mjs';
7
+ import { npmrcUsesToken, renderComposedDockerfile, syncComposedDockerfile } from './composed-image-dockerfile.mjs';
8
8
 
9
9
  const OPTIONS = {
10
10
  toolingVersion: '0.9.0',
11
11
  copy: ['packages', 'tooling/dev/', 'biomes'],
12
12
  cacheId: 'alpha-composed-process',
13
13
  buildSource: 'alpha',
14
+ usesToken: true,
14
15
  };
15
16
 
16
17
  test('the template carries every parameter and no per-repository literal', () => {
@@ -51,9 +52,35 @@ test('the tool version, the directories and the ids are validated', () => {
51
52
  test('--write renders and --check refuses a stale or missing file', () => {
52
53
  const root = fs.mkdtempSync(path.join(os.tmpdir(), 'xema-dockerfile-'));
53
54
  fs.mkdirSync(path.join(root, 'infra/composed-process'), { recursive: true });
55
+ fs.writeFileSync(path.join(root, '.npmrc'), '//registry.npmjs.org/:_authToken=${NPM_TOKEN}\n');
54
56
  assert.throws(() => syncComposedDockerfile({ mode: 'check', root, ...OPTIONS }), /is stale/);
55
57
  assert.equal(syncComposedDockerfile({ mode: 'write', root, ...OPTIONS }), 'infra/composed-process/Dockerfile');
56
58
  syncComposedDockerfile({ mode: 'check', root, ...OPTIONS });
57
59
  assert.throws(() => syncComposedDockerfile({ mode: 'check', root, ...OPTIONS, toolingVersion: '0.9.1' }), /is stale/);
58
60
  assert.throws(() => syncComposedDockerfile({ mode: 'sideways', root, ...OPTIONS }), /write or check/);
59
61
  });
62
+
63
+ test('the npm_token secret is mounted only when .npmrc references a token', () => {
64
+ const withToken = renderComposedDockerfile(OPTIONS);
65
+ assert.equal(withToken.match(/type=secret,id=npm_token,required=true/g).length, 4);
66
+ assert.equal(withToken.match(/NPM_TOKEN="\$\(cat \/run\/secrets\/npm_token\)"/g).length, 4);
67
+ const without = renderComposedDockerfile({ ...OPTIONS, usesToken: false });
68
+ assert.doesNotMatch(without, /npm_token|NPM_TOKEN|GH_PACKAGES_TOKEN|type=secret/);
69
+ assert.match(without, /RUN --mount=type=cache,id=pnpm-store-alpha-composed-process,target=\/root\/.pnpm-store \\\n {2}pnpm fetch/);
70
+ assert.match(without, /RUN pnpm \$\(cat \/tmp\/member-filters\) run build/);
71
+ assert.throws(() => renderComposedDockerfile({ ...OPTIONS, usesToken: undefined }), /usesToken/);
72
+ });
73
+
74
+ test('the token use is derived from .npmrc, deterministically', () => {
75
+ assert.equal(npmrcUsesToken('//registry.npmjs.org/:_authToken=${NPM_TOKEN}\n'), true);
76
+ assert.equal(npmrcUsesToken('@x:registry=https://npm.pkg.github.com\n//npm.pkg.github.com/:_authToken=abc'), true);
77
+ assert.equal(npmrcUsesToken('registry=https://registry.npmjs.org/\nauto-install-peers=true\n'), false);
78
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'xema-dockerfile-npmrc-'));
79
+ assert.throws(() => syncComposedDockerfile({ mode: 'write', root, ...OPTIONS }), /\.npmrc is absent/);
80
+ fs.writeFileSync(path.join(root, '.npmrc'), 'registry=https://registry.npmjs.org/\n');
81
+ syncComposedDockerfile({ mode: 'write', root, ...OPTIONS });
82
+ const text = fs.readFileSync(path.join(root, 'infra/composed-process/Dockerfile'), 'utf8');
83
+ assert.doesNotMatch(text, /npm_token/);
84
+ fs.writeFileSync(path.join(root, '.npmrc'), '//registry.npmjs.org/:_authToken=${NPM_TOKEN}\n');
85
+ assert.throws(() => syncComposedDockerfile({ mode: 'check', root, ...OPTIONS }), /is stale/);
86
+ });
@@ -70,7 +70,7 @@
70
70
  // ═══════════════════════════════════════════════════════════════════════════
71
71
 
72
72
  import { execFileSync } from 'node:child_process';
73
- import { existsSync, readFileSync, realpathSync, symlinkSync, writeFileSync } from 'node:fs';
73
+ import { existsSync, mkdirSync, readFileSync, realpathSync, symlinkSync, writeFileSync } from 'node:fs';
74
74
  import { createRequire } from 'node:module';
75
75
  import { dirname, join, posix, relative, resolve } from 'node:path';
76
76
  import { pathToFileURL } from 'node:url';
@@ -263,8 +263,10 @@ export function syncImageBuildInputs({ mode, root = process.cwd(), imageDir = CO
263
263
  const rendered = renderImageBuildInputs(derived);
264
264
  const relativeTarget = posix.join(imageDir, IMAGE_BUILD_INPUTS_FILE);
265
265
  const target = join(root, relativeTarget);
266
- if (mode === 'write') writeFileSync(target, rendered);
267
- else if ((existsSync(target) ? readFileSync(target, 'utf8') : '') !== rendered) {
266
+ if (mode === 'write') {
267
+ mkdirSync(dirname(target), { recursive: true });
268
+ writeFileSync(target, rendered);
269
+ } else if ((existsSync(target) ? readFileSync(target, 'utf8') : '') !== rendered) {
268
270
  throw new Error(
269
271
  `${relativeTarget} is stale — the composed image would not carry exactly the composable services. ` +
270
272
  'Run: xema-composed-image members --write (with the same options)',
@@ -247,3 +247,12 @@ test('the new commands parse their options; coordinate is asynchronous and refus
247
247
  assert.throws(() => main(['dockerfile', '--write'], { log() {} }), /exact x\.y\.z/);
248
248
  assert.throws(() => main(['smoke-refusal'], { log() {} }), /usage/);
249
249
  });
250
+
251
+ test('members --write on a first run creates the missing image directory', () => {
252
+ const root = fixture();
253
+ assert.equal(fs.existsSync(path.join(root, 'infra/composed-process')), false);
254
+ main(['members', '--root', root, '--manifests', 'products/**/xema-biome.json', '--write'], { log() {} });
255
+ assert.deepEqual(JSON.parse(fs.readFileSync(path.join(root, 'infra/composed-process/image-build-inputs.json'), 'utf8')), {
256
+ workspacePackages: ['products/pai/core/pai-core/api/pai-core-api'],
257
+ });
258
+ });