sepal 0.2.0-alpha.0 → 0.3.0-alpha.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/README.md +14 -5
- package/cli.js +127 -10
- package/package.json +48 -4
- package/src/abort.js +129 -0
- package/src/app.js +13 -0
- package/src/dom.js +173 -60
- package/src/fetch.js +347 -80
- package/src/form-data.js +141 -0
- package/src/host-call.js +57 -0
- package/src/index.js +9 -11
- package/src/lru.js +45 -0
- package/src/native.js +32 -0
- package/src/notifications.js +60 -0
- package/src/os.js +35 -0
- package/src/process.js +164 -0
- package/src/protocol.js +3 -4
- package/src/renderer.js +140 -10
- package/src/secrets.js +42 -0
- package/src/settings.js +52 -0
- package/src/shims.js +30 -8
- package/src/stem.js +125 -0
- package/src/updater.js +3 -2
- package/src/url.js +65 -20
- package/src/web.js +86 -0
- package/src/window.js +6 -2
- package/types/abort.d.ts +46 -0
- package/types/app.d.ts +7 -0
- package/types/fetch.d.ts +118 -28
- package/types/form-data.d.ts +19 -0
- package/types/host-call.d.ts +18 -0
- package/types/index.d.ts +5 -2
- package/types/lru.d.ts +18 -0
- package/types/native.d.ts +22 -0
- package/types/notifications.d.ts +53 -0
- package/types/os.d.ts +53 -0
- package/types/process.d.ts +90 -0
- package/types/protocol.d.ts +0 -2
- package/types/renderer.d.ts +16 -1
- package/types/secrets.d.ts +22 -0
- package/types/settings.d.ts +29 -0
- package/types/shims.d.ts +1 -0
- package/types/stem.d.ts +21 -0
- package/types/updater.d.ts +6 -4
- package/types/url.d.ts +1 -1
- package/types/vite/adapter.d.ts +22 -0
- package/types/vite/add.d.ts +77 -0
- package/types/vite/app.d.ts +52 -1
- package/types/vite/licenses.d.ts +11 -0
- package/types/vite/native.d.ts +135 -0
- package/types/vite/page.d.ts +20 -0
- package/types/vite/permissions.d.ts +56 -0
- package/types/vite/sveltekit.d.ts +5 -42
- package/types/vite/vite.d.ts +86 -29
- package/types/web.d.ts +17 -0
- package/types/window.d.ts +5 -1
- package/vite/adapter.js +118 -0
- package/vite/add.js +254 -0
- package/vite/app.js +195 -27
- package/vite/head.js +2 -2
- package/vite/licenses.js +115 -0
- package/vite/native.js +535 -0
- package/vite/page.js +95 -0
- package/vite/permissions.d.ts +56 -0
- package/vite/sveltekit.js +11 -263
- package/vite/vite.js +378 -109
package/types/web.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { URL } from './url.js';
|
|
2
|
+
import { URLSearchParams } from './url.js';
|
|
3
|
+
import { TextEncoder } from './text.js';
|
|
4
|
+
import { TextDecoder } from './text.js';
|
|
5
|
+
import { AbortController } from './abort.js';
|
|
6
|
+
import { AbortSignal } from './abort.js';
|
|
7
|
+
import { fetch } from './fetch.js';
|
|
8
|
+
import { Headers } from './fetch.js';
|
|
9
|
+
import { Request } from './fetch.js';
|
|
10
|
+
import { Response } from './fetch.js';
|
|
11
|
+
import { ReadableStream } from 'web-streams-polyfill';
|
|
12
|
+
import { WritableStream } from 'web-streams-polyfill';
|
|
13
|
+
import { TransformStream } from 'web-streams-polyfill';
|
|
14
|
+
import { Blob } from 'formdata-node';
|
|
15
|
+
import { File } from 'formdata-node';
|
|
16
|
+
import { FormData } from 'formdata-node';
|
|
17
|
+
export { URL, URLSearchParams, TextEncoder, TextDecoder, AbortController, AbortSignal, fetch, Headers, Request, Response, ReadableStream, WritableStream, TransformStream, Blob, File, FormData };
|
package/types/window.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Set the window's title. Needs no permission: the title is the app's own,
|
|
3
|
+
* as a page's is on the web (`document.title` does the same).
|
|
4
|
+
* @param {string} title @returns {Promise<void>}
|
|
5
|
+
*/
|
|
2
6
|
export function setTitle(title: string): Promise<void>;
|
|
3
7
|
/** @returns {Promise<void>} */
|
|
4
8
|
export function minimize(): Promise<void>;
|
package/vite/adapter.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// `sepal/adapter`: the SvelteKit adapter for Sepal apps.
|
|
2
|
+
//
|
|
3
|
+
// // vite.config.ts
|
|
4
|
+
// import { sveltekit } from '@sveltejs/kit/vite';
|
|
5
|
+
// import adapter from 'sepal/adapter';
|
|
6
|
+
// import { sepal, withSepal } from 'sepal/vite';
|
|
7
|
+
//
|
|
8
|
+
// export default defineConfig({
|
|
9
|
+
// plugins: [sveltekit(withSepal({ adapter: adapter() })), sepal()]
|
|
10
|
+
// });
|
|
11
|
+
//
|
|
12
|
+
// It writes SvelteKit's output as SvelteKit writes it, a single-page app:
|
|
13
|
+
// index.html as the shell every address starts from, the client chunks,
|
|
14
|
+
// stylesheets, fonts, static files and `_app/version.json`. Then `app.json`,
|
|
15
|
+
// for the host. Before that it checks what it was given, and stops the
|
|
16
|
+
// build saying what to add.
|
|
17
|
+
//
|
|
18
|
+
// Runs in Node.
|
|
19
|
+
|
|
20
|
+
import { cpSync, existsSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
21
|
+
import { join, relative, resolve } from 'node:path';
|
|
22
|
+
import { compile_bytecode, kit, manifest } from './app.js';
|
|
23
|
+
import { check_css, check_page } from './page.js';
|
|
24
|
+
import { kit_setup_problems } from './vite.js';
|
|
25
|
+
|
|
26
|
+
/** Every file under a directory, as paths relative to it, with `/`. @param {string} directory @returns {string[]} */
|
|
27
|
+
function files_in(directory) {
|
|
28
|
+
return readdirSync(directory, { recursive: true, withFileTypes: true })
|
|
29
|
+
.filter((entry) => entry.isFile())
|
|
30
|
+
.map((entry) => relative(directory, join(entry.parentPath, entry.name)).replaceAll('\\', '/'));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The files under `src` that SvelteKit would run on a server. An app has a
|
|
35
|
+
* thread for server code (the stem), but how an app's server code gets
|
|
36
|
+
* there is not decided (HANDOVER.md, "The stem's API: open"), so these
|
|
37
|
+
* would build and then never run. Relative to `src`.
|
|
38
|
+
* @param {string} src
|
|
39
|
+
*/
|
|
40
|
+
export function server_files(src) {
|
|
41
|
+
if (!existsSync(src)) return [];
|
|
42
|
+
return files_in(src).filter((file) => /(^|\/)(\+(page|layout)\.server\.|\+server\.|hooks\.server\.)|\.remote\.[^/]+$/.test(file));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* What is missing from the setup, as messages saying what to add.
|
|
47
|
+
* @param {import('@sveltejs/kit').Builder['config']} config
|
|
48
|
+
* @param {string} root the project
|
|
49
|
+
* @returns {string[]}
|
|
50
|
+
*/
|
|
51
|
+
export function problems(config, root) {
|
|
52
|
+
const found = [];
|
|
53
|
+
if (kit.session === null) {
|
|
54
|
+
found.push('add `sepal()` from `sepal/vite` to the plugins in vite.config.ts, after `sveltekit()`: it builds the app for the Sepal renderer, and the adapter needs it.');
|
|
55
|
+
}
|
|
56
|
+
found.push(...kit_setup_problems(config));
|
|
57
|
+
const src = resolve(root, config.files?.src ?? 'src');
|
|
58
|
+
const server = server_files(src).map((file) => relative(root, join(src, file)).replaceAll('\\', '/'));
|
|
59
|
+
if (server.length > 0) {
|
|
60
|
+
found.push(
|
|
61
|
+
`remove ${server.join(', ')}: SvelteKit's server files are not supported yet. Use +page.ts and +layout.ts instead (\`load\` runs in the app).`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
return found;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* SvelteKit's route announcer is a 1px `overflow: hidden` box at the top
|
|
69
|
+
* left. Blitz hit-tests what overflows such a box, so after the first
|
|
70
|
+
* navigation its invisible text took clicks meant for what is drawn there.
|
|
71
|
+
* ponytail: goes when Blitz's hit testing clips to `overflow: hidden` (HANDOVER.md, "Blitz hit testing clipped to overflow: hidden").
|
|
72
|
+
*/
|
|
73
|
+
const ANNOUNCER = '<style>#svelte-announcer { pointer-events: none }</style>';
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @param {{ out?: string }} [options] `out` is where the app is written (default `dist`).
|
|
77
|
+
* @returns {import('@sveltejs/kit').Adapter}
|
|
78
|
+
*/
|
|
79
|
+
export default function adapter({ out = 'dist' } = {}) {
|
|
80
|
+
return {
|
|
81
|
+
name: 'sepal',
|
|
82
|
+
async adapt(builder) {
|
|
83
|
+
const session = kit.session;
|
|
84
|
+
const root = session?.root ?? process.cwd();
|
|
85
|
+
const missing = problems(builder.config, root);
|
|
86
|
+
if (missing.length > 0) throw new Error(`sepal: ${missing.join('\nsepal: ')}`);
|
|
87
|
+
if (session === null) return;
|
|
88
|
+
|
|
89
|
+
const to = resolve(root, out);
|
|
90
|
+
rmSync(to, { force: true, recursive: true });
|
|
91
|
+
builder.writeClient(to);
|
|
92
|
+
// The shell SvelteKit serves for an address it did not prerender:
|
|
93
|
+
// what the app starts from, whatever its address.
|
|
94
|
+
const page = join(to, 'index.html');
|
|
95
|
+
await builder.generateFallback(page);
|
|
96
|
+
if (builder.prerendered.paths.length > 0) {
|
|
97
|
+
builder.log.warn(`sepal: prerendering is not done yet; ${builder.prerendered.paths.join(', ')} render in the app instead`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const html = readFileSync(page, 'utf8');
|
|
101
|
+
const { errors, warnings } = check_page(html);
|
|
102
|
+
for (const file of files_in(to).filter((file) => file.endsWith('.css'))) errors.push(...check_css(readFileSync(join(to, file), 'utf8'), file));
|
|
103
|
+
if (errors.length > 0) throw new Error(`sepal: ${errors.join('\nsepal: ')}`);
|
|
104
|
+
for (const warning of warnings) builder.log.warn(`sepal: ${warning}`);
|
|
105
|
+
writeFileSync(page, html.replace('</head>', `\t${ANNOUNCER}\n\t</head>`));
|
|
106
|
+
|
|
107
|
+
const assets = resolve(root, builder.config.files?.assets ?? 'static');
|
|
108
|
+
const written = manifest(session.options, root, existsSync(assets) ? assets : '');
|
|
109
|
+
for (const warning of written?.warnings ?? []) builder.log.warn(`sepal: ${warning}`);
|
|
110
|
+
if (written) writeFileSync(join(to, 'app.json'), written.json);
|
|
111
|
+
if (written?.icon) cpSync(written.icon, join(to, 'icon.png'));
|
|
112
|
+
const compiled = compile_bytecode(to, (message) => builder.log.warn(message));
|
|
113
|
+
if (compiled) builder.log.minor(compiled);
|
|
114
|
+
builder.log.minor(`Wrote the app to ${relative(root, to) || '.'}`);
|
|
115
|
+
session.written(to);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
package/vite/add.js
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
// `sepal add` and `sepal check`: adding a plugin to an app (its npm
|
|
2
|
+
// package, its crate in native/, its line in native/src/plugins.rs, its key
|
|
3
|
+
// in sepal.config.ts), handing anything else to `sv add`, and checking that
|
|
4
|
+
// those agree. Runs in Node.
|
|
5
|
+
//
|
|
6
|
+
// An add-on that is a plugin says so in its package.json:
|
|
7
|
+
//
|
|
8
|
+
// "sepal": { "plugin": { "crate": "sepal-watch", "init": "sepal_watch::init()",
|
|
9
|
+
// "permission": { "key": "watch", "default": ["documents"] } } }
|
|
10
|
+
|
|
11
|
+
import { spawnSync } from 'node:child_process';
|
|
12
|
+
import { existsSync, readFileSync, realpathSync, writeFileSync } from 'node:fs';
|
|
13
|
+
import { dirname, join, sep } from 'node:path';
|
|
14
|
+
import { tsPlugin } from '@sveltejs/acorn-typescript';
|
|
15
|
+
import { Parser } from 'acorn';
|
|
16
|
+
import MagicString from 'magic-string';
|
|
17
|
+
import { caret_accepts, init, native_manifest, tag_problem, VERSION } from './native.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sepal's own plugins, by short name. `sepal add watch` means these and
|
|
21
|
+
* nothing else: never whatever `sepal-<name>` someone published, which would
|
|
22
|
+
* then be installed as if it were ours, crate and all.
|
|
23
|
+
*/
|
|
24
|
+
export const FIRST_PARTY = /** @type {Record<string, string>} */ ({ watch: 'sepal-watch' });
|
|
25
|
+
|
|
26
|
+
/** The keys the host reads itself (`BUILT_IN` in crates/host/src/api/mod.rs). */
|
|
27
|
+
const BUILT_IN = ['http', 'fs', 'dialog', 'clipboard', 'open', 'window', 'os', 'settings', 'process', 'secrets', 'notifications'];
|
|
28
|
+
|
|
29
|
+
/** @typedef {{ crate: string, init: string, permission: { key: string, default?: unknown } }} PluginInfo */
|
|
30
|
+
|
|
31
|
+
/** @param {string} root */
|
|
32
|
+
export function package_manager(root) {
|
|
33
|
+
if (existsSync(join(root, 'pnpm-lock.yaml')) || process.env.npm_config_user_agent?.startsWith('pnpm')) return 'pnpm';
|
|
34
|
+
if (existsSync(join(root, 'yarn.lock'))) return 'yarn';
|
|
35
|
+
if (existsSync(join(root, 'bun.lock')) || existsSync(join(root, 'bun.lockb'))) return 'bun';
|
|
36
|
+
return 'npm';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** @param {string} file */
|
|
40
|
+
const json = (file) => JSON.parse(readFileSync(file, 'utf8'));
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The plugin an installed package is, or null. `sepal` is its peer range
|
|
44
|
+
* on sepal, which is its crate's requirement too.
|
|
45
|
+
* @param {string} root @param {string} name
|
|
46
|
+
* @returns {{ info: PluginInfo, version: string, dir: string, sepal: string | undefined } | null}
|
|
47
|
+
*/
|
|
48
|
+
export function installed_plugin(root, name) {
|
|
49
|
+
const manifest = join(root, 'node_modules', name, 'package.json');
|
|
50
|
+
if (!existsSync(manifest)) return null;
|
|
51
|
+
const pkg = json(manifest);
|
|
52
|
+
return pkg.sepal?.plugin ? { info: pkg.sepal.plugin, version: pkg.version, dir: realpathSync(dirname(manifest)), sepal: pkg.peerDependencies?.sepal } : null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The project's dependencies, by name. @param {string} root @returns {string[]} */
|
|
56
|
+
function dependencies(root) {
|
|
57
|
+
const pkg = json(join(root, 'package.json'));
|
|
58
|
+
return Object.keys({ ...pkg.dependencies, ...pkg.devDependencies });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Every plugin the project depends on, in name order. @param {string} root */
|
|
62
|
+
export function plugins(root) {
|
|
63
|
+
return dependencies(root)
|
|
64
|
+
.sort()
|
|
65
|
+
.map((name) => installed_plugin(root, name))
|
|
66
|
+
.filter((plugin) => plugin !== null);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** `native/src/plugins.rs` for these plugins. @param {PluginInfo[]} infos */
|
|
70
|
+
export function plugins_rs(infos) {
|
|
71
|
+
const lines = infos.map((info) => `\n .plugin(${info.init})`).join('');
|
|
72
|
+
return `// Written by \`sepal add\`: it lists the app's plugins, and is written again
|
|
73
|
+
// when one is added or removed. Edits here are lost.
|
|
74
|
+
pub fn add(app: sepal::App) -> sepal::App {
|
|
75
|
+
app${lines}
|
|
76
|
+
}
|
|
77
|
+
`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** @param {unknown} value as TypeScript source */
|
|
81
|
+
function source(value) {
|
|
82
|
+
return JSON.stringify(value).replaceAll('"', "'");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Add `key: value` to `permissions` in a `sepal.config.ts`, in place: the
|
|
87
|
+
* file is parsed (acorn with TypeScript) and the text inserted where it
|
|
88
|
+
* goes, nothing reprinted. Null when the file is not `export default
|
|
89
|
+
* defineConfig({ … })` with an object literal, which then needs the line
|
|
90
|
+
* by hand.
|
|
91
|
+
* @param {string} code @param {string} key @param {unknown} value
|
|
92
|
+
* @returns {{ code: string, changed: boolean } | null}
|
|
93
|
+
*/
|
|
94
|
+
export function add_permission(code, key, value) {
|
|
95
|
+
/** @type {any} */
|
|
96
|
+
let ast;
|
|
97
|
+
try {
|
|
98
|
+
ast = Parser.extend(/** @type {any} */ (tsPlugin())).parse(code, { sourceType: 'module', ecmaVersion: 'latest' });
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const declaration = ast.body.find((/** @type {any} */ node) => node.type === 'ExportDefaultDeclaration')?.declaration;
|
|
103
|
+
const config = declaration?.type === 'CallExpression' ? declaration.arguments[0] : declaration;
|
|
104
|
+
if (config?.type !== 'ObjectExpression') return null;
|
|
105
|
+
const name = (/** @type {any} */ property) => property.key?.name ?? property.key?.value;
|
|
106
|
+
const edited = new MagicString(code);
|
|
107
|
+
/** Insert `text` as the last property of `object`, indented as its others. @param {any} object @param {string} text */
|
|
108
|
+
const append = (object, text) => {
|
|
109
|
+
const last = object.properties.at(-1);
|
|
110
|
+
if (!last) {
|
|
111
|
+
const indent = code.slice(code.lastIndexOf('\n', object.start) + 1).match(/^\s*/)?.[0] ?? '';
|
|
112
|
+
edited.appendLeft(object.start + 1, `\n${indent}\t${text}\n${indent}`);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
// On one line with its braces (`{ delay: true }`): stays on one line.
|
|
116
|
+
if (!code.slice(object.start, last.start).includes('\n')) {
|
|
117
|
+
edited.appendLeft(last.end, `, ${text}`);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const indent = code.slice(code.lastIndexOf('\n', last.start) + 1).match(/^[ \t]*/)?.[0] ?? '';
|
|
121
|
+
edited.appendLeft(last.end, `,\n${indent}${text}`);
|
|
122
|
+
};
|
|
123
|
+
const permissions = config.properties.find((/** @type {any} */ property) => name(property) === 'permissions');
|
|
124
|
+
if (!permissions) {
|
|
125
|
+
append(config, `permissions: { ${key}: ${source(value)} }`);
|
|
126
|
+
} else if (permissions.value.type !== 'ObjectExpression') {
|
|
127
|
+
return null;
|
|
128
|
+
} else if (permissions.value.properties.some((/** @type {any} */ property) => name(property) === key)) {
|
|
129
|
+
return { code, changed: false };
|
|
130
|
+
} else {
|
|
131
|
+
append(permissions.value, `${key}: ${source(value)}`);
|
|
132
|
+
}
|
|
133
|
+
return { code: edited.toString(), changed: true };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** @typedef {(command: string, args: string[], cwd: string) => { status: number | null }} Run */
|
|
137
|
+
|
|
138
|
+
/** @type {Run} */
|
|
139
|
+
const run_inheriting = (command, args, cwd) => spawnSync(command, args, { cwd, stdio: 'inherit' });
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* `sepal add <names…>`.
|
|
143
|
+
* @param {string} root the project
|
|
144
|
+
* @param {string[]} names
|
|
145
|
+
* @param {{ run?: Run, view?: (name: string) => boolean, crate_name: string, log?: (line: string) => void }} options
|
|
146
|
+
* `view` says whether a package that is not one of ours is a plugin (by default, the registry's word)
|
|
147
|
+
*/
|
|
148
|
+
export function add(root, names, { run = run_inheriting, view = registry_says_plugin, crate_name, log = console.log }) {
|
|
149
|
+
for (const name of names) {
|
|
150
|
+
const pkg = FIRST_PARTY[name] ?? name;
|
|
151
|
+
let plugin = dependencies(root).includes(pkg) ? installed_plugin(root, pkg) : null;
|
|
152
|
+
if (!plugin && !FIRST_PARTY[name] && !view(name)) {
|
|
153
|
+
// Not a Sepal plugin: sv's add-ons work on a Sepal app as they are.
|
|
154
|
+
log(`sepal: ${name} is not a Sepal plugin; handing it to sv add`);
|
|
155
|
+
if (run('npx', ['sv', 'add', name], root).status !== 0) throw new Error(`sv add ${name} failed`);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (!plugin) {
|
|
159
|
+
const manager = package_manager(root);
|
|
160
|
+
if (run(manager, [manager === 'npm' ? 'install' : 'add', pkg], root).status !== 0) {
|
|
161
|
+
throw new Error(`${manager} could not install ${pkg}`);
|
|
162
|
+
}
|
|
163
|
+
plugin = installed_plugin(root, pkg);
|
|
164
|
+
if (!plugin) throw new Error(`${pkg} is not a Sepal plugin: its package.json has no "sepal": { "plugin" }`);
|
|
165
|
+
}
|
|
166
|
+
if (!native_manifest(root)) {
|
|
167
|
+
init(root, crate_name);
|
|
168
|
+
log('sepal: wrote native/, the app’s own host');
|
|
169
|
+
}
|
|
170
|
+
// A plugin from this workspace (not from node_modules) is added by
|
|
171
|
+
// path; a published one at the package's version, its crate's.
|
|
172
|
+
const local = !plugin.dir.split(sep).includes('node_modules') && existsSync(join(plugin.dir, 'Cargo.toml'));
|
|
173
|
+
const manifest = join(root, 'native', 'Cargo.toml');
|
|
174
|
+
const crate = local ? [plugin.info.crate, '--path', plugin.dir] : [`${plugin.info.crate}@=${plugin.version}`];
|
|
175
|
+
if (run('cargo', ['add', ...crate, '--manifest-path', manifest], root).status !== 0) {
|
|
176
|
+
throw new Error(`cargo add ${plugin.info.crate} failed`);
|
|
177
|
+
}
|
|
178
|
+
write_plugins_rs(root);
|
|
179
|
+
const key = plugin.info.permission.key;
|
|
180
|
+
const config = ['sepal.config.ts', 'sepal.config.js', 'sepal.config.mjs'].map((file) => join(root, file)).find(existsSync);
|
|
181
|
+
const value = plugin.info.permission.default ?? true;
|
|
182
|
+
const edited = config ? add_permission(readFileSync(config, 'utf8'), key, value) : null;
|
|
183
|
+
if (config && edited) {
|
|
184
|
+
if (edited.changed) writeFileSync(config, edited.code);
|
|
185
|
+
log(`sepal: added ${pkg}: its crate to native/, \`${plugin.info.init}\` to native/src/plugins.rs, and ${edited.changed ? `\`${key}: ${source(value)}\` to permissions` : `\`${key}\` was in permissions already`}.`);
|
|
186
|
+
} else {
|
|
187
|
+
log(`sepal: added ${pkg} to native/. Add this to \`permissions\` in sepal.config.ts:\n\n ${key}: ${source(value)}\n`);
|
|
188
|
+
}
|
|
189
|
+
log('sepal: the next `vite build --watch` builds it into the app’s own host.');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Whether a package that is not one of ours is a Sepal plugin, by asking
|
|
195
|
+
* the registry for its `sepal` field. Only then is it installed.
|
|
196
|
+
* @param {string} name
|
|
197
|
+
*/
|
|
198
|
+
function registry_says_plugin(name) {
|
|
199
|
+
const viewed = spawnSync('npm', ['view', name, 'sepal', '--json'], { encoding: 'utf8' });
|
|
200
|
+
return viewed.status === 0 && viewed.stdout.includes('"plugin"');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Write native/src/plugins.rs again from the plugins the project has. @param {string} root */
|
|
204
|
+
export function write_plugins_rs(root) {
|
|
205
|
+
writeFileSync(join(root, 'native', 'src', 'plugins.rs'), plugins_rs(plugins(root).map((plugin) => plugin.info)));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* `sepal check`'s own part: what has to agree between the npm packages,
|
|
210
|
+
* native/ and the config. svelte-check runs beside it.
|
|
211
|
+
* @param {string} root @param {Record<string, unknown> | undefined} permissions the config's
|
|
212
|
+
* @param {string} [version] this sepal's
|
|
213
|
+
* @returns {string[]} the problems
|
|
214
|
+
*/
|
|
215
|
+
export function check(root, permissions, version = VERSION) {
|
|
216
|
+
const problems = [];
|
|
217
|
+
const installed = plugins(root);
|
|
218
|
+
for (const plugin of installed) {
|
|
219
|
+
// Said here, before cargo says "failed to select a version for `sepal`":
|
|
220
|
+
// native/ pins sepal exactly, and a build holds one sepal 0.x.
|
|
221
|
+
// ponytail: carets only, which is what the template and `workspace:^`
|
|
222
|
+
// publish; a range of another shape is not judged. A range parser
|
|
223
|
+
// when a plugin ships one.
|
|
224
|
+
const range = /^\^(\S+)$/.exec(plugin.sepal ?? '')?.[1];
|
|
225
|
+
if (range && !caret_accepts(range, version)) {
|
|
226
|
+
problems.push(
|
|
227
|
+
`${plugin.info.crate} ${plugin.version} is written for sepal ^${range}, and this is sepal ${version}: cargo finds no sepal that fits both. A plugin fits one 0.x line of sepal; install a ${plugin.info.crate} released for this one`
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
const manifest = native_manifest(root);
|
|
232
|
+
if (manifest) {
|
|
233
|
+
const tag = tag_problem(manifest, VERSION);
|
|
234
|
+
if (tag) problems.push(tag.trim());
|
|
235
|
+
const written = readFileSync(join(root, 'native', 'src', 'plugins.rs'), 'utf8');
|
|
236
|
+
if (written !== plugins_rs(installed.map((plugin) => plugin.info))) {
|
|
237
|
+
problems.push('native/src/plugins.rs does not list the plugins the app depends on: run `sepal add` again, or remove the package');
|
|
238
|
+
}
|
|
239
|
+
} else if (installed.length > 0) {
|
|
240
|
+
problems.push(`the app depends on ${installed.map((plugin) => plugin.info.crate).join(', ')} but has no native/ to build it into: \`sepal add\` writes one`);
|
|
241
|
+
}
|
|
242
|
+
const keys = installed.map((plugin) => plugin.info.permission.key);
|
|
243
|
+
for (const key of Object.keys(permissions ?? {})) {
|
|
244
|
+
if (!BUILT_IN.includes(key) && !keys.includes(key)) {
|
|
245
|
+
problems.push(`permissions.${key} is neither built in nor a plugin's the app has: the host stops at launch on it`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
for (const plugin of installed) {
|
|
249
|
+
if (!(plugin.info.permission.key in (permissions ?? {}))) {
|
|
250
|
+
problems.push(`${plugin.info.crate} needs permissions.${plugin.info.permission.key}; without it its commands are refused`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return problems;
|
|
254
|
+
}
|
package/vite/app.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// What every way of building an app shares, whichever router it uses: reading
|
|
2
|
-
// `sepal.config.ts`, writing the manifest the host reads (`app.json`),
|
|
3
|
-
//
|
|
2
|
+
// `sepal.config.ts`, writing the manifest the host reads (`app.json`),
|
|
3
|
+
// compiling the modules to bytecode, and opening the app in dev and
|
|
4
|
+
// reloading it after each rebuild.
|
|
4
5
|
//
|
|
5
6
|
// Runs in Node.
|
|
6
7
|
|
|
@@ -13,6 +14,16 @@ import { find_host } from '@ethercorps/calyx';
|
|
|
13
14
|
|
|
14
15
|
const CONFIG_FILES = ['sepal.config.ts', 'sepal.config.js', 'sepal.config.mjs'];
|
|
15
16
|
|
|
17
|
+
/**
|
|
18
|
+
* What `sepal()` tells the SvelteKit adapter (`sepal/adapter`), which runs
|
|
19
|
+
* inside SvelteKit's build and has no other way to reach it: the project,
|
|
20
|
+
* the settings from `sepal.config.ts`, and what to do once the app is
|
|
21
|
+
* written to a directory (open or reload the dev window). `null` until
|
|
22
|
+
* `sepal()` has run.
|
|
23
|
+
* @type {{ session: { root: string, options: import('./vite.js').Config, written: (out_dir: string) => void } | null }}
|
|
24
|
+
*/
|
|
25
|
+
export const kit = { session: null };
|
|
26
|
+
|
|
16
27
|
/**
|
|
17
28
|
* Read the project's config file, if it has one.
|
|
18
29
|
* @param {string} project
|
|
@@ -38,7 +49,7 @@ export const SEPAL_ICON = fileURLToPath(new URL('./icon.png', import.meta.url));
|
|
|
38
49
|
* @param {import('./vite.js').Config} options
|
|
39
50
|
* @param {string} root the project
|
|
40
51
|
* @param {string} static_dir where the icon is looked for; empty when there is none
|
|
41
|
-
* @returns {{ json: string, icon: string | null } | null}
|
|
52
|
+
* @returns {{ json: string, icon: string | null, warnings: string[] } | null}
|
|
42
53
|
*/
|
|
43
54
|
export function manifest(options, root, static_dir) {
|
|
44
55
|
if (!options.app && !options.permissions) return null;
|
|
@@ -65,42 +76,72 @@ export function manifest(options, root, static_dir) {
|
|
|
65
76
|
}
|
|
66
77
|
}
|
|
67
78
|
const json = JSON.stringify(options.permissions ? { ...app, permissions: options.permissions } : app, null, '\t') + '\n';
|
|
68
|
-
return { json, icon };
|
|
79
|
+
return { json, icon, warnings: process_warnings(options.permissions) };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Programs that run whatever they are given (`SHELLS` in
|
|
84
|
+
* crates/host/src/api/process.rs).
|
|
85
|
+
*/
|
|
86
|
+
const SHELLS = ['sh', 'bash', 'zsh', 'fish', 'dash', 'ksh', 'csh', 'tcsh', 'osascript', 'powershell', 'pwsh', 'cmd', 'env', 'xargs', 'python', 'perl', 'ruby', 'node', 'bun', 'deno', 'npx'];
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* What to say about `permissions.process` when it allows running anything:
|
|
90
|
+
* `true`, or a list that names a shell or a script runner. A warning, not
|
|
91
|
+
* an error: some apps are for exactly that.
|
|
92
|
+
* @param {{ process?: unknown } | undefined} permissions
|
|
93
|
+
* @returns {string[]}
|
|
94
|
+
*/
|
|
95
|
+
export function process_warnings(permissions) {
|
|
96
|
+
const allowed = permissions?.process;
|
|
97
|
+
if (allowed === true) return ['permissions.process is true: the app can run any program. List the programs it needs instead'];
|
|
98
|
+
if (!Array.isArray(allowed)) return [];
|
|
99
|
+
return allowed.flatMap((entry) => {
|
|
100
|
+
// Without its folder and version: `/usr/bin/python3.12` is `python`.
|
|
101
|
+
const name = String(entry).split('/').pop()?.replace(/[\d.]+$/, '') ?? '';
|
|
102
|
+
return SHELLS.includes(name) ? [`permissions.process lists \`${entry}\`: ${name} runs whatever it is given, so the app can run any program`] : [];
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The flags for dev's throwaway bundle: `--dev`, which leaves the page out
|
|
108
|
+
* of it and signs it for this Mac, with the app's identity when
|
|
109
|
+
* `SEPAL_SIGN` names one. Its signature is then the same from one build
|
|
110
|
+
* of the app to the next, and so is the program the keychain and the
|
|
111
|
+
* notification settings know.
|
|
112
|
+
*/
|
|
113
|
+
export function dev_bundle_flags() {
|
|
114
|
+
return process.env.SEPAL_SIGN ? ['--dev', '--sign', process.env.SEPAL_SIGN] : ['--dev'];
|
|
69
115
|
}
|
|
70
116
|
|
|
71
117
|
/**
|
|
72
118
|
* The app's window in dev. The first call opens it; every later one reloads
|
|
73
119
|
* it in place. Returns a function to call after each successful build.
|
|
120
|
+
*
|
|
121
|
+
* An app with `native/` runs its own host: built first (the window opens
|
|
122
|
+
* when it is), built again on every change to it or to a crate it has by
|
|
123
|
+
* path, and restarted when that changes the binary. A build that fails keeps
|
|
124
|
+
* the last good host running; one that never succeeded opens nothing. Both
|
|
125
|
+
* keep dev going until the next change.
|
|
74
126
|
* @param {() => { root: string, out_dir: string, name: string | undefined }} project read at the first call
|
|
75
127
|
*/
|
|
76
128
|
export function dev_window(project) {
|
|
77
129
|
/** @type {import('node:child_process').ChildProcess | null} */
|
|
78
130
|
let host = null;
|
|
79
131
|
let gave_up = false;
|
|
132
|
+
/** The app's own host, once there is one; `undefined` until the first call looks. @type {Promise<void> | null | undefined} */
|
|
133
|
+
let native;
|
|
134
|
+
/** The binary to start once the running host has quit, for a restart. @type {string | null} */
|
|
135
|
+
let next = null;
|
|
80
136
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (host !== null) {
|
|
84
|
-
// The host reloads on every line it reads.
|
|
85
|
-
host.stdin?.write('reload\n');
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
let bin;
|
|
90
|
-
try {
|
|
91
|
-
bin = find_host().path;
|
|
92
|
-
} catch (error) {
|
|
93
|
-
console.warn(`sepal: the app will not open: ${/** @type {Error} */ (error).message}`);
|
|
94
|
-
gave_up = true;
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
137
|
+
/** Start the host on `bin`, out of a throwaway bundle on macOS. @param {string} bin */
|
|
138
|
+
const open = (bin) => {
|
|
97
139
|
const { root, out_dir, name } = project();
|
|
98
|
-
|
|
99
140
|
// macOS takes an app's name and Dock icon from the bundle it runs
|
|
100
141
|
// from, so in dev the host runs out of a throwaway one. It still
|
|
101
142
|
// loads the app from the build directory, not from the copy inside the bundle.
|
|
102
143
|
if (process.platform === 'darwin' && name) {
|
|
103
|
-
const made = spawnSync(bin, ['--bundle', out_dir, resolve(root, 'node_modules/.sepal')], {
|
|
144
|
+
const made = spawnSync(bin, ['--bundle', out_dir, resolve(root, 'node_modules/.sepal'), ...dev_bundle_flags()], {
|
|
104
145
|
encoding: 'utf8'
|
|
105
146
|
});
|
|
106
147
|
if (made.status === 0) {
|
|
@@ -112,13 +153,140 @@ export function dev_window(project) {
|
|
|
112
153
|
}
|
|
113
154
|
|
|
114
155
|
const started = bin;
|
|
115
|
-
|
|
116
|
-
host
|
|
156
|
+
const child = spawn(started, ['--watch', out_dir], { stdio: ['pipe', 'inherit', 'inherit'] });
|
|
157
|
+
host = child;
|
|
158
|
+
child.on('error', (error) => {
|
|
117
159
|
console.error(`sepal: cannot start ${started}: ${error.message}`);
|
|
118
160
|
process.exit(1);
|
|
119
161
|
});
|
|
120
|
-
// Closing the window ends the session
|
|
121
|
-
// code: when the build tool exits,
|
|
122
|
-
host
|
|
162
|
+
// Closing the window ends the session, unless it closed for a new
|
|
163
|
+
// host. The other direction needs no code: when the build tool exits,
|
|
164
|
+
// the host's stdin closes and it quits.
|
|
165
|
+
child.on('exit', (code) => {
|
|
166
|
+
if (next === null) process.exit(code ?? 0);
|
|
167
|
+
const binary = next;
|
|
168
|
+
next = null;
|
|
169
|
+
open(binary);
|
|
170
|
+
});
|
|
123
171
|
};
|
|
172
|
+
|
|
173
|
+
/** Build native/ and keep it built, opening and restarting the window. @param {string} manifest */
|
|
174
|
+
const own_host = async (manifest) => {
|
|
175
|
+
const { announce, build, path_crates, tag_problem, toolchain_problem, watch_crates, written, VERSION } =
|
|
176
|
+
await import('./native.js');
|
|
177
|
+
const problem = tag_problem(manifest, VERSION) ?? toolchain_problem();
|
|
178
|
+
if (problem) {
|
|
179
|
+
console.error(problem);
|
|
180
|
+
process.exit(1);
|
|
181
|
+
}
|
|
182
|
+
let built_at = 0;
|
|
183
|
+
const run = async () => {
|
|
184
|
+
announce(manifest, 'dev');
|
|
185
|
+
const started = Date.now();
|
|
186
|
+
const built = await build(manifest, 'dev');
|
|
187
|
+
if ('error' in built) {
|
|
188
|
+
console.error(
|
|
189
|
+
`sepal: native/ did not build (${built.error}); ${host ? 'the window stays on the last host that did' : 'the app opens once it does'}. Save again to retry.`
|
|
190
|
+
);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
console.error(`sepal: native/ built in ${((Date.now() - started) / 1000).toFixed(1)} s`);
|
|
194
|
+
process.env.SEPAL_HOST = built.binary;
|
|
195
|
+
const at = written(built.binary);
|
|
196
|
+
if (host === null) open(built.binary);
|
|
197
|
+
else if (at !== built_at) {
|
|
198
|
+
// A new binary: the page starts over on it.
|
|
199
|
+
next = built.binary;
|
|
200
|
+
host.stdin?.end();
|
|
201
|
+
}
|
|
202
|
+
built_at = at;
|
|
203
|
+
};
|
|
204
|
+
// One build at a time; a change during one builds again after it.
|
|
205
|
+
let building = Promise.resolve();
|
|
206
|
+
let again = false;
|
|
207
|
+
const rebuild = () => {
|
|
208
|
+
if (again) return;
|
|
209
|
+
again = true;
|
|
210
|
+
building = building
|
|
211
|
+
.then(() => {
|
|
212
|
+
again = false;
|
|
213
|
+
return run();
|
|
214
|
+
})
|
|
215
|
+
.catch((error) => console.error(`sepal: building native/ failed: ${error.message ?? error}`));
|
|
216
|
+
};
|
|
217
|
+
let stop = () => {};
|
|
218
|
+
const watch_all = async () => {
|
|
219
|
+
stop();
|
|
220
|
+
stop = watch_crates(await path_crates(manifest), (file) => {
|
|
221
|
+
// The crates by path may have changed with a manifest.
|
|
222
|
+
if (file.endsWith('Cargo.toml')) void watch_all();
|
|
223
|
+
rebuild();
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
await watch_all();
|
|
227
|
+
rebuild();
|
|
228
|
+
await building;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
return () => {
|
|
232
|
+
if (gave_up) return;
|
|
233
|
+
if (host !== null) {
|
|
234
|
+
// The host reloads on every line it reads.
|
|
235
|
+
host.stdin?.write('reload\n');
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (native === undefined) {
|
|
239
|
+
// SEPAL_HOST, when set, is the host to run, whatever native/ has.
|
|
240
|
+
const { root } = project();
|
|
241
|
+
const manifest = process.env.SEPAL_HOST ? null : join(root, 'native', 'Cargo.toml');
|
|
242
|
+
native = manifest && existsSync(manifest) ? own_host(manifest) : null;
|
|
243
|
+
}
|
|
244
|
+
// The window opens once the app's own host is built, on the build
|
|
245
|
+
// that is newest then.
|
|
246
|
+
if (native !== null) return;
|
|
247
|
+
|
|
248
|
+
let bin;
|
|
249
|
+
try {
|
|
250
|
+
bin = find_host().path;
|
|
251
|
+
} catch (error) {
|
|
252
|
+
console.warn(`sepal: the app will not open: ${/** @type {Error} */ (error).message}`);
|
|
253
|
+
gave_up = true;
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
open(bin);
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Compile the app's modules to bytecode with this machine's calyx (`calyx
|
|
262
|
+
* --compile`), which writes `x.js.qbc` next to each `x.js`: the host then
|
|
263
|
+
* loads them without parsing them. The app folder is a new build, so every
|
|
264
|
+
* module is compiled. Without a calyx that can, the app loads its
|
|
265
|
+
* JavaScript, as it always could: the build says so, and goes on.
|
|
266
|
+
* @param {string} dir the app folder
|
|
267
|
+
* @param {(message: string) => void} warn
|
|
268
|
+
* @returns {string | null} calyx's summary, or null when nothing was compiled
|
|
269
|
+
*/
|
|
270
|
+
export function compile_bytecode(dir, warn) {
|
|
271
|
+
let bin;
|
|
272
|
+
try {
|
|
273
|
+
bin = find_host().path;
|
|
274
|
+
} catch (error) {
|
|
275
|
+
warn(`sepal: the app's modules were not compiled to bytecode, so it loads their JavaScript: ${/** @type {Error} */ (error).message}`);
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
const done = spawnSync(bin, ['--compile', dir], { encoding: 'utf8' });
|
|
279
|
+
if (done.error || done.status !== 0) {
|
|
280
|
+
// A calyx from before --compile takes it for the app folder.
|
|
281
|
+
if (done.stderr?.includes('--compile is not an app folder')) {
|
|
282
|
+
warn(`sepal: ${bin} is a calyx from before \`--compile\`, so the app's modules were not compiled to bytecode and it loads their JavaScript. A current calyx compiles them.`);
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
const why = done.error?.message ?? done.stderr.trim();
|
|
286
|
+
warn(`sepal: compiling the app's modules to bytecode failed, so it loads their JavaScript: ${why}`);
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
// Modules that stayed JavaScript, one line each.
|
|
290
|
+
for (const line of done.stderr.split('\n').filter(Boolean)) warn(`sepal: ${line}`);
|
|
291
|
+
return done.stdout.trim() || null;
|
|
124
292
|
}
|