@pollis-inc/cli 0.0.0-stage → 0.1.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/build.d.ts +39 -0
- package/dist/build.js +351 -0
- package/dist/devtool.d.ts +12 -0
- package/dist/devtool.js +62 -0
- package/dist/errors.d.ts +3 -0
- package/dist/errors.js +3 -0
- package/dist/main.d.ts +11 -0
- package/dist/main.js +104 -0
- package/dist/new.d.ts +18 -0
- package/dist/new.js +85 -0
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.js +160 -0
- package/dist/rust.d.ts +22 -0
- package/dist/rust.js +150 -0
- package/dist/test.d.ts +13 -0
- package/dist/test.js +49 -0
- package/package.json +33 -3
- package/templates/rust/Cargo.toml.tmpl +25 -0
- package/templates/rust/README.md +18 -0
- package/templates/rust/gitignore +2 -0
- package/templates/rust/rust-toolchain.toml +5 -0
- package/templates/rust/src/lib.rs +26 -0
- package/templates/rust/tests/hello.rs +24 -0
- package/templates/ts/README.md +17 -0
- package/templates/ts/gitignore +2 -0
- package/templates/ts/package.json.tmpl +20 -0
- package/templates/ts/src/app.test.ts +22 -0
- package/templates/ts/src/app.ts +23 -0
- package/templates/ts/tsconfig.json +12 -0
- package/README.md +0 -3
package/dist/build.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `pollis app build`: an App's source in, a sandboxed component and its manifest
|
|
3
|
+
* out. Rust Apps compile in `rust.ts`; both languages end at the same check.
|
|
4
|
+
*
|
|
5
|
+
* For TypeScript:
|
|
6
|
+
*
|
|
7
|
+
* 1. Bundle the App with esbuild (refusing Node built-ins) and import it in
|
|
8
|
+
* Node to read `app({ manifest })`; check it and write `pollis-app.json`.
|
|
9
|
+
* 2. Bundle it again for the sandbox, behind a generated entry that exports
|
|
10
|
+
* the six `pollis:app@0.1.0` handlers.
|
|
11
|
+
* 3. Compile that with ComponentizeJS (StarlingMonkey), pre-initialized by
|
|
12
|
+
* `pollis-app-dev` with fixed randomness and clocks, in a fixed staging
|
|
13
|
+
* directory: the same source and lockfile give the same bytes.
|
|
14
|
+
* 4. Load the component in the real App host (`pollis-app-dev check`), so an
|
|
15
|
+
* App the runtime would refuse is refused here, and print its App hash.
|
|
16
|
+
*/
|
|
17
|
+
import { type Plugin } from "esbuild";
|
|
18
|
+
import { type ResolvedManifest } from "@pollis-inc/app/manifest";
|
|
19
|
+
import { BuildError } from "./errors.js";
|
|
20
|
+
export { BuildError };
|
|
21
|
+
export interface BuildOptions {
|
|
22
|
+
/** Project directory (holds package.json). */
|
|
23
|
+
dir: string;
|
|
24
|
+
/** App entry, relative to `dir`. Default: package.json `pollis.entry`, else src/app.ts. */
|
|
25
|
+
entry?: string;
|
|
26
|
+
/** Output directory, relative to `dir`. Default dist. */
|
|
27
|
+
out?: string;
|
|
28
|
+
quiet?: boolean;
|
|
29
|
+
}
|
|
30
|
+
export interface BuildResult {
|
|
31
|
+
component: string;
|
|
32
|
+
manifestPath: string;
|
|
33
|
+
manifest: ResolvedManifest;
|
|
34
|
+
appHash: string;
|
|
35
|
+
bytes: number;
|
|
36
|
+
}
|
|
37
|
+
/** Refuses every Node built-in, naming the importer and saying why. */
|
|
38
|
+
export declare function noNodeBuiltins(): Plugin;
|
|
39
|
+
export declare function buildApp(options: BuildOptions): Promise<BuildResult>;
|
package/dist/build.js
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `pollis app build`: an App's source in, a sandboxed component and its manifest
|
|
3
|
+
* out. Rust Apps compile in `rust.ts`; both languages end at the same check.
|
|
4
|
+
*
|
|
5
|
+
* For TypeScript:
|
|
6
|
+
*
|
|
7
|
+
* 1. Bundle the App with esbuild (refusing Node built-ins) and import it in
|
|
8
|
+
* Node to read `app({ manifest })`; check it and write `pollis-app.json`.
|
|
9
|
+
* 2. Bundle it again for the sandbox, behind a generated entry that exports
|
|
10
|
+
* the six `pollis:app@0.1.0` handlers.
|
|
11
|
+
* 3. Compile that with ComponentizeJS (StarlingMonkey), pre-initialized by
|
|
12
|
+
* `pollis-app-dev` with fixed randomness and clocks, in a fixed staging
|
|
13
|
+
* directory: the same source and lockfile give the same bytes.
|
|
14
|
+
* 4. Load the component in the real App host (`pollis-app-dev check`), so an
|
|
15
|
+
* App the runtime would refuse is refused here, and print its App hash.
|
|
16
|
+
*/
|
|
17
|
+
import { builtinModules, createRequire } from "node:module";
|
|
18
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
19
|
+
import { tmpdir } from "node:os";
|
|
20
|
+
import { join, relative, resolve } from "node:path";
|
|
21
|
+
import { pathToFileURL } from "node:url";
|
|
22
|
+
import { build as esbuild } from "esbuild";
|
|
23
|
+
import { isApp } from "@pollis-inc/app";
|
|
24
|
+
import { HANDLER_FOR, ManifestError, resolveManifest } from "@pollis-inc/app/manifest";
|
|
25
|
+
import { devTool, findDevTool } from "./devtool.js";
|
|
26
|
+
import { BuildError } from "./errors.js";
|
|
27
|
+
import { compileRust, isRustApp } from "./rust.js";
|
|
28
|
+
export { BuildError };
|
|
29
|
+
/**
|
|
30
|
+
* Packages are resolved as for a browser: the sandbox has web basics
|
|
31
|
+
* (`crypto.getRandomValues`, TextEncoder, timers) and no Node built-ins, so a
|
|
32
|
+
* package's browser build is usually the one that works. HTTP is ctx.fetch.
|
|
33
|
+
*/
|
|
34
|
+
const MAIN_FIELDS = ["browser", "module", "main"];
|
|
35
|
+
const CONDITIONS = ["browser", "import", "default"];
|
|
36
|
+
const BUILTINS = new Set(builtinModules.flatMap((m) => [m, `node:${m}`]));
|
|
37
|
+
/** What each Node built-in is usually reached for, and what to use instead. */
|
|
38
|
+
function builtinAdvice(name) {
|
|
39
|
+
const bare = name.replace(/^node:/, "").split("/")[0];
|
|
40
|
+
switch (bare) {
|
|
41
|
+
case "fs":
|
|
42
|
+
case "path":
|
|
43
|
+
return "Apps have no files. Keep data in ctx.state (sealed key-value state, `state.quota` in the manifest).";
|
|
44
|
+
case "net":
|
|
45
|
+
case "http":
|
|
46
|
+
case "https":
|
|
47
|
+
case "http2":
|
|
48
|
+
case "dgram":
|
|
49
|
+
case "dns":
|
|
50
|
+
case "tls":
|
|
51
|
+
return "Apps have no sockets. Use ctx.fetch, which reaches only the hosts listed in `egress`.";
|
|
52
|
+
case "child_process":
|
|
53
|
+
case "worker_threads":
|
|
54
|
+
case "cluster":
|
|
55
|
+
return "Apps cannot start processes or threads.";
|
|
56
|
+
case "crypto":
|
|
57
|
+
return "Use the web's crypto.getRandomValues() (built in) or a pure-JS library.";
|
|
58
|
+
default:
|
|
59
|
+
return "Apps run in a sandbox with no files, sockets or processes; only pure-JS packages work.";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Refuses every Node built-in, naming the importer and saying why. */
|
|
63
|
+
export function noNodeBuiltins() {
|
|
64
|
+
return {
|
|
65
|
+
name: "pollis-no-node-builtins",
|
|
66
|
+
setup(build) {
|
|
67
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
68
|
+
if (!BUILTINS.has(args.path)) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
errors: [
|
|
73
|
+
{
|
|
74
|
+
text: `\`${args.path}\` is a Node built-in, and Pollis Apps cannot use Node built-ins. ${builtinAdvice(args.path)}`,
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function readJson(path) {
|
|
83
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
84
|
+
}
|
|
85
|
+
function formatEsbuildError(e) {
|
|
86
|
+
const errors = e.errors;
|
|
87
|
+
if (!errors?.length) {
|
|
88
|
+
return e.message ?? String(e);
|
|
89
|
+
}
|
|
90
|
+
return errors
|
|
91
|
+
.map((err) => (err.location ? `${err.location.file}:${err.location.line}:${err.location.column}: ${err.text}` : err.text))
|
|
92
|
+
.join("\n");
|
|
93
|
+
}
|
|
94
|
+
/** Bundle for Node and import it, to read the App's definition. */
|
|
95
|
+
async function loadDefinition(dir, entry, scratch) {
|
|
96
|
+
const outfile = join(scratch, "manifest-probe.mjs");
|
|
97
|
+
try {
|
|
98
|
+
await esbuild({
|
|
99
|
+
absWorkingDir: dir,
|
|
100
|
+
entryPoints: [entry],
|
|
101
|
+
bundle: true,
|
|
102
|
+
platform: "neutral",
|
|
103
|
+
mainFields: MAIN_FIELDS,
|
|
104
|
+
conditions: CONDITIONS,
|
|
105
|
+
format: "esm",
|
|
106
|
+
target: "es2022",
|
|
107
|
+
outfile,
|
|
108
|
+
logLevel: "silent",
|
|
109
|
+
plugins: [noNodeBuiltins()],
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
throw new BuildError(formatEsbuildError(e));
|
|
114
|
+
}
|
|
115
|
+
const mod = (await import(pathToFileURL(outfile).href + `?t=${Date.now()}`));
|
|
116
|
+
if (!isApp(mod.default)) {
|
|
117
|
+
throw new BuildError(`${entry} must \`export default app({ manifest, ...handlers })\` from "@pollis-inc/app"`);
|
|
118
|
+
}
|
|
119
|
+
return mod.default;
|
|
120
|
+
}
|
|
121
|
+
/** The generated module the component is built from. */
|
|
122
|
+
function entrySource(entry) {
|
|
123
|
+
const handlers = Object.values(HANDLER_FOR);
|
|
124
|
+
return [
|
|
125
|
+
`import definition from ${JSON.stringify("./" + entry.replace(/\\/g, "/"))};`,
|
|
126
|
+
`import { exportsFor } from "@pollis-inc/app/runtime";`,
|
|
127
|
+
`const handlers = exportsFor(definition);`,
|
|
128
|
+
...handlers.map((h) => `export function ${h}(arg) { return handlers.${h}(arg); }`),
|
|
129
|
+
"",
|
|
130
|
+
].join("\n");
|
|
131
|
+
}
|
|
132
|
+
/** Where ComponentizeJS runs. Fixed, because its paths end up in the component. */
|
|
133
|
+
function stageDir() {
|
|
134
|
+
return process.platform === "win32" ? join(tmpdir(), "pollis-app-build") : "/tmp/pollis-app-build";
|
|
135
|
+
}
|
|
136
|
+
/** Hold the staging directory for one build at a time. */
|
|
137
|
+
function lockStage(stage) {
|
|
138
|
+
const lock = `${stage}.lock`;
|
|
139
|
+
try {
|
|
140
|
+
mkdirSync(lock);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
let owner = 0;
|
|
144
|
+
try {
|
|
145
|
+
owner = Number(readFileSync(join(lock, "pid"), "utf8"));
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
// Lock directory without a pid: a build that crashed while starting.
|
|
149
|
+
}
|
|
150
|
+
let alive = false;
|
|
151
|
+
if (owner > 0) {
|
|
152
|
+
try {
|
|
153
|
+
process.kill(owner, 0);
|
|
154
|
+
alive = true;
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
alive = false;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (alive) {
|
|
161
|
+
throw new BuildError(`another \`pollis app build\` is running (pid ${owner}); run this one when it finishes`);
|
|
162
|
+
}
|
|
163
|
+
rmSync(lock, { recursive: true, force: true });
|
|
164
|
+
mkdirSync(lock);
|
|
165
|
+
}
|
|
166
|
+
writeFileSync(join(lock, "pid"), String(process.pid));
|
|
167
|
+
return () => rmSync(lock, { recursive: true, force: true });
|
|
168
|
+
}
|
|
169
|
+
/** A seeded `Math.random`, for ComponentizeJS's work-directory name. */
|
|
170
|
+
function seededRandom() {
|
|
171
|
+
let seed = 1;
|
|
172
|
+
return () => {
|
|
173
|
+
seed = (seed * 16807) % 2147483647;
|
|
174
|
+
return seed / 2147483647;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
async function componentize(bundle, wit, wizerBin) {
|
|
178
|
+
const { componentize: run } = await import("@bytecodealliance/componentize-js");
|
|
179
|
+
const stage = stageDir();
|
|
180
|
+
const unlock = lockStage(stage);
|
|
181
|
+
const cwd = process.cwd();
|
|
182
|
+
const tmp = process.env.TMPDIR;
|
|
183
|
+
const random = Math.random;
|
|
184
|
+
try {
|
|
185
|
+
rmSync(stage, { recursive: true, force: true });
|
|
186
|
+
mkdirSync(join(stage, "src"), { recursive: true });
|
|
187
|
+
writeFileSync(join(stage, "src", "app.js"), bundle);
|
|
188
|
+
process.chdir(join(stage, "src"));
|
|
189
|
+
process.env.TMPDIR = stage;
|
|
190
|
+
Math.random = seededRandom();
|
|
191
|
+
// `witWorld` (the WIT as text) is supported but missing from the package's
|
|
192
|
+
// types. A path (`witPath`) would be read through ComponentizeJS's WASI
|
|
193
|
+
// shim, which cannot see arbitrary host paths.
|
|
194
|
+
const { component } = await run({
|
|
195
|
+
sourcePath: "app.js",
|
|
196
|
+
witWorld: wit,
|
|
197
|
+
worldName: "app",
|
|
198
|
+
// No wasi:http: the App host refuses it. Network access is ctx.fetch.
|
|
199
|
+
disableFeatures: ["http", "fetch-event"],
|
|
200
|
+
wizerBin,
|
|
201
|
+
});
|
|
202
|
+
return component;
|
|
203
|
+
}
|
|
204
|
+
finally {
|
|
205
|
+
Math.random = random;
|
|
206
|
+
process.chdir(cwd);
|
|
207
|
+
if (tmp === undefined) {
|
|
208
|
+
delete process.env.TMPDIR;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
process.env.TMPDIR = tmp;
|
|
212
|
+
}
|
|
213
|
+
rmSync(stage, { recursive: true, force: true });
|
|
214
|
+
unlock();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function log(quiet, line) {
|
|
218
|
+
if (!quiet) {
|
|
219
|
+
console.log(line);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Write the build out and load it in the real App host, so an App the runtime
|
|
224
|
+
* would refuse is refused here, then compute its App hash.
|
|
225
|
+
*/
|
|
226
|
+
function finishBuild(bin, out, manifest, component) {
|
|
227
|
+
const manifestPath = join(out, "pollis-app.json");
|
|
228
|
+
writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + "\n");
|
|
229
|
+
const componentPath = join(out, "app.wasm");
|
|
230
|
+
writeFileSync(componentPath, component);
|
|
231
|
+
// A development signature from an earlier `pollis app dev` signs another App hash. Left beside
|
|
232
|
+
// this build it makes a development installation refuse the build as tampered instead of
|
|
233
|
+
// offering it to an admin; `pollis app dev` signs each new build itself.
|
|
234
|
+
rmSync(`${componentPath}.sig.json`, { force: true });
|
|
235
|
+
try {
|
|
236
|
+
devTool(bin, ["check", componentPath, manifestPath]);
|
|
237
|
+
}
|
|
238
|
+
catch (e) {
|
|
239
|
+
rmSync(componentPath, { force: true });
|
|
240
|
+
throw new BuildError(`the App host refuses this build: ${e.message}`);
|
|
241
|
+
}
|
|
242
|
+
const appHash = devTool(bin, ["hash", componentPath, manifestPath]);
|
|
243
|
+
return { component: componentPath, manifestPath, manifest, appHash, bytes: component.length };
|
|
244
|
+
}
|
|
245
|
+
async function buildRustApp(dir, options) {
|
|
246
|
+
const out = resolve(dir, options.out ?? "dist");
|
|
247
|
+
const bin = findDevTool();
|
|
248
|
+
mkdirSync(out, { recursive: true });
|
|
249
|
+
const scratch = join(out, ".build");
|
|
250
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
251
|
+
mkdirSync(scratch, { recursive: true });
|
|
252
|
+
try {
|
|
253
|
+
log(options.quiet, `compiling ${relative(process.cwd(), dir) || "."} to a component...`);
|
|
254
|
+
const { manifest, component } = compileRust(dir, scratch);
|
|
255
|
+
return finishBuild(bin, out, manifest, component);
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
export async function buildApp(options) {
|
|
262
|
+
const dir = resolve(options.dir);
|
|
263
|
+
if (isRustApp(dir)) {
|
|
264
|
+
return buildRustApp(dir, options);
|
|
265
|
+
}
|
|
266
|
+
const pkgPath = join(dir, "package.json");
|
|
267
|
+
if (!existsSync(pkgPath)) {
|
|
268
|
+
throw new BuildError(`no App in ${dir} (no package.json, or Cargo.toml that depends on pollis-app); run this in your App's directory`);
|
|
269
|
+
}
|
|
270
|
+
const pkg = readJson(pkgPath);
|
|
271
|
+
const configured = pkg.pollis?.entry;
|
|
272
|
+
const entry = options.entry ?? configured ?? "src/app.ts";
|
|
273
|
+
if (!existsSync(join(dir, entry))) {
|
|
274
|
+
throw new BuildError(`no App entry at ${entry} (set "pollis": { "entry": "..." } in package.json)`);
|
|
275
|
+
}
|
|
276
|
+
const out = resolve(dir, options.out ?? "dist");
|
|
277
|
+
const bin = findDevTool();
|
|
278
|
+
mkdirSync(out, { recursive: true });
|
|
279
|
+
const scratch = join(out, ".build");
|
|
280
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
281
|
+
mkdirSync(scratch, { recursive: true });
|
|
282
|
+
try {
|
|
283
|
+
// 1. The manifest, from the code.
|
|
284
|
+
const definition = await loadDefinition(dir, entry, scratch);
|
|
285
|
+
const handlers = Object.keys(definition).filter((k) => Object.values(HANDLER_FOR).includes(k) && typeof definition[k] === "function");
|
|
286
|
+
let manifest;
|
|
287
|
+
try {
|
|
288
|
+
manifest = resolveManifest(definition.manifest, handlers, {
|
|
289
|
+
name: pkg.name,
|
|
290
|
+
version: pkg.version,
|
|
291
|
+
description: pkg.description,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
catch (e) {
|
|
295
|
+
if (e instanceof ManifestError) {
|
|
296
|
+
throw new BuildError(`manifest: ${e.message}`);
|
|
297
|
+
}
|
|
298
|
+
throw e;
|
|
299
|
+
}
|
|
300
|
+
// 2. The sandbox bundle.
|
|
301
|
+
let bundle;
|
|
302
|
+
try {
|
|
303
|
+
const result = await esbuild({
|
|
304
|
+
absWorkingDir: dir,
|
|
305
|
+
stdin: { contents: entrySource(entry), resolveDir: dir, sourcefile: "pollis-app-entry.js", loader: "js" },
|
|
306
|
+
bundle: true,
|
|
307
|
+
write: false,
|
|
308
|
+
platform: "neutral",
|
|
309
|
+
mainFields: MAIN_FIELDS,
|
|
310
|
+
conditions: CONDITIONS,
|
|
311
|
+
format: "esm",
|
|
312
|
+
target: "es2022",
|
|
313
|
+
charset: "utf8",
|
|
314
|
+
legalComments: "none",
|
|
315
|
+
// Drops esbuild's "// path/to/file.ts" comments, whose paths depend on
|
|
316
|
+
// where the dependencies are installed; names stay readable.
|
|
317
|
+
minifyWhitespace: true,
|
|
318
|
+
external: ["pollis:app/*"],
|
|
319
|
+
logLevel: "silent",
|
|
320
|
+
plugins: [noNodeBuiltins()],
|
|
321
|
+
});
|
|
322
|
+
bundle = result.outputFiles[0].text;
|
|
323
|
+
}
|
|
324
|
+
catch (e) {
|
|
325
|
+
throw new BuildError(formatEsbuildError(e));
|
|
326
|
+
}
|
|
327
|
+
writeFileSync(join(scratch, "app.js"), bundle);
|
|
328
|
+
// 3. The component.
|
|
329
|
+
const require = createRequire(join(dir, "package.json"));
|
|
330
|
+
let witPath;
|
|
331
|
+
try {
|
|
332
|
+
witPath = require.resolve("@pollis-inc/app/wit/app.wit");
|
|
333
|
+
}
|
|
334
|
+
catch {
|
|
335
|
+
throw new BuildError("@pollis-inc/app is not installed in this project; add it with `pnpm add @pollis-inc/app`");
|
|
336
|
+
}
|
|
337
|
+
log(options.quiet, `compiling ${relative(process.cwd(), join(dir, entry)) || entry} to a component...`);
|
|
338
|
+
let component;
|
|
339
|
+
try {
|
|
340
|
+
component = await componentize(bundle, readFileSync(witPath, "utf8"), bin);
|
|
341
|
+
}
|
|
342
|
+
catch (e) {
|
|
343
|
+
throw new BuildError(`compiling to a component failed:\n${e.message}`);
|
|
344
|
+
}
|
|
345
|
+
// 4. The real host's verdict, and the App hash.
|
|
346
|
+
return finishBuild(bin, out, manifest, component);
|
|
347
|
+
}
|
|
348
|
+
finally {
|
|
349
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
350
|
+
}
|
|
351
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finding and running `pollis-app-dev`, the native half of `pollis app`: the
|
|
3
|
+
* offline test runner (the real App host), the deterministic build step and
|
|
4
|
+
* the App hash. Built from the Pollis repo (`cargo build --release -p
|
|
5
|
+
* pollis-app-dev`).
|
|
6
|
+
*/
|
|
7
|
+
export declare class DevToolMissing extends Error {
|
|
8
|
+
}
|
|
9
|
+
/** `$POLLIS_APP_DEV_BIN`, else `pollis-app-dev` on PATH, else a build in the Pollis repo this CLI lives in. */
|
|
10
|
+
export declare function findDevTool(): string;
|
|
11
|
+
/** Run a `pollis-app-dev` subcommand; returns stdout, throws with stderr on failure. */
|
|
12
|
+
export declare function devTool(bin: string, args: string[]): string;
|
package/dist/devtool.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finding and running `pollis-app-dev`, the native half of `pollis app`: the
|
|
3
|
+
* offline test runner (the real App host), the deterministic build step and
|
|
4
|
+
* the App hash. Built from the Pollis repo (`cargo build --release -p
|
|
5
|
+
* pollis-app-dev`).
|
|
6
|
+
*/
|
|
7
|
+
import { spawnSync } from "node:child_process";
|
|
8
|
+
import { accessSync, constants, existsSync } from "node:fs";
|
|
9
|
+
import { delimiter, dirname, join } from "node:path";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
const EXE = process.platform === "win32" ? "pollis-app-dev.exe" : "pollis-app-dev";
|
|
12
|
+
export class DevToolMissing extends Error {
|
|
13
|
+
}
|
|
14
|
+
function executable(path) {
|
|
15
|
+
try {
|
|
16
|
+
accessSync(path, constants.X_OK);
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** `$POLLIS_APP_DEV_BIN`, else `pollis-app-dev` on PATH, else a build in the Pollis repo this CLI lives in. */
|
|
24
|
+
export function findDevTool() {
|
|
25
|
+
const fromEnv = process.env.POLLIS_APP_DEV_BIN;
|
|
26
|
+
if (fromEnv) {
|
|
27
|
+
if (!executable(fromEnv)) {
|
|
28
|
+
throw new DevToolMissing(`POLLIS_APP_DEV_BIN is ${fromEnv}, which is not an executable file`);
|
|
29
|
+
}
|
|
30
|
+
return fromEnv;
|
|
31
|
+
}
|
|
32
|
+
for (const dir of (process.env.PATH ?? "").split(delimiter)) {
|
|
33
|
+
if (dir && executable(join(dir, EXE))) {
|
|
34
|
+
return join(dir, EXE);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
// Inside the Pollis repo (sdk/ts/cli/dist/devtool.js): its cargo target dir.
|
|
38
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
39
|
+
for (let i = 0; i < 6; i++) {
|
|
40
|
+
for (const profile of ["release", "debug"]) {
|
|
41
|
+
const candidate = join(dir, "target", profile, EXE);
|
|
42
|
+
if (existsSync(join(dir, "Cargo.toml")) && executable(candidate)) {
|
|
43
|
+
return candidate;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
dir = dirname(dir);
|
|
47
|
+
}
|
|
48
|
+
throw new DevToolMissing("`pollis-app-dev` was not found. It runs your App in the real sandbox for `build` and `test`.\n" +
|
|
49
|
+
"Build it from the Pollis repo with `cargo build --release -p pollis-app-dev`, then put\n" +
|
|
50
|
+
"target/release on your PATH or set POLLIS_APP_DEV_BIN to the binary.");
|
|
51
|
+
}
|
|
52
|
+
/** Run a `pollis-app-dev` subcommand; returns stdout, throws with stderr on failure. */
|
|
53
|
+
export function devTool(bin, args) {
|
|
54
|
+
const result = spawnSync(bin, args, { encoding: "utf8" });
|
|
55
|
+
if (result.error) {
|
|
56
|
+
throw result.error;
|
|
57
|
+
}
|
|
58
|
+
if (result.status !== 0) {
|
|
59
|
+
throw new Error((result.stderr || result.stdout || `pollis-app-dev ${args[0]} failed`).trim());
|
|
60
|
+
}
|
|
61
|
+
return result.stdout.trim();
|
|
62
|
+
}
|
package/dist/errors.d.ts
ADDED
package/dist/errors.js
ADDED
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `pollis`: the Pollis command line for App developers.
|
|
4
|
+
*
|
|
5
|
+
* pollis app new <name> --lang ts|rust a new App: one handler, a test, a README
|
|
6
|
+
* pollis app build compile to dist/app.wasm + dist/pollis-app.json, print the App hash
|
|
7
|
+
* pollis app test build, then run the tests offline in the real sandbox
|
|
8
|
+
* pollis app dev --installation ID development installation: rebuild and sign on save, in pollis-bot
|
|
9
|
+
* pollis app run --installation ID install the build in pollis-bot (prints the App hash) and run it
|
|
10
|
+
*/
|
|
11
|
+
export {};
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `pollis`: the Pollis command line for App developers.
|
|
4
|
+
*
|
|
5
|
+
* pollis app new <name> --lang ts|rust a new App: one handler, a test, a README
|
|
6
|
+
* pollis app build compile to dist/app.wasm + dist/pollis-app.json, print the App hash
|
|
7
|
+
* pollis app test build, then run the tests offline in the real sandbox
|
|
8
|
+
* pollis app dev --installation ID development installation: rebuild and sign on save, in pollis-bot
|
|
9
|
+
* pollis app run --installation ID install the build in pollis-bot (prints the App hash) and run it
|
|
10
|
+
*/
|
|
11
|
+
import { parseArgs } from "node:util";
|
|
12
|
+
import { BuildError, buildApp } from "./build.js";
|
|
13
|
+
import { DevToolMissing } from "./devtool.js";
|
|
14
|
+
import { NewError, newApp } from "./new.js";
|
|
15
|
+
import { devApp, runApp } from "./runtime.js";
|
|
16
|
+
import { testApp } from "./test.js";
|
|
17
|
+
const HELP = `pollis app <command>
|
|
18
|
+
|
|
19
|
+
new <name> --lang ts|rust
|
|
20
|
+
Create an App in TypeScript or Rust: one handler, a test and a README.
|
|
21
|
+
build Compile the App to dist/app.wasm and write dist/pollis-app.json.
|
|
22
|
+
Prints the App hash an admin approves.
|
|
23
|
+
test [files...] Build, then run the App's tests offline in the real sandbox.
|
|
24
|
+
dev --installation ID Run the App in this machine's bot as a development installation:
|
|
25
|
+
prints your developer key (an admin approves it once), then
|
|
26
|
+
rebuilds and signs on every save. Needs pollis-bot.
|
|
27
|
+
run --installation ID Hand the App to this machine's bot and run it. Prints the App hash
|
|
28
|
+
an admin approves on the bot's page. Needs pollis-bot.
|
|
29
|
+
|
|
30
|
+
Options:
|
|
31
|
+
--dir <path> The App's directory (default: the current directory).
|
|
32
|
+
--no-build test: use the existing dist/ instead of building first.
|
|
33
|
+
--sdk <path> new: use the SDK from a Pollis checkout (its sdk/ts directory).
|
|
34
|
+
--artifact <dir|url> run: a built App (a directory or an https:// URL) instead of building this one.
|
|
35
|
+
--data-dir <path> dev, run: pollis-bot's data directory, if not its default.
|
|
36
|
+
|
|
37
|
+
Guide: https://github.com/pollis-app/pollis/blob/main/docs/bots/developer-guide.md`;
|
|
38
|
+
async function main(argv) {
|
|
39
|
+
const { values, positionals } = parseArgs({
|
|
40
|
+
args: argv,
|
|
41
|
+
allowPositionals: true,
|
|
42
|
+
options: {
|
|
43
|
+
dir: { type: "string" },
|
|
44
|
+
lang: { type: "string" },
|
|
45
|
+
sdk: { type: "string" },
|
|
46
|
+
"no-build": { type: "boolean" },
|
|
47
|
+
installation: { type: "string" },
|
|
48
|
+
artifact: { type: "string" },
|
|
49
|
+
"data-dir": { type: "string" },
|
|
50
|
+
help: { type: "boolean", short: "h" },
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
const [group, command, ...rest] = positionals;
|
|
54
|
+
if (values.help || group !== "app" || !command) {
|
|
55
|
+
console.log(HELP);
|
|
56
|
+
return values.help ? 0 : 2;
|
|
57
|
+
}
|
|
58
|
+
const dir = values.dir ?? ".";
|
|
59
|
+
switch (command) {
|
|
60
|
+
case "new": {
|
|
61
|
+
const name = rest[0];
|
|
62
|
+
if (!name) {
|
|
63
|
+
console.error("usage: pollis app new <name> --lang ts|rust");
|
|
64
|
+
return 2;
|
|
65
|
+
}
|
|
66
|
+
if (!values.lang) {
|
|
67
|
+
console.error("say which language with --lang (TypeScript: --lang ts, Rust: --lang rust)");
|
|
68
|
+
return 2;
|
|
69
|
+
}
|
|
70
|
+
const target = newApp({ name, lang: values.lang, parent: values.dir, sdk: values.sdk });
|
|
71
|
+
const next = values.lang === "rust" ? "npx pollis app test" : "pnpm install\n pnpm test";
|
|
72
|
+
console.log(`Created ${target}\n\nNext:\n cd ${name}\n ${next}`);
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
case "build": {
|
|
76
|
+
const built = await buildApp({ dir });
|
|
77
|
+
console.log(`built ${built.manifest.name} ${built.manifest.version}\n` +
|
|
78
|
+
` dist/app.wasm ${(built.bytes / 1024 / 1024).toFixed(1)} MB\n` +
|
|
79
|
+
` dist/pollis-app.json\n` +
|
|
80
|
+
`App hash: ${built.appHash}`);
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
83
|
+
case "test":
|
|
84
|
+
return testApp({ dir, build: !values["no-build"], patterns: rest });
|
|
85
|
+
case "dev":
|
|
86
|
+
return devApp({ dir, installation: values.installation, dataDir: values["data-dir"] });
|
|
87
|
+
case "run":
|
|
88
|
+
return runApp({ dir, installation: values.installation, dataDir: values["data-dir"], artifact: values.artifact });
|
|
89
|
+
default:
|
|
90
|
+
console.error(`unknown command \`pollis app ${command}\`\n\n${HELP}`);
|
|
91
|
+
return 2;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
main(process.argv.slice(2)).then((code) => {
|
|
95
|
+
process.exitCode = code;
|
|
96
|
+
}, (e) => {
|
|
97
|
+
if (e instanceof BuildError || e instanceof NewError || e instanceof DevToolMissing) {
|
|
98
|
+
console.error(`error: ${e.message}`);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
console.error(e);
|
|
102
|
+
}
|
|
103
|
+
process.exitCode = 1;
|
|
104
|
+
});
|
package/dist/new.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `pollis app new <name> --lang ts|rust`: a new App from the template: one
|
|
3
|
+
* handler, a test and a README.
|
|
4
|
+
*/
|
|
5
|
+
export declare class NewError extends Error {
|
|
6
|
+
}
|
|
7
|
+
export interface NewOptions {
|
|
8
|
+
name: string;
|
|
9
|
+
lang: string;
|
|
10
|
+
/** Parent directory. Default: the current directory. */
|
|
11
|
+
parent?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Use local copies of the SDK packages (a path to the Pollis repo's
|
|
14
|
+
* `sdk/ts`), for working before or beside the npm and crates.io releases.
|
|
15
|
+
*/
|
|
16
|
+
sdk?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare function newApp(options: NewOptions): string;
|