@parall/cli 1.74.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-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 -163
- 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/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
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Command } from 'commander';
|
|
2
|
+
/** `parall app add <block…>` copies @parall blocks into a Mini App project
|
|
3
|
+
* (apps-blocks.ts). It writes local files only: the block's tables are
|
|
4
|
+
* created by the author with `parall app db exec --file`, as printed. */
|
|
5
|
+
export declare function registerAppAddCommand(app: Command): void;
|
|
6
|
+
//# sourceMappingURL=apps-add.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apps-add.d.ts","sourceRoot":"","sources":["../../src/commands/apps-add.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAYzC;;yEAEyE;AACzE,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,OAAO,QAkEjD"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { relative, resolve } from 'node:path';
|
|
2
|
+
import { printError, printJson } from '../lib/output.js';
|
|
3
|
+
import { applyBlock, blockNames, conflictMessage, findBlock, listBlocks, planBlock, } from './apps-blocks.js';
|
|
4
|
+
/** `parall app add <block…>` copies @parall blocks into a Mini App project
|
|
5
|
+
* (apps-blocks.ts). It writes local files only: the block's tables are
|
|
6
|
+
* created by the author with `parall app db exec --file`, as printed. */
|
|
7
|
+
export function registerAppAddCommand(app) {
|
|
8
|
+
app
|
|
9
|
+
.command('add')
|
|
10
|
+
.description('Add ready-made blocks (calendar, kanban, …) to an `app init --actions` project: pages, actions, SQL and manifest declarations')
|
|
11
|
+
.argument('[blocks...]', 'Blocks to add (see --list)')
|
|
12
|
+
.option('--dir <dir>', 'The app project (default: the current directory)', '.')
|
|
13
|
+
.option('--list', 'List the available blocks')
|
|
14
|
+
.option('--force', 'Overwrite files and action declarations that differ from the block')
|
|
15
|
+
.option('--dry-run', 'Show what would change without writing anything')
|
|
16
|
+
.action(async (names, opts) => {
|
|
17
|
+
try {
|
|
18
|
+
if (opts.list) {
|
|
19
|
+
printJson(listBlocks());
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (names.length === 0) {
|
|
23
|
+
throw new Error(`Name a block to add: ${blockNames().join(', ')} (parall app add --list)`);
|
|
24
|
+
}
|
|
25
|
+
const blocks = names.map((name) => {
|
|
26
|
+
const block = findBlock(name.replace(/^@parall\//, ''));
|
|
27
|
+
if (!block) {
|
|
28
|
+
throw new Error(`Unknown block ${JSON.stringify(name)}; available: ${blockNames().join(', ')}`);
|
|
29
|
+
}
|
|
30
|
+
return block;
|
|
31
|
+
});
|
|
32
|
+
const dir = resolve(opts.dir);
|
|
33
|
+
// Refuse before writing anything if any block would clobber a file.
|
|
34
|
+
for (const block of blocks) {
|
|
35
|
+
const plan = await planBlock(dir, block, { force: opts.force });
|
|
36
|
+
if (plan.conflicts.length > 0 || plan.actions.conflicts.length > 0) {
|
|
37
|
+
throw new Error(conflictMessage(plan));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const plans = [];
|
|
41
|
+
for (const block of blocks) {
|
|
42
|
+
// Planned again after the previous block landed: blocks edit the
|
|
43
|
+
// same manifest, package.json and plug-in points.
|
|
44
|
+
const plan = await planBlock(dir, block, { force: opts.force });
|
|
45
|
+
if (!opts.dryRun)
|
|
46
|
+
await applyBlock(plan);
|
|
47
|
+
plans.push(plan);
|
|
48
|
+
}
|
|
49
|
+
printJson({
|
|
50
|
+
dir,
|
|
51
|
+
dry_run: Boolean(opts.dryRun),
|
|
52
|
+
blocks: plans.map((plan) => ({
|
|
53
|
+
name: plan.block,
|
|
54
|
+
files: Object.fromEntries(plan.files.map((file) => [file.path, file.state])),
|
|
55
|
+
actions_added: plan.actions.added,
|
|
56
|
+
dependencies_added: plan.dependencies.added,
|
|
57
|
+
dependencies_kept: plan.dependencies.kept,
|
|
58
|
+
edited: plan.edits.map((edit) => edit.path),
|
|
59
|
+
manual_steps: plan.manual,
|
|
60
|
+
sql: plan.sql,
|
|
61
|
+
})),
|
|
62
|
+
});
|
|
63
|
+
console.error(nextSteps(dir, plans, Boolean(opts.dryRun)));
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
printError(err);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function nextSteps(dir, plans, dryRun) {
|
|
71
|
+
const lines = [];
|
|
72
|
+
const where = relative(process.cwd(), dir) || '.';
|
|
73
|
+
for (const plan of plans) {
|
|
74
|
+
const created = plan.files.filter((file) => file.state === 'create').length;
|
|
75
|
+
const overwritten = plan.files.filter((file) => file.state === 'overwrite').length;
|
|
76
|
+
const verb = dryRun ? 'Would add' : 'Added';
|
|
77
|
+
lines.push(`${verb} ${plan.block} to ${where}: ${created} new file(s)${overwritten ? `, ${overwritten} overwritten` : ''}, actions ${[...plan.actions.added, ...plan.actions.unchanged].join(', ')}.`);
|
|
78
|
+
for (const [name, version] of Object.entries(plan.dependencies.kept)) {
|
|
79
|
+
lines.push(` ${name}: kept the project's ${version} (the block is tested with another version)`);
|
|
80
|
+
}
|
|
81
|
+
for (const step of plan.manual)
|
|
82
|
+
lines.push(` By hand — ${step}`);
|
|
83
|
+
}
|
|
84
|
+
const added = plans.flatMap((plan) => Object.keys(plan.dependencies.added));
|
|
85
|
+
lines.push('', 'Next:');
|
|
86
|
+
const sqlFiles = plans.map((plan) => plan.sql);
|
|
87
|
+
lines.push(` 1. Create the tables (PostgreSQL; safe to rerun): ${sqlFiles
|
|
88
|
+
.map((file) => `parall app db exec --app <app_…> --file ${file}`)
|
|
89
|
+
.join(' && ')}`);
|
|
90
|
+
lines.push(` 2. ${added.length > 0 ? `Install the new packages (${added.join(', ')}) and build` : 'Build'}: pnpm install && pnpm build`);
|
|
91
|
+
lines.push(' 3. parall app check dist, then parall app publish dist --app <app_…> --source .');
|
|
92
|
+
return lines.join('\n');
|
|
93
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The @parall blocks `parall app add` installs: ready-made pages wired end
|
|
3
|
+
* to end — page components, an actions file, schema SQL and manifest
|
|
4
|
+
* declarations. Each block is a shadcn registry item (registry-item.json
|
|
5
|
+
* format, files inlined) shipped inside the CLI, so installing one needs no
|
|
6
|
+
* network and matches the CLI's version. What shadcn has no field for — the
|
|
7
|
+
* manifest declarations, where the page and actions plug in, the SQL file —
|
|
8
|
+
* rides in meta.parall.
|
|
9
|
+
*/
|
|
10
|
+
export interface RegistryFile {
|
|
11
|
+
path: string;
|
|
12
|
+
type: string;
|
|
13
|
+
target: string;
|
|
14
|
+
content: string;
|
|
15
|
+
}
|
|
16
|
+
export interface ParallBlockMeta {
|
|
17
|
+
/** The page entry: an import line and the expression added to the pages
|
|
18
|
+
* list in src/pages.tsx. */
|
|
19
|
+
page: {
|
|
20
|
+
import: string;
|
|
21
|
+
entry: string;
|
|
22
|
+
};
|
|
23
|
+
/** The actions entry: an import line and the spread added to
|
|
24
|
+
* defineActions in actions/index.ts. */
|
|
25
|
+
actions: {
|
|
26
|
+
import: string;
|
|
27
|
+
entry: string;
|
|
28
|
+
};
|
|
29
|
+
/** Merged into public/manifest.json actions.functions. */
|
|
30
|
+
functions: Record<string, unknown>;
|
|
31
|
+
/** The schema file to apply with `parall app db exec --file`. */
|
|
32
|
+
sql: string;
|
|
33
|
+
}
|
|
34
|
+
export interface RegistryItem {
|
|
35
|
+
$schema?: string;
|
|
36
|
+
name: string;
|
|
37
|
+
type: string;
|
|
38
|
+
title: string;
|
|
39
|
+
description: string;
|
|
40
|
+
author?: string;
|
|
41
|
+
/** npm packages, pinned as name@version. */
|
|
42
|
+
dependencies?: string[];
|
|
43
|
+
registryDependencies?: string[];
|
|
44
|
+
files: RegistryFile[];
|
|
45
|
+
docs?: string;
|
|
46
|
+
meta: {
|
|
47
|
+
parall: ParallBlockMeta;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export declare function findBlock(name: string): RegistryItem | undefined;
|
|
51
|
+
export declare function blockNames(): string[];
|
|
52
|
+
/** What `parall app add --list` prints. */
|
|
53
|
+
export declare function listBlocks(): {
|
|
54
|
+
name: string;
|
|
55
|
+
title: string;
|
|
56
|
+
description: string;
|
|
57
|
+
actions: string[];
|
|
58
|
+
dependencies: string[];
|
|
59
|
+
files: string[];
|
|
60
|
+
}[];
|
|
61
|
+
export interface BlockPlan {
|
|
62
|
+
block: string;
|
|
63
|
+
dir: string;
|
|
64
|
+
files: Array<{
|
|
65
|
+
path: string;
|
|
66
|
+
content: string;
|
|
67
|
+
state: 'create' | 'overwrite' | 'unchanged';
|
|
68
|
+
}>;
|
|
69
|
+
/** Files that exist with other content (refused without --force). */
|
|
70
|
+
conflicts: string[];
|
|
71
|
+
dependencies: {
|
|
72
|
+
added: Record<string, string>;
|
|
73
|
+
kept: Record<string, string>;
|
|
74
|
+
};
|
|
75
|
+
actions: {
|
|
76
|
+
added: string[];
|
|
77
|
+
unchanged: string[];
|
|
78
|
+
conflicts: string[];
|
|
79
|
+
};
|
|
80
|
+
/** Rewritten files: the manifest, package.json and the two plug-in points. */
|
|
81
|
+
edits: Array<{
|
|
82
|
+
path: string;
|
|
83
|
+
content: string;
|
|
84
|
+
}>;
|
|
85
|
+
/** Steps left to the author when a plug-in marker is missing. */
|
|
86
|
+
manual: string[];
|
|
87
|
+
sql: string;
|
|
88
|
+
}
|
|
89
|
+
/** Plans adding a block to the project in dir without writing anything. */
|
|
90
|
+
export declare function planBlock(dir: string, block: RegistryItem, options?: {
|
|
91
|
+
force?: boolean;
|
|
92
|
+
}): Promise<BlockPlan>;
|
|
93
|
+
/** Writes a plan; refuses one with conflicts. */
|
|
94
|
+
export declare function applyBlock(plan: BlockPlan): Promise<void>;
|
|
95
|
+
export declare function conflictMessage(plan: BlockPlan): string;
|
|
96
|
+
//# sourceMappingURL=apps-blocks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apps-blocks.d.ts","sourceRoot":"","sources":["../../src/commands/apps-blocks.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AAEH,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,eAAe;IAC9B;gCAC4B;IAC5B,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IACxC;4CACwC;IACxC,OAAO,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAC3C,0DAA0D;IAC1D,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,iEAAiE;IACjE,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,4CAA4C;IAC5C,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;IAChC,KAAK,EAAE,YAAY,EAAE,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,EAAE;QAAE,MAAM,EAAE,eAAe,CAAA;KAAE,CAAC;CACnC;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAEhE;AAED,wBAAgB,UAAU,IAAI,MAAM,EAAE,CAErC;AAED,2CAA2C;AAC3C,wBAAgB,UAAU;;;;;;;IASzB;AAsBD,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,QAAQ,GAAG,WAAW,GAAG,WAAW,CAAA;KAAE,CAAC,CAAC;IAC7F,qEAAqE;IACrE,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,YAAY,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAAE,CAAC;IAC9E,OAAO,EAAE;QAAE,KAAK,EAAE,MAAM,EAAE,CAAC;QAAC,SAAS,EAAE,MAAM,EAAE,CAAC;QAAC,SAAS,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IACvE,8EAA8E;IAC9E,KAAK,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAChD,iEAAiE;IACjE,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;CACb;AAWD,2EAA2E;AAC3E,wBAAsB,SAAS,CAC7B,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,YAAY,EACnB,OAAO,GAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAO,GAChC,OAAO,CAAC,SAAS,CAAC,CAmGpB;AAkCD,iDAAiD;AACjD,wBAAsB,UAAU,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAW/D;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAWvD"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import { dirname, join, resolve, sep } from 'node:path';
|
|
3
|
+
import { sortKeys } from './apps-template.js';
|
|
4
|
+
import { TEMPLATE_BLOCKS } from './apps-templates.gen.js';
|
|
5
|
+
export function findBlock(name) {
|
|
6
|
+
return TEMPLATE_BLOCKS.find((block) => block.name === name);
|
|
7
|
+
}
|
|
8
|
+
export function blockNames() {
|
|
9
|
+
return TEMPLATE_BLOCKS.map((block) => block.name);
|
|
10
|
+
}
|
|
11
|
+
/** What `parall app add --list` prints. */
|
|
12
|
+
export function listBlocks() {
|
|
13
|
+
return TEMPLATE_BLOCKS.map((block) => ({
|
|
14
|
+
name: block.name,
|
|
15
|
+
title: block.title,
|
|
16
|
+
description: block.description,
|
|
17
|
+
actions: Object.keys(block.meta.parall.functions),
|
|
18
|
+
dependencies: block.dependencies ?? [],
|
|
19
|
+
files: block.files.map((file) => file.target),
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
22
|
+
// What a project must have for a block to plug in: the --actions scaffold
|
|
23
|
+
// with its shell, hooks and UI primitives.
|
|
24
|
+
const REQUIRED = [
|
|
25
|
+
'package.json',
|
|
26
|
+
'public/manifest.json',
|
|
27
|
+
'actions/index.ts',
|
|
28
|
+
'actions/people.ts',
|
|
29
|
+
'src/pages.tsx',
|
|
30
|
+
'src/hooks/use-action.ts',
|
|
31
|
+
'src/hooks/use-people.ts',
|
|
32
|
+
'src/components/ui/button.tsx',
|
|
33
|
+
];
|
|
34
|
+
const MARKERS = {
|
|
35
|
+
pageImports: '// parall:page-imports',
|
|
36
|
+
pages: '// parall:pages',
|
|
37
|
+
actionImports: '// parall:action-imports',
|
|
38
|
+
actions: '// parall:actions',
|
|
39
|
+
};
|
|
40
|
+
async function readIfExists(path) {
|
|
41
|
+
try {
|
|
42
|
+
return await fs.readFile(path, 'utf8');
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
if (err.code === 'ENOENT')
|
|
46
|
+
return undefined;
|
|
47
|
+
throw err;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Plans adding a block to the project in dir without writing anything. */
|
|
51
|
+
export async function planBlock(dir, block, options = {}) {
|
|
52
|
+
const root = resolve(dir);
|
|
53
|
+
const missing = [];
|
|
54
|
+
for (const path of REQUIRED) {
|
|
55
|
+
if ((await readIfExists(join(root, path))) === undefined)
|
|
56
|
+
missing.push(path);
|
|
57
|
+
}
|
|
58
|
+
if (missing.length > 0) {
|
|
59
|
+
throw new Error(`${root} is not a Mini App project with actions (missing ${missing.join(', ')}). Blocks plug into the scaffold of \`parall app init <dir> --actions\`.`);
|
|
60
|
+
}
|
|
61
|
+
const meta = block.meta.parall;
|
|
62
|
+
const plan = {
|
|
63
|
+
block: block.name,
|
|
64
|
+
dir: root,
|
|
65
|
+
files: [],
|
|
66
|
+
conflicts: [],
|
|
67
|
+
dependencies: { added: {}, kept: {} },
|
|
68
|
+
actions: { added: [], unchanged: [], conflicts: [] },
|
|
69
|
+
edits: [],
|
|
70
|
+
manual: [],
|
|
71
|
+
sql: meta.sql,
|
|
72
|
+
};
|
|
73
|
+
for (const file of block.files) {
|
|
74
|
+
const target = join(root, file.target);
|
|
75
|
+
if (!target.startsWith(root + sep))
|
|
76
|
+
throw new Error(`block file escapes the project: ${file.target}`);
|
|
77
|
+
const current = await readIfExists(target);
|
|
78
|
+
if (current === undefined) {
|
|
79
|
+
plan.files.push({ path: file.target, content: file.content, state: 'create' });
|
|
80
|
+
}
|
|
81
|
+
else if (current === file.content) {
|
|
82
|
+
plan.files.push({ path: file.target, content: file.content, state: 'unchanged' });
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
plan.files.push({ path: file.target, content: file.content, state: 'overwrite' });
|
|
86
|
+
if (!options.force)
|
|
87
|
+
plan.conflicts.push(file.target);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// public/manifest.json: declare the block's actions.
|
|
91
|
+
const manifestPath = 'public/manifest.json';
|
|
92
|
+
const manifest = JSON.parse((await readIfExists(join(root, manifestPath))) ?? '{}');
|
|
93
|
+
if (!manifest.actions || typeof manifest.actions !== 'object') {
|
|
94
|
+
throw new Error(`${manifestPath} declares no actions; blocks plug into the scaffold of \`parall app init <dir> --actions\``);
|
|
95
|
+
}
|
|
96
|
+
manifest.actions.functions ??= {};
|
|
97
|
+
for (const [name, declaration] of Object.entries(meta.functions)) {
|
|
98
|
+
const current = manifest.actions.functions[name];
|
|
99
|
+
if (current === undefined) {
|
|
100
|
+
plan.actions.added.push(name);
|
|
101
|
+
}
|
|
102
|
+
else if (JSON.stringify(current) === JSON.stringify(declaration)) {
|
|
103
|
+
plan.actions.unchanged.push(name);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
else if (!options.force) {
|
|
107
|
+
plan.actions.conflicts.push(name);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
plan.actions.added.push(name);
|
|
112
|
+
}
|
|
113
|
+
manifest.actions.functions[name] = declaration;
|
|
114
|
+
}
|
|
115
|
+
if (plan.actions.added.length > 0) {
|
|
116
|
+
plan.edits.push({ path: manifestPath, content: `${JSON.stringify(manifest, null, 2)}\n` });
|
|
117
|
+
}
|
|
118
|
+
// package.json: add the block's packages at its pinned versions; a
|
|
119
|
+
// package the project already has keeps its version.
|
|
120
|
+
const pkg = JSON.parse((await readIfExists(join(root, 'package.json'))) ?? '{}');
|
|
121
|
+
pkg.dependencies ??= {};
|
|
122
|
+
for (const spec of block.dependencies ?? []) {
|
|
123
|
+
const at = spec.lastIndexOf('@');
|
|
124
|
+
const name = at > 0 ? spec.slice(0, at) : spec;
|
|
125
|
+
const version = at > 0 ? spec.slice(at + 1) : 'latest';
|
|
126
|
+
const have = pkg.dependencies[name] ?? pkg.devDependencies?.[name];
|
|
127
|
+
if (have === undefined) {
|
|
128
|
+
pkg.dependencies[name] = version;
|
|
129
|
+
plan.dependencies.added[name] = version;
|
|
130
|
+
}
|
|
131
|
+
else if (have !== version) {
|
|
132
|
+
plan.dependencies.kept[name] = have;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (Object.keys(plan.dependencies.added).length > 0) {
|
|
136
|
+
pkg.dependencies = sortKeys(pkg.dependencies);
|
|
137
|
+
plan.edits.push({ path: 'package.json', content: `${JSON.stringify(pkg, null, 2)}\n` });
|
|
138
|
+
}
|
|
139
|
+
// The two plug-in points.
|
|
140
|
+
await plugIn(plan, root, 'actions/index.ts', meta.actions, MARKERS.actionImports, MARKERS.actions);
|
|
141
|
+
await plugIn(plan, root, 'src/pages.tsx', meta.page, MARKERS.pageImports, MARKERS.pages);
|
|
142
|
+
return plan;
|
|
143
|
+
}
|
|
144
|
+
/** Adds an import above importMarker and an entry above entryMarker, unless
|
|
145
|
+
* already there. Without the markers it leaves the file alone and records a
|
|
146
|
+
* manual step. */
|
|
147
|
+
async function plugIn(plan, root, path, spec, importMarker, entryMarker) {
|
|
148
|
+
const source = (await readIfExists(join(root, path))) ?? '';
|
|
149
|
+
const lines = source.split('\n');
|
|
150
|
+
const hasImport = lines.some((line) => line.trim() === spec.import);
|
|
151
|
+
const hasEntry = lines.some((line) => line.trim().replace(/,$/, '') === spec.entry);
|
|
152
|
+
if (hasImport && hasEntry)
|
|
153
|
+
return;
|
|
154
|
+
const importAt = lines.findIndex((line) => line.trim().startsWith(importMarker));
|
|
155
|
+
const entryAt = lines.findIndex((line) => line.trim().startsWith(entryMarker));
|
|
156
|
+
if (importAt === -1 || entryAt === -1) {
|
|
157
|
+
plan.manual.push(`${path}: add \`${spec.import}\` and \`${spec.entry},\``);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const indent = lines[entryAt]?.match(/^\s*/)?.[0] ?? '';
|
|
161
|
+
// Insert the later marker first so the earlier index stays valid.
|
|
162
|
+
const inserts = [];
|
|
163
|
+
if (!hasImport)
|
|
164
|
+
inserts.push([importAt, spec.import]);
|
|
165
|
+
if (!hasEntry)
|
|
166
|
+
inserts.push([entryAt, `${indent}${spec.entry},`]);
|
|
167
|
+
inserts.sort(([a], [b]) => b - a);
|
|
168
|
+
for (const [at, line] of inserts)
|
|
169
|
+
lines.splice(at, 0, line);
|
|
170
|
+
plan.edits.push({ path, content: lines.join('\n') });
|
|
171
|
+
}
|
|
172
|
+
/** Writes a plan; refuses one with conflicts. */
|
|
173
|
+
export async function applyBlock(plan) {
|
|
174
|
+
if (plan.conflicts.length > 0 || plan.actions.conflicts.length > 0) {
|
|
175
|
+
throw new Error(conflictMessage(plan));
|
|
176
|
+
}
|
|
177
|
+
for (const file of plan.files) {
|
|
178
|
+
if (file.state === 'unchanged')
|
|
179
|
+
continue;
|
|
180
|
+
const target = join(plan.dir, file.path);
|
|
181
|
+
await fs.mkdir(dirname(target), { recursive: true });
|
|
182
|
+
await fs.writeFile(target, file.content);
|
|
183
|
+
}
|
|
184
|
+
for (const edit of plan.edits)
|
|
185
|
+
await fs.writeFile(join(plan.dir, edit.path), edit.content);
|
|
186
|
+
}
|
|
187
|
+
export function conflictMessage(plan) {
|
|
188
|
+
const parts = [];
|
|
189
|
+
if (plan.conflicts.length > 0) {
|
|
190
|
+
parts.push(`these files exist with other content: ${plan.conflicts.join(', ')}`);
|
|
191
|
+
}
|
|
192
|
+
if (plan.actions.conflicts.length > 0) {
|
|
193
|
+
parts.push(`public/manifest.json declares these actions differently: ${plan.actions.conflicts.join(', ')}`);
|
|
194
|
+
}
|
|
195
|
+
return `cannot add ${plan.block}: ${parts.join('; ')}. Nothing was written; rerun with --force to overwrite them.`;
|
|
196
|
+
}
|
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* (
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* build time), a second Vite build that writes dist/_actions/index.js —
|
|
6
|
-
* one unminified ES module with a source map, whose only import is
|
|
7
|
-
* cloudflare:workers — miniflare for `parall app action run --local`, and
|
|
8
|
-
* a page that calls the example action. */
|
|
9
|
-
export declare function withActions(files: Array<[string, string]>): Array<[string, string]>;
|
|
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. */
|
|
10
5
|
export declare const MINIFLARE_RANGE = "^4.20260415.0";
|
|
11
6
|
//# sourceMappingURL=apps-scaffold-actions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apps-scaffold-actions.d.ts","sourceRoot":"","sources":["../../src/commands/apps-scaffold-actions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"apps-scaffold-actions.d.ts","sourceRoot":"","sources":["../../src/commands/apps-scaffold-actions.ts"],"names":[],"mappings":"AAAA;;;kDAGkD;AAClD,eAAO,MAAM,eAAe,kBAAkB,CAAC"}
|
|
@@ -1,164 +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
|
-
- 要调 Clip:在 manifest 里给写类 action 加 \`"clip": true\`,代码里 \`await ctx.clip.exec('<clip>', '<command>', args, { connection: '<alias>', timeoutMs: 60_000 })\`。它以调用 action 的人的身份执行,那个人没有这个 connection 的权限就被拒(\`ClipError\`,\`code\` 如 \`CONNECTION_NOT_ALLOWED\`);接住它,换成给人看的 \`ActionError\`。\`OUTCOME_UNKNOWN\` 表示命令可能已经执行,不要自动重试。调 Clip 的 action 有 100 秒、最多 10 次 Clip 调用;单次命令默认只等 30 秒,命令慢就传 \`timeoutMs\`,并给之后的数据库写入留出时间(平台会把它截到 action 剩余时间减 8 秒)。给已有的 app 加 Clip 调用时,先从新跑的 \`parall app init --actions\` 里拷来 \`src/parall.ts\` 与 \`actions/parall-actions.ts\` 换掉旧副本再构建:旧的页面 SDK 等 action 只等 30 秒,旧的 action SDK 没有 \`ctx.clip\`。
|
|
160
|
-
- 调试:改完 \`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_…>\` 只看一次调用;本机运行的写入版本号为空)。
|
|
161
|
-
- 页面连本地 action:\`parall app dev --dist dist --app <app_…>\`,打开 CLI 打印的地址(带令牌)。页面调的 action 在本机运行,同样**写会真的生效**;右侧列出每次调用、结果与 \`console\` 输出,终端也各打一行。改完重新 \`pnpm build\` 即可,不用重启。
|
|
162
|
-
- 页面用 \`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,在里面重读。
|
|
163
|
-
- \`actions/parall-actions.ts\` 是 SDK 的 vendored 副本,不要改。
|
|
164
|
-
`;
|
|
@@ -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"}
|