@mondaydotcomorg/z2h-cli 0.6.0 → 0.6.1

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.
Files changed (88) hide show
  1. package/dist/commands/create-workspace.d.ts.map +1 -1
  2. package/dist/commands/create-workspace.js +2 -5
  3. package/dist/commands/create.js +2 -2
  4. package/dist/constants.d.ts +18 -0
  5. package/dist/constants.d.ts.map +1 -1
  6. package/dist/constants.js +41 -0
  7. package/dist/esm/commands/create-workspace.d.ts.map +1 -1
  8. package/dist/esm/commands/create-workspace.mjs +2 -5
  9. package/dist/esm/commands/create.mjs +2 -2
  10. package/dist/esm/constants.d.ts +18 -0
  11. package/dist/esm/constants.d.ts.map +1 -1
  12. package/dist/esm/constants.mjs +35 -1
  13. package/dist/esm/index.mjs +1 -1
  14. package/dist/esm/migrations/003-npm-broker-scope-split.d.ts +14 -0
  15. package/dist/esm/migrations/003-npm-broker-scope-split.d.ts.map +1 -0
  16. package/dist/esm/migrations/003-npm-broker-scope-split.mjs +49 -0
  17. package/dist/esm/migrations/__tests__/003-npm-broker-scope-split.test.d.ts +2 -0
  18. package/dist/esm/migrations/__tests__/003-npm-broker-scope-split.test.d.ts.map +1 -0
  19. package/dist/esm/shadow/sync.d.ts.map +1 -1
  20. package/dist/esm/shadow/sync.mjs +4 -6
  21. package/dist/esm/util/auth/auth-store.d.ts +10 -0
  22. package/dist/esm/util/auth/auth-store.d.ts.map +1 -1
  23. package/dist/esm/util/auth/auth-store.mjs +20 -2
  24. package/dist/esm/util/auth/broker-credential-provider.d.ts +0 -1
  25. package/dist/esm/util/auth/broker-credential-provider.d.ts.map +1 -1
  26. package/dist/esm/util/auth/broker-credential-provider.mjs +2 -5
  27. package/dist/esm/util/npm/__tests__/broker-config.test.d.ts +2 -0
  28. package/dist/esm/util/npm/__tests__/broker-config.test.d.ts.map +1 -0
  29. package/dist/esm/util/npm/__tests__/install.test.d.ts +2 -0
  30. package/dist/esm/util/npm/__tests__/install.test.d.ts.map +1 -0
  31. package/dist/esm/util/npm/broker-config.d.ts +20 -0
  32. package/dist/esm/util/npm/broker-config.d.ts.map +1 -0
  33. package/dist/esm/util/npm/broker-config.mjs +59 -0
  34. package/dist/esm/util/npm/ensure-token.d.ts +19 -0
  35. package/dist/esm/util/npm/ensure-token.d.ts.map +1 -0
  36. package/dist/esm/util/npm/ensure-token.mjs +22 -0
  37. package/dist/esm/util/npm/install.d.ts +18 -0
  38. package/dist/esm/util/npm/install.d.ts.map +1 -0
  39. package/dist/esm/util/npm/install.mjs +74 -0
  40. package/dist/index.js +1 -1
  41. package/dist/migrations/003-npm-broker-scope-split.d.ts +14 -0
  42. package/dist/migrations/003-npm-broker-scope-split.d.ts.map +1 -0
  43. package/dist/migrations/003-npm-broker-scope-split.js +51 -0
  44. package/dist/migrations/__tests__/003-npm-broker-scope-split.test.d.ts +2 -0
  45. package/dist/migrations/__tests__/003-npm-broker-scope-split.test.d.ts.map +1 -0
  46. package/dist/shadow/sync.d.ts.map +1 -1
  47. package/dist/shadow/sync.js +4 -6
  48. package/dist/util/auth/auth-store.d.ts +10 -0
  49. package/dist/util/auth/auth-store.d.ts.map +1 -1
  50. package/dist/util/auth/auth-store.js +19 -0
  51. package/dist/util/auth/broker-credential-provider.d.ts +0 -1
  52. package/dist/util/auth/broker-credential-provider.d.ts.map +1 -1
  53. package/dist/util/auth/broker-credential-provider.js +2 -6
  54. package/dist/util/npm/__tests__/broker-config.test.d.ts +2 -0
  55. package/dist/util/npm/__tests__/broker-config.test.d.ts.map +1 -0
  56. package/dist/util/npm/__tests__/install.test.d.ts +2 -0
  57. package/dist/util/npm/__tests__/install.test.d.ts.map +1 -0
  58. package/dist/util/npm/broker-config.d.ts +20 -0
  59. package/dist/util/npm/broker-config.d.ts.map +1 -0
  60. package/dist/util/npm/broker-config.js +63 -0
  61. package/dist/util/npm/ensure-token.d.ts +19 -0
  62. package/dist/util/npm/ensure-token.d.ts.map +1 -0
  63. package/dist/util/npm/ensure-token.js +24 -0
  64. package/dist/util/npm/install.d.ts +18 -0
  65. package/dist/util/npm/install.d.ts.map +1 -0
  66. package/dist/util/npm/install.js +76 -0
  67. package/migrations.json +5 -0
  68. package/package.json +1 -1
  69. package/src/commands/__tests__/create-workspace.test.ts +9 -11
  70. package/src/commands/__tests__/create.test.ts +9 -6
  71. package/src/commands/create-workspace.ts +2 -5
  72. package/src/commands/create.ts +2 -2
  73. package/src/constants.ts +41 -0
  74. package/src/index.ts +1 -1
  75. package/src/migrations/003-npm-broker-scope-split.ts +52 -0
  76. package/src/migrations/__tests__/003-npm-broker-scope-split.test.ts +99 -0
  77. package/src/shadow/sync.ts +4 -6
  78. package/src/templates/workspace/npmrc +3 -2
  79. package/src/templates/workspace/yarnrc.yml +6 -2
  80. package/src/util/__tests__/workspace-templates-shipped.test.ts +19 -0
  81. package/src/util/auth/__tests__/broker-credential-provider.test.ts +2 -1
  82. package/src/util/auth/auth-store.ts +19 -1
  83. package/src/util/auth/broker-credential-provider.ts +1 -5
  84. package/src/util/npm/__tests__/broker-config.test.ts +48 -0
  85. package/src/util/npm/__tests__/install.test.ts +94 -0
  86. package/src/util/npm/broker-config.ts +63 -0
  87. package/src/util/npm/ensure-token.ts +28 -0
  88. package/src/util/npm/install.ts +84 -0
