@tauri-cordis/cli 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -2
- package/bin/fuyu-plugin.mjs +12 -0
- package/bin/tauri-cordis-plugin.mjs +12 -0
- package/package.json +46 -3
- package/src/build.mjs +137 -0
- package/src/bundle.mjs +148 -0
- package/src/check.mjs +152 -0
- package/src/cli.mjs +257 -0
- package/src/declarations.mjs +50 -0
- package/src/dev.mjs +145 -0
- package/src/entries.mjs +68 -0
- package/src/errors.mjs +12 -0
- package/src/files.mjs +74 -0
- package/src/git.mjs +76 -0
- package/src/index.d.mts +232 -0
- package/src/index.mjs +23 -0
- package/src/installer.mjs +191 -0
- package/src/keys.mjs +67 -0
- package/src/pack.mjs +25 -0
- package/src/review.mjs +16 -0
- package/src/shared.mjs +17 -0
- package/src/sign.mjs +51 -0
- package/src/sources.mjs +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TixXin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@tauri-cordis/cli`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Command-line toolkit (`build`, `pack`, `check`, `dev`, `review`, `keygen`, `sign`) and installer planner for [`tauri-cordis`](https://github.com/TixXin/tauri-cordis) plugins.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { run } from "../src/cli.mjs";
|
|
3
|
+
|
|
4
|
+
run(process.argv.slice(2)).then(
|
|
5
|
+
(code) => {
|
|
6
|
+
process.exitCode = code;
|
|
7
|
+
},
|
|
8
|
+
(error) => {
|
|
9
|
+
process.stderr.write(`error: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
10
|
+
process.exitCode = 1;
|
|
11
|
+
},
|
|
12
|
+
);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { run } from "../src/cli.mjs";
|
|
3
|
+
|
|
4
|
+
run(process.argv.slice(2)).then(
|
|
5
|
+
(code) => {
|
|
6
|
+
process.exitCode = code;
|
|
7
|
+
},
|
|
8
|
+
(error) => {
|
|
9
|
+
process.stderr.write(`error: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
10
|
+
process.exitCode = 1;
|
|
11
|
+
},
|
|
12
|
+
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tauri-cordis/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "CLI toolkit (build, pack, check, dev, review, keygen, sign) for tauri-cordis plugins",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/TixXin/tauri-cordis.git",
|
|
10
|
+
"directory": "packages/cli"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/TixXin/tauri-cordis",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/TixXin/tauri-cordis/issues"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"bin",
|
|
21
|
+
"src",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"bin": {
|
|
26
|
+
"tauri-cordis-plugin": "bin/tauri-cordis-plugin.mjs",
|
|
27
|
+
"fuyu-plugin": "bin/fuyu-plugin.mjs"
|
|
28
|
+
},
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./src/index.d.mts",
|
|
32
|
+
"default": "./src/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=22.15"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@tauri-cordis/manifest": "0.2.0",
|
|
41
|
+
"@tauri-cordis/market": "0.2.0",
|
|
42
|
+
"@tauri-cordis/review": "0.2.0",
|
|
43
|
+
"esbuild": "0.28.1",
|
|
44
|
+
"tauri-cordis": "0.2.0"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"test": "node --test \"test/**/*.test.mjs\""
|
|
48
|
+
}
|
|
6
49
|
}
|
package/src/build.mjs
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
3
|
+
import { validateManifest } from "@tauri-cordis/manifest";
|
|
4
|
+
import { bundleClient, bundleHost } from "./bundle.mjs";
|
|
5
|
+
import { compareDeclarations } from "./declarations.mjs";
|
|
6
|
+
import { planEntries } from "./entries.mjs";
|
|
7
|
+
import { BuildError } from "./errors.mjs";
|
|
8
|
+
import { selectFiles } from "./files.mjs";
|
|
9
|
+
import { APP_MODULES, DEFAULT_SHARED_MODULES, HOST_EXTERNALS, INSTALLED_PACKAGE_DEPTH } from "./shared.mjs";
|
|
10
|
+
|
|
11
|
+
export const VERSION = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
12
|
+
export const DEFAULT_OUT_DIR = join("build", "package");
|
|
13
|
+
export const BUILD_ONLY_FIELDS = [
|
|
14
|
+
"scripts", "dependencies", "devDependencies", "optionalDependencies", "peerDependencies", "peerDependenciesMeta",
|
|
15
|
+
"bundleDependencies", "bundledDependencies", "overrides", "workspaces", "packageManager", "devEngines",
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
function packageDepthOf(depth) {
|
|
19
|
+
if (depth === undefined || depth === null || depth === false) return null;
|
|
20
|
+
if (depth === true) return INSTALLED_PACKAGE_DEPTH;
|
|
21
|
+
if (Number.isInteger(depth) && depth >= 0) return depth;
|
|
22
|
+
throw new BuildError(`packageDepth must be true or a whole number of folders, got ${JSON.stringify(depth)}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function resolveBuildOptions(options = {}) {
|
|
26
|
+
const packageDepth = packageDepthOf(options.packageDepth);
|
|
27
|
+
return {
|
|
28
|
+
appKey: options.appKey ?? "fuyu",
|
|
29
|
+
official: options.official ?? false,
|
|
30
|
+
allowExperimental: options.allowExperimental ?? true,
|
|
31
|
+
shared: options.shared ?? DEFAULT_SHARED_MODULES,
|
|
32
|
+
hostExternal: options.hostExternal ?? HOST_EXTERNALS,
|
|
33
|
+
appModules: options.appModules ?? APP_MODULES,
|
|
34
|
+
packageDepth,
|
|
35
|
+
nodeTarget: options.nodeTarget ?? "node22",
|
|
36
|
+
browserTarget: options.browserTarget ?? "es2022",
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function realPath(path) {
|
|
41
|
+
if (existsSync(path)) return realpathSync(path);
|
|
42
|
+
const parent = dirname(path);
|
|
43
|
+
return parent === path ? path : join(realPath(parent), basename(path));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function readManifest(dir) {
|
|
47
|
+
const file = join(dir, "package.json");
|
|
48
|
+
if (!existsSync(file)) throw new BuildError(`no package.json in ${dir}`, [{ path: "package.json", code: "missing", message: "missing" }]);
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(readFileSync(file, "utf8"));
|
|
51
|
+
} catch (error) {
|
|
52
|
+
throw new BuildError(`package.json is not valid JSON: ${error instanceof Error ? error.message : String(error)}`, [{ path: "package.json", code: "json", message: "invalid JSON" }]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function builtManifest(pkg, appKey, nodeApis, builtWith = VERSION) {
|
|
57
|
+
const built = structuredClone(pkg);
|
|
58
|
+
for (const field of BUILD_ONLY_FIELDS) delete built[field];
|
|
59
|
+
built[appKey] = { ...built[appKey], nodeApis, builtWith };
|
|
60
|
+
return built;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function prepareOutDir(root, outDir, appKey) {
|
|
64
|
+
const back = relative(outDir, root);
|
|
65
|
+
if (back === "" || (!back.startsWith("..") && !isAbsolute(back))) throw new BuildError(`the output folder ${outDir} must not contain the plugin folder`);
|
|
66
|
+
if (!existsSync(outDir) || readdirSync(outDir).length === 0) return;
|
|
67
|
+
let previous = null;
|
|
68
|
+
try {
|
|
69
|
+
previous = JSON.parse(readFileSync(join(outDir, "package.json"), "utf8"));
|
|
70
|
+
} catch {
|
|
71
|
+
previous = null;
|
|
72
|
+
}
|
|
73
|
+
if (typeof previous?.[appKey]?.builtWith !== "string") throw new BuildError(`refusing to empty ${outDir}: it is not an earlier build output`);
|
|
74
|
+
rmSync(outDir, { recursive: true, force: true });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function write(outDir, path, data) {
|
|
78
|
+
const file = join(outDir, path);
|
|
79
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
80
|
+
writeFileSync(file, data);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function build(dir, options = {}) {
|
|
84
|
+
const opts = resolveBuildOptions(options);
|
|
85
|
+
const root = realPath(resolve(dir));
|
|
86
|
+
const outDir = realPath(resolve(root, options.outDir ?? DEFAULT_OUT_DIR));
|
|
87
|
+
const pkg = readManifest(root);
|
|
88
|
+
const source = validateManifest(pkg, { appKey: opts.appKey, official: opts.official, allowExperimental: opts.allowExperimental, stage: "source" });
|
|
89
|
+
if (!source.ok) throw new BuildError("package.json has errors", source.errors);
|
|
90
|
+
const app = pkg[opts.appKey];
|
|
91
|
+
const warnings = source.warnings.filter((issue) => issue.code !== "generated").map((issue) => ({ ...issue }));
|
|
92
|
+
const plan = planEntries(root, app, opts.appKey);
|
|
93
|
+
const outputs = new Map();
|
|
94
|
+
let host = null;
|
|
95
|
+
let nodeApis = [];
|
|
96
|
+
if (plan.host) {
|
|
97
|
+
const result = await bundleHost({ dir: root, entry: plan.host.source, output: plan.host.output, external: opts.hostExternal, target: opts.nodeTarget });
|
|
98
|
+
outputs.set(plan.host.output, result.code);
|
|
99
|
+
warnings.push(...result.warnings);
|
|
100
|
+
host = { file: plan.host.output, text: result.code };
|
|
101
|
+
nodeApis = result.nodeApis;
|
|
102
|
+
}
|
|
103
|
+
const clients = [];
|
|
104
|
+
for (const client of plan.clients) {
|
|
105
|
+
const result = await bundleClient({
|
|
106
|
+
dir: root,
|
|
107
|
+
entry: client.source,
|
|
108
|
+
output: client.output,
|
|
109
|
+
plugin: pkg.name,
|
|
110
|
+
shared: opts.shared,
|
|
111
|
+
packageDepth: opts.packageDepth,
|
|
112
|
+
appModules: opts.appModules,
|
|
113
|
+
target: opts.browserTarget,
|
|
114
|
+
});
|
|
115
|
+
outputs.set(client.output, result.code);
|
|
116
|
+
warnings.push(...result.warnings);
|
|
117
|
+
clients.push({ file: client.output, text: result.code });
|
|
118
|
+
}
|
|
119
|
+
if (plan.config) outputs.set(plan.config.output, readFileSync(join(root, plan.config.source)));
|
|
120
|
+
const files = selectFiles(root, app, pkg.files, { exclude: [outDir], warn: (issue) => warnings.push(issue) });
|
|
121
|
+
warnings.push(...compareDeclarations({ app, appKey: opts.appKey, host, clients, nodeApis, sourceNodeApis: app.nodeApis }));
|
|
122
|
+
|
|
123
|
+
const manifest = builtManifest(pkg, opts.appKey, nodeApis);
|
|
124
|
+
const built = validateManifest(manifest, { appKey: opts.appKey, official: opts.official, allowExperimental: opts.allowExperimental, stage: "built" });
|
|
125
|
+
if (!built.ok) throw new BuildError("the built package.json has errors", built.errors);
|
|
126
|
+
|
|
127
|
+
prepareOutDir(root, outDir, opts.appKey);
|
|
128
|
+
mkdirSync(outDir, { recursive: true });
|
|
129
|
+
write(outDir, "package.json", `${JSON.stringify(manifest, null, 2)}\n`);
|
|
130
|
+
for (const [path, data] of outputs) write(outDir, path, data);
|
|
131
|
+
for (const path of files) {
|
|
132
|
+
mkdirSync(dirname(join(outDir, path)), { recursive: true });
|
|
133
|
+
copyFileSync(join(root, path), join(outDir, path));
|
|
134
|
+
}
|
|
135
|
+
const all = ["package.json", ...outputs.keys(), ...files].sort();
|
|
136
|
+
return { dir: root, outDir, manifest, nodeApis, files: all, warnings };
|
|
137
|
+
}
|
package/src/bundle.mjs
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import * as esbuild from "esbuild";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { isBuiltin } from "node:module";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { BuildError } from "./errors.mjs";
|
|
7
|
+
import { isPackageOrSubpath, sharedPath } from "./shared.mjs";
|
|
8
|
+
|
|
9
|
+
const ASSET_LOADERS = Object.fromEntries(
|
|
10
|
+
[".png", ".jpg", ".jpeg", ".gif", ".webp", ".avif", ".svg", ".woff", ".woff2", ".ttf", ".otf", ".mp3", ".wav", ".ogg"].map((extension) => [extension, "dataurl"]),
|
|
11
|
+
);
|
|
12
|
+
const REQUIRE_KINDS = new Set(["require-call", "require-resolve"]);
|
|
13
|
+
const REQUIRE_BANNER = 'import { createRequire as __fuyuCreateRequire } from "node:module";\nconst require = __fuyuCreateRequire(import.meta.url);';
|
|
14
|
+
const STYLES_MODULE = "fuyu-plugin:styles";
|
|
15
|
+
|
|
16
|
+
export function builtinName(specifier) {
|
|
17
|
+
if (!isBuiltin(specifier)) return null;
|
|
18
|
+
const bare = specifier.startsWith("node:") ? specifier.slice(5) : specifier;
|
|
19
|
+
return bare.split("/")[0];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function common(dir, target) {
|
|
23
|
+
return {
|
|
24
|
+
absWorkingDir: dir,
|
|
25
|
+
bundle: true,
|
|
26
|
+
format: "esm",
|
|
27
|
+
target,
|
|
28
|
+
write: false,
|
|
29
|
+
metafile: true,
|
|
30
|
+
logLevel: "silent",
|
|
31
|
+
charset: "utf8",
|
|
32
|
+
sourcemap: false,
|
|
33
|
+
minify: false,
|
|
34
|
+
tsconfigRaw: {},
|
|
35
|
+
jsx: "automatic",
|
|
36
|
+
loader: ASSET_LOADERS,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function issuesOf(messages) {
|
|
41
|
+
return messages.map((message) => ({
|
|
42
|
+
file: message.location?.file,
|
|
43
|
+
line: message.location?.line,
|
|
44
|
+
column: message.location ? message.location.column + 1 : undefined,
|
|
45
|
+
message: message.text,
|
|
46
|
+
code: "esbuild",
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function run(options, label) {
|
|
51
|
+
try {
|
|
52
|
+
const result = await esbuild.build(options);
|
|
53
|
+
return { result, warnings: issuesOf(result.warnings) };
|
|
54
|
+
} catch (error) {
|
|
55
|
+
if (Array.isArray(error?.errors)) throw new BuildError(`${label} failed to build`, issuesOf(error.errors));
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const outputOf = (result) => result.outputFiles.find((file) => !file.path.endsWith(".css"));
|
|
61
|
+
|
|
62
|
+
function exactly(names) {
|
|
63
|
+
return new RegExp(`^(?:${names.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})$`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function hostSharedPlugin(names) {
|
|
67
|
+
const filter = names.length ? exactly(names) : null;
|
|
68
|
+
return {
|
|
69
|
+
name: "fuyu-host-shared",
|
|
70
|
+
setup(build) {
|
|
71
|
+
if (filter) build.onResolve({ filter }, (args) => ({ path: args.path, external: true }));
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function bundleHost({ dir, entry, output, external, target = "node22" }) {
|
|
77
|
+
const plugins = [hostSharedPlugin(external)];
|
|
78
|
+
const options = { ...common(dir, target), platform: "node", entryPoints: [entry], outfile: join(dir, output), plugins };
|
|
79
|
+
let { result, warnings } = await run(options, output);
|
|
80
|
+
const imports = () => Object.entries(result.metafile.outputs).find(([path]) => path.endsWith(".js"))?.[1].imports ?? [];
|
|
81
|
+
const usesRequire = imports().some((item) => item.external && REQUIRE_KINDS.has(item.kind));
|
|
82
|
+
if (usesRequire) ({ result, warnings } = await run({ ...options, banner: { js: REQUIRE_BANNER } }, output));
|
|
83
|
+
const nodeApis = new Set(imports().filter((item) => item.external).map((item) => builtinName(item.path)).filter(Boolean));
|
|
84
|
+
if (usesRequire) nodeApis.add("module");
|
|
85
|
+
return { code: outputOf(result).text, nodeApis: [...nodeApis].sort(), inputs: Object.keys(result.metafile.inputs).sort(), warnings };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function sharedPlugin({ output, shared, packageDepth, appModules }) {
|
|
89
|
+
return {
|
|
90
|
+
name: "fuyu-shared-modules",
|
|
91
|
+
setup(build) {
|
|
92
|
+
build.onResolve({ filter: /^[^./]/ }, (args) => {
|
|
93
|
+
if (Object.hasOwn(shared, args.path)) return { path: sharedPath(output, shared[args.path], packageDepth), external: true };
|
|
94
|
+
const owner = appModules.find((name) => isPackageOrSubpath(args.path, name));
|
|
95
|
+
if (owner) return { errors: [{ text: `"${args.path}" is provided by the app, but window code cannot import it yet (shared modules: ${Object.keys(shared).join(", ")})` }] };
|
|
96
|
+
return undefined;
|
|
97
|
+
});
|
|
98
|
+
build.onResolve({ filter: /^fuyu-plugin:styles$/ }, () => ({ path: "styles", namespace: "fuyu-plugin" }));
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function stylesWrapper(entry, plugin) {
|
|
104
|
+
return [
|
|
105
|
+
`import plugin from ${JSON.stringify(`./${entry}`)};`,
|
|
106
|
+
`import css from ${JSON.stringify(STYLES_MODULE)};`,
|
|
107
|
+
'const apply = typeof plugin === "function" ? plugin : plugin.apply;',
|
|
108
|
+
"export default function applyWithStyles(ctx) {",
|
|
109
|
+
" ctx.effect(() => {",
|
|
110
|
+
' const style = document.createElement("style");',
|
|
111
|
+
` style.dataset.fuyuPlugin = ${JSON.stringify(plugin)};`,
|
|
112
|
+
" style.textContent = css;",
|
|
113
|
+
" document.head.append(style);",
|
|
114
|
+
" return () => style.remove();",
|
|
115
|
+
' }, "styles");',
|
|
116
|
+
" return apply.call(plugin, ctx);",
|
|
117
|
+
"}",
|
|
118
|
+
"",
|
|
119
|
+
].join("\n");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function bundleClient({ dir, entry, output, plugin, shared, packageDepth, appModules, target = "es2022" }) {
|
|
123
|
+
const base = {
|
|
124
|
+
...common(dir, target),
|
|
125
|
+
platform: "browser",
|
|
126
|
+
outfile: join(dir, output),
|
|
127
|
+
define: { "process.env.NODE_ENV": '"production"' },
|
|
128
|
+
plugins: [sharedPlugin({ output, shared, packageDepth, appModules })],
|
|
129
|
+
};
|
|
130
|
+
const first = await run({ ...base, entryPoints: [entry] }, output);
|
|
131
|
+
const css = first.result.outputFiles.find((file) => file.path.endsWith(".css"))?.text ?? "";
|
|
132
|
+
if (!css) return { code: outputOf(first.result).text, css: "", inputs: Object.keys(first.result.metafile.inputs).sort(), warnings: first.warnings };
|
|
133
|
+
const styles = {
|
|
134
|
+
name: "fuyu-styles",
|
|
135
|
+
setup(build) {
|
|
136
|
+
build.onLoad({ filter: /.*/, namespace: "fuyu-plugin" }, () => ({ contents: `export default ${JSON.stringify(css)};\n`, loader: "js" }));
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
const second = await run(
|
|
140
|
+
{
|
|
141
|
+
...base,
|
|
142
|
+
plugins: [...base.plugins, styles],
|
|
143
|
+
stdin: { contents: stylesWrapper(entry, plugin), resolveDir: dir, sourcefile: "fuyu-client-styles.js", loader: "js" },
|
|
144
|
+
},
|
|
145
|
+
output,
|
|
146
|
+
);
|
|
147
|
+
return { code: outputOf(second.result).text, css, inputs: Object.keys(first.result.metafile.inputs).sort(), warnings: first.warnings };
|
|
148
|
+
}
|
package/src/check.mjs
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { SERVICE_NAME, SLOTS } from "tauri-cordis/protocol";
|
|
3
|
+
import { review } from "@tauri-cordis/review";
|
|
4
|
+
import { build, readManifest } from "./build.mjs";
|
|
5
|
+
import { BuildError } from "./errors.mjs";
|
|
6
|
+
import { createScratch, snapshotDirectory } from "./sources.mjs";
|
|
7
|
+
|
|
8
|
+
const KNOWN_SLOTS = Object.freeze(Object.keys(SLOTS));
|
|
9
|
+
const KNOWN_INJECT = Object.freeze([
|
|
10
|
+
"clients",
|
|
11
|
+
"commands",
|
|
12
|
+
"config",
|
|
13
|
+
"http",
|
|
14
|
+
"windows",
|
|
15
|
+
"translator.engines",
|
|
16
|
+
"lyrics.sources",
|
|
17
|
+
"calendar.sources",
|
|
18
|
+
"agents.sources",
|
|
19
|
+
"clipboard.actions",
|
|
20
|
+
"screenshot.actions",
|
|
21
|
+
"messages.sources",
|
|
22
|
+
]);
|
|
23
|
+
const KNOWN_INJECT_SET = new Set(KNOWN_INJECT);
|
|
24
|
+
|
|
25
|
+
function isExperimental(finding) {
|
|
26
|
+
return finding.check === "manifest" && finding.file === "package.json" && finding.message.includes("fuyu.experimental");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function fromFinding(finding) {
|
|
30
|
+
return {
|
|
31
|
+
code: `review:${finding.check}`,
|
|
32
|
+
file: finding.file,
|
|
33
|
+
line: finding.line,
|
|
34
|
+
message: finding.detail ? `${finding.message}: ${finding.detail}` : finding.message,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function safeReadManifest(dir) {
|
|
39
|
+
try {
|
|
40
|
+
return readManifest(dir);
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function checkInterfaces(pkg, appKey, allowExperimental) {
|
|
47
|
+
const app = pkg?.[appKey];
|
|
48
|
+
if (!app || typeof app !== "object" || Array.isArray(app)) return [];
|
|
49
|
+
const experimental = new Set(Array.isArray(app.experimental) ? app.experimental.filter((item) => typeof item === "string") : []);
|
|
50
|
+
const issues = [];
|
|
51
|
+
if (Array.isArray(app.slots)) {
|
|
52
|
+
app.slots.forEach((slot, index) => {
|
|
53
|
+
if (typeof slot !== "string" || slot.trim() === "") return;
|
|
54
|
+
if (Object.hasOwn(SLOTS, slot)) {
|
|
55
|
+
if (SLOTS[slot].experimental === true) {
|
|
56
|
+
if (allowExperimental && (experimental.has(slot) || experimental.has("windows/v1"))) return;
|
|
57
|
+
issues.push({
|
|
58
|
+
path: `${appKey}.slots[${index}]`,
|
|
59
|
+
code: "experimental",
|
|
60
|
+
message: `"${slot}" is an experimental slot and requires ${appKey}.experimental to include "${slot}" or "windows/v1"`,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (allowExperimental && experimental.has(slot)) return;
|
|
66
|
+
issues.push({
|
|
67
|
+
path: `${appKey}.slots[${index}]`,
|
|
68
|
+
code: "unknown-slot",
|
|
69
|
+
message: `"${slot}" is not a known slot (known slots: ${KNOWN_SLOTS.join(", ")})`,
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
for (const field of ["provides", "replaces"]) {
|
|
74
|
+
const list = app[field];
|
|
75
|
+
if (!Array.isArray(list)) continue;
|
|
76
|
+
list.forEach((name, index) => {
|
|
77
|
+
if (typeof name !== "string" || name.trim() === "") return;
|
|
78
|
+
if (SERVICE_NAME.test(name)) return;
|
|
79
|
+
issues.push({
|
|
80
|
+
path: `${appKey}.${field}[${index}]`,
|
|
81
|
+
code: "service-name",
|
|
82
|
+
message: `"${name}" is not a valid service name (expected <name>/v<version>, e.g. "timers/v1")`,
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
if (app.inject && typeof app.inject === "object" && !Array.isArray(app.inject)) {
|
|
87
|
+
for (const kind of ["required", "optional"]) {
|
|
88
|
+
const list = app.inject[kind];
|
|
89
|
+
if (!Array.isArray(list)) continue;
|
|
90
|
+
list.forEach((name, index) => {
|
|
91
|
+
if (typeof name !== "string" || name.trim() === "") return;
|
|
92
|
+
if (name === "windows") {
|
|
93
|
+
if (allowExperimental && (experimental.has("windows") || experimental.has("windows/v1"))) return;
|
|
94
|
+
issues.push({
|
|
95
|
+
path: `${appKey}.inject.${kind}[${index}]`,
|
|
96
|
+
code: "experimental",
|
|
97
|
+
message: `"windows" is experimental and requires ${appKey}.experimental to include "windows/v1"`,
|
|
98
|
+
});
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (SERVICE_NAME.test(name) || KNOWN_INJECT_SET.has(name)) return;
|
|
102
|
+
if (allowExperimental && experimental.has(name)) return;
|
|
103
|
+
issues.push({
|
|
104
|
+
path: `${appKey}.inject.${kind}[${index}]`,
|
|
105
|
+
code: "service-name",
|
|
106
|
+
message: `"${name}" is not a valid service name (expected <name>/v<version> such as "timers/v1", or one of ${KNOWN_INJECT.join(", ")})`,
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return issues;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function check(dir, options = {}) {
|
|
115
|
+
const root = resolve(dir);
|
|
116
|
+
const appKey = options.appKey ?? "fuyu";
|
|
117
|
+
const allowExperimental = options.allowExperimental ?? true;
|
|
118
|
+
const errors = [];
|
|
119
|
+
const warnings = [];
|
|
120
|
+
const scratch = createScratch("fuyu-plugin-check-");
|
|
121
|
+
try {
|
|
122
|
+
let built = null;
|
|
123
|
+
let manifest = null;
|
|
124
|
+
try {
|
|
125
|
+
const result = await build(root, { ...options, outDir: scratch.dir("package") });
|
|
126
|
+
warnings.push(...result.warnings);
|
|
127
|
+
built = result.outDir;
|
|
128
|
+
manifest = result.manifest;
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (!(error instanceof BuildError)) throw error;
|
|
131
|
+
errors.push({ code: "build", message: error.message }, ...error.issues);
|
|
132
|
+
}
|
|
133
|
+
errors.push(...checkInterfaces(manifest ?? safeReadManifest(root), appKey, allowExperimental));
|
|
134
|
+
const report = review({ newDir: snapshotDirectory(root, scratch.dir("source")), newBuilt: built });
|
|
135
|
+
const listed = new Set(report.summary.dependencies.added.map((item) => `${item.name}@${item.versions.join(", ")}`));
|
|
136
|
+
const domains = new Set(warnings.filter((issue) => issue.code === "undeclared-domain").map((issue) => issue.domain));
|
|
137
|
+
const undeclared = new Set(report.summary.network.undeclared);
|
|
138
|
+
const findings = report.findings.filter((finding) => {
|
|
139
|
+
if (finding.check === "release" || finding.check === "nodeApis") return false;
|
|
140
|
+
if (finding.check === "manifest") return finding.tier === "red" && !(allowExperimental && isExperimental(finding));
|
|
141
|
+
if (finding.check === "network") return undeclared.has(finding.detail) && !domains.has(finding.detail);
|
|
142
|
+
return !(finding.check === "dependencies" && listed.has(finding.detail));
|
|
143
|
+
});
|
|
144
|
+
for (const finding of findings) {
|
|
145
|
+
if (finding.tier === "red") errors.push(fromFinding(finding));
|
|
146
|
+
else if (finding.tier === "yellow") warnings.push(fromFinding(finding));
|
|
147
|
+
}
|
|
148
|
+
return { ok: errors.length === 0, errors, warnings, findings, manifest };
|
|
149
|
+
} finally {
|
|
150
|
+
scratch.dispose();
|
|
151
|
+
}
|
|
152
|
+
}
|