@mondaydotcomorg/z2h-cli 0.6.0 → 0.6.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/dist/commands/create-workspace.d.ts.map +1 -1
- package/dist/commands/create-workspace.js +2 -5
- package/dist/commands/create.js +2 -2
- package/dist/constants.d.ts +18 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +41 -0
- package/dist/esm/commands/create-workspace.d.ts.map +1 -1
- package/dist/esm/commands/create-workspace.mjs +2 -5
- package/dist/esm/commands/create.mjs +2 -2
- package/dist/esm/constants.d.ts +18 -0
- package/dist/esm/constants.d.ts.map +1 -1
- package/dist/esm/constants.mjs +35 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/migrations/003-npm-broker-scope-split.d.ts +14 -0
- package/dist/esm/migrations/003-npm-broker-scope-split.d.ts.map +1 -0
- package/dist/esm/migrations/003-npm-broker-scope-split.mjs +49 -0
- package/dist/esm/migrations/__tests__/003-npm-broker-scope-split.test.d.ts +2 -0
- package/dist/esm/migrations/__tests__/003-npm-broker-scope-split.test.d.ts.map +1 -0
- package/dist/esm/migrations/auto-migrate.d.ts.map +1 -1
- package/dist/esm/migrations/auto-migrate.mjs +4 -1
- package/dist/esm/shadow/sync.d.ts.map +1 -1
- package/dist/esm/shadow/sync.mjs +4 -6
- package/dist/esm/util/auth/auth-store.d.ts +10 -0
- package/dist/esm/util/auth/auth-store.d.ts.map +1 -1
- package/dist/esm/util/auth/auth-store.mjs +20 -2
- package/dist/esm/util/auth/broker-credential-provider.d.ts +0 -1
- package/dist/esm/util/auth/broker-credential-provider.d.ts.map +1 -1
- package/dist/esm/util/auth/broker-credential-provider.mjs +2 -5
- package/dist/esm/util/npm/__tests__/broker-config.test.d.ts +2 -0
- package/dist/esm/util/npm/__tests__/broker-config.test.d.ts.map +1 -0
- package/dist/esm/util/npm/__tests__/install.test.d.ts +2 -0
- package/dist/esm/util/npm/__tests__/install.test.d.ts.map +1 -0
- package/dist/esm/util/npm/broker-config.d.ts +20 -0
- package/dist/esm/util/npm/broker-config.d.ts.map +1 -0
- package/dist/esm/util/npm/broker-config.mjs +59 -0
- package/dist/esm/util/npm/ensure-token.d.ts +19 -0
- package/dist/esm/util/npm/ensure-token.d.ts.map +1 -0
- package/dist/esm/util/npm/ensure-token.mjs +22 -0
- package/dist/esm/util/npm/install.d.ts +18 -0
- package/dist/esm/util/npm/install.d.ts.map +1 -0
- package/dist/esm/util/npm/install.mjs +74 -0
- package/dist/index.js +1 -1
- package/dist/migrations/003-npm-broker-scope-split.d.ts +14 -0
- package/dist/migrations/003-npm-broker-scope-split.d.ts.map +1 -0
- package/dist/migrations/003-npm-broker-scope-split.js +51 -0
- package/dist/migrations/__tests__/003-npm-broker-scope-split.test.d.ts +2 -0
- package/dist/migrations/__tests__/003-npm-broker-scope-split.test.d.ts.map +1 -0
- package/dist/migrations/auto-migrate.d.ts.map +1 -1
- package/dist/migrations/auto-migrate.js +4 -1
- package/dist/shadow/sync.d.ts.map +1 -1
- package/dist/shadow/sync.js +4 -6
- package/dist/util/auth/auth-store.d.ts +10 -0
- package/dist/util/auth/auth-store.d.ts.map +1 -1
- package/dist/util/auth/auth-store.js +19 -0
- package/dist/util/auth/broker-credential-provider.d.ts +0 -1
- package/dist/util/auth/broker-credential-provider.d.ts.map +1 -1
- package/dist/util/auth/broker-credential-provider.js +2 -6
- package/dist/util/npm/__tests__/broker-config.test.d.ts +2 -0
- package/dist/util/npm/__tests__/broker-config.test.d.ts.map +1 -0
- package/dist/util/npm/__tests__/install.test.d.ts +2 -0
- package/dist/util/npm/__tests__/install.test.d.ts.map +1 -0
- package/dist/util/npm/broker-config.d.ts +20 -0
- package/dist/util/npm/broker-config.d.ts.map +1 -0
- package/dist/util/npm/broker-config.js +63 -0
- package/dist/util/npm/ensure-token.d.ts +19 -0
- package/dist/util/npm/ensure-token.d.ts.map +1 -0
- package/dist/util/npm/ensure-token.js +24 -0
- package/dist/util/npm/install.d.ts +18 -0
- package/dist/util/npm/install.d.ts.map +1 -0
- package/dist/util/npm/install.js +76 -0
- package/migrations.json +5 -0
- package/package.json +1 -1
- package/src/commands/__tests__/create-workspace.test.ts +9 -11
- package/src/commands/__tests__/create.test.ts +9 -6
- package/src/commands/create-workspace.ts +2 -5
- package/src/commands/create.ts +2 -2
- package/src/constants.ts +41 -0
- package/src/index.ts +1 -1
- package/src/migrations/003-npm-broker-scope-split.ts +52 -0
- package/src/migrations/__tests__/003-npm-broker-scope-split.test.ts +99 -0
- package/src/migrations/auto-migrate.ts +4 -1
- package/src/shadow/sync.ts +4 -6
- package/src/templates/workspace/npmrc +3 -2
- package/src/templates/workspace/yarnrc.yml +6 -2
- package/src/util/__tests__/workspace-templates-shipped.test.ts +19 -0
- package/src/util/auth/__tests__/broker-credential-provider.test.ts +2 -1
- package/src/util/auth/auth-store.ts +19 -1
- package/src/util/auth/broker-credential-provider.ts +1 -5
- package/src/util/npm/__tests__/broker-config.test.ts +48 -0
- package/src/util/npm/__tests__/install.test.ts +94 -0
- package/src/util/npm/broker-config.ts +63 -0
- package/src/util/npm/ensure-token.ts +28 -0
- package/src/util/npm/install.ts +84 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { BROKER_NPM_SCOPE, PUBLIC_NPM_REGISTRY_URL, Z2H_NPM_TOKEN_ENV, brokerNpmRegistryUrl } from '../../constants';
|
|
2
|
+
|
|
3
|
+
/** `https://host/path/` (with trailing slash) for the registry= line. */
|
|
4
|
+
function registryUrlWithSlash(): string {
|
|
5
|
+
const url = brokerNpmRegistryUrl();
|
|
6
|
+
return url.endsWith('/') ? url : `${url}/`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The `//host/path/` prefix (scheme stripped, trailing slash kept) that npm
|
|
11
|
+
* requires the `:_authToken` key to be keyed under so it aligns with the
|
|
12
|
+
* registry path. Derived from the same registry URL so staging overrides flow
|
|
13
|
+
* through.
|
|
14
|
+
*/
|
|
15
|
+
function authKeyPrefix(): string {
|
|
16
|
+
return registryUrlWithSlash().replace(/^https?:/, '');
|
|
17
|
+
}
|
|
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
|
+
export function brokerNpmrcLines(): string[] {
|
|
25
|
+
return [
|
|
26
|
+
`${BROKER_NPM_SCOPE}:registry=${registryUrlWithSlash()}`,
|
|
27
|
+
`${authKeyPrefix()}:_authToken=\${${Z2H_NPM_TOKEN_ENV}}`,
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Full contents for a **workspace** `.npmrc` (§3.3): broker scope-split plus an
|
|
33
|
+
* explicit public default registry and always-auth. Used by the template and
|
|
34
|
+
* the migration so both stay byte-identical.
|
|
35
|
+
*/
|
|
36
|
+
export function workspaceNpmrcContents(): string {
|
|
37
|
+
return [...brokerNpmrcLines(), `registry=${PUBLIC_NPM_REGISTRY_URL}`, 'always-auth=true'].join('\n') + '\n';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Full contents for a **workspace** `.yarnrc.yml` (§3.2): the brokered scope
|
|
42
|
+
* points at the proxy with the `${Z2H_NPM_TOKEN}` placeholder + always-auth;
|
|
43
|
+
* the default `npmRegistryServer` stays the public registry (direct, unchanged
|
|
44
|
+
* speed). `yarnPath` keeps the bundled Berry release so any `yarn` runs 4.x.
|
|
45
|
+
*/
|
|
46
|
+
export function workspaceYarnrcContents(): string {
|
|
47
|
+
const scopeKey = BROKER_NPM_SCOPE.replace(/^@/, '');
|
|
48
|
+
return (
|
|
49
|
+
[
|
|
50
|
+
'nodeLinker: node-modules',
|
|
51
|
+
'',
|
|
52
|
+
'npmScopes:',
|
|
53
|
+
` ${scopeKey}:`,
|
|
54
|
+
` npmRegistryServer: "${brokerNpmRegistryUrl()}"`,
|
|
55
|
+
` npmAuthToken: "\${${Z2H_NPM_TOKEN_ENV}}"`,
|
|
56
|
+
' npmAlwaysAuth: true',
|
|
57
|
+
'',
|
|
58
|
+
`npmRegistryServer: "${PUBLIC_NPM_REGISTRY_URL}"`,
|
|
59
|
+
'',
|
|
60
|
+
'yarnPath: .yarn/releases/yarn-4.5.1.cjs',
|
|
61
|
+
].join('\n') + '\n'
|
|
62
|
+
);
|
|
63
|
+
}
|
|
@@ -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
|
+
}
|