@@ -0,0 +1,18 @@
1
+ export interface RunYarnInstallOptions {
2
+ /** Directory to run `yarn install` in. */
3
+ cwd: string;
4
+ /** Extra env to merge on top of `process.env` + the broker token. */
5
+ extraEnv?: NodeJS.ProcessEnv;
6
+ }
7
+ /**
8
+ * Run `yarn install` for an install-bearing command:
9
+ * 1. Preflight — ensure a z2h token exists (prompt `generate-z2h-token` if not).
10
+ * 2. Install through the broker.
11
+ * 3. On a broker 401, surface a friendly "your access expired — refreshing it"
12
+ * message, re-auth, and retry the install exactly once.
13
+ *
14
+ * A non-auth failure (real dependency error, network, etc.) propagates as-is so
15
+ * the command's normal error path reports it.
16
+ */
17
+ export declare function runYarnInstall(opts: RunYarnInstallOptions): Promise<void>;
18
+ //# sourceMappingURL=install.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"install.d.ts","sourceRoot":"","sources":["../../../../src/util/npm/install.ts"],"names":[],"mappings":"AAiBA,MAAM,WAAW,qBAAqB;IACpC,0CAA0C;IAC1C,GAAG,EAAE,MAAM,CAAC;IACZ,qEAAqE;IACrE,QAAQ,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;CAC9B;AAqCD;;;;;;;;;GASG;AACH,wBAAsB,cAAc,CAAC,IAAI,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAc/E"}
@@ -0,0 +1,74 @@
1
+ import { execa } from 'execa';
2
+ import { warn, info } from '../logger.mjs';
3
+ import { tryReadAuthTokenSync } from '../auth/auth-store.mjs';
4
+ import { Z2H_NPM_TOKEN_ENV } from '../../constants.mjs';
5
+ import { ensureZ2hToken } from './ensure-token.mjs';
6
+
7
+ // Substrings that, in a failed install's output, indicate the broker rejected
8
+ // the z2h JWT (expired/invalid). npm surfaces `E401`/`code E401`; yarn berry
9
+ // surfaces an HTTP 401 line. We re-auth on any of these rather than dumping a
10
+ // raw npm error on a non-technical user.
11
+ const AUTH_FAILURE_MARKERS = ['401', 'e401', 'unauthorized', 'authentication'];
12
+ function looksLikeAuthFailure(text) {
13
+ const haystack = text.toLowerCase();
14
+ return AUTH_FAILURE_MARKERS.some(marker => haystack.includes(marker));
15
+ }
16
+ /**
17
+ * Spawn `yarn install` in `cwd` with the npm-broker credential injected, while
18
+ * streaming yarn's output live to the user. stdout is inherited; stderr is
19
+ * piped so we can both echo it and inspect it for a broker auth failure.
20
+ *
21
+ * Returns the captured stderr on success; throws an Error tagged
22
+ * `isAuthFailure` when the failure output looks like a broker 401 so the
23
+ * caller can re-auth, or rethrows the raw execa error otherwise.
24
+ */
25
+ async function spawnYarnInstall(opts) {
26
+ const token = tryReadAuthTokenSync();
27
+ const env = { ...process.env, ...(token && { [Z2H_NPM_TOKEN_ENV]: token }), ...opts.extraEnv };
28
+ const child = execa('yarn', ['install'], {
29
+ cwd: opts.cwd,
30
+ stdout: 'inherit',
31
+ stderr: ['inherit', 'pipe'],
32
+ env,
33
+ });
34
+ try {
35
+ await child;
36
+ }
37
+ catch (err) {
38
+ const e = err;
39
+ const combined = `${e.stderr}\n${e.message ?? ''}`;
40
+ if (looksLikeAuthFailure(combined)) {
41
+ const authErr = new Error('broker rejected the z2h token during install');
42
+ authErr.isAuthFailure = true;
43
+ throw authErr;
44
+ }
45
+ throw err;
46
+ }
47
+ }
48
+ /**
49
+ * Run `yarn install` for an install-bearing command:
50
+ * 1. Preflight — ensure a z2h token exists (prompt `generate-z2h-token` if not).
51
+ * 2. Install through the broker.
52
+ * 3. On a broker 401, surface a friendly "your access expired — refreshing it"
53
+ * message, re-auth, and retry the install exactly once.
54
+ *
55
+ * A non-auth failure (real dependency error, network, etc.) propagates as-is so
56
+ * the command's normal error path reports it.
57
+ */
58
+ async function runYarnInstall(opts) {
59
+ await ensureZ2hToken();
60
+ try {
61
+ await spawnYarnInstall(opts);
62
+ }
63
+ catch (err) {
64
+ if (!err.isAuthFailure) {
65
+ throw err;
66
+ }
67
+ warn('your access expired — refreshing it now…');
68
+ await ensureZ2hToken({ forceRefresh: true });
69
+ info('access refreshed — retrying install');
70
+ await spawnYarnInstall(opts);
71
+ }
72
+ }
73
+
74
+ export { runYarnInstall };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const node_module = require('node:module');
2
- const commander = require('commander');
3
2
  const fs = require('node:fs');
