@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,28 @@
1
+ import { tryReadAuthTokenSync } from '../auth/auth-store';
2
+ import { info } from '../logger';
3
+ import { generateZ2HTokenCommand } from '../../commands/generate-z2h-token';
4
+
5
+ export interface EnsureZ2hTokenOptions {
6
+ /**
7
+ * Re-run the token flow even if a token already exists on disk. Used after a
8
+ * broker 401, where the stored token is present but expired/invalid.
9
+ */
10
+ forceRefresh?: boolean;
11
+ }
12
+
13
+ /**
14
+ * Ensure a z2h JWT exists at `~/.zth/auth.json` before an install-bearing
15
+ * command runs. If none is present (or `forceRefresh` is set), drive the
16
+ * existing `generate-z2h-token` flow — loopback capture with manual-paste
17
+ * fallback — which writes the token. No-ops when a token already exists and no
18
+ * refresh was requested.
19
+ *
20
+ * This is the install preflight: it keeps a friendly auth prompt in front of
21
+ * the raw npm error a non-technical user would otherwise hit.
22
+ */
23
+ export async function ensureZ2hToken(opts: EnsureZ2hTokenOptions = {}): Promise<void> {
24
+ if (opts.forceRefresh || !tryReadAuthTokenSync()) {
25
+ info('you need to sign in to install monday packages — opening the sign-in page');
26
+ await generateZ2HTokenCommand();
27
+ }
28
+ }
@@ -0,0 +1,84 @@
1
+ import { execa, type Options as ExecaOptions } from 'execa';
2
+ import { info, warn } from '../logger';
3
+ import { tryReadAuthTokenSync } from '../auth/auth-store';
4
+ import { Z2H_NPM_TOKEN_ENV } from '../../constants';
5
+ import { ensureZ2hToken } from './ensure-token';
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
+
13
+ function looksLikeAuthFailure(text: string): boolean {
14
+ const haystack = text.toLowerCase();
15
+ return AUTH_FAILURE_MARKERS.some(marker => haystack.includes(marker));
16
+ }
17
+
18
+ export interface RunYarnInstallOptions {
19
+ /** Directory to run `yarn install` in. */
20
+ cwd: string;
21
+ /** Extra env to merge on top of `process.env` + the broker token. */
22
+ extraEnv?: NodeJS.ProcessEnv;
23
+ }
24
+
25
+ /**
26
+ * Spawn `yarn install` in `cwd` with the npm-broker credential injected, while
27
+ * streaming yarn's output live to the user. stdout is inherited; stderr is
28
+ * piped so we can both echo it and inspect it for a broker auth failure.
29
+ *
30
+ * Returns the captured stderr on success; throws an Error tagged
31
+ * `isAuthFailure` when the failure output looks like a broker 401 so the
32
+ * caller can re-auth, or rethrows the raw execa error otherwise.
33
+ */
34
+ async function spawnYarnInstall(opts: RunYarnInstallOptions): Promise<void> {
35
+ const token = tryReadAuthTokenSync();
36
+ const env = { ...process.env, ...(token && { [Z2H_NPM_TOKEN_ENV]: token }), ...opts.extraEnv };
37
+ const child = execa('yarn', ['install'], {
38
+ cwd: opts.cwd,
39
+ stdout: 'inherit',
40
+ stderr: ['inherit', 'pipe'],
41
+ env,
42
+ } satisfies ExecaOptions);
43
+
44
+ try {
45
+ await child;
46
+ } catch (err) {
47
+ const e = err as Error & { stderr?: string };
48
+ const combined = `${e.stderr}\n${e.message ?? ''}`;
49
+ if (looksLikeAuthFailure(combined)) {
50
+ const authErr = new Error('broker rejected the z2h token during install') as Error & {
51
+ isAuthFailure: true;
52
+ };
53
+ authErr.isAuthFailure = true;
54
+ throw authErr;
55
+ }
56
+ throw err;
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Run `yarn install` for an install-bearing command:
62
+ * 1. Preflight — ensure a z2h token exists (prompt `generate-z2h-token` if not).
63
+ * 2. Install through the broker.
64
+ * 3. On a broker 401, surface a friendly "your access expired — refreshing it"
65
+ * message, re-auth, and retry the install exactly once.
66
+ *
67
+ * A non-auth failure (real dependency error, network, etc.) propagates as-is so
68
+ * the command's normal error path reports it.
69
+ */
70
+ export async function runYarnInstall(opts: RunYarnInstallOptions): Promise<void> {
71
+ await ensureZ2hToken();
72
+
73
+ try {
74
+ await spawnYarnInstall(opts);
75
+ } catch (err) {
76
+ if (!(err as { isAuthFailure?: boolean }).isAuthFailure) {
77
+ throw err;
78
+ }
79
+ warn('your access expired — refreshing it now…');
80
+ await ensureZ2hToken({ forceRefresh: true });
81
+ info('access refreshed — retrying install');
82
+ await spawnYarnInstall(opts);
83
+ }
84
+ }