@parall/cli 1.73.0 → 1.75.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/dist/commands/apps-add.d.ts +6 -0
- package/dist/commands/apps-add.d.ts.map +1 -0
- package/dist/commands/apps-add.js +93 -0
- package/dist/commands/apps-blocks.d.ts +96 -0
- package/dist/commands/apps-blocks.d.ts.map +1 -0
- package/dist/commands/apps-blocks.js +196 -0
- package/dist/commands/apps-page-sdk.generated.d.ts +1 -1
- package/dist/commands/apps-page-sdk.generated.d.ts.map +1 -1
- package/dist/commands/apps-page-sdk.generated.js +1 -1
- package/dist/commands/apps-scaffold-actions.d.ts +4 -9
- package/dist/commands/apps-scaffold-actions.d.ts.map +1 -1
- package/dist/commands/apps-scaffold-actions.js +4 -162
- package/dist/commands/apps-scaffold.d.ts +5 -4
- package/dist/commands/apps-scaffold.d.ts.map +1 -1
- package/dist/commands/apps-scaffold.js +24 -144
- package/dist/commands/apps-template.d.ts +15 -0
- package/dist/commands/apps-template.d.ts.map +1 -0
- package/dist/commands/apps-template.js +105 -0
- package/dist/commands/apps-templates.gen.d.ts +5 -0
- package/dist/commands/apps-templates.gen.d.ts.map +1 -0
- package/dist/commands/apps-templates.gen.js +540 -0
- package/dist/commands/apps.d.ts +5 -4
- package/dist/commands/apps.d.ts.map +1 -1
- package/dist/commands/apps.js +7 -4
- package/dist/lib/edge-exec.d.ts +1 -1
- package/dist/lib/edge-exec.js +1 -1
- package/dist/lib/output.d.ts +11 -0
- package/dist/lib/output.d.ts.map +1 -1
- package/dist/lib/output.js +40 -4
- package/package.json +25 -5
|
@@ -1,163 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* actions/parall-actions.ts (ts/miniapp-sdk/src/actions.ts, embedded at
|
|
6
|
-
* build time), a second Vite build that writes dist/_actions/index.js —
|
|
7
|
-
* one unminified ES module with a source map, whose only import is
|
|
8
|
-
* cloudflare:workers — miniflare for `parall app action run --local`, and
|
|
9
|
-
* a page that calls the example action. */
|
|
10
|
-
export function withActions(files) {
|
|
11
|
-
const out = files.map(([rel, content]) => {
|
|
12
|
-
if (rel === 'src/App.tsx')
|
|
13
|
-
return [rel, exampleApp(content)];
|
|
14
|
-
if (rel === 'package.json') {
|
|
15
|
-
const pkg = JSON.parse(content);
|
|
16
|
-
pkg.scripts.build = 'vite build && vite build --config vite.actions.config.ts';
|
|
17
|
-
pkg.devDependencies.miniflare = MINIFLARE_RANGE;
|
|
18
|
-
return [rel, `${JSON.stringify(pkg, null, 2)}\n`];
|
|
19
|
-
}
|
|
20
|
-
if (rel === 'public/manifest.json') {
|
|
21
|
-
const manifest = JSON.parse(content);
|
|
22
|
-
manifest.actions = {
|
|
23
|
-
entry: '_actions/index.js',
|
|
24
|
-
functions: { whoami: { kind: 'read' } },
|
|
25
|
-
};
|
|
26
|
-
return [rel, `${JSON.stringify(manifest, null, 2)}\n`];
|
|
27
|
-
}
|
|
28
|
-
if (rel === 'AGENTS.md')
|
|
29
|
-
return [rel, content + ACTIONS_AGENTS_NOTES];
|
|
30
|
-
return [rel, content];
|
|
31
|
-
});
|
|
32
|
-
return [
|
|
33
|
-
...out,
|
|
34
|
-
['actions/index.ts', EXAMPLE_ACTIONS],
|
|
35
|
-
['actions/parall-actions.ts', VENDORED_HEADER + ACTIONS_SDK_SOURCE],
|
|
36
|
-
['actions/cloudflare-workers.d.ts', CLOUDFLARE_WORKERS_SHIM],
|
|
37
|
-
['actions/tsconfig.json', ACTIONS_TSCONFIG],
|
|
38
|
-
['vite.actions.config.ts', VITE_ACTIONS_CONFIG],
|
|
39
|
-
];
|
|
40
|
-
}
|
|
41
|
-
// workerd with the worker_loaders binding the local runner needs.
|
|
1
|
+
/** The miniflare range the `--actions` scaffold installs: workerd with the
|
|
2
|
+
* worker_loaders binding the local runner needs (apps-action-local.ts).
|
|
3
|
+
* ts/cli/templates/mini-app/with-actions/package.json pins the same range;
|
|
4
|
+
* test/apps-init.test.mjs keeps the two equal. */
|
|
42
5
|
export const MINIFLARE_RANGE = '^4.20260415.0';
|
|
43
|
-
/** The page of an --actions scaffold: the plain page's heading, then the
|
|
44
|
-
* example action read through the bridge, read again on data changes. */
|
|
45
|
-
function exampleApp(plain) {
|
|
46
|
-
const heading = /<h1>.*<\/h1>/.exec(plain)?.[0] ?? '<h1>Mini App</h1>';
|
|
47
|
-
return `import { useCallback, useEffect, useState } from 'react';
|
|
48
|
-
import { parall, type ReadyResult } from './parall';
|
|
49
|
-
|
|
50
|
-
type Whoami = { viewer: string; role?: string; server_time: string };
|
|
51
|
-
|
|
52
|
-
export function App() {
|
|
53
|
-
const [host, setHost] = useState<ReadyResult | null>(null);
|
|
54
|
-
const [me, setMe] = useState<Whoami | null>(null);
|
|
55
|
-
const [error, setError] = useState<string | null>(null);
|
|
56
|
-
|
|
57
|
-
// Actions run on the server as the viewer. Read again whenever the
|
|
58
|
-
// app's Database has new writes.
|
|
59
|
-
const load = useCallback(() => {
|
|
60
|
-
parall
|
|
61
|
-
.action<Whoami>('whoami')
|
|
62
|
-
.then(setMe)
|
|
63
|
-
.catch((err: Error) => setError(err.message));
|
|
64
|
-
}, []);
|
|
65
|
-
|
|
66
|
-
useEffect(() => {
|
|
67
|
-
parall
|
|
68
|
-
.ready()
|
|
69
|
-
.then((result) => {
|
|
70
|
-
setHost(result);
|
|
71
|
-
load();
|
|
72
|
-
})
|
|
73
|
-
.catch((err: Error) => setError(err.message));
|
|
74
|
-
return parall.onDataChanged(load);
|
|
75
|
-
}, [load]);
|
|
76
|
-
|
|
77
|
-
return (
|
|
78
|
-
<main style={{ fontFamily: 'system-ui', padding: 24 }}>
|
|
79
|
-
${heading}
|
|
80
|
-
{host ? <p>Hello, {host.viewer.display_name}.</p> : <p>Waiting for the host…</p>}
|
|
81
|
-
{me ? (
|
|
82
|
-
<p>
|
|
83
|
-
The server time is {me.server_time} (your role: {me.role ?? 'member'}).
|
|
84
|
-
</p>
|
|
85
|
-
) : null}
|
|
86
|
-
{error ? <p>Error: {error}</p> : null}
|
|
87
|
-
</main>
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
`;
|
|
91
|
-
}
|
|
92
|
-
const VENDORED_HEADER = `// Vendored copy of @parall/miniapp-sdk/actions, written by
|
|
93
|
-
// \`parall app init --actions\`. Do not edit; replace it with the npm package
|
|
94
|
-
// once that ships.
|
|
95
|
-
`;
|
|
96
|
-
const EXAMPLE_ACTIONS = `import { ActionError, defineActions } from './parall-actions';
|
|
97
|
-
|
|
98
|
-
// Each action is declared in public/manifest.json under actions.functions
|
|
99
|
-
// (kind read | write, params schema) and implemented here; \`parall app check\`
|
|
100
|
-
// compares the two. The loader looks the entry up by the name ActionRunner.
|
|
101
|
-
export const ActionRunner = defineActions({
|
|
102
|
-
// A read action may only query. ctx.viewer is the caller, as the platform
|
|
103
|
-
// vouches for it: use it for row-level rules.
|
|
104
|
-
async whoami(ctx) {
|
|
105
|
-
const { rows } = await ctx.db.query('SELECT now() AS server_time');
|
|
106
|
-
if (rows.length === 0) throw new ActionError('the database gave no time');
|
|
107
|
-
return { viewer: ctx.viewer.display_name, role: ctx.viewer.role, server_time: rows[0].server_time };
|
|
108
|
-
},
|
|
109
|
-
});
|
|
110
|
-
`;
|
|
111
|
-
const CLOUDFLARE_WORKERS_SHIM = `// The Workers runtime module the action wrapper imports; the runtime
|
|
112
|
-
// provides it. Declared only as far as parall-actions.ts uses it.
|
|
113
|
-
declare module 'cloudflare:workers' {
|
|
114
|
-
export abstract class WorkerEntrypoint<Env = unknown> {
|
|
115
|
-
protected readonly ctx: unknown;
|
|
116
|
-
protected readonly env: Env;
|
|
117
|
-
constructor(ctx: unknown, env: Env);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
`;
|
|
121
|
-
const ACTIONS_TSCONFIG = `${JSON.stringify({
|
|
122
|
-
compilerOptions: {
|
|
123
|
-
target: 'ES2022',
|
|
124
|
-
module: 'ES2022',
|
|
125
|
-
moduleResolution: 'bundler',
|
|
126
|
-
lib: ['ES2022'],
|
|
127
|
-
strict: true,
|
|
128
|
-
noEmit: true,
|
|
129
|
-
skipLibCheck: true,
|
|
130
|
-
},
|
|
131
|
-
include: ['.'],
|
|
132
|
-
}, null, 2)}\n`;
|
|
133
|
-
const VITE_ACTIONS_CONFIG = `import { defineConfig } from 'vite';
|
|
134
|
-
|
|
135
|
-
// The action module: one ES module file in dist/_actions/, built after the
|
|
136
|
-
// page (which empties dist/). Not minified and with a source map, so
|
|
137
|
-
// \`parall app action run\` can map a stack back to actions/*.ts. The only
|
|
138
|
-
// import left is cloudflare:workers, which the Workers runtime provides.
|
|
139
|
-
export default defineConfig({
|
|
140
|
-
publicDir: false,
|
|
141
|
-
build: {
|
|
142
|
-
outDir: 'dist/_actions',
|
|
143
|
-
emptyOutDir: false,
|
|
144
|
-
minify: false,
|
|
145
|
-
sourcemap: true,
|
|
146
|
-
target: 'es2022',
|
|
147
|
-
lib: { entry: 'actions/index.ts', formats: ['es'], fileName: () => 'index.js' },
|
|
148
|
-
rollupOptions: { external: ['cloudflare:workers'] },
|
|
149
|
-
},
|
|
150
|
-
});
|
|
151
|
-
`;
|
|
152
|
-
const ACTIONS_AGENTS_NOTES = `
|
|
153
|
-
## Actions(服务端函数)
|
|
154
|
-
|
|
155
|
-
- \`actions/index.ts\` 实现,\`public/manifest.json\` 的 \`actions.functions\` 声明(\`kind\`: read 只能查询 / write 可写;\`params\` 是 JSON Schema 子集)。两边要一一对应:\`parall app check dist\` 核对,\`publish\` 也会先跑。
|
|
156
|
-
- \`ctx.viewer\` 是平台担保的调用者,行级规则写在 SQL 里(例:\`WHERE created_by = $1\` 传 \`ctx.viewer.id\`)。能打开 app 的人都能调全部 action。
|
|
157
|
-
- 给调用者看的错误抛 \`ActionError\`;其他异常只有作者与 owner / admin 看得到完整消息与堆栈。写类调用的参数记进全组织可查的调用记录:不要经参数传凭据。
|
|
158
|
-
- 没有网络、没有文件系统;模块级变量在调用者之间共享,不能存调用者的状态。每次 \`ctx.db\` 调用都是一次往返,能合并就合并。
|
|
159
|
-
- 调试:改完 \`pnpm build\` 后 \`parall app action run <app_…> whoami --local\` 在本机 workerd 里跑本地构建,不用先发布;它连的是 app 的真实 Database,**写会真的生效**,\`console.log\` 打到 stderr。出错时堆栈按本地 source map 映射回 \`actions/*.ts\`。要在线上运行时里试,\`parall app publish dist --app <app_…> --preview\` 后 \`parall app action run <app_…> whoami --v <seq>\`。调用记录 \`parall app action log <app_…>\`,执行过的 SQL \`parall app db log --app <app_…>\`(加 \`--invocation <aci_… 或 acd_…>\` 只看一次调用;本机运行的写入版本号为空)。
|
|
160
|
-
- 页面连本地 action:\`parall app dev --dist dist --app <app_…>\`,打开 CLI 打印的地址(带令牌)。页面调的 action 在本机运行,同样**写会真的生效**;右侧列出每次调用、结果与 \`console\` 输出,终端也各打一行。改完重新 \`pnpm build\` 即可,不用重启。
|
|
161
|
-
- 页面用 \`parall.action('whoami')\` 调(\`src/App.tsx\` 是例子)。失败时 \`err.code\` 是服务端的错误码:\`ACTION_FAILED\`(消息是 \`ActionError\` 的原文)、\`INVALID_ACTION_PARAMS\`、\`ACTION_TIMEOUT\`(写类可能已生效)、\`APP_VERSION_CHANGED\`(app 已更新,宿主会提示看的人重新加载);\`HOST_TIMEOUT\` / \`HOST_UNAVAILABLE\` 是没拿到答复,写类先重读再决定是否重试。\`parall.onDataChanged(fn)\` 在 Database 有新写入时调 fn,在里面重读。
|
|
162
|
-
- \`actions/parall-actions.ts\` 是 SDK 的 vendored 副本,不要改。
|
|
163
|
-
`;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { Command } from 'commander';
|
|
2
|
-
/** `parall app init`: the
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
/** `parall app init`: the scaffold of design §7.4 — Vite + React + TS +
|
|
3
|
+
* Tailwind + shadcn/ui with the host bridge wired (apps-template.ts);
|
|
4
|
+
* `--actions` adds the server-side actions, the schema and an example list
|
|
5
|
+
* that reads and writes through them. Blocks go on top with `parall app add`
|
|
6
|
+
* (apps-add.ts). */
|
|
6
7
|
export declare function registerAppScaffoldCommands(app: Command): void;
|
|
7
8
|
//# sourceMappingURL=apps-scaffold.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apps-scaffold.d.ts","sourceRoot":"","sources":["../../src/commands/apps-scaffold.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"apps-scaffold.d.ts","sourceRoot":"","sources":["../../src/commands/apps-scaffold.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIzC;;;;oBAIoB;AACpB,wBAAgB,2BAA2B,CAAC,GAAG,EAAE,OAAO,QAuDvD"}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { promises as fs } from 'node:fs';
|
|
2
2
|
import { join, resolve, sep } from 'node:path';
|
|
3
3
|
import { printError } from '../lib/output.js';
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
4
|
+
import { scaffoldProject } from './apps-template.js';
|
|
5
|
+
/** `parall app init`: the scaffold of design §7.4 — Vite + React + TS +
|
|
6
|
+
* Tailwind + shadcn/ui with the host bridge wired (apps-template.ts);
|
|
7
|
+
* `--actions` adds the server-side actions, the schema and an example list
|
|
8
|
+
* that reads and writes through them. Blocks go on top with `parall app add`
|
|
9
|
+
* (apps-add.ts). */
|
|
10
10
|
export function registerAppScaffoldCommands(app) {
|
|
11
11
|
app
|
|
12
12
|
.command('init')
|
|
13
|
-
.description('Scaffold a new Mini App project (Vite + React + TS, bridge wired)')
|
|
13
|
+
.description('Scaffold a new Mini App project (Vite + React + TS + Tailwind + shadcn/ui, bridge wired)')
|
|
14
14
|
.argument('<dir>', 'Directory to create (must be empty or absent)')
|
|
15
|
-
.option('--actions', 'Also scaffold server-side actions (actions/ → dist/_actions/index.js)')
|
|
15
|
+
.option('--actions', 'Also scaffold server-side actions, the schema and an example list wired to them (actions/ → dist/_actions/index.js)')
|
|
16
16
|
.action(async (dir, opts) => {
|
|
17
17
|
try {
|
|
18
18
|
const out = resolve(dir);
|
|
@@ -21,7 +21,7 @@ export function registerAppScaffoldCommands(app) {
|
|
|
21
21
|
throw new Error(`${out} is not empty`);
|
|
22
22
|
}
|
|
23
23
|
const name = out.split(sep).pop() ?? 'mini-app';
|
|
24
|
-
const files =
|
|
24
|
+
const files = scaffoldProject({ name, actions: Boolean(opts.actions) });
|
|
25
25
|
for (const [rel, content] of files) {
|
|
26
26
|
const target = join(out, rel);
|
|
27
27
|
await fs.mkdir(resolve(target, '..'), { recursive: true });
|
|
@@ -32,12 +32,24 @@ export function registerAppScaffoldCommands(app) {
|
|
|
32
32
|
'',
|
|
33
33
|
'Next steps:',
|
|
34
34
|
` cd ${dir} && pnpm install && pnpm build`,
|
|
35
|
-
...(opts.actions ? [' parall app check dist # manifest ↔ actions/'] : []),
|
|
36
35
|
` parall app create --name "${name}"`,
|
|
36
|
+
...(opts.actions
|
|
37
|
+
? [
|
|
38
|
+
' parall app db exec --app <app_…> --file db/schema.sql # create the tables',
|
|
39
|
+
' parall app check dist # manifest ↔ actions/',
|
|
40
|
+
]
|
|
41
|
+
: []),
|
|
37
42
|
` parall app publish dist --app <app_…> --source .`,
|
|
43
|
+
// `app dev` runs until stopped: an agent starts it in the background.
|
|
38
44
|
opts.actions
|
|
39
|
-
? ` parall app dev --dist dist --app <app_…> #
|
|
40
|
-
: ` parall app dev --dist dist
|
|
45
|
+
? ` parall app dev --dist dist --app <app_…> > dev.log 2>&1 & # runs until stopped; actions run on this machine`
|
|
46
|
+
: ` parall app dev --dist dist > dev.log 2>&1 & # runs until stopped; look at it locally first`,
|
|
47
|
+
...(opts.actions
|
|
48
|
+
? [` parall app action run <app_…> <name> --local # one action, then returns`]
|
|
49
|
+
: []),
|
|
50
|
+
...(opts.actions
|
|
51
|
+
? ['', 'Ready-made pages (calendar, kanban, …): parall app add --list']
|
|
52
|
+
: []),
|
|
41
53
|
].join('\n'));
|
|
42
54
|
}
|
|
43
55
|
catch (err) {
|
|
@@ -45,135 +57,3 @@ export function registerAppScaffoldCommands(app) {
|
|
|
45
57
|
}
|
|
46
58
|
});
|
|
47
59
|
}
|
|
48
|
-
function scaffoldFiles(name) {
|
|
49
|
-
return [
|
|
50
|
-
[
|
|
51
|
-
'package.json',
|
|
52
|
-
`${JSON.stringify({
|
|
53
|
-
name,
|
|
54
|
-
private: true,
|
|
55
|
-
type: 'module',
|
|
56
|
-
scripts: { dev: 'vite', build: 'vite build' },
|
|
57
|
-
dependencies: { react: '^19.1.0', 'react-dom': '^19.1.0' },
|
|
58
|
-
devDependencies: {
|
|
59
|
-
'@types/react': '^19.1.2',
|
|
60
|
-
'@types/react-dom': '^19.1.2',
|
|
61
|
-
'@vitejs/plugin-react': '^4.4.1',
|
|
62
|
-
typescript: '^5.8.3',
|
|
63
|
-
vite: '^6.3.5',
|
|
64
|
-
},
|
|
65
|
-
}, null, 2)}\n`,
|
|
66
|
-
],
|
|
67
|
-
[
|
|
68
|
-
'vite.config.ts',
|
|
69
|
-
`import react from '@vitejs/plugin-react';
|
|
70
|
-
import { defineConfig } from 'vite';
|
|
71
|
-
|
|
72
|
-
// Everything must land inside the bundle: the app origin's CSP only allows
|
|
73
|
-
// 'self' for scripts, styles and fonts (no CDN imports, no external URLs).
|
|
74
|
-
export default defineConfig({
|
|
75
|
-
plugins: [react()],
|
|
76
|
-
base: './',
|
|
77
|
-
build: { outDir: 'dist' },
|
|
78
|
-
});
|
|
79
|
-
`,
|
|
80
|
-
],
|
|
81
|
-
[
|
|
82
|
-
'tsconfig.json',
|
|
83
|
-
`${JSON.stringify({
|
|
84
|
-
compilerOptions: {
|
|
85
|
-
target: 'ES2022',
|
|
86
|
-
module: 'ES2022',
|
|
87
|
-
moduleResolution: 'bundler',
|
|
88
|
-
jsx: 'react-jsx',
|
|
89
|
-
lib: ['ES2022', 'DOM'],
|
|
90
|
-
strict: true,
|
|
91
|
-
noEmit: true,
|
|
92
|
-
skipLibCheck: true,
|
|
93
|
-
},
|
|
94
|
-
include: ['src'],
|
|
95
|
-
}, null, 2)}\n`,
|
|
96
|
-
],
|
|
97
|
-
[
|
|
98
|
-
'index.html',
|
|
99
|
-
`<!doctype html>
|
|
100
|
-
<html lang="en">
|
|
101
|
-
<head>
|
|
102
|
-
<meta charset="UTF-8" />
|
|
103
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
104
|
-
<title>${name}</title>
|
|
105
|
-
</head>
|
|
106
|
-
<body>
|
|
107
|
-
<div id="root"></div>
|
|
108
|
-
<script type="module" src="./src/main.tsx"></script>
|
|
109
|
-
</body>
|
|
110
|
-
</html>
|
|
111
|
-
`,
|
|
112
|
-
],
|
|
113
|
-
[
|
|
114
|
-
'public/manifest.json',
|
|
115
|
-
`${JSON.stringify({ entry: 'index.html', sdk_version: '0.1.0', capabilities: [] }, null, 2)}\n`,
|
|
116
|
-
],
|
|
117
|
-
[
|
|
118
|
-
'src/main.tsx',
|
|
119
|
-
`import { StrictMode } from 'react';
|
|
120
|
-
import { createRoot } from 'react-dom/client';
|
|
121
|
-
import { App } from './App';
|
|
122
|
-
|
|
123
|
-
createRoot(document.getElementById('root') as HTMLElement).render(
|
|
124
|
-
<StrictMode>
|
|
125
|
-
<App />
|
|
126
|
-
</StrictMode>,
|
|
127
|
-
);
|
|
128
|
-
`,
|
|
129
|
-
],
|
|
130
|
-
[
|
|
131
|
-
'src/App.tsx',
|
|
132
|
-
`import { useEffect, useState } from 'react';
|
|
133
|
-
import { parall, type ReadyResult } from './parall';
|
|
134
|
-
|
|
135
|
-
export function App() {
|
|
136
|
-
const [host, setHost] = useState<ReadyResult | null>(null);
|
|
137
|
-
const [error, setError] = useState<string | null>(null);
|
|
138
|
-
useEffect(() => {
|
|
139
|
-
parall
|
|
140
|
-
.ready()
|
|
141
|
-
.then(setHost)
|
|
142
|
-
.catch((err: Error) => setError(err.message));
|
|
143
|
-
}, []);
|
|
144
|
-
return (
|
|
145
|
-
<main style={{ fontFamily: 'system-ui', padding: 24 }}>
|
|
146
|
-
<h1>${name}</h1>
|
|
147
|
-
{host ? (
|
|
148
|
-
<p>
|
|
149
|
-
Hello, {host.viewer.display_name} — you are looking at version {host.app.version_seq} of{' '}
|
|
150
|
-
{host.app.name}.
|
|
151
|
-
</p>
|
|
152
|
-
) : error ? (
|
|
153
|
-
<p>Bridge error: {error}</p>
|
|
154
|
-
) : (
|
|
155
|
-
<p>Waiting for the host…</p>
|
|
156
|
-
)}
|
|
157
|
-
</main>
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
`,
|
|
161
|
-
],
|
|
162
|
-
['src/parall.ts', PAGE_VENDORED_HEADER + PAGE_SDK_SOURCE],
|
|
163
|
-
[
|
|
164
|
-
'AGENTS.md',
|
|
165
|
-
`# ${name} — Mini App 约定
|
|
166
|
-
|
|
167
|
-
- 这是一个 Parall Mini App:纯静态 bundle,所有脚本 / 样式 / 字体都必须打进 dist(app origin 的 CSP 只放 'self',任何外部 URL 引用会在 publish 验收时被拒)。
|
|
168
|
-
- \`public/manifest.json\` 随构建进入 dist 根部;entry 固定 index.html。
|
|
169
|
-
- 页面的数据只经 action:\`parall.action(name, params)\` 以看页面的人的身份调本 app 的服务端 action(\`parall app init --actions\` 生成 actions/),\`parall.onDataChanged(fn)\` 在 Database 有新写入时通知页面重新读。桥上没有 SQL、files、clip 方法。
|
|
170
|
-
- 流程:\`pnpm build\` → \`parall app publish dist --app <app_…> --source .\` → 在 Parall 里打开;先 \`parall app dev --dist dist\` 本地看一眼(不带 \`--app\` 时只托管页面、不跑 action)。
|
|
171
|
-
- \`src/parall.ts\` 是 @parall/miniapp-sdk 的 vendored 副本(npm 发布前);不要改。
|
|
172
|
-
`,
|
|
173
|
-
],
|
|
174
|
-
['.gitignore', 'node_modules\ndist\n'],
|
|
175
|
-
];
|
|
176
|
-
}
|
|
177
|
-
const PAGE_VENDORED_HEADER = `// Vendored copy of @parall/miniapp-sdk, written by \`parall app init\`. Do
|
|
178
|
-
// not edit; replace it with the npm package once that ships.
|
|
179
|
-
`;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface ScaffoldOptions {
|
|
2
|
+
/** The app's display name (the directory name). */
|
|
3
|
+
name: string;
|
|
4
|
+
/** Include actions, the schema and the example list. */
|
|
5
|
+
actions: boolean;
|
|
6
|
+
}
|
|
7
|
+
/** The project's files, sorted by path. */
|
|
8
|
+
export declare function scaffoldProject(options: ScaffoldOptions): Array<[string, string]>;
|
|
9
|
+
/** An npm-valid package name from a directory name ("My App" → "my-app"). */
|
|
10
|
+
export declare function packageName(name: string): string;
|
|
11
|
+
/** Objects merge key by key; anything else (arrays included) is replaced. */
|
|
12
|
+
export declare function mergeJSON(base: unknown, over: unknown): unknown;
|
|
13
|
+
/** The same object with its keys in sorted order (package.json style). */
|
|
14
|
+
export declare function sortKeys<T>(record: Record<string, T>): Record<string, T>;
|
|
15
|
+
//# sourceMappingURL=apps-template.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apps-template.d.ts","sourceRoot":"","sources":["../../src/commands/apps-template.ts"],"names":[],"mappings":"AA4BA,MAAM,WAAW,eAAe;IAC9B,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAC;IACb,wDAAwD;IACxD,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,2CAA2C;AAC3C,wBAAgB,eAAe,CAAC,OAAO,EAAE,eAAe,GAAG,KAAK,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAsBjF;AAMD,6EAA6E;AAC7E,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAOhD;AA2BD,6EAA6E;AAC7E,wBAAgB,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAO/D;AAED,0EAA0E;AAC1E,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAIxE"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { ACTIONS_SDK_SOURCE } from './apps-actions-sdk.generated.js';
|
|
2
|
+
import { PAGE_SDK_SOURCE } from './apps-page-sdk.generated.js';
|
|
3
|
+
import { TEMPLATE_BASE, TEMPLATE_WITH_ACTIONS } from './apps-templates.gen.js';
|
|
4
|
+
/** What `parall app init` writes (design §7.4): the base template — Vite +
|
|
5
|
+
* React + TypeScript + Tailwind + shadcn/ui, a responsive app shell, light
|
|
6
|
+
* and dark themes, loading / empty / error states and typed action hooks —
|
|
7
|
+
* and, with --actions, the server-side actions, the schema and an example
|
|
8
|
+
* list wired to them. The sources live in ts/cli/templates/mini-app and are
|
|
9
|
+
* embedded by scripts/generate-app-templates.mjs (the build checks the
|
|
10
|
+
* embedded copy is current); the two SDK halves are vendored from
|
|
11
|
+
* ts/miniapp-sdk until it is on npm. */
|
|
12
|
+
const PAGE_VENDORED_HEADER = `// Vendored copy of @parall/miniapp-sdk, written by \`parall app init\`. Do
|
|
13
|
+
// not edit; replace it with the npm package once that ships.
|
|
14
|
+
`;
|
|
15
|
+
const ACTIONS_VENDORED_HEADER = `// Vendored copy of @parall/miniapp-sdk/actions, written by
|
|
16
|
+
// \`parall app init --actions\`. Do not edit; replace it with the npm package
|
|
17
|
+
// once that ships.
|
|
18
|
+
`;
|
|
19
|
+
// Not __APP_NAME__: Markdown reads that as bold.
|
|
20
|
+
const NAME_PLACEHOLDER = /\{\{APP_NAME\}\}/g;
|
|
21
|
+
// Template files whose name a package would treat specially are stored
|
|
22
|
+
// under another name and renamed when written.
|
|
23
|
+
const RENAMES = { _gitignore: '.gitignore' };
|
|
24
|
+
/** The project's files, sorted by path. */
|
|
25
|
+
export function scaffoldProject(options) {
|
|
26
|
+
const files = new Map(Object.entries(TEMPLATE_BASE));
|
|
27
|
+
if (options.actions)
|
|
28
|
+
applyOverlay(files, TEMPLATE_WITH_ACTIONS);
|
|
29
|
+
files.set('src/parall.ts', PAGE_VENDORED_HEADER + PAGE_SDK_SOURCE);
|
|
30
|
+
if (options.actions) {
|
|
31
|
+
files.set('actions/parall-actions.ts', ACTIONS_VENDORED_HEADER + ACTIONS_SDK_SOURCE);
|
|
32
|
+
}
|
|
33
|
+
const pkg = JSON.parse(files.get('package.json') ?? '{}');
|
|
34
|
+
pkg.name = packageName(options.name);
|
|
35
|
+
for (const field of ['dependencies', 'devDependencies']) {
|
|
36
|
+
if (pkg[field])
|
|
37
|
+
pkg[field] = sortKeys(pkg[field]);
|
|
38
|
+
}
|
|
39
|
+
files.set('package.json', `${JSON.stringify(pkg, null, 2)}\n`);
|
|
40
|
+
const out = [];
|
|
41
|
+
for (const [path, content] of files) {
|
|
42
|
+
const target = RENAMES[path] ?? path;
|
|
43
|
+
const name = path.endsWith('.html') ? escapeHtml(options.name) : options.name;
|
|
44
|
+
// A function, so a `$&` in the name is not a replacement pattern.
|
|
45
|
+
out.push([target, content.replace(NAME_PLACEHOLDER, () => name)]);
|
|
46
|
+
}
|
|
47
|
+
return out.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
48
|
+
}
|
|
49
|
+
function escapeHtml(text) {
|
|
50
|
+
return text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
51
|
+
}
|
|
52
|
+
/** An npm-valid package name from a directory name ("My App" → "my-app"). */
|
|
53
|
+
export function packageName(name) {
|
|
54
|
+
const cleaned = name
|
|
55
|
+
.toLowerCase()
|
|
56
|
+
.replace(/[^a-z0-9._-]+/g, '-')
|
|
57
|
+
.replace(/^[._-]+|[-]+$/g, '')
|
|
58
|
+
.slice(0, 214);
|
|
59
|
+
return cleaned || 'mini-app';
|
|
60
|
+
}
|
|
61
|
+
/** Applies the --actions overlay: _overlay.json's remove list drops base
|
|
62
|
+
* paths (a directory drops everything under it), JSON files present in both
|
|
63
|
+
* are deep-merged, AGENTS.md is appended, anything else is added or
|
|
64
|
+
* replaced. */
|
|
65
|
+
function applyOverlay(files, overlay) {
|
|
66
|
+
const meta = JSON.parse(overlay['_overlay.json'] ?? '{}');
|
|
67
|
+
for (const prefix of meta.remove ?? []) {
|
|
68
|
+
for (const path of [...files.keys()]) {
|
|
69
|
+
if (path === prefix || path.startsWith(`${prefix}/`))
|
|
70
|
+
files.delete(path);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (const [path, content] of Object.entries(overlay)) {
|
|
74
|
+
if (path === '_overlay.json')
|
|
75
|
+
continue;
|
|
76
|
+
const current = files.get(path);
|
|
77
|
+
if (current !== undefined && path === 'AGENTS.md') {
|
|
78
|
+
files.set(path, `${current.trimEnd()}\n\n${content}`);
|
|
79
|
+
}
|
|
80
|
+
else if (current !== undefined && path.endsWith('.json')) {
|
|
81
|
+
const merged = mergeJSON(JSON.parse(current), JSON.parse(content));
|
|
82
|
+
files.set(path, `${JSON.stringify(merged, null, 2)}\n`);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
files.set(path, content);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Objects merge key by key; anything else (arrays included) is replaced. */
|
|
90
|
+
export function mergeJSON(base, over) {
|
|
91
|
+
if (!isPlainObject(base) || !isPlainObject(over))
|
|
92
|
+
return over;
|
|
93
|
+
const out = { ...base };
|
|
94
|
+
for (const [key, value] of Object.entries(over)) {
|
|
95
|
+
out[key] = key in base ? mergeJSON(base[key], value) : value;
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
/** The same object with its keys in sorted order (package.json style). */
|
|
100
|
+
export function sortKeys(record) {
|
|
101
|
+
return Object.fromEntries(Object.entries(record).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
102
|
+
}
|
|
103
|
+
function isPlainObject(value) {
|
|
104
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
105
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { RegistryItem } from './apps-blocks.js';
|
|
2
|
+
export declare const TEMPLATE_BASE: Readonly<Record<string, string>>;
|
|
3
|
+
export declare const TEMPLATE_WITH_ACTIONS: Readonly<Record<string, string>>;
|
|
4
|
+
export declare const TEMPLATE_BLOCKS: readonly RegistryItem[];
|
|
5
|
+
//# sourceMappingURL=apps-templates.gen.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apps-templates.gen.d.ts","sourceRoot":"","sources":["../../src/commands/apps-templates.gen.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAErD,eAAO,MAAM,aAAa,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CA0D1D,CAAC;AAEF,eAAO,MAAM,qBAAqB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAsBlE,CAAC;AAEF,eAAO,MAAM,eAAe,EAAE,SAAS,YAAY,EAyclD,CAAC"}
|