3
+ const commander = require('commander');
4
4
  const commands_dev = require('./commands/dev.js');
5
5
  const commands_build = require('./commands/build.js');
6
6
  const commands_clean = require('./commands/clean.js');
@@ -0,0 +1,14 @@
1
+ import type { Tree } from '@nx/devkit';
2
+ /**
3
+ * Migrate the workspace npm/yarn config from the legacy single-registry
4
+ * `${NPM_TOKEN}` form to the `@mondaydotcomorg`→broker scope-split
5
+ * (`${Z2H_NPM_TOKEN}` placeholder). Rewrites the workspace-root `.yarnrc.yml`
6
+ * and `.npmrc`, plus any consumer-app copies that still carry the legacy
7
+ * placeholder (migration 001 normally removes per-consumer `.yarnrc.yml`, so
8
+ * this is just defensive cleanup).
9
+ *
10
+ * The global `~/.npmrc` broker block is written separately by `migrateCommand`
11
+ * (it lives outside the workspace Tree).
12
+ */
13
+ export default function npmBrokerScopeSplit(tree: Tree): void;
14
+ //# sourceMappingURL=003-npm-broker-scope-split.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"003-npm-broker-scope-split.d.ts","sourceRoot":"","sources":["../../src/migrations/003-npm-broker-scope-split.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAuBvC;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAgB5D"}
@@ -0,0 +1,51 @@
1
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
2
+
3
+ const util_npm_brokerConfig = require('../util/npm/broker-config.js');
4
+
5
+ // Legacy marker: the old configs interpolated a real npm token via
6
+ // `${NPM_TOKEN}`. Its presence means the file predates npm brokering and must
7
+ // be rewritten to the scope-split. Once rewritten (placeholder is now
8
+ // `${Z2H_NPM_TOKEN}`) this no longer matches, so the migration is idempotent.
9
+ const LEGACY_TOKEN_PLACEHOLDER = '${NPM_TOKEN}';
10
+ /**
11
+ * Rewrite a single rc file to `contents` iff it exists and still carries the
12
+ * legacy `${NPM_TOKEN}` placeholder. Returns nothing; mutates the Tree.
13
+ */
14
+ function rewriteIfLegacy(tree, filePath, contents) {
15
+ if (!tree.exists(filePath)) {
16
+ return;
17
+ }
18
+ const current = tree.read(filePath, 'utf-8') ?? '';
19
+ if (!current.includes(LEGACY_TOKEN_PLACEHOLDER)) {
20
+ return; // already migrated (or never legacy) — leave untouched
21
+ }
22
+ tree.write(filePath, contents);
23
+ }
24
+ /**
25
+ * Migrate the workspace npm/yarn config from the legacy single-registry
26
+ * `${NPM_TOKEN}` form to the `@mondaydotcomorg`→broker scope-split
27
+ * (`${Z2H_NPM_TOKEN}` placeholder). Rewrites the workspace-root `.yarnrc.yml`
28
+ * and `.npmrc`, plus any consumer-app copies that still carry the legacy
29
+ * placeholder (migration 001 normally removes per-consumer `.yarnrc.yml`, so
30
+ * this is just defensive cleanup).
31
+ *
32
+ * The global `~/.npmrc` broker block is written separately by `migrateCommand`
33
+ * (it lives outside the workspace Tree).
34
+ */
35
+ function npmBrokerScopeSplit(tree) {
36
+ const yarnrc = util_npm_brokerConfig.workspaceYarnrcContents();
37
+ const npmrc = util_npm_brokerConfig.workspaceNpmrcContents();
38
+ // Workspace root.
39
+ rewriteIfLegacy(tree, '.yarnrc.yml', yarnrc);
40
+ rewriteIfLegacy(tree, '.npmrc', npmrc);
41
+ // Any consumer apps that still carry their own legacy rc files.
42
+ for (const entry of tree.children('.')) {
43
+ if (entry.startsWith('.')) {
44
+ continue;
45
+ }
46
+ rewriteIfLegacy(tree, `${entry}/.yarnrc.yml`, yarnrc);
47
+ rewriteIfLegacy(tree, `${entry}/.npmrc`, npmrc);
48
+ }
49
+ }
50
+
51
+ exports.default = npmBrokerScopeSplit;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=003-npm-broker-scope-split.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"003-npm-broker-scope-split.test.d.ts","sourceRoot":"","sources":["../../../src/migrations/__tests__/003-npm-broker-scope-split.test.ts"],"names":[],"mappings":""}
@@ -1 +1 @@
1
- {"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../../src/shadow/sync.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE3D;;;;;GAKG;AACH,wBAAsB,UAAU,CAAC,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAiB9F"}
1
+ {"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../../src/shadow/sync.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE3D;;;;;GAKG;AACH,wBAAsB,UAAU,CAAC,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAe9F"}
@@ -1,8 +1,8 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
 
