create-mostra 0.0.1 → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # create-mostra
2
+
3
+ Create a [Mostra](https://github.com/mostra-dev/mostra) artifact:
4
+
5
+ ```sh
6
+ npm create mostra@latest my-artifact
7
+ ```
8
+
9
+ The first run downloads Mostra's runtime for your platform (about 200 MB, macOS and Linux),
10
+ checks it against the SHA-256 its release pins, and caches it; later runs reuse it. Mostra runs on
11
+ its own Node.js, so the version you have doesn't matter. Then:
12
+
13
+ ```sh
14
+ npx @mostra/cli verify my-artifact
15
+ npx @mostra/cli publish my-artifact
16
+ ```
17
+
18
+ Every flag after `--` is `mostra new`'s, for example `npm create mostra@latest review -- --title "Q3 review"`.
19
+
20
+ Part of Mostra, the agent artifact runtime. Apache-2.0.
package/dist/bin.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {}
package/dist/bin.js ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env node
2
+ import { i as create } from "./src-Db8IadT7.js";
3
+ //#region src/bin.ts
4
+ /** `create-mostra`'s binary: `npm create mostra [dir]` (see `index.ts`). */
5
+ process.stdout.on("error", () => {});
6
+ process.stderr.on("error", () => {});
7
+ const code = await create({
8
+ args: process.argv.slice(2),
9
+ cwd: process.cwd(),
10
+ env: process.env,
11
+ stdout: (text) => {
12
+ process.stdout.write(text);
13
+ },
14
+ stderr: (text) => {
15
+ process.stderr.write(text);
16
+ },
17
+ terminal: process.stdin.isTTY === true || process.stdout.isTTY === true || process.stderr.isTTY === true
18
+ });
19
+ process.stdout.write("", () => process.exit(code));
20
+ //#endregion
21
+ export {};
22
+
23
+ //# sourceMappingURL=bin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bin.js","names":[],"sources":["../src/bin.ts"],"sourcesContent":["#!/usr/bin/env node\n/** `create-mostra`'s binary: `npm create mostra [dir]` (see `index.ts`). */\nimport { create } from \"./index.ts\";\n\nprocess.stdout.on(\"error\", () => {});\nprocess.stderr.on(\"error\", () => {});\nconst code = await create({\n args: process.argv.slice(2),\n cwd: process.cwd(),\n env: process.env,\n stdout: (text) => {\n process.stdout.write(text);\n },\n stderr: (text) => {\n process.stderr.write(text);\n },\n terminal:\n process.stdin.isTTY === true || process.stdout.isTTY === true || process.stderr.isTTY === true,\n});\nprocess.stdout.write(\"\", () => process.exit(code));\n"],"mappings":";;;;AAIA,QAAQ,OAAO,GAAG,eAAe,CAAC,CAAC;AACnC,QAAQ,OAAO,GAAG,eAAe,CAAC,CAAC;AACnC,MAAM,OAAO,MAAM,OAAO;CACxB,MAAM,QAAQ,KAAK,MAAM,CAAC;CAC1B,KAAK,QAAQ,IAAI;CACjB,KAAK,QAAQ;CACb,SAAS,SAAS;EAChB,QAAQ,OAAO,MAAM,IAAI;CAC3B;CACA,SAAS,SAAS;EAChB,QAAQ,OAAO,MAAM,IAAI;CAC3B;CACA,UACE,QAAQ,MAAM,UAAU,QAAQ,QAAQ,OAAO,UAAU,QAAQ,QAAQ,OAAO,UAAU;AAC9F,CAAC;AACD,QAAQ,OAAO,MAAM,UAAU,QAAQ,KAAK,IAAI,CAAC"}
@@ -0,0 +1,54 @@
1
+ //#region src/index.d.ts
2
+ /** The directory an artifact goes into when none is named. */
3
+ export declare const DEFAULT_DIR: string;
4
+ /** What one run does: the `mostra` arguments, and the directory it creates. */
5
+ export interface CreatePlan {
6
+ /** The arguments for `mostra`: `new <dir>` and the flags passed on. */
7
+ readonly argv: readonly string[];
8
+ /** The artifact's directory, absolute. */
9
+ readonly dir: string;
10
+ /** Whether `--json` was given: then stdout is exactly the CLI's envelope. */
11
+ readonly json: boolean;
12
+ /** Whether to print this package's usage instead. */
13
+ readonly help: boolean;
14
+ }
15
+ /**
16
+ * Plans a run from `npm create mostra [dir] -- [flags]`: the first argument that isn't a flag is
17
+ * the directory (else {@link DEFAULT_DIR}); every flag goes to `mostra new` as given.
18
+ */
19
+ export declare function plan(args: readonly string[], cwd: string): CreatePlan;
20
+ /** This package's usage. */
21
+ export declare const USAGE: string;
22
+ /** What to run next, for a person; `rel` is the artifact's directory as they'd type it. */
23
+ export declare function nextSteps(rel: string): string;
24
+ /** The `mostra` binary of the `@mostra/cli` this package depends on. */
25
+ export declare function cliBin(): string;
26
+ /** How a run talks to the terminal. */
27
+ export interface CreateIo {
28
+ /** The arguments after `create-mostra`, as `npm create mostra` passes them. */
29
+ readonly args: readonly string[];
30
+ /** The directory relative paths resolve from. */
31
+ readonly cwd: string;
32
+ /** The environment the CLI runs with. */
33
+ readonly env: NodeJS.ProcessEnv;
34
+ /** Writes to stdout: the usage and the next steps (the CLI writes its own output itself). */
35
+ readonly stdout: (text: string) => void;
36
+ /** Writes to stderr: why the CLI couldn't be started. */
37
+ readonly stderr?: (text: string) => void;
38
+ /**
39
+ * Whether a terminal is attached (stdin, stdout or stderr is one). Ctrl-C there signals the
40
+ * whole foreground process group, the CLI included, so SIGINT isn't passed on: a second one
41
+ * would cut short a command that stops gracefully on the first (as the CLI's bootstrap does).
42
+ */
43
+ readonly terminal?: boolean;
44
+ /** Finds the `mostra` binary to run; default {@link cliBin}. */
45
+ readonly cli?: () => string;
46
+ }
47
+ /**
48
+ * Runs `npm create mostra`: resolves with the exit code, the CLI's own (128 plus the signal's
49
+ * number when a signal ended it, as a shell reports it). Signals this process gets are passed on
50
+ * as the CLI's bootstrap passes them.
51
+ */
52
+ export declare function create(io: CreateIo): Promise<number>;
53
+ //#endregion
54
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;qBAea;;iBAGI;;WAEN;;WAEA;;WAEA;;WAEA;;;;;;wBAOK,KAAK,yBAAyB,cAAc;;qBAqC/C;;wBAaG,UAAU;;wBAcV;;iBASC;;WAEN;;WAEA;;WAEA,KAAK,OAAO;;WAEZ,SAAS;;WAET,UAAU;;;;;;WAMV;;WAEA;;;;;;;wBA6BW,OAAO,IAAI,WAAW"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import { a as nextSteps, i as create, n as USAGE, o as plan, r as cliBin, t as DEFAULT_DIR } from "./src-Db8IadT7.js";
2
+ export { DEFAULT_DIR, USAGE, cliBin, create, nextSteps, plan };
@@ -0,0 +1,163 @@
1
+ import { createRequire } from "node:module";
2
+ import { spawn } from "node:child_process";
3
+ import { readFileSync } from "node:fs";
4
+ import { constants } from "node:os";
5
+ import { dirname, join, relative, resolve } from "node:path";
6
+ //#region src/index.ts
7
+ /**
8
+ * `create-mostra`, what `npm create mostra` runs (RT-01). It creates an artifact with `mostra new`
9
+ * through the `@mostra/cli` it depends on, so the first run also installs the runtime bundle that
10
+ * release pins (the CLI's bootstrap), then says what to run next. It adds no behavior of its own:
11
+ * every flag but its own is the CLI's, and its exit code is the CLI's.
12
+ *
13
+ * @packageDocumentation
14
+ */
15
+ /** The directory an artifact goes into when none is named. */
16
+ const DEFAULT_DIR = "my-artifact";
17
+ /**
18
+ * Plans a run from `npm create mostra [dir] -- [flags]`: the first argument that isn't a flag is
19
+ * the directory (else {@link DEFAULT_DIR}); every flag goes to `mostra new` as given.
20
+ */
21
+ function plan(args, cwd) {
22
+ let dir;
23
+ const flags = [];
24
+ for (let index = 0; index < args.length; index++) {
25
+ const arg = args[index];
26
+ if (arg === "--") continue;
27
+ if (arg.startsWith("-")) {
28
+ flags.push(arg);
29
+ const next = args[index + 1];
30
+ if (!arg.includes("=") && VALUED.has(arg) && next !== void 0) {
31
+ flags.push(next);
32
+ index++;
33
+ }
34
+ } else if (dir === void 0) dir = arg;
35
+ else flags.push(arg);
36
+ }
37
+ const chosen = dir ?? "my-artifact";
38
+ return {
39
+ argv: [
40
+ "new",
41
+ chosen,
42
+ ...flags
43
+ ],
44
+ dir: resolve(cwd, chosen),
45
+ json: flags.includes("--json"),
46
+ help: flags.includes("--help") || flags.includes("-h")
47
+ };
48
+ }
49
+ /** `mostra new`'s flags (and the global ones) that take a value. */
50
+ const VALUED = /* @__PURE__ */ new Set([
51
+ "--title",
52
+ "--template",
53
+ "--request-id",
54
+ "--home",
55
+ "--cwd",
56
+ "--timeout"
57
+ ]);
58
+ /** This package's usage. */
59
+ const USAGE = `Create a Mostra artifact.
60
+
61
+ Usage: npm create mostra@latest [dir] -- [--title <text>] [--json]
62
+
63
+ dir The directory to create it in; it must be empty or not exist. Default: ${DEFAULT_DIR}
64
+ --title The artifact's title. Default: the directory's name.
65
+ --json Print only mostra's JSON envelope on stdout.
66
+
67
+ The first run downloads Mostra's runtime (about 200 MB), checks it and caches it; later runs reuse it.
68
+ Every flag is mostra new's: npx @mostra/cli new --help lists them.
69
+ `;
70
+ /** What to run next, for a person; `rel` is the artifact's directory as they'd type it. */
71
+ function nextSteps(rel) {
72
+ const at = rel.includes(" ") ? JSON.stringify(rel) : rel;
73
+ return `
74
+ Next:
75
+ edit ${at}/index.tsx ordinary React; the agent writes it
76
+ npx @mostra/cli verify ${at} build it, render it and check it
77
+ npx @mostra/cli publish ${at} publish a version and print its link
78
+ npm install --global @mostra/cli for a plain \`mostra\` command
79
+
80
+ Docs: npx @mostra/cli docs
81
+ `;
82
+ }
83
+ /** The `mostra` binary of the `@mostra/cli` this package depends on. */
84
+ function cliBin() {
85
+ const manifestPath = createRequire(import.meta.url).resolve("@mostra/cli/package.json");
86
+ const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
87
+ if (!manifest.bin?.mostra) throw new Error("@mostra/cli declares no mostra binary.");
88
+ return join(dirname(manifestPath), manifest.bin.mostra);
89
+ }
90
+ /**
91
+ * The one diagnostic this package writes itself: the CLI it depends on couldn't be started, so
92
+ * nothing of it (nor of `@mostra/protocol`, which it may not import) can describe the failure.
93
+ * The fields are the catalog entry's (`MOSTRA_ENV_RUNTIME_MISSING`, exit 3).
94
+ */
95
+ function cliMissing(io, json, why) {
96
+ const message = `create-mostra couldn't start the mostra CLI it depends on (${why}). Install it again: npm create mostra@latest.`;
97
+ io.stderr?.(`create-mostra: ${message}\n`);
98
+ if (json) {
99
+ const error = {
100
+ code: "MOSTRA_ENV_RUNTIME_MISSING",
101
+ severity: "error",
102
+ phase: "host",
103
+ message,
104
+ docs: "https://mostra-dev.github.io/mostra/errors/MOSTRA_ENV_RUNTIME_MISSING"
105
+ };
106
+ io.stdout(`${JSON.stringify({
107
+ ok: false,
108
+ error,
109
+ warnings: []
110
+ })}\n`);
111
+ }
112
+ return 3;
113
+ }
114
+ /**
115
+ * Runs `npm create mostra`: resolves with the exit code, the CLI's own (128 plus the signal's
116
+ * number when a signal ended it, as a shell reports it). Signals this process gets are passed on
117
+ * as the CLI's bootstrap passes them.
118
+ */
119
+ async function create(io) {
120
+ const run = plan(io.args, io.cwd);
121
+ if (run.help) {
122
+ io.stdout(USAGE);
123
+ return 0;
124
+ }
125
+ let bin;
126
+ try {
127
+ bin = (io.cli ?? cliBin)();
128
+ } catch (error) {
129
+ return cliMissing(io, run.json, error instanceof Error ? error.message : String(error));
130
+ }
131
+ const code = await new Promise((done) => {
132
+ const child = spawn(process.execPath, [bin, ...run.argv], {
133
+ cwd: io.cwd,
134
+ env: io.env,
135
+ stdio: "inherit"
136
+ });
137
+ const forward = (signal) => {
138
+ if (signal === "SIGINT" && io.terminal) return;
139
+ child.kill(signal);
140
+ };
141
+ const signals = [
142
+ "SIGINT",
143
+ "SIGTERM",
144
+ "SIGHUP"
145
+ ];
146
+ for (const signal of signals) process.on(signal, forward);
147
+ let settled = false;
148
+ const settle = (exit) => {
149
+ if (settled) return;
150
+ settled = true;
151
+ for (const signal of signals) process.off(signal, forward);
152
+ done(exit);
153
+ };
154
+ child.on("error", (error) => settle(cliMissing(io, run.json, error.message)));
155
+ child.on("exit", (exit, signal) => settle(exit ?? 128 + (signal ? constants.signals[signal] ?? 0 : 0)));
156
+ });
157
+ if (code === 0 && !run.json) io.stdout(nextSteps(relative(io.cwd, run.dir) || "."));
158
+ return code;
159
+ }
160
+ //#endregion
161
+ export { nextSteps as a, create as i, USAGE as n, plan as o, cliBin as r, DEFAULT_DIR as t };
162
+
163
+ //# sourceMappingURL=src-Db8IadT7.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"src-Db8IadT7.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * `create-mostra`, what `npm create mostra` runs (RT-01). It creates an artifact with `mostra new`\n * through the `@mostra/cli` it depends on, so the first run also installs the runtime bundle that\n * release pins (the CLI's bootstrap), then says what to run next. It adds no behavior of its own:\n * every flag but its own is the CLI's, and its exit code is the CLI's.\n *\n * @packageDocumentation\n */\nimport { spawn } from \"node:child_process\";\nimport { readFileSync } from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport { constants } from \"node:os\";\nimport { dirname, join, relative, resolve } from \"node:path\";\n\n/** The directory an artifact goes into when none is named. */\nexport const DEFAULT_DIR: string = \"my-artifact\";\n\n/** What one run does: the `mostra` arguments, and the directory it creates. */\nexport interface CreatePlan {\n /** The arguments for `mostra`: `new <dir>` and the flags passed on. */\n readonly argv: readonly string[];\n /** The artifact's directory, absolute. */\n readonly dir: string;\n /** Whether `--json` was given: then stdout is exactly the CLI's envelope. */\n readonly json: boolean;\n /** Whether to print this package's usage instead. */\n readonly help: boolean;\n}\n\n/**\n * Plans a run from `npm create mostra [dir] -- [flags]`: the first argument that isn't a flag is\n * the directory (else {@link DEFAULT_DIR}); every flag goes to `mostra new` as given.\n */\nexport function plan(args: readonly string[], cwd: string): CreatePlan {\n let dir: string | undefined;\n const flags: string[] = [];\n for (let index = 0; index < args.length; index++) {\n const arg = args[index] as string;\n if (arg === \"--\") continue;\n if (arg.startsWith(\"-\")) {\n flags.push(arg);\n // A flag with a value in the next argument: `--title \"Q3 review\"`.\n const next = args[index + 1];\n if (!arg.includes(\"=\") && VALUED.has(arg) && next !== undefined) {\n flags.push(next);\n index++;\n }\n } else if (dir === undefined) dir = arg;\n else flags.push(arg);\n }\n const chosen = dir ?? DEFAULT_DIR;\n return {\n argv: [\"new\", chosen, ...flags],\n dir: resolve(cwd, chosen),\n json: flags.includes(\"--json\"),\n help: flags.includes(\"--help\") || flags.includes(\"-h\"),\n };\n}\n\n/** `mostra new`'s flags (and the global ones) that take a value. */\nconst VALUED: ReadonlySet<string> = new Set([\n \"--title\",\n \"--template\",\n \"--request-id\",\n \"--home\",\n \"--cwd\",\n \"--timeout\",\n]);\n\n/** This package's usage. */\nexport const USAGE: string = `Create a Mostra artifact.\n\nUsage: npm create mostra@latest [dir] -- [--title <text>] [--json]\n\n dir The directory to create it in; it must be empty or not exist. Default: ${DEFAULT_DIR}\n --title The artifact's title. Default: the directory's name.\n --json Print only mostra's JSON envelope on stdout.\n\nThe first run downloads Mostra's runtime (about 200 MB), checks it and caches it; later runs reuse it.\nEvery flag is mostra new's: npx @mostra/cli new --help lists them.\n`;\n\n/** What to run next, for a person; `rel` is the artifact's directory as they'd type it. */\nexport function nextSteps(rel: string): string {\n const at = rel.includes(\" \") ? JSON.stringify(rel) : rel;\n return `\nNext:\n edit ${at}/index.tsx ordinary React; the agent writes it\n npx @mostra/cli verify ${at} build it, render it and check it\n npx @mostra/cli publish ${at} publish a version and print its link\n npm install --global @mostra/cli for a plain \\`mostra\\` command\n\nDocs: npx @mostra/cli docs\n`;\n}\n\n/** The `mostra` binary of the `@mostra/cli` this package depends on. */\nexport function cliBin(): string {\n const require = createRequire(import.meta.url);\n const manifestPath = require.resolve(\"@mostra/cli/package.json\");\n const manifest = JSON.parse(readFileSync(manifestPath, \"utf8\")) as { bin?: { mostra?: string } };\n if (!manifest.bin?.mostra) throw new Error(\"@mostra/cli declares no mostra binary.\");\n return join(dirname(manifestPath), manifest.bin.mostra);\n}\n\n/** How a run talks to the terminal. */\nexport interface CreateIo {\n /** The arguments after `create-mostra`, as `npm create mostra` passes them. */\n readonly args: readonly string[];\n /** The directory relative paths resolve from. */\n readonly cwd: string;\n /** The environment the CLI runs with. */\n readonly env: NodeJS.ProcessEnv;\n /** Writes to stdout: the usage and the next steps (the CLI writes its own output itself). */\n readonly stdout: (text: string) => void;\n /** Writes to stderr: why the CLI couldn't be started. */\n readonly stderr?: (text: string) => void;\n /**\n * Whether a terminal is attached (stdin, stdout or stderr is one). Ctrl-C there signals the\n * whole foreground process group, the CLI included, so SIGINT isn't passed on: a second one\n * would cut short a command that stops gracefully on the first (as the CLI's bootstrap does).\n */\n readonly terminal?: boolean;\n /** Finds the `mostra` binary to run; default {@link cliBin}. */\n readonly cli?: () => string;\n}\n\n/**\n * The one diagnostic this package writes itself: the CLI it depends on couldn't be started, so\n * nothing of it (nor of `@mostra/protocol`, which it may not import) can describe the failure.\n * The fields are the catalog entry's (`MOSTRA_ENV_RUNTIME_MISSING`, exit 3).\n */\nfunction cliMissing(io: CreateIo, json: boolean, why: string): number {\n const message = `create-mostra couldn't start the mostra CLI it depends on (${why}). Install it again: npm create mostra@latest.`;\n io.stderr?.(`create-mostra: ${message}\\n`);\n if (json) {\n const error = {\n code: \"MOSTRA_ENV_RUNTIME_MISSING\",\n severity: \"error\",\n phase: \"host\",\n message,\n docs: \"https://mostra-dev.github.io/mostra/errors/MOSTRA_ENV_RUNTIME_MISSING\",\n };\n io.stdout(`${JSON.stringify({ ok: false, error, warnings: [] })}\\n`);\n }\n return 3;\n}\n\n/**\n * Runs `npm create mostra`: resolves with the exit code, the CLI's own (128 plus the signal's\n * number when a signal ended it, as a shell reports it). Signals this process gets are passed on\n * as the CLI's bootstrap passes them.\n */\nexport async function create(io: CreateIo): Promise<number> {\n const run = plan(io.args, io.cwd);\n if (run.help) {\n io.stdout(USAGE);\n return 0;\n }\n let bin: string;\n try {\n bin = (io.cli ?? cliBin)();\n } catch (error) {\n return cliMissing(io, run.json, error instanceof Error ? error.message : String(error));\n }\n const code = await new Promise<number>((done) => {\n const child = spawn(process.execPath, [bin, ...run.argv], {\n cwd: io.cwd,\n env: io.env,\n stdio: \"inherit\",\n });\n const forward = (signal: NodeJS.Signals): void => {\n if (signal === \"SIGINT\" && io.terminal) return; // the CLI got the terminal's own\n child.kill(signal);\n };\n const signals: NodeJS.Signals[] = [\"SIGINT\", \"SIGTERM\", \"SIGHUP\"];\n for (const signal of signals) process.on(signal, forward);\n let settled = false;\n const settle = (exit: number): void => {\n if (settled) return;\n settled = true;\n for (const signal of signals) process.off(signal, forward);\n done(exit);\n };\n child.on(\"error\", (error) => settle(cliMissing(io, run.json, error.message)));\n child.on(\"exit\", (exit, signal) =>\n settle(exit ?? 128 + (signal ? (constants.signals[signal] ?? 0) : 0)),\n );\n });\n if (code === 0 && !run.json) io.stdout(nextSteps(relative(io.cwd, run.dir) || \".\"));\n return code;\n}\n"],"mappings":"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"}
package/package.json CHANGED
@@ -1,24 +1,62 @@
1
1
  {
2
2
  "name": "create-mostra",
3
- "version": "0.0.1",
4
- "description": "Create a Mostra artifact. Placeholder: the real starter ships with the first Mostra release.",
3
+ "version": "0.1.0",
4
+ "description": "npm create mostra: create a Mostra artifact, installing the Mostra runtime on first use.",
5
5
  "license": "Apache-2.0",
6
- "bin": {
7
- "create-mostra": "index.js"
6
+ "author": "The Mostra Authors",
7
+ "homepage": "https://github.com/mostra-dev/mostra/tree/main/packages/create-mostra#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/mostra-dev/mostra.git",
11
+ "directory": "packages/create-mostra"
12
+ },
13
+ "bugs": "https://github.com/mostra-dev/mostra/issues",
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "exports": {
17
+ ".": {
18
+ "@mostra/source": "./src/index.ts",
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./dist/index.js"
21
+ },
22
+ "./package.json": "./package.json"
8
23
  },
9
24
  "files": [
10
- "index.js",
11
- "LICENSE"
25
+ "dist",
26
+ "src",
27
+ "!src/**/*.test.ts"
12
28
  ],
13
- "repository": {
14
- "type": "git",
15
- "url": "git+https://github.com/mostra-dev/mostra.git"
29
+ "engines": {
30
+ "node": ">=22.19"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public",
34
+ "provenance": true
35
+ },
36
+ "dependencies": {
37
+ "@mostra/cli": "0.1.0"
38
+ },
39
+ "devDependencies": {
40
+ "@mostra/tools": "0.0.0",
41
+ "@types/node": "24.19.1",
42
+ "tsdown": "0.23.0",
43
+ "typescript": "7.0.2",
44
+ "vitest": "5.0.3"
45
+ },
46
+ "bin": {
47
+ "create-mostra": "./dist/bin.js"
16
48
  },
17
- "homepage": "https://github.com/mostra-dev/mostra",
18
49
  "keywords": [
19
50
  "mostra",
20
- "agents",
21
- "artifacts",
22
- "react"
23
- ]
24
- }
51
+ "create",
52
+ "agent",
53
+ "artifact"
54
+ ],
55
+ "scripts": {
56
+ "build": "tsdown",
57
+ "typecheck": "tsc -b",
58
+ "test": "vitest run --project unit",
59
+ "api": "mostra-tools api",
60
+ "test:integration": "vitest run --project integration"
61
+ }
62
+ }
package/src/bin.ts ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env node
2
+ /** `create-mostra`'s binary: `npm create mostra [dir]` (see `index.ts`). */
3
+ import { create } from "./index.ts";
4
+
5
+ process.stdout.on("error", () => {});
6
+ process.stderr.on("error", () => {});
7
+ const code = await create({
8
+ args: process.argv.slice(2),
9
+ cwd: process.cwd(),
10
+ env: process.env,
11
+ stdout: (text) => {
12
+ process.stdout.write(text);
13
+ },
14
+ stderr: (text) => {
15
+ process.stderr.write(text);
16
+ },
17
+ terminal:
18
+ process.stdin.isTTY === true || process.stdout.isTTY === true || process.stderr.isTTY === true,
19
+ });
20
+ process.stdout.write("", () => process.exit(code));
package/src/index.ts ADDED
@@ -0,0 +1,192 @@
1
+ /**
2
+ * `create-mostra`, what `npm create mostra` runs (RT-01). It creates an artifact with `mostra new`
3
+ * through the `@mostra/cli` it depends on, so the first run also installs the runtime bundle that
4
+ * release pins (the CLI's bootstrap), then says what to run next. It adds no behavior of its own:
5
+ * every flag but its own is the CLI's, and its exit code is the CLI's.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ import { spawn } from "node:child_process";
10
+ import { readFileSync } from "node:fs";
11
+ import { createRequire } from "node:module";
12
+ import { constants } from "node:os";
13
+ import { dirname, join, relative, resolve } from "node:path";
14
+
15
+ /** The directory an artifact goes into when none is named. */
16
+ export const DEFAULT_DIR: string = "my-artifact";
17
+
18
+ /** What one run does: the `mostra` arguments, and the directory it creates. */
19
+ export interface CreatePlan {
20
+ /** The arguments for `mostra`: `new <dir>` and the flags passed on. */
21
+ readonly argv: readonly string[];
22
+ /** The artifact's directory, absolute. */
23
+ readonly dir: string;
24
+ /** Whether `--json` was given: then stdout is exactly the CLI's envelope. */
25
+ readonly json: boolean;
26
+ /** Whether to print this package's usage instead. */
27
+ readonly help: boolean;
28
+ }
29
+
30
+ /**
31
+ * Plans a run from `npm create mostra [dir] -- [flags]`: the first argument that isn't a flag is
32
+ * the directory (else {@link DEFAULT_DIR}); every flag goes to `mostra new` as given.
33
+ */
34
+ export function plan(args: readonly string[], cwd: string): CreatePlan {
35
+ let dir: string | undefined;
36
+ const flags: string[] = [];
37
+ for (let index = 0; index < args.length; index++) {
38
+ const arg = args[index] as string;
39
+ if (arg === "--") continue;
40
+ if (arg.startsWith("-")) {
41
+ flags.push(arg);
42
+ // A flag with a value in the next argument: `--title "Q3 review"`.
43
+ const next = args[index + 1];
44
+ if (!arg.includes("=") && VALUED.has(arg) && next !== undefined) {
45
+ flags.push(next);
46
+ index++;
47
+ }
48
+ } else if (dir === undefined) dir = arg;
49
+ else flags.push(arg);
50
+ }
51
+ const chosen = dir ?? DEFAULT_DIR;
52
+ return {
53
+ argv: ["new", chosen, ...flags],
54
+ dir: resolve(cwd, chosen),
55
+ json: flags.includes("--json"),
56
+ help: flags.includes("--help") || flags.includes("-h"),
57
+ };
58
+ }
59
+
60
+ /** `mostra new`'s flags (and the global ones) that take a value. */
61
+ const VALUED: ReadonlySet<string> = new Set([
62
+ "--title",
63
+ "--template",
64
+ "--request-id",
65
+ "--home",
66
+ "--cwd",
67
+ "--timeout",
68
+ ]);
69
+
70
+ /** This package's usage. */
71
+ export const USAGE: string = `Create a Mostra artifact.
72
+
73
+ Usage: npm create mostra@latest [dir] -- [--title <text>] [--json]
74
+
75
+ dir The directory to create it in; it must be empty or not exist. Default: ${DEFAULT_DIR}
76
+ --title The artifact's title. Default: the directory's name.
77
+ --json Print only mostra's JSON envelope on stdout.
78
+
79
+ The first run downloads Mostra's runtime (about 200 MB), checks it and caches it; later runs reuse it.
80
+ Every flag is mostra new's: npx @mostra/cli new --help lists them.
81
+ `;
82
+
83
+ /** What to run next, for a person; `rel` is the artifact's directory as they'd type it. */
84
+ export function nextSteps(rel: string): string {
85
+ const at = rel.includes(" ") ? JSON.stringify(rel) : rel;
86
+ return `
87
+ Next:
88
+ edit ${at}/index.tsx ordinary React; the agent writes it
89
+ npx @mostra/cli verify ${at} build it, render it and check it
90
+ npx @mostra/cli publish ${at} publish a version and print its link
91
+ npm install --global @mostra/cli for a plain \`mostra\` command
92
+
93
+ Docs: npx @mostra/cli docs
94
+ `;
95
+ }
96
+
97
+ /** The `mostra` binary of the `@mostra/cli` this package depends on. */
98
+ export function cliBin(): string {
99
+ const require = createRequire(import.meta.url);
100
+ const manifestPath = require.resolve("@mostra/cli/package.json");
101
+ const manifest = JSON.parse(readFileSync(manifestPath, "utf8")) as { bin?: { mostra?: string } };
102
+ if (!manifest.bin?.mostra) throw new Error("@mostra/cli declares no mostra binary.");
103
+ return join(dirname(manifestPath), manifest.bin.mostra);
104
+ }
105
+
106
+ /** How a run talks to the terminal. */
107
+ export interface CreateIo {
108
+ /** The arguments after `create-mostra`, as `npm create mostra` passes them. */
109
+ readonly args: readonly string[];
110
+ /** The directory relative paths resolve from. */
111
+ readonly cwd: string;
112
+ /** The environment the CLI runs with. */
113
+ readonly env: NodeJS.ProcessEnv;
114
+ /** Writes to stdout: the usage and the next steps (the CLI writes its own output itself). */
115
+ readonly stdout: (text: string) => void;
116
+ /** Writes to stderr: why the CLI couldn't be started. */
117
+ readonly stderr?: (text: string) => void;
118
+ /**
119
+ * Whether a terminal is attached (stdin, stdout or stderr is one). Ctrl-C there signals the
120
+ * whole foreground process group, the CLI included, so SIGINT isn't passed on: a second one
121
+ * would cut short a command that stops gracefully on the first (as the CLI's bootstrap does).
122
+ */
123
+ readonly terminal?: boolean;
124
+ /** Finds the `mostra` binary to run; default {@link cliBin}. */
125
+ readonly cli?: () => string;
126
+ }
127
+
128
+ /**
129
+ * The one diagnostic this package writes itself: the CLI it depends on couldn't be started, so
130
+ * nothing of it (nor of `@mostra/protocol`, which it may not import) can describe the failure.
131
+ * The fields are the catalog entry's (`MOSTRA_ENV_RUNTIME_MISSING`, exit 3).
132
+ */
133
+ function cliMissing(io: CreateIo, json: boolean, why: string): number {
134
+ const message = `create-mostra couldn't start the mostra CLI it depends on (${why}). Install it again: npm create mostra@latest.`;
135
+ io.stderr?.(`create-mostra: ${message}\n`);
136
+ if (json) {
137
+ const error = {
138
+ code: "MOSTRA_ENV_RUNTIME_MISSING",
139
+ severity: "error",
140
+ phase: "host",
141
+ message,
142
+ docs: "https://mostra-dev.github.io/mostra/errors/MOSTRA_ENV_RUNTIME_MISSING",
143
+ };
144
+ io.stdout(`${JSON.stringify({ ok: false, error, warnings: [] })}\n`);
145
+ }
146
+ return 3;
147
+ }
148
+
149
+ /**
150
+ * Runs `npm create mostra`: resolves with the exit code, the CLI's own (128 plus the signal's
151
+ * number when a signal ended it, as a shell reports it). Signals this process gets are passed on
152
+ * as the CLI's bootstrap passes them.
153
+ */
154
+ export async function create(io: CreateIo): Promise<number> {
155
+ const run = plan(io.args, io.cwd);
156
+ if (run.help) {
157
+ io.stdout(USAGE);
158
+ return 0;
159
+ }
160
+ let bin: string;
161
+ try {
162
+ bin = (io.cli ?? cliBin)();
163
+ } catch (error) {
164
+ return cliMissing(io, run.json, error instanceof Error ? error.message : String(error));
165
+ }
166
+ const code = await new Promise<number>((done) => {
167
+ const child = spawn(process.execPath, [bin, ...run.argv], {
168
+ cwd: io.cwd,
169
+ env: io.env,
170
+ stdio: "inherit",
171
+ });
172
+ const forward = (signal: NodeJS.Signals): void => {
173
+ if (signal === "SIGINT" && io.terminal) return; // the CLI got the terminal's own
174
+ child.kill(signal);
175
+ };
176
+ const signals: NodeJS.Signals[] = ["SIGINT", "SIGTERM", "SIGHUP"];
177
+ for (const signal of signals) process.on(signal, forward);
178
+ let settled = false;
179
+ const settle = (exit: number): void => {
180
+ if (settled) return;
181
+ settled = true;
182
+ for (const signal of signals) process.off(signal, forward);
183
+ done(exit);
184
+ };
185
+ child.on("error", (error) => settle(cliMissing(io, run.json, error.message)));
186
+ child.on("exit", (exit, signal) =>
187
+ settle(exit ?? 128 + (signal ? (constants.signals[signal] ?? 0) : 0)),
188
+ );
189
+ });
190
+ if (code === 0 && !run.json) io.stdout(nextSteps(relative(io.cwd, run.dir) || "."));
191
+ return code;
192
+ }