@mondaydotcomorg/z2h-cli 0.10.1 → 0.11.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.
- package/dist/commands/__tests__/edit.test.d.ts +2 -0
- package/dist/commands/__tests__/edit.test.d.ts.map +1 -0
- package/dist/commands/create.d.ts +2 -2
- package/dist/commands/create.d.ts.map +1 -1
- package/dist/commands/create.js +14 -34
- package/dist/commands/deploy.d.ts +3 -0
- package/dist/commands/deploy.d.ts.map +1 -1
- package/dist/commands/deploy.js +120 -21
- package/dist/commands/edit.d.ts +5 -0
- package/dist/commands/edit.d.ts.map +1 -0
- package/dist/commands/edit.js +61 -0
- package/dist/commands/grant.d.ts +12 -0
- package/dist/commands/grant.d.ts.map +1 -0
- package/dist/commands/grant.js +94 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +7 -0
- package/dist/esm/commands/__tests__/edit.test.d.ts +2 -0
- package/dist/esm/commands/__tests__/edit.test.d.ts.map +1 -0
- package/dist/esm/commands/create.d.ts +2 -2
- package/dist/esm/commands/create.d.ts.map +1 -1
- package/dist/esm/commands/create.mjs +16 -35
- package/dist/esm/commands/deploy.d.ts +3 -0
- package/dist/esm/commands/deploy.d.ts.map +1 -1
- package/dist/esm/commands/deploy.mjs +122 -23
- package/dist/esm/commands/edit.d.ts +5 -0
- package/dist/esm/commands/edit.d.ts.map +1 -0
- package/dist/esm/commands/edit.mjs +55 -0
- package/dist/esm/commands/grant.d.ts +12 -0
- package/dist/esm/commands/grant.d.ts.map +1 -0
- package/dist/esm/commands/grant.mjs +90 -0
- package/dist/esm/constants.d.ts +1 -0
- package/dist/esm/constants.d.ts.map +1 -1
- package/dist/esm/constants.mjs +7 -1
- package/dist/esm/index.mjs +67 -10
- package/dist/esm/shadow/prepare.d.ts +1 -1
- package/dist/esm/shadow/prepare.d.ts.map +1 -1
- package/dist/esm/shadow/prepare.mjs +2 -2
- package/dist/esm/util/app-name.d.ts +1 -0
- package/dist/esm/util/app-name.d.ts.map +1 -1
- package/dist/esm/util/app-name.mjs +11 -1
- package/dist/esm/util/auth/broker-credential-provider.d.ts +10 -2
- package/dist/esm/util/auth/broker-credential-provider.d.ts.map +1 -1
- package/dist/esm/util/auth/broker-credential-provider.mjs +35 -13
- package/dist/esm/util/broker/app.d.ts +14 -0
- package/dist/esm/util/broker/app.d.ts.map +1 -0
- package/dist/esm/util/broker/app.mjs +27 -0
- package/dist/esm/util/git/git-env.d.ts +9 -0
- package/dist/esm/util/git/git-env.d.ts.map +1 -0
- package/dist/esm/util/git/git-env.mjs +22 -0
- package/dist/esm/util/git/remote-options.d.ts +17 -0
- package/dist/esm/util/git/remote-options.d.ts.map +1 -0
- package/dist/esm/util/git/remote-options.mjs +30 -0
- package/dist/esm/util/git/repo.d.ts +21 -3
- package/dist/esm/util/git/repo.d.ts.map +1 -1
- package/dist/esm/util/git/repo.mjs +96 -48
- package/dist/esm/util/s3/client.d.ts +2 -7
- package/dist/esm/util/s3/client.d.ts.map +1 -1
- package/dist/esm/util/s3/client.mjs +3 -11
- package/dist/index.js +69 -12
- package/dist/shadow/prepare.d.ts +1 -1
- package/dist/shadow/prepare.d.ts.map +1 -1
- package/dist/shadow/prepare.js +2 -2
- package/dist/util/app-name.d.ts +1 -0
- package/dist/util/app-name.d.ts.map +1 -1
- package/dist/util/app-name.js +11 -0
- package/dist/util/auth/broker-credential-provider.d.ts +10 -2
- package/dist/util/auth/broker-credential-provider.d.ts.map +1 -1
- package/dist/util/auth/broker-credential-provider.js +35 -11
- package/dist/util/broker/app.d.ts +14 -0
- package/dist/util/broker/app.d.ts.map +1 -0
- package/dist/util/broker/app.js +29 -0
- package/dist/util/git/git-env.d.ts +9 -0
- package/dist/util/git/git-env.d.ts.map +1 -0
- package/dist/util/git/git-env.js +24 -0
- package/dist/util/git/remote-options.d.ts +17 -0
- package/dist/util/git/remote-options.d.ts.map +1 -0
- package/dist/util/git/remote-options.js +34 -0
- package/dist/util/git/repo.d.ts +21 -3
- package/dist/util/git/repo.d.ts.map +1 -1
- package/dist/util/git/repo.js +97 -48
- package/dist/util/s3/client.d.ts +2 -7
- package/dist/util/s3/client.d.ts.map +1 -1
- package/dist/util/s3/client.js +3 -11
- package/package.json +1 -1
- package/src/commands/__tests__/create.test.ts +9 -7
- package/src/commands/__tests__/edit.test.ts +184 -0
- package/src/commands/create.ts +15 -33
- package/src/commands/deploy.ts +145 -31
- package/src/commands/edit.ts +63 -0
- package/src/commands/grant.ts +115 -0
- package/src/constants.ts +7 -0
- package/src/index.ts +83 -13
- package/src/shadow/prepare.ts +2 -2
- package/src/templates/app/CLAUDE.md +15 -0
- package/src/util/app-name.ts +10 -0
- package/src/util/auth/__tests__/broker-credential-provider.test.ts +34 -2
- package/src/util/auth/broker-credential-provider.ts +43 -15
- package/src/util/broker/app.ts +34 -0
- package/src/util/git/__tests__/repo.test.ts +114 -90
- package/src/util/git/git-env.ts +23 -0
- package/src/util/git/remote-options.ts +37 -0
- package/src/util/git/repo.ts +109 -48
- package/src/util/s3/client.ts +4 -11
package/src/commands/create.ts
CHANGED
|
@@ -4,15 +4,15 @@ import { ensureDir, pathExists, readdir, writeFile, copy } from 'fs-extra';
|
|
|
4
4
|
import { info, warn, output, setOutputMode } from '../util/logger';
|
|
5
5
|
import { assertWorkspace } from '../util/workspace';
|
|
6
6
|
import { getTemplatesDir, renderTemplate, APP_TEMPLATES, type AppTemplate } from '../util/templates';
|
|
7
|
-
import { readManifest } from '../util/manifest';
|
|
8
|
-
import { mfAppName } from '../util/app-name';
|
|
9
7
|
import { APP_NAME_PATTERN } from '../constants';
|
|
10
8
|
import { runYarnInstall } from '../util/npm/install';
|
|
11
|
-
import { initRepo
|
|
12
|
-
import {
|
|
9
|
+
import { initRepo } from '../util/git/repo';
|
|
10
|
+
import { REMOTE_TYPES, type RemoteType, validateRemoteOptions, resolveRemoteUrl } from '../util/git/remote-options';
|
|
11
|
+
import { fetchApp } from '../util/broker/app';
|
|
12
|
+
import { mfAppName } from '../util/app-name';
|
|
13
|
+
import { prepareShadow } from '../shadow/prepare';
|
|
13
14
|
|
|
14
|
-
export
|
|
15
|
-
export type RemoteType = (typeof REMOTE_TYPES)[number];
|
|
15
|
+
export { REMOTE_TYPES, type RemoteType };
|
|
16
16
|
|
|
17
17
|
export interface CreateOptions {
|
|
18
18
|
template?: string;
|
|
@@ -71,15 +71,7 @@ export async function createCommand(appName: string, opts: CreateOptions = {}):
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
// Validate --remote up front before any file I/O.
|
|
74
|
-
|
|
75
|
-
throw new Error('--remote is required. Use --remote=s3 (shared storage) or --remote=github (your GitHub repo).');
|
|
76
|
-
}
|
|
77
|
-
if (!(REMOTE_TYPES as readonly string[]).includes(opts.remote)) {
|
|
78
|
-
throw new Error(`Unknown remote "${opts.remote}". Use ${REMOTE_TYPES.join(' or ')}.`);
|
|
79
|
-
}
|
|
80
|
-
if (opts.remote === 'github' && !opts.repoUrl) {
|
|
81
|
-
throw new Error('--repo-url is required when using --remote=github.');
|
|
82
|
-
}
|
|
74
|
+
validateRemoteOptions(opts, true);
|
|
83
75
|
|
|
84
76
|
// Resolve template.
|
|
85
77
|
// `--html-file` implies `--template html-embed` unless the user already picked one.
|
|
@@ -98,9 +90,9 @@ export async function createCommand(appName: string, opts: CreateOptions = {}):
|
|
|
98
90
|
throw new Error(`${appDir} is non-empty. Pick a different name or wipe it first.`);
|
|
99
91
|
}
|
|
100
92
|
|
|
101
|
-
// Check
|
|
102
|
-
const
|
|
103
|
-
if (
|
|
93
|
+
// Check the broker — fail fast if the app is already registered.
|
|
94
|
+
const existing = await fetchApp(mfAppName(appName));
|
|
95
|
+
if (existing) {
|
|
104
96
|
throw new Error(`An app named "${appName}" already exists in the Z2H registry. Pick a different name.`);
|
|
105
97
|
}
|
|
106
98
|
|
|
@@ -134,27 +126,17 @@ export async function createCommand(appName: string, opts: CreateOptions = {}):
|
|
|
134
126
|
}
|
|
135
127
|
|
|
136
128
|
info(`initializing git repo for ${appName}...`);
|
|
137
|
-
|
|
138
|
-
if (opts.remote === 's3') {
|
|
139
|
-
gitRemote = defaultS3Remote(appName);
|
|
140
|
-
} else {
|
|
141
|
-
gitRemote = opts.repoUrl as string;
|
|
142
|
-
}
|
|
129
|
+
const gitRemote = resolveRemoteUrl(opts.remote as RemoteType, appName, opts.repoUrl);
|
|
143
130
|
await initRepo(appDir, gitRemote);
|
|
144
131
|
|
|
145
132
|
info(`installing dependencies for ${appName}...`);
|
|
146
133
|
await runYarnInstall({ cwd: workspace });
|
|
147
134
|
|
|
148
|
-
//
|
|
149
|
-
const prevCwd = process.cwd();
|
|
135
|
+
// Best-effort workspace wiring (.zth shadow). Non-fatal — dev/deploy re-run it.
|
|
150
136
|
try {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
} catch {
|
|
155
|
-
warn(`pre-build failed. No worries — it will run again when needed.`);
|
|
156
|
-
} finally {
|
|
157
|
-
process.chdir(prevCwd);
|
|
137
|
+
await prepareShadow(appDir);
|
|
138
|
+
} catch (err) {
|
|
139
|
+
warn(`prepare shadow failed: ${(err as Error).message} — it will run again when needed.`);
|
|
158
140
|
}
|
|
159
141
|
|
|
160
142
|
output(`[z2h-cli] app ready at ${appDir}`, {
|
package/src/commands/deploy.ts
CHANGED
|
@@ -1,28 +1,27 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import os from 'node:os';
|
|
3
|
+
import { createInterface } from 'node:readline';
|
|
3
4
|
import { pathExists, readJson } from 'fs-extra';
|
|
4
5
|
import { info, output, setOutputMode, warn } from '../util/logger';
|
|
5
6
|
import { resolveConsumerPaths } from '../shadow/paths';
|
|
6
|
-
import {
|
|
7
|
-
nextVersion,
|
|
8
|
-
readManifest,
|
|
9
|
-
upsertEntry,
|
|
10
|
-
writeManifest,
|
|
11
|
-
type AssetManifestFile,
|
|
12
|
-
type ManifestEntry,
|
|
13
|
-
} from '../util/manifest';
|
|
7
|
+
import { type AssetManifestFile, type ManifestEntry } from '../util/manifest';
|
|
14
8
|
import { uploadDir } from '../util/s3/upload';
|
|
15
|
-
import { BUCKET_WRITE_NAME, BUCKET_READ_DNS_URL, brokerBaseUrl } from '../constants';
|
|
16
|
-
import {
|
|
9
|
+
import { BUCKET_WRITE_NAME, BUCKET_READ_DNS_URL, brokerBaseUrl, routingHeaders } from '../constants';
|
|
10
|
+
import { brokerCredentialProvider, fetchDeployVersion } from '../util/auth/broker-credential-provider';
|
|
17
11
|
import { readZthAuth } from '../util/auth/auth-store';
|
|
18
12
|
import { cleanAppName, mfAppName } from '../util/app-name';
|
|
19
13
|
import { getMachineAppName } from '../util/machine-identity';
|
|
20
|
-
import { getRemoteUrl } from '../util/git/repo';
|
|
14
|
+
import { getRemoteUrl, guardCleanTree, pullAndResolve, pushToRemote, initRepo } from '../util/git/repo';
|
|
15
|
+
import { resolveGitEnv } from '../util/git/git-env';
|
|
16
|
+
import { type RemoteType, validateRemoteOptions, resolveRemoteUrl } from '../util/git/remote-options';
|
|
17
|
+
import { setupS3Client } from '../util/s3/client';
|
|
21
18
|
import { buildCommand } from './build';
|
|
22
19
|
|
|
23
20
|
export interface DeployOptions {
|
|
24
21
|
dryRun?: boolean;
|
|
25
22
|
json?: boolean;
|
|
23
|
+
remote?: RemoteType;
|
|
24
|
+
repoUrl?: string;
|
|
26
25
|
}
|
|
27
26
|
|
|
28
27
|
// Should be run from the consumer app directory — uses process.cwd() to locate it.
|
|
@@ -41,12 +40,51 @@ export async function deployCommand(opts: DeployOptions = {}): Promise<void> {
|
|
|
41
40
|
// Validate the user's deploy token is on disk before doing any expensive
|
|
42
41
|
// work. Throws Z2hAuthMissingError with a remediation hint if not.
|
|
43
42
|
const { token } = await readZthAuth();
|
|
44
|
-
|
|
43
|
+
|
|
44
|
+
let gitRemote = await getRemoteUrl(process.cwd());
|
|
45
|
+
|
|
46
|
+
if (gitRemote) {
|
|
47
|
+
if (opts.remote || opts.repoUrl) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`--remote and --repo-url only apply on the first deploy of an app with no git remote.\n` +
|
|
50
|
+
`This app already tracks source at: ${gitRemote}`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
if (!opts.remote) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`This app has no git remote. On first deploy, tell Z2H how to track source:\n\n` +
|
|
57
|
+
` z2h-cli deploy --remote s3 (default)\n` +
|
|
58
|
+
` Stores source history in S3 alongside your app. No extra setup.\n\n` +
|
|
59
|
+
` z2h-cli deploy --remote github --repo-url https://github.com/org/repo.git\n` +
|
|
60
|
+
` Uses an existing GitHub repo (must exist before deploying).`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
validateRemoteOptions(opts);
|
|
64
|
+
// Pass the raw consumer name (not the mf-prefixed appName) so defaultS3Remote
|
|
65
|
+
// produces the correct s3://bucket/mf-<name>/git path without double prefix.
|
|
66
|
+
const remote = resolveRemoteUrl(opts.remote as RemoteType, consumerPkg.name, opts.repoUrl);
|
|
67
|
+
info(`initializing git repo with ${opts.remote} remote...`);
|
|
68
|
+
await initRepo(process.cwd(), remote);
|
|
69
|
+
gitRemote = remote;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const version = await fetchDeployVersion(appName);
|
|
73
|
+
const nextVersion = version + 1;
|
|
74
|
+
|
|
75
|
+
const gitEnv = await resolveGitEnv(appName, gitRemote);
|
|
76
|
+
|
|
77
|
+
// Sync git first so a divergence (GitConflictError) stops us.
|
|
78
|
+
// The skill commits the working tree (with an LLM-authored message) before
|
|
79
|
+
// invoking deploy, so the tree should be clean here.
|
|
80
|
+
if (gitRemote) {
|
|
81
|
+
await guardCleanTree(process.cwd());
|
|
82
|
+
info('syncing with git remote');
|
|
83
|
+
await pullAndResolve(process.cwd(), gitEnv);
|
|
84
|
+
}
|
|
45
85
|
|
|
46
86
|
info('reading current Z2H manifest');
|
|
47
|
-
const
|
|
48
|
-
const version = nextVersion(manifest, appName);
|
|
49
|
-
const keyPrefix = `${appName}/${version}`;
|
|
87
|
+
const keyPrefix = `${appName}/${nextVersion}`;
|
|
50
88
|
const url = `${BUCKET_READ_DNS_URL}/${keyPrefix}`;
|
|
51
89
|
|
|
52
90
|
info('building');
|
|
@@ -64,10 +102,9 @@ export async function deployCommand(opts: DeployOptions = {}): Promise<void> {
|
|
|
64
102
|
const assetManifest = (await readJson(assetManifestPath)) as AssetManifestFile;
|
|
65
103
|
|
|
66
104
|
const deployedBy = getMachineAppName() ?? os.userInfo().username;
|
|
67
|
-
const gitRemote = await getRemoteUrl(process.cwd());
|
|
68
105
|
const entry: ManifestEntry = {
|
|
69
106
|
...assetManifest,
|
|
70
|
-
version,
|
|
107
|
+
version: nextVersion,
|
|
71
108
|
url,
|
|
72
109
|
deployedBy,
|
|
73
110
|
gitRemote,
|
|
@@ -87,16 +124,36 @@ export async function deployCommand(opts: DeployOptions = {}): Promise<void> {
|
|
|
87
124
|
return;
|
|
88
125
|
}
|
|
89
126
|
|
|
127
|
+
// lock → git push → bundle upload → manifest PUT (the manifest write is the single commit point).
|
|
90
128
|
try {
|
|
91
|
-
//
|
|
129
|
+
// Acquire the deploy lock explicitly. Done after the build
|
|
130
|
+
// so the lock hold stays tiny — it wraps only push → upload → manifest.
|
|
131
|
+
// A 409 here means someone else is mid-deploy;
|
|
132
|
+
const creds = await brokerCredentialProvider({ appName })();
|
|
133
|
+
setupS3Client(creds);
|
|
134
|
+
await acquireDeployLock(appName, token);
|
|
135
|
+
|
|
136
|
+
// Push before any S3 write so a rejected push (someone deployed during our
|
|
137
|
+
// build) stops the deploy before the bundle/manifest go live
|
|
138
|
+
if (gitRemote) {
|
|
139
|
+
info('pushing to git remote');
|
|
140
|
+
await pushToRemote(process.cwd(), gitEnv);
|
|
141
|
+
}
|
|
142
|
+
|
|
92
143
|
info(`uploading build → s3://${BUCKET_WRITE_NAME}/${keyPrefix}/`);
|
|
93
144
|
await uploadDir(paths.shadowBuild, keyPrefix);
|
|
94
145
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
146
|
+
// v1 creates the app row (+ owner grant); later versions release onto it.
|
|
147
|
+
// The two are separate endpoints, so the CLI picks based on the version it
|
|
148
|
+
// was handed.
|
|
149
|
+
if (nextVersion === 1) {
|
|
150
|
+
const visibility = await resolveFirstDeployVisibility(appName, opts.json);
|
|
151
|
+
info(`registering app "${appName}" → v${nextVersion} (${visibility})`);
|
|
152
|
+
await registerApp(appName, token, nextVersion, entry, visibility, gitRemote);
|
|
153
|
+
} else {
|
|
154
|
+
info(`releasing "${appName}" → v${nextVersion}`);
|
|
155
|
+
await updateApp(appName, token, nextVersion, entry);
|
|
156
|
+
}
|
|
100
157
|
} finally {
|
|
101
158
|
await releaseLockBestEffort(appName, token);
|
|
102
159
|
}
|
|
@@ -105,10 +162,10 @@ export async function deployCommand(opts: DeployOptions = {}): Promise<void> {
|
|
|
105
162
|
// before looking it up in the manifest. So the user-facing URL must use
|
|
106
163
|
// the consumer's de-scoped package name (no `mf-` prefix, no `@scope/`).
|
|
107
164
|
const liveUrl = `https://bigbrain.me/bigbrain-vibe/${cleanAppName(consumerPkg.name)}`;
|
|
108
|
-
output(`[z2h-cli] deployed ✅\n app: ${appName}\n version: ${
|
|
165
|
+
output(`[z2h-cli] deployed ✅\n app: ${appName}\n version: ${nextVersion}\n open: ${liveUrl}`, {
|
|
109
166
|
ok: true,
|
|
110
167
|
appName,
|
|
111
|
-
version,
|
|
168
|
+
version: nextVersion,
|
|
112
169
|
bucket: BUCKET_WRITE_NAME,
|
|
113
170
|
keyPrefix,
|
|
114
171
|
url,
|
|
@@ -117,11 +174,18 @@ export async function deployCommand(opts: DeployOptions = {}): Promise<void> {
|
|
|
117
174
|
});
|
|
118
175
|
}
|
|
119
176
|
|
|
120
|
-
async function registerApp(
|
|
177
|
+
async function registerApp(
|
|
178
|
+
appName: string,
|
|
179
|
+
token: string,
|
|
180
|
+
version: number,
|
|
181
|
+
entry: ManifestEntry,
|
|
182
|
+
visibility: 'public' | 'private',
|
|
183
|
+
gitRemote: string
|
|
184
|
+
): Promise<void> {
|
|
121
185
|
const res = await fetch(`${brokerBaseUrl()}/z2h-cli/apps/register`, {
|
|
122
186
|
method: 'POST',
|
|
123
|
-
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
|
|
124
|
-
body: JSON.stringify({ appName, version, entry }),
|
|
187
|
+
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...routingHeaders() },
|
|
188
|
+
body: JSON.stringify({ appName, version, entry, visibility, gitRemote }),
|
|
125
189
|
});
|
|
126
190
|
if (!res.ok) {
|
|
127
191
|
const text = await res.text();
|
|
@@ -129,14 +193,64 @@ async function registerApp(appName: string, token: string, version: number, entr
|
|
|
129
193
|
}
|
|
130
194
|
}
|
|
131
195
|
|
|
132
|
-
//
|
|
196
|
+
// Acquire the per-app deploy lock. Throws a friendly error on 409 (someone else is deploying)
|
|
197
|
+
// so the deploy stops and the caller can retry; the backend TTL backstops a crashed holder.
|
|
198
|
+
async function acquireDeployLock(appName: string, token: string): Promise<void> {
|
|
199
|
+
const res = await fetch(`${brokerBaseUrl()}/z2h-cli/lock/acquire`, {
|
|
200
|
+
method: 'POST',
|
|
201
|
+
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...routingHeaders() },
|
|
202
|
+
body: JSON.stringify({ appName }),
|
|
203
|
+
});
|
|
204
|
+
if (res.status === 409) {
|
|
205
|
+
throw new Error(`Someone is deploying "${appName}" right now — try again in a moment.`);
|
|
206
|
+
}
|
|
207
|
+
if (!res.ok) {
|
|
208
|
+
const text = await res.text();
|
|
209
|
+
throw new Error(`Failed to acquire deploy lock (${res.status}): ${text.slice(0, 512)}`);
|
|
210
|
+
}
|
|
211
|
+
info(`deploy lock acquired for ${appName}`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async function updateApp(appName: string, token: string, version: number, entry: ManifestEntry): Promise<void> {
|
|
215
|
+
const res = await fetch(`${brokerBaseUrl()}/z2h-cli/apps/update`, {
|
|
216
|
+
method: 'POST',
|
|
217
|
+
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...routingHeaders() },
|
|
218
|
+
body: JSON.stringify({ appName, version, entry }),
|
|
219
|
+
});
|
|
220
|
+
if (!res.ok) {
|
|
221
|
+
const text = await res.text();
|
|
222
|
+
throw new Error(`App update failed (${res.status}): ${text.slice(0, 1024)}`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// First deploy only: decide whether the app is born public or private. The
|
|
227
|
+
// choice is passed to the register endpoint (default private). Non-interactive
|
|
228
|
+
// runs (--json or no TTY) always default to private.
|
|
229
|
+
async function resolveFirstDeployVisibility(appName: string, json?: boolean): Promise<'public' | 'private'> {
|
|
230
|
+
if (json || !process.stdout.isTTY) {
|
|
231
|
+
return 'private';
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
235
|
+
const answer = await new Promise<string>(resolve => {
|
|
236
|
+
rl.question(`Make "${appName}" public to all monday.com employees? (y/N) `, resolve);
|
|
237
|
+
});
|
|
238
|
+
rl.close();
|
|
239
|
+
return answer.trim().toLowerCase() === 'y' ? 'public' : 'private';
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// best-effort — the 2-min TTL backstop handles failures
|
|
133
243
|
async function releaseLockBestEffort(appName: string, token: string): Promise<void> {
|
|
134
244
|
try {
|
|
135
|
-
await fetch(`${brokerBaseUrl()}/z2h-cli/lock/release`, {
|
|
245
|
+
const res = await fetch(`${brokerBaseUrl()}/z2h-cli/lock/release`, {
|
|
136
246
|
method: 'POST',
|
|
137
|
-
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
|
|
247
|
+
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...routingHeaders() },
|
|
138
248
|
body: JSON.stringify({ appName }),
|
|
139
249
|
});
|
|
250
|
+
if (!res.ok) {
|
|
251
|
+
warn(`failed to release lock for ${appName} (${res.status})`);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
140
254
|
info(`lock released for ${appName}`);
|
|
141
255
|
} catch {
|
|
142
256
|
warn(`failed to release lock for ${appName}`);
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { pathExists } from 'fs-extra';
|
|
3
|
+
import { info, warn, output, setOutputMode } from '../util/logger';
|
|
4
|
+
import { assertWorkspace } from '../util/workspace';
|
|
5
|
+
import { mfAppName } from '../util/app-name';
|
|
6
|
+
import { fetchApp } from '../util/broker/app';
|
|
7
|
+
import { resolveGitEnv } from '../util/git/git-env';
|
|
8
|
+
import { cloneRepo, pullAndResolve } from '../util/git/repo';
|
|
9
|
+
import { runYarnInstall } from '../util/npm/install';
|
|
10
|
+
import { prepareShadow } from '../shadow/prepare';
|
|
11
|
+
|
|
12
|
+
export interface EditOptions {
|
|
13
|
+
json?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function editCommand(appName: string, opts: EditOptions = {}): Promise<void> {
|
|
17
|
+
if (opts.json) {
|
|
18
|
+
setOutputMode('json');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const workspace = await assertWorkspace();
|
|
22
|
+
const mfName = mfAppName(appName);
|
|
23
|
+
|
|
24
|
+
const app = await fetchApp(mfName);
|
|
25
|
+
if (!app?.gitRemote) {
|
|
26
|
+
output(`[z2h-cli] ${appName} was deployed without git tracking — run "z2h-cli deploy" to set up a git remote`, {
|
|
27
|
+
ok: true,
|
|
28
|
+
appName,
|
|
29
|
+
});
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const { gitRemote } = app;
|
|
33
|
+
|
|
34
|
+
const gitEnv = await resolveGitEnv(mfName, gitRemote);
|
|
35
|
+
|
|
36
|
+
const appDir = path.join(workspace, appName);
|
|
37
|
+
const hasLocal = await pathExists(appDir);
|
|
38
|
+
|
|
39
|
+
if (!hasLocal) {
|
|
40
|
+
info(`cloning ${appName}...`);
|
|
41
|
+
await cloneRepo(gitRemote, appDir, gitEnv);
|
|
42
|
+
|
|
43
|
+
info(`installing dependencies for ${appName}...`);
|
|
44
|
+
await runYarnInstall({ cwd: workspace });
|
|
45
|
+
|
|
46
|
+
// Best-effort workspace wiring (.zth shadow). Non-fatal — dev/deploy re-run it.
|
|
47
|
+
try {
|
|
48
|
+
await prepareShadow(appDir);
|
|
49
|
+
} catch (err) {
|
|
50
|
+
warn(`prepare shadow failed: ${(err as Error).message} — it will run again when needed.`);
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
info(`pulling latest ${appName}...`);
|
|
54
|
+
await pullAndResolve(appDir, gitEnv);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
output(`[z2h-cli] ${appName} is ready at ${appDir}`, {
|
|
58
|
+
ok: true,
|
|
59
|
+
appDir,
|
|
60
|
+
appName,
|
|
61
|
+
gitRemote,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { output, setOutputMode } from '../util/logger';
|
|
2
|
+
import { getCwdAppName, mfAppName } from '../util/app-name';
|
|
3
|
+
import { brokerBaseUrl, routingHeaders } from '../constants';
|
|
4
|
+
import { readZthAuth } from '../util/auth/auth-store';
|
|
5
|
+
|
|
6
|
+
export interface GrantOptions {
|
|
7
|
+
public?: boolean;
|
|
8
|
+
level?: 'owner' | 'editor' | 'viewer';
|
|
9
|
+
json?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function postGrant(
|
|
13
|
+
appName: string,
|
|
14
|
+
token: string,
|
|
15
|
+
body: { type: string; value?: string; accessLevel: string }
|
|
16
|
+
): Promise<void> {
|
|
17
|
+
const res = await fetch(`${brokerBaseUrl()}/z2h-apps/${appName}/grants`, {
|
|
18
|
+
method: 'POST',
|
|
19
|
+
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...routingHeaders() },
|
|
20
|
+
body: JSON.stringify(body),
|
|
21
|
+
});
|
|
22
|
+
if (!res.ok) {
|
|
23
|
+
const text = await res.text();
|
|
24
|
+
throw new Error(`Grant failed (${res.status}): ${text.slice(0, 1024)}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function grantCommand(email: string | undefined, opts: GrantOptions): Promise<void> {
|
|
29
|
+
if (opts.json) {
|
|
30
|
+
setOutputMode('json');
|
|
31
|
+
}
|
|
32
|
+
if (!opts.public && !email) {
|
|
33
|
+
throw new Error('Specify what to grant — e.g. --public or provide an email');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const level = opts.level ?? 'viewer';
|
|
37
|
+
if (!['owner', 'editor', 'viewer'].includes(level)) {
|
|
38
|
+
throw new Error(`Invalid --level "${level}". Must be owner, editor, or viewer`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const appName = mfAppName(getCwdAppName()!);
|
|
42
|
+
const { token } = await readZthAuth();
|
|
43
|
+
|
|
44
|
+
if (email) {
|
|
45
|
+
await postGrant(appName, token, { type: 'user', value: email, accessLevel: level });
|
|
46
|
+
}
|
|
47
|
+
if (opts.public) {
|
|
48
|
+
await postGrant(appName, token, { type: 'global', accessLevel: 'viewer' });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const parts: string[] = [];
|
|
52
|
+
if (email) {
|
|
53
|
+
parts.push(`${email} (${level})`);
|
|
54
|
+
}
|
|
55
|
+
if (opts.public) {
|
|
56
|
+
parts.push('public');
|
|
57
|
+
}
|
|
58
|
+
output(`[z2h-cli] "${appName}" granted to: ${parts.join(', ')}`, { ok: true, appName, granted: parts });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function revokeCommand(email: string | undefined, opts: GrantOptions): Promise<void> {
|
|
62
|
+
if (opts.json) {
|
|
63
|
+
setOutputMode('json');
|
|
64
|
+
}
|
|
65
|
+
if (!opts.public && !email) {
|
|
66
|
+
throw new Error('Specify what to revoke — e.g. --public or provide an email');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const appName = mfAppName(getCwdAppName()!);
|
|
70
|
+
const { token } = await readZthAuth();
|
|
71
|
+
|
|
72
|
+
const target = email ? `user/${encodeURIComponent(email)}` : 'global';
|
|
73
|
+
const res = await fetch(`${brokerBaseUrl()}/z2h-apps/${appName}/grants/${target}`, {
|
|
74
|
+
method: 'DELETE',
|
|
75
|
+
headers: { Authorization: `Bearer ${token}`, ...routingHeaders() },
|
|
76
|
+
});
|
|
77
|
+
if (!res.ok) {
|
|
78
|
+
const text = await res.text();
|
|
79
|
+
throw new Error(`Revoke failed (${res.status}): ${text.slice(0, 1024)}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const revoked = email ?? 'public';
|
|
83
|
+
const msg = email
|
|
84
|
+
? `[z2h-cli] "${appName}" access revoked from ${email}`
|
|
85
|
+
: `[z2h-cli] "${appName}" is now private (owners/admins only)`;
|
|
86
|
+
output(msg, { ok: true, appName, revoked });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface TransferOwnerOptions {
|
|
90
|
+
json?: boolean;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export async function transferOwnerCommand(email: string | undefined, opts: TransferOwnerOptions): Promise<void> {
|
|
94
|
+
if (opts.json) {
|
|
95
|
+
setOutputMode('json');
|
|
96
|
+
}
|
|
97
|
+
if (!email) {
|
|
98
|
+
throw new Error('Specify the new owner — e.g. z2h-cli transfer-owner someone@monday.com');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const appName = mfAppName(getCwdAppName()!);
|
|
102
|
+
const { token } = await readZthAuth();
|
|
103
|
+
|
|
104
|
+
const res = await fetch(`${brokerBaseUrl()}/z2h-apps/${appName}/transfer-owner`, {
|
|
105
|
+
method: 'POST',
|
|
106
|
+
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...routingHeaders() },
|
|
107
|
+
body: JSON.stringify({ email }),
|
|
108
|
+
});
|
|
109
|
+
if (!res.ok) {
|
|
110
|
+
const text = await res.text();
|
|
111
|
+
throw new Error(`Transfer owner failed (${res.status}): ${text.slice(0, 1024)}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
output(`[z2h-cli] "${appName}" ownership transferred to ${email}`, { ok: true, appName, newOwner: email });
|
|
115
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -71,6 +71,13 @@ export function brokerBaseUrl(): string {
|
|
|
71
71
|
return process.env.Z2H_BROKER_BASE_URL || DEFAULT_BROKER_BASE_URL;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
// Returns Baggage header for monday-mirror routing when Z2H_ROUTING_KEY is set.
|
|
75
|
+
// No-op in production (returns {}) — only activates when the env var is set.
|
|
76
|
+
export function routingHeaders(): Record<string, string> {
|
|
77
|
+
const key = process.env.Z2H_ROUTING_KEY;
|
|
78
|
+
return key ? { Baggage: `routingKey=${key}` } : {};
|
|
79
|
+
}
|
|
80
|
+
|
|
74
81
|
// The authenticated npm reverse-proxy is mounted here on the broker. Only the
|
|
75
82
|
// `@mondaydotcomorg` scope is served; everything else resolves from npmjs
|
|
76
83
|
// directly. See docs/superpowers/specs/2026-06-03-npm-brokering-design.md §3.
|