3
3
  const fsExtra = require('fs-extra');
4
- const execa = require('execa');
5
4
  const util_logger = require('../util/logger.js');
5
+ const util_npm_install = require('../util/npm/install.js');
6
6
  const shadow_mergePackageJson = require('./merge-package-json.js');
7
7
 
8
8
  /**
@@ -19,11 +19,9 @@ async function syncShadow(paths, consumerPkg) {
19
19
  if (oldHash !== newHash) {
20
20
  util_logger.info(oldHash ? 'consumer deps changed — yarn install' : 'first run — yarn install');
21
21
  // Yarn 4 is cache-first and non-interactive by default in non-TTY
22
- // contexts, so plain `yarn install` is all we need.
23
- await execa.execa('yarn', ['install'], {
24
- cwd: paths.consumerDir,
25
- stdio: 'inherit',
26
- });
22
+ // contexts, so plain `yarn install` is all we need. runYarnInstall injects
23
+ // the npm-broker token, preflights auth, and re-auths on a broker 401.
24
+ await util_npm_install.runYarnInstall({ cwd: paths.consumerDir });
27
25
  await fsExtra.writeFile(paths.shadowDepsHash, newHash);
28
26
  }
29
27
  }
@@ -8,4 +8,14 @@ export declare class Z2hAuthMissingError extends Error {
8
8
  constructor(authFilePath: string);
9
9
  }
10
10
  export declare function readZthAuth(): Promise<ZthAuth>;
11
+ /**
12
+ * Synchronous, never-throws read of the z2h JWT from `~/.zth/auth.json`.
13
+ * Returns the token string, or `null` when the file is absent, unreadable,
14
+ * malformed, or missing the `token` field.
15
+ *
16
+ * Used when building the env for a spawned package-manager process, where a
17
+ * missing token must degrade gracefully (best-effort self-update) rather than
18
+ * blow up — and where an inline sync read keeps the spawn call site simple.
19
+ */
20
+ export declare function tryReadAuthTokenSync(): string | null;
11
21
  //# sourceMappingURL=auth-store.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"auth-store.d.ts","sourceRoot":"","sources":["../../../src/util/auth/auth-store.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,YAAY,QAAkC,CAAC;AAC5D,eAAO,MAAM,aAAa,QAAuC,CAAC;AAElE,MAAM,WAAW,OAAO;IACtB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,qBAAa,mBAAoB,SAAQ,KAAK;aAChB,YAAY,EAAE,MAAM;gBAApB,YAAY,EAAE,MAAM;CAIjD;AAED,wBAAsB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC,CASpD"}
1
+ {"version":3,"file":"auth-store.d.ts","sourceRoot":"","sources":["../../../src/util/auth/auth-store.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,YAAY,QAAkC,CAAC;AAC5D,eAAO,MAAM,aAAa,QAAuC,CAAC;AAElE,MAAM,WAAW,OAAO;IACtB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,qBAAa,mBAAoB,SAAQ,KAAK;aAChB,YAAY,EAAE,MAAM;gBAApB,YAAY,EAAE,MAAM;CAIjD;AAED,wBAAsB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC,CASpD;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,GAAG,IAAI,CAOpD"}
@@ -32,8 +32,27 @@ async function readZthAuth() {
32
32
  }
33
33
  return { token: parsed.token };
34
34
  }
