mnfst-publish 0.1.17 → 0.1.18

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/README.md CHANGED
@@ -15,6 +15,29 @@ project, zips it gitignore-aware, uploads it, and prints the live URL.
15
15
  Flags: `--staging` / `--production` (default), `--no-render`, `--promote`,
16
16
  `--source render|spa` (auto-detected), `--key <key>`, `--mcp <url>`.
17
17
 
18
+ ## Release scripts
19
+
20
+ Review a build at its own address before it reaches production, then ship
21
+ exactly that build:
22
+
23
+ ```bash
24
+ npx mnfst-publish --no-live --label "v2.4.0" --json
25
+ # {"ok":true,"env":"staging","deployment_id":"dep_…","live":false,"preview_url":"https://preview--….manifestx.ai",…}
26
+ npx mnfst-publish --promote --deployment dep_… --expect-live dep_… --json
27
+ ```
28
+
29
+ - `--no-live`: staging only. Stores the deployment without changing what
30
+ staging serves and prints its preview URL (unlisted, noindex).
31
+ - `--label <text>`: names the deployment (up to 200 characters).
32
+ - `--promote --deployment <id>`: promotes exactly that staging deployment;
33
+ staging is unchanged. Without `--deployment`, promotes what staging serves.
34
+ - `--expect-live <id>`: with `--promote`, refuses unless production is serving
35
+ that deployment, checked in the same step as the switch.
36
+ - `--json`: the result as one JSON object on stdout; progress goes to stderr.
37
+
38
+ Against a server that doesn't support these yet, mnfst-publish stops before
39
+ uploading or promoting anything.
40
+
18
41
  Publishing from Claude with the Manifest connector needs no key: the command it
19
42
  gives you carries a one-time upload link. While the site renders, mnfst-publish
20
43
  keeps that link alive (up to 3 hours). If a link still expires, ask Claude to
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import { main } from '../manifest.publish.mjs';
2
+ import { main, isJsonMode, jsonFailure } from '../manifest.publish.mjs';
3
3
 
4
4
  main().catch((err) => {
5
+ if (isJsonMode()) process.stdout.write(JSON.stringify(jsonFailure(err)) + '\n');
5
6
  console.error('mnfst-publish:', err && err.message ? err.message : err);
6
7
  process.exit(1);
7
8
  });
@@ -14,32 +14,70 @@ import { join, dirname, relative, sep } from 'node:path';
14
14
 
15
15
  const DEFAULT_MCP = 'https://mcp.manifestx.dev/mcp';
16
16
 
17
+ // --json: progress goes to stderr, the final result is one JSON object on stdout.
18
+ let jsonMode = false;
19
+
17
20
  function log(msg) {
18
- process.stdout.write(msg + '\n');
21
+ (jsonMode ? process.stderr : process.stdout).write(msg + '\n');
19
22
  }
20
23
  function fail(msg) {
24
+ if (jsonMode) process.stdout.write(JSON.stringify({ ok: false, error: 'failed', message: msg }) + '\n');
21
25
  console.error('mnfst-publish: ' + msg);
22
26
  process.exit(1);
23
27
  }
24
28
 