35
+ /**
36
+ * Synchronous, never-throws read of the z2h JWT from `~/.zth/auth.json`.
37
+ * Returns the token string, or `null` when the file is absent, unreadable,
38
+ * malformed, or missing the `token` field.
39
+ *
40
+ * Used when building the env for a spawned package-manager process, where a
41
+ * missing token must degrade gracefully (best-effort self-update) rather than
42
+ * blow up — and where an inline sync read keeps the spawn call site simple.
43
+ */
44
+ function tryReadAuthTokenSync() {
45
+ try {
46
+ const parsed = fsExtra.readJsonSync(Z2H_AUTH_FILE);
47
+ return parsed?.token ?? null;
48
+ }
49
+ catch {
50
+ return null;
51
+ }
52
+ }
35
53
 
36
54
  exports.Z2H_AUTH_DIR = Z2H_AUTH_DIR;
37
55
  exports.Z2H_AUTH_FILE = Z2H_AUTH_FILE;
38
56
  exports.Z2hAuthMissingError = Z2hAuthMissingError;
39
57
  exports.readZthAuth = readZthAuth;
58
+ exports.tryReadAuthTokenSync = tryReadAuthTokenSync;
@@ -1,5 +1,4 @@
1
1
  import type { AwsCredentialIdentityProvider } from '@aws-sdk/types';
2
- export declare const DEFAULT_BROKER_BASE_URL = "https://bigbrain-zth.bigbrain.me";
3
2
  export declare const STS_BROKER_PATH = "/z2h-cli/sts/credentials";
4
3
  /**
5
4
  * AWS SDK credential provider that fetches short-lived deploy credentials
@@ -1 +1 @@
1
- {"version":3,"file":"broker-credential-provider.d.ts","sourceRoot":"","sources":["../../../src/util/auth/broker-credential-provider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAyB,6BAA6B,EAAE,MAAM,gBAAgB,CAAC;AAE3F,eAAO,MAAM,uBAAuB,qCAAqC,CAAC;AAC1E,eAAO,MAAM,eAAe,6BAA6B,CAAC;AA4D1D;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,EAAE,OAAO,EAAE,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,6BAA6B,CAuBxG"}
1
+ {"version":3,"file":"broker-credential-provider.d.ts","sourceRoot":"","sources":["../../../src/util/auth/broker-credential-provider.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAyB,6BAA6B,EAAE,MAAM,gBAAgB,CAAC;AAE3F,eAAO,MAAM,eAAe,6BAA6B,CAAC;AAwD1D;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,EAAE,OAAO,EAAE,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,6BAA6B,CAuBxG"}
@@ -1,18 +1,15 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
 
3
+ const constants = require('../../constants.js');
3
4
  const util_auth_authStore = require('./auth-store.js');
4
5
 
5
- const DEFAULT_BROKER_BASE_URL = 'https://bigbrain-zth.bigbrain.me';
6
6
  const STS_BROKER_PATH = '/z2h-cli/sts/credentials';
7
7
  // Refresh the cached credentials this many ms before they expire so the next
8
8
  // AWS API call doesn't race the broker round-trip against a hard expiry.
9
9
  const REFRESH_BUFFER_MS = 5 * 60 * 1000;
10
- function brokerBaseUrl() {
11
- return process.env.Z2H_BROKER_BASE_URL || DEFAULT_BROKER_BASE_URL;
12
- }
13
10
  async function fetchCredentialsOnce(appName) {
14
11
  const { token } = await util_auth_authStore.readZthAuth();
15
- const url = `${brokerBaseUrl()}${STS_BROKER_PATH}`;
12
+ const url = `${constants.brokerBaseUrl()}${STS_BROKER_PATH}`;
16
13
  let res;
17
14
  try {
18
15
  res = await fetch(url, {
@@ -75,6 +72,5 @@ function brokerCredentialProvider({ appName }) {
75
72
  };
76
73
  }
77
74
 
78
- exports.DEFAULT_BROKER_BASE_URL = DEFAULT_BROKER_BASE_URL;
79
75
  exports.STS_BROKER_PATH = STS_BROKER_PATH;
80
76
  exports.brokerCredentialProvider = brokerCredentialProvider;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=broker-config.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"broker-config.test.d.ts","sourceRoot":"","sources":["../../../../src/util/npm/__tests__/broker-config.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=install.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"install.test.d.ts","sourceRoot":"","sources":["../../../../src/util/npm/__tests__/install.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The two npm config lines (§3.1 / §3.3) that route the brokered scope to the
3
+ * proxy and supply the `${Z2H_NPM_TOKEN}` placeholder. No literal token — npm
4
+ * interpolates the placeholder from the env at invocation time.
5
+ */
6
+ export declare function brokerNpmrcLines(): string[];
7
+ /**
8
+ * Full contents for a **workspace** `.npmrc` (§3.3): broker scope-split plus an
9
+ * explicit public default registry and always-auth. Used by the template and
10
+ * the migration so both stay byte-identical.
11
+ */
12
+ export declare function workspaceNpmrcContents(): string;
13
+ /**
14
+ * Full contents for a **workspace** `.yarnrc.yml` (§3.2): the brokered scope
15
+ * points at the proxy with the `${Z2H_NPM_TOKEN}` placeholder + always-auth;
16
+ * the default `npmRegistryServer` stays the public registry (direct, unchanged
17
+ * speed). `yarnPath` keeps the bundled Berry release so any `yarn` runs 4.x.
18
+ */
19
+ export declare function workspaceYarnrcContents(): string;
20
+ //# sourceMappingURL=broker-config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"broker-config.d.ts","sourceRoot":"","sources":["../../../src/util/npm/broker-config.ts"],"names":[],"mappings":"AAkBA;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,MAAM,EAAE,CAK3C;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,IAAI,MAAM,CAE/C;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,IAAI,MAAM,CAiBhD"}
@@ -0,0 +1,63 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+
3
+ const constants = require('../../constants.js');
4
+
5
+ /** `https://host/path/` (with trailing slash) for the registry= line. */
6
+ function registryUrlWithSlash() {
7
+ const url = constants.brokerNpmRegistryUrl();
8
+ return url.endsWith('/') ? url : `${url}/`;
9
+ }
10
+ /**
11
+ * The `//host/path/` prefix (scheme stripped, trailing slash kept) that npm
12
+ * requires the `:_authToken` key to be keyed under so it aligns with the
13
+ * registry path. Derived from the same registry URL so staging overrides flow
14
+ * through.
15
+ */
16
+ function authKeyPrefix() {
17
+ return registryUrlWithSlash().replace(/^https?:/, '');
18
+ }
19
+ /**
20
+ * The two npm config lines (§3.1 / §3.3) that route the brokered scope to the
21
+ * proxy and supply the `${Z2H_NPM_TOKEN}` placeholder. No literal token — npm
22
+ * interpolates the placeholder from the env at invocation time.
23
+ */
24
+ function brokerNpmrcLines() {
25
+ return [
26
+ `${constants.BROKER_NPM_SCOPE}:registry=${registryUrlWithSlash()}`,
27
+ `${authKeyPrefix()}:_authToken=\${${constants.Z2H_NPM_TOKEN_ENV}}`,
28
+ ];
29
+ }
30
+ /**
31
+ * Full contents for a **workspace** `.npmrc` (§3.3): broker scope-split plus an
32
+ * explicit public default registry and always-auth. Used by the template and
33
+ * the migration so both stay byte-identical.
34
+ */
35
+ function workspaceNpmrcContents() {
36
+ return [...brokerNpmrcLines(), `registry=${constants.PUBLIC_NPM_REGISTRY_URL}`, 'always-auth=true'].join('\n') + '\n';
37
+ }
38
+ /**
39
+ * Full contents for a **workspace** `.yarnrc.yml` (§3.2): the brokered scope
40
+ * points at the proxy with the `${Z2H_NPM_TOKEN}` placeholder + always-auth;
41
+ * the default `npmRegistryServer` stays the public registry (direct, unchanged
42
+ * speed). `yarnPath` keeps the bundled Berry release so any `yarn` runs 4.x.
43
+ */
44
+ function workspaceYarnrcContents() {
45
+ const scopeKey = constants.BROKER_NPM_SCOPE.replace(/^@/, '');
46
+ return ([
47
+ 'nodeLinker: node-modules',
48
+ '',
49
+ 'npmScopes:',
50
+ ` ${scopeKey}:`,
51
+ ` npmRegistryServer: "${constants.brokerNpmRegistryUrl()}"`,
52
+ ` npmAuthToken: "\${${constants.Z2H_NPM_TOKEN_ENV}}"`,
53
+ ' npmAlwaysAuth: true',
54
+ '',
55
+ `npmRegistryServer: "${constants.PUBLIC_NPM_REGISTRY_URL}"`,
56
+ '',
57
+ 'yarnPath: .yarn/releases/yarn-4.5.1.cjs',
58
+ ].join('\n') + '\n');
59
+ }
60
+
61
+ exports.brokerNpmrcLines = brokerNpmrcLines;
62
+ exports.workspaceNpmrcContents = workspaceNpmrcContents;
63
+ exports.workspaceYarnrcContents = workspaceYarnrcContents;
@@ -0,0 +1,19 @@
1
+ export interface EnsureZ2hTokenOptions {
2
+ /**
3
+ * Re-run the token flow even if a token already exists on disk. Used after a
4
+ * broker 401, where the stored token is present but expired/invalid.
5
+ */
6
+ forceRefresh?: boolean;
7
+ }
8
+ /**
9
+ * Ensure a z2h JWT exists at `~/.zth/auth.json` before an install-bearing
10
+ * command runs. If none is present (or `forceRefresh` is set), drive the
11
+ * existing `generate-z2h-token` flow — loopback capture with manual-paste
12
+ * fallback — which writes the token. No-ops when a token already exists and no
13
+ * refresh was requested.
14
+ *
15
+ * This is the install preflight: it keeps a friendly auth prompt in front of
16
+ * the raw npm error a non-technical user would otherwise hit.
17
+ */
18
+ export declare function ensureZ2hToken(opts?: EnsureZ2hTokenOptions): Promise<void>;
19
+ //# sourceMappingURL=ensure-token.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ensure-token.d.ts","sourceRoot":"","sources":["../../../src/util/npm/ensure-token.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED;;;;;;;;;GASG;AACH,wBAAsB,cAAc,CAAC,IAAI,GAAE,qBAA0B,GAAG,OAAO,CAAC,IAAI,CAAC,CAKpF"}
@@ -0,0 +1,24 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+
3
+ const util_auth_authStore = require('../auth/auth-store.js');
4
+ const util_logger = require('../logger.js');
5
+ const commands_generateZ2hToken = require('../../commands/generate-z2h-token.js');
6
+
7
+ /**
8
+ * Ensure a z2h JWT exists at `~/.zth/auth.json` before an install-bearing
9
+ * command runs. If none is present (or `forceRefresh` is set), drive the
10
+ * existing `generate-z2h-token` flow — loopback capture with manual-paste
11
+ * fallback — which writes the token. No-ops when a token already exists and no
12
+ * refresh was requested.
13
+ *
14
+ * This is the install preflight: it keeps a friendly auth prompt in front of
15
+ * the raw npm error a non-technical user would otherwise hit.
16
+ */
17
+ async function ensureZ2hToken(opts = {}) {
18
+ if (opts.forceRefresh || !util_auth_authStore.tryReadAuthTokenSync()) {
19
+ util_logger.info('you need to sign in to install monday packages — opening the sign-in page');
20
+ await commands_generateZ2hToken.generateZ2HTokenCommand();
21
+ }
22
+ }
23
+
24
+ exports.ensureZ2hToken = ensureZ2hToken;
@@ -0,0 +1,18 @@
1
+ export interface RunYarnInstallOptions {
2
+ /** Directory to run `yarn install` in. */
3
+ cwd: string;
4
+ /** Extra env to merge on top of `process.env` + the broker token. */
5
+ extraEnv?: NodeJS.ProcessEnv;
6
+ }
7
+ /**
8
+ * Run `yarn install` for an install-bearing command:
9
+ * 1. Preflight — ensure a z2h token exists (prompt `generate-z2h-token` if not).
10
+ * 2. Install through the broker.
11
+ * 3. On a broker 401, surface a friendly "your access expired — refreshing it"
12
+ * message, re-auth, and retry the install exactly once.
13
+ *
14
+ * A non-auth failure (real dependency error, network, etc.) propagates as-is so
15
+ * the command's normal error path reports it.
16
+ */
17
+ export declare function runYarnInstall(opts: RunYarnInstallOptions): Promise<void>;
18
+ //# sourceMappingURL=install.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"install.d.ts","sourceRoot":"","sources":["../../../src/util/npm/install.ts"],"names":[],"mappings":"AAiBA,MAAM,WAAW,qBAAqB;IACpC,0CAA0C;IAC1C,GAAG,EAAE,MAAM,CAAC;IACZ,qEAAqE;IACrE,QAAQ,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;CAC9B;AAqCD;;;;;;;;;GASG;AACH,wBAAsB,cAAc,CAAC,IAAI,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAc/E"}
@@ -0,0 +1,76 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+
3
+ const execa = require('execa');
4
+ const util_logger = require('../logger.js');
5
+ const util_auth_authStore = require('../auth/auth-store.js');
6
+ const constants = require('../../constants.js');
7
+ const util_npm_ensureToken = require('./ensure-token.js');
8
+
9
+ // Substrings that, in a failed install's output, indicate the broker rejected
10
+ // the z2h JWT (expired/invalid). npm surfaces `E401`/`code E401`; yarn berry
11
+ // surfaces an HTTP 401 line. We re-auth on any of these rather than dumping a
12
+ // raw npm error on a non-technical user.
13
+ const AUTH_FAILURE_MARKERS = ['401', 'e401', 'unauthorized', 'authentication'];
14
+ function looksLikeAuthFailure(text) {
15
+ const haystack = text.toLowerCase();
16
+ return AUTH_FAILURE_MARKERS.some(marker => haystack.includes(marker));
17
+ }
18
+ /**
19
+ * Spawn `yarn install` in `cwd` with the npm-broker credential injected, while
20
+ * streaming yarn's output live to the user. stdout is inherited; stderr is
21
+ * piped so we can both echo it and inspect it for a broker auth failure.
22
+ *
23
+ * Returns the captured stderr on success; throws an Error tagged
24
+ * `isAuthFailure` when the failure output looks like a broker 401 so the
25
+ * caller can re-auth, or rethrows the raw execa error otherwise.
26
+ */
27
+ async function spawnYarnInstall(opts) {
28
+ const token = util_auth_authStore.tryReadAuthTokenSync();
29
+ const env = { ...process.env, ...(token && { [constants.Z2H_NPM_TOKEN_ENV]: token }), ...opts.extraEnv };
30
+ const child = execa.execa('yarn', ['install'], {
31
+ cwd: opts.cwd,
32
+ stdout: 'inherit',
33
+ stderr: ['inherit', 'pipe'],
34
+ env,
35
+ });
36
+ try {
37
+ await child;
38
+ }
39
+ catch (err) {
40
+ const e = err;
41
+ const combined = `${e.stderr}\n${e.message ?? ''}`;
42
+ if (looksLikeAuthFailure(combined)) {
43
+ const authErr = new Error('broker rejected the z2h token during install');
44
+ authErr.isAuthFailure = true;
45
+ throw authErr;
46
+ }
47
+ throw err;
48
+ }
49
+ }
50
+ /**
51
+ * Run `yarn install` for an install-bearing command:
52
+ * 1. Preflight — ensure a z2h token exists (prompt `generate-z2h-token` if not).
53
+ * 2. Install through the broker.
54
+ * 3. On a broker 401, surface a friendly "your access expired — refreshing it"
55
+ * message, re-auth, and retry the install exactly once.
56
+ *
57
+ * A non-auth failure (real dependency error, network, etc.) propagates as-is so
58
+ * the command's normal error path reports it.
59
+ */
60
+ async function runYarnInstall(opts) {
61
+ await util_npm_ensureToken.ensureZ2hToken();
62
+ try {
63
+ await spawnYarnInstall(opts);
64
+ }
65
+ catch (err) {
66
+ if (!err.isAuthFailure) {
67
+ throw err;
68
+ }
69
+ util_logger.warn('your access expired — refreshing it now…');
70
+ await util_npm_ensureToken.ensureZ2hToken({ forceRefresh: true });
71
+ util_logger.info('access refreshed — retrying install');
72
+ await spawnYarnInstall(opts);
73
+ }
74
+ }
75
+
76
+ exports.runYarnInstall = runYarnInstall;
package/migrations.json CHANGED
@@ -9,6 +9,11 @@
9
9
  "version": "0.4.1",