25
- function parseArgs(argv) {
29
+ export const LABEL_MAX_LENGTH = 200;
30
+ // C0/C1 controls, DEL, line/paragraph separators, zero-width and bidi override/isolate characters.
31
+ const NON_PRINTABLE = new RegExp('[\\u0000-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2066-\\u2069\\ufeff]');
32
+
33
+ /** Parse argv; throws an Error with a user-facing message on a bad combination. */
34
+ export function parseArgs(argv) {
26
35
  const out = { env: 'production', render: undefined, promote: false };
36
+ let envGiven = false;
37
+ const value = (flag, i) => {
38
+ const v = argv[i];
39
+ if (v === undefined || v.startsWith('--')) throw new Error(`${flag} needs a value`);
40
+ return v;
41
+ };
27
42
  for (let i = 0; i < argv.length; i++) {
28
43
  const a = argv[i];
29
- if (a === '--staging') out.env = 'staging';
30
- else if (a === '--production' || a === '--prod') out.env = 'production';
31
- else if (a === '--env') out.env = argv[++i];
32
- else if (a === '--source') out.source = argv[++i];
33
- else if (a === '--output-dir') out.outputDir = argv[++i];
44
+ if (a === '--staging') { out.env = 'staging'; envGiven = true; }
45
+ else if (a === '--production' || a === '--prod') { out.env = 'production'; envGiven = true; }
46
+ else if (a === '--env') { out.env = value(a, ++i); envGiven = true; }
47
+ else if (a === '--source') out.source = value(a, ++i);
48
+ else if (a === '--output-dir') out.outputDir = value(a, ++i);
34
49
  else if (a === '--no-render') out.render = false;
35
50
  else if (a === '--render') out.render = true;
36
51
  else if (a === '--promote') out.promote = true;
37
- else if (a === '--key') out.key = argv[++i];
38
- else if (a === '--mcp') out.mcp = argv[++i];
39
- else if (a === '--root') out.root = argv[++i];
52
+ else if (a === '--no-live') out.live = false;
53
+ else if (a === '--label') out.label = value(a, ++i);
54
+ else if (a === '--deployment') out.deployment = value(a, ++i);
55
+ else if (a === '--expect-live') out.expectLive = value(a, ++i);
56
+ else if (a === '--json') out.json = true;
57
+ else if (a === '--key') out.key = value(a, ++i);
58
+ else if (a === '--mcp') out.mcp = value(a, ++i);
59
+ else if (a === '--root') out.root = value(a, ++i);
40
60
  else if (a === '-h' || a === '--help') out.help = true;
41
61
  }
42
- if (out.env !== 'staging' && out.env !== 'production') fail(`--env must be "staging" or "production" (got "${out.env}")`);
62
+ if (out.env !== 'staging' && out.env !== 'production') throw new Error(`--env must be "staging" or "production" (got "${out.env}")`);
63
+ if (out.promote) {
64
+ if (out.live === false) throw new Error('--no-live applies to a publish, not to --promote');
65
+ if (out.label !== undefined) throw new Error('--label is set when publishing; --promote carries the label of the deployment it promotes');
66
+ } else {
67
+ if (out.deployment !== undefined) throw new Error('--deployment only applies with --promote');
68
+ if (out.expectLive !== undefined) throw new Error('--expect-live only applies with --promote');
69
+ }
70
+ if (out.live === false) {
71
+ // A held publish is a staging publish; production has no held form.
72
+ if (envGiven && out.env !== 'staging') throw new Error('--no-live only applies to staging (drop --production, or use --staging)');
73
+ out.env = 'staging';
74
+ }
75
+ if (out.label !== undefined) {
76
+ out.label = out.label.trim();
77
+ if (!out.label) throw new Error('--label must not be empty');
78
+ if (out.label.length > LABEL_MAX_LENGTH) throw new Error(`--label must be at most ${LABEL_MAX_LENGTH} characters`);
79
+ if (NON_PRINTABLE.test(out.label)) throw new Error('--label must be printable (no control, zero-width or bidi characters, no line breaks)');
80
+ }
43
81
  return out;
44
82
  }
45
83
 
@@ -165,7 +203,7 @@ async function mcp(url, key, method, params, sessionId) {
165
203
  return { json, sessionId: sid, status: res.status, raw: text };
166
204
  }
167
205
 
168
- async function callTool(url, key, name, args) {
206
+ async function openSession(url, key) {
169
207
  const init = await mcp(url, key, 'initialize', {
170
208
  protocolVersion: '2024-11-05',
171
209
  capabilities: {},
@@ -174,6 +212,19 @@ async function callTool(url, key, name, args) {
174
212
  const sid = init.sessionId;
175
213
  if (!sid) throw new Error('no MCP session — check your API key and network connection');
176
214
  await mcp(url, key, 'notifications/initialized', {}, sid);
215
+ return sid;
216
+ }
217
+
218
+ /** The input-schema property names of `name`, or null when the server does not list it. */
219
+ async function toolParams(url, key, name) {
220
+ const sid = await openSession(url, key);
221
+ const res = await mcp(url, key, 'tools/list', {}, sid);
222
+ const tool = ((res.json && res.json.result && res.json.result.tools) || []).find((t) => t.name === name);
223
+ return tool ? Object.keys((tool.inputSchema && tool.inputSchema.properties) || {}) : null;
224
+ }
225
+
226
+ async function callTool(url, key, name, args) {
227
+ const sid = await openSession(url, key);
177
228
  const res = await mcp(url, key, 'tools/call', { name, arguments: args }, sid);
178
229
  const result = res.json && res.json.result;
179
230
  if (!result) throw new Error(`unexpected response from ${name} (HTTP ${res.status})`);
@@ -784,11 +835,141 @@ function checkUploadUrl(uploadUrl, mcpUrl) {
784
835
  }
785
836
  }
786
837
 
838
+ /** Child process stdio: with --json, the child's stdout joins stderr so stdout stays one JSON object. */
839
+ export function childStdio(json) {
840
+ return json ? ['inherit', 2, 'inherit'] : 'inherit';
841
+ }
842
+
843
+ /** Whether --json is on (the bin reports uncaught errors as JSON then). */
844
+ export function isJsonMode() {
845
+ return jsonMode;
846
+ }
847
+
848
+ /** The --json failure object for an error. */
849
+ export function jsonFailure(err) {
850
+ const message = err && err.message ? err.message : String(err);
851
+ return { ok: false, error: (err && err.code) || 'failed', message };
852
+ }
853
+
854
+ /** manifest_promote arguments for these options. */
855
+ export function promoteToolArgs(opts) {
856
+ const args = {};
857
+ if (opts.deployment) args.deployment_id = opts.deployment;
858
+ if (opts.expectLive) args.expect_live = opts.expectLive;
859
+ return args;
860
+ }
861
+
862
+ /** manifest_publish handshake arguments for these options (headless/CI path). */
863
+ export function publishToolArgs(opts, source) {
864
+ const args = { env: opts.env, source, via_cli: true };
865
+ if (opts.live === false) args.live = false;
866
+ if (opts.label) args.label = opts.label;
867
+ return args;
868
+ }
869
+
870
+ /**
871
+ * An older server drops arguments it does not know, so a promote by id would
872
+ * promote whatever staging serves instead. Refuse before calling it.
873
+ */
874
+ export function unsupportedPromoteArgs(args, params) {
875
+ if (!params) return Object.keys(args);
876
+ return Object.keys(args).filter((k) => !params.includes(k));
877
+ }
878
+
879
+ /** Promote, by id when asked. Resolves the tool result; throws on refusal or an unsupported server. */
880
+ export async function runPromote(opts, { url, key, callToolImpl = callTool, toolParamsImpl = toolParams }) {
881
+ const args = promoteToolArgs(opts);
882
+ if (Object.keys(args).length) {
883
+ const missing = unsupportedPromoteArgs(args, await toolParamsImpl(url, key, 'manifest_promote'));
884
+ if (missing.length) {
885
+ throw new Error(`this Manifest server does not support ${missing.map((k) => (k === 'deployment_id' ? '--deployment' : '--expect-live')).join(' or ')} yet, so nothing was promoted.`);
886
+ }
887
+ }
888
+ const res = await callToolImpl(url, key, 'manifest_promote', args);
889
+ if (opts.deployment && res.from_deployment_id && res.from_deployment_id !== opts.deployment) {
890
+ throw new Error(`the server promoted ${res.from_deployment_id}, not ${opts.deployment}. Check production now.`);
891
+ }
892
+ return res;
893
+ }
894
+
895
+ /** The handshake must confirm a held publish and the label before anything is uploaded; an older server drops both. */
896
+ export function checkHeldHandshake(opts, handshake) {
897
+ if (opts.live === false && handshake.live !== false) {
898
+ throw new Error('this Manifest server does not support --no-live yet, so nothing was uploaded.');
899
+ }
900
+ if (opts.label && handshake.label !== opts.label) {
901
+ throw new Error('this Manifest server does not support --label yet, so nothing was uploaded.');
902
+ }
903
+ }
904
+
905
+ /**
906
+ * A connector link carries no flags of its own: with --no-live, confirm from its
907
+ * status that the server minted it held before uploading anything.
908
+ */
909
+ export async function checkHeldLink(uploadUrl, { fetchImpl = fetch } = {}) {
910
+ let json = null;
911
+ try {
912
+ const res = await fetchImpl(statusUrlFor(uploadUrl), { method: 'GET', headers: { accept: 'application/json' } });
913
+ json = JSON.parse(await res.text());
914
+ } catch {
915
+ /* unreadable: treated as unconfirmed */
916
+ }
917
+ if (!json || json.live !== false) {
918
+ throw new Error('--no-live was given, but this upload link was not minted as a held publish, so nothing was uploaded. Call manifest_publish with live=false for a new link.');
919
+ }
920
+ }
921
+
922
+ /** Human lines and the --json object for a finished publish. */
923
+ export function publishSummary(opts, up) {
924
+ const held = up.live === false;
925
+ const json = {
926
+ ok: true,
927
+ env: up.env || opts.env,
928
+ deployment_id: up.deployment_id || null,
929
+ live: !held,
930
+ url: up.url || null,
931
+ ...(held ? { preview_url: up.preview_url || up.url || null } : {}),
932
+ label: up.label ?? opts.label ?? null,
933
+ };
934
+ const lines = [''];
935
+ if (held) {
936
+ lines.push(`✓ Stored on staging without going live: ${json.deployment_id}`);
937
+ if (json.preview_url) lines.push(` Preview: ${json.preview_url}`);
938
+ if (json.label) lines.push(` Label: ${json.label}`);
939
+ lines.push(` Ship it with \`npx mnfst-publish --promote --deployment ${json.deployment_id}\`.`);
940
+ } else {
941
+ lines.push(`✓ Published to ${json.env}: ${json.url}`);
942
+ if (json.deployment_id) lines.push(` Deployment: ${json.deployment_id}`);
943
+ if (json.env === 'staging') lines.push(' Review it, then run `npx mnfst-publish --promote` to take it live.');
944
+ }
945
+ return { lines, json };
946
+ }
947
+
948
+ const USAGE = [
949
+ 'Usage: npx mnfst-publish [--staging|--production] [--source spa|render] [--output-dir <dir>] [--no-render]',
950
+ ' [--no-live] [--label <text>] [--json]',
951
+ ' npx mnfst-publish --promote [--deployment <id>] [--expect-live <id>] [--json]',
952
+ 'Publishes the current Manifest project to managed hosting and prints the live URL.',
953
+ ' --no-live staging only: store the build without changing what staging serves; prints its preview URL',
954
+ ' --label <text> name the deployment (up to 200 characters)',
955
+ ' --promote promote what staging serves to production',
956
+ ' --deployment <id> with --promote: promote exactly this staging deployment',
957
+ ' --expect-live <id> with --promote: refuse unless production is serving this deployment now',
958
+ ' --json print the result as one JSON object on stdout (progress goes to stderr)',
959
+ ];
960
+
787
961
  export async function main() {
788
- const opts = parseArgs(process.argv.slice(2));
962
+ const argv = process.argv.slice(2);
963
+ // Set before parsing, so a bad flag is reported as JSON too.
964
+ jsonMode = argv.includes('--json');
965
+ let opts;
966
+ try {
967
+ opts = parseArgs(argv);
968
+ } catch (e) {
969
+ fail(e.message);
970
+ }
789
971
  if (opts.help) {
790
- log('Usage: npx mnfst-publish [--staging|--production] [--source spa|render] [--output-dir <dir>] [--no-render] [--promote]');
791
- log('Publishes the current Manifest project to managed hosting and prints the live URL.');
972
+ for (const line of USAGE) log(line);
792
973
  return;
793
974
  }
794
975
 
@@ -808,9 +989,27 @@ export async function main() {
808
989
  // tool (no key). Needs a key.
809
990
  if (opts.promote) {
810
991
  if (!key) fail('no API key found for --promote. Set MANIFEST_API_KEY in .env for headless use, or promote from Claude with the Manifest connector (no key needed).');
811
- log('Promoting the staged version to production…');
812
- const res = await callTool(url, key, 'manifest_promote', {});
992
+ log(opts.deployment ? `Promoting ${opts.deployment} to production…` : 'Promoting the staged version to production…');
993
+ let res;
994
+ try {
995
+ res = await runPromote(opts, { url, key });
996
+ } catch (e) {
997
+ fail(e.message);
998
+ }
999
+ if (jsonMode) {
1000
+ process.stdout.write(
1001
+ JSON.stringify({
1002
+ ok: true,
1003
+ env: 'production',
1004
+ deployment_id: res.live_deployment_id ?? null,
1005
+ from_deployment_id: res.from_deployment_id ?? null,
1006
+ label: res.label ?? null,
1007
+ url: res.url ?? null,
1008
+ }) + '\n',
1009
+ );
1010
+ }
813
1011
  log('✓ Live: ' + (res.url || res._text || 'production updated'));
1012
+ if (res.live_deployment_id) log(` Deployment: ${res.live_deployment_id}${res.from_deployment_id ? ` (from ${res.from_deployment_id})` : ''}`);
814
1013
  return;
815
1014
  }
816
1015
 
@@ -822,13 +1021,22 @@ export async function main() {
822
1021
 
823
1022
  // Checked before rendering, since the link is renewed while the render runs.
824
1023
  if (injectedUpload) checkUploadUrl(injectedUpload, url);
1024
+ // Before any render, so a wrong link costs nothing.
1025
+ if (injectedUpload && opts.live === false) {
1026
+ try {
1027
+ await checkHeldLink(injectedUpload);
1028
+ } catch (e) {
1029
+ fail(e.message);
1030
+ }
1031
+ }
1032
+ if (injectedUpload && opts.label) log(' note: --label is ignored here; the connector set the label when it minted this upload link.');
825
1033
  const keepAlive = injectedUpload ? keepUploadLinkAlive(injectedUpload) : null;
826
1034
 
827
1035
  const rendering = source === 'render' && opts.render !== false;
828
1036
  if (rendering) {
829
1037
  log('Rendering the site…');
830
1038
  // Async, so the renewal timer runs during the render.
831
- const code = await runCommand('npx', ['mnfst-render'], { cwd: root, stdio: 'inherit', shell: process.platform === 'win32' });
1039
+ const code = await runCommand('npx', ['mnfst-render'], { cwd: root, stdio: childStdio(jsonMode), shell: process.platform === 'win32' });
832
1040
  if (code !== 0) fail('render failed — fix the errors above and try again.');
833
1041
  }
834
1042
 
@@ -857,9 +1065,14 @@ export async function main() {
857
1065
  } else {
858
1066
  // Headless/CI: authenticate the handshake with the key to get an upload URL.
859
1067
  log(`Preparing ${opts.env} deploy…`);
860
- const handshake = await callTool(url, key, 'manifest_publish', { env: opts.env, source, via_cli: true });
1068
+ const handshake = await callTool(url, key, 'manifest_publish', publishToolArgs(opts, source));
861
1069
  uploadUrl = handshake.upload_url;
862
1070
  if (!uploadUrl) fail(handshake._text || 'could not start the publish (no upload URL returned).');
1071
+ try {
1072
+ checkHeldHandshake(opts, handshake);
1073
+ } catch (e) {
1074
+ fail(e.message);
1075
+ }
863
1076
  checkUploadUrl(uploadUrl, url);
864
1077
  }
865
1078
 
@@ -884,7 +1097,7 @@ export async function main() {
884
1097
  fail(e.message);
885
1098
  }
886
1099
 
887
- log('');
888
- log(`✓ Published to ${opts.env}: ${upJson.url}`);
889
- if (opts.env === 'staging') log(' Review it, then run `npx mnfst-publish --promote` to take it live.');
1100
+ const summary = publishSummary(opts, upJson);
1101
+ if (jsonMode) process.stdout.write(JSON.stringify(summary.json) + '\n');
1102
+ for (const line of summary.lines) log(line);
890
1103
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mnfst-publish",
3
- "version": "0.1.17",
3
+ "version": "0.1.18",
4
4
  "description": "One-command managed publishing for Manifest projects — render, zip, upload, live URL.",
5
5
  "type": "module",
6
6
  "bin": {