10
10
  "description": "Remove z2h-cli from workspace and consumer devDependencies (managed globally)",
11
11
  "migrate": "./dist/migrations/002-remove-z2h-cli-from-workspace-deps"
12
+ },
13
+ "npm-broker-scope-split": {
14
+ "version": "0.6.0",
15
+ "description": "Rewrite workspace .yarnrc.yml/.npmrc from ${NPM_TOKEN} to the @mondaydotcomorg broker scope-split",
16
+ "migrate": "./dist/migrations/003-npm-broker-scope-split"
12
17
  }
13
18
  }
14
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mondaydotcomorg/z2h-cli",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "bin": "./bin/z2h-cli.js",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -3,12 +3,14 @@ import os from 'node:os';
3
3
  import { mkdtemp, rm, writeJson, readJson, ensureDir, pathExists } from 'fs-extra';
4
4
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
5
5
 
6
- import { execa } from 'execa';
6
+ import { runYarnInstall } from '../../util/npm/install';
7
7
  import { createWorkspaceCommand } from '../create-workspace';
8
8
 
9
- // Mock execa so `yarn install` doesn't actually run.
10
- vi.mock('execa', () => ({
11
- execa: vi.fn().mockResolvedValue({ stdout: '', stderr: '', exitCode: 0 }),
9
+ // Mock the install helper so `yarn install` (with its broker preflight) doesn't
10
+ // actually run. The token-injection + 401 re-auth behavior is covered in
11
+ // util/npm/__tests__/install.test.ts.
12
+ vi.mock('../../util/npm/install', () => ({
13
+ runYarnInstall: vi.fn().mockResolvedValue(undefined),
12
14
  }));
13
15
 
14
16
  describe('create-workspace', () => {
@@ -17,7 +19,7 @@ describe('create-workspace', () => {
17
19
  beforeEach(async () => {
18
20
  tmp = await mkdtemp(path.join(os.tmpdir(), 'z2h-cw-'));
19
21
  process.env.Z2H_WORKSPACE_HOME = tmp;
20
- vi.mocked(execa).mockClear();
22
+ vi.mocked(runYarnInstall).mockClear();
21
23
  // After mkdtemp the dir exists and is empty; we want absent for the
22
24
  // "happy path" tests, so remove it.
23
25
  await rm(tmp, { recursive: true, force: true });
@@ -46,11 +48,7 @@ describe('create-workspace', () => {
46
48
  expect(await pathExists(path.join(tmp, '.npmrc'))).toBe(true);
47
49
  expect(await pathExists(path.join(tmp, 'README.md'))).toBe(true);
48
50
 
49
- expect(execa).toHaveBeenCalledWith(
50
- 'yarn',
51
- expect.arrayContaining(['install']),
52
- expect.objectContaining({ cwd: tmp })
53
- );
51
+ expect(runYarnInstall).toHaveBeenCalledWith(expect.objectContaining({ cwd: tmp }));
54
52
  });
55
53
 
56
54
  it('refuses if the target dir is non-empty', async () => {
@@ -58,7 +56,7 @@ describe('create-workspace', () => {
58
56
  await writeJson(path.join(tmp, 'somefile.json'), { hello: 'world' });
59
57
 
60
58
  await expect(createWorkspaceCommand({})).rejects.toThrow(/Workspace already exists/);
61
- expect(execa).not.toHaveBeenCalled();
59
+ expect(runYarnInstall).not.toHaveBeenCalled();
62
60
  });
63
61
 
64
62
  it('does not include z2h-cli in workspace devDeps (managed globally)', async () => {