Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Exclude stale frontend bundles from Android development packages

Daniel3b8481a04491
diff --git a/app/scripts/androidAssets.test.js b/app/scripts/androidAssets.test.js
new file mode 100644
index 0000000000..9faf981c12
--- /dev/null
+++ b/app/scripts/androidAssets.test.js
@@ -0,0 +1,64 @@
+const {test} = require("node:test");
+const assert = require("node:assert/strict");
+const {mkdtempSync, mkdirSync, writeFileSync, rmSync} = require("node:fs");
+const os = require("node:os");
+const path = require("node:path");
+
+function fixture(t) {
+ const root = mkdtempSync(path.join(os.tmpdir(), "siyuan-android-assets-test-"));
+ t.after(() => {
+ assert.equal(path.dirname(path.resolve(root)), path.resolve(os.tmpdir()));
+ rmSync(root, {recursive: true, force: true});
+ });
+ const write = (name, content = "") => {
+ const file = path.join(root, name);
+ mkdirSync(path.dirname(file), {recursive: true});
+ writeFileSync(file, content);
+ };
+ write("build/mobile/index.html", '<script src="startup.12345678.js"></script><script src="main.12345678.js"></script><link href="base.12345678.css">');
+ write("build/mobile/startup.12345678.js");
+ write("build/mobile/main.12345678.js", '"chunk.12345678.js"');
+ write("build/mobile/chunk.12345678.js");
+ write("build/mobile/base.12345678.css");
+ write("build/desktop/index.html", '<script src="main.abcdef12.js"></script>');
+ write("build/desktop/main.abcdef12.js");
+ write("build/export/protyle-method.js");
+ write("build/export/base.css");
+ return {root, write};
+}
+
+test("Android assets retain serving, export and shared resources without historical or Electron bundles", async t => {
+ const {androidStageFiles} = await import("../../scripts/android-assets.mjs");
+ const {root, write} = fixture(t);
+ write("build/app/main.12345678.js");
+ write("build/desktop/main.87654321.js");
+ write("build/mobile/startup.87654321.js");
+ write("build/mobile/main.12345678.js.map");
+ write("protyle/js/editor.js");
+ write("icon.png");
+ assert.deepEqual(androidStageFiles(root), [
+ "stage/build/desktop/index.html",
+ "stage/build/desktop/main.abcdef12.js",
+ "stage/build/export/base.css",
+ "stage/build/export/protyle-method.js",
+ "stage/build/mobile/base.12345678.css",
+ "stage/build/mobile/chunk.12345678.js",
+ "stage/build/mobile/index.html",
+ "stage/build/mobile/main.12345678.js",
+ "stage/build/mobile/startup.12345678.js",
+ "stage/icon.png",
+ "stage/protyle/js/editor.js",
+ ]);
+});
+
+test("Android packaging rejects missing current bundles and serving/export entry points", async t => {
+ const {androidStageFiles} = await import("../../scripts/android-assets.mjs");
+ const {root} = fixture(t);
+ for (const name of ["build/mobile/main.12345678.js", "build/desktop/index.html", "build/export/base.css"]) {
+ const file = path.join(root, name);
+ const content = require("node:fs").readFileSync(file);
+ rmSync(file);
+ assert.throws(() => androidStageFiles(root), /Missing Android resource/);
+ writeFileSync(file, content);
+ }
+});
diff --git a/scripts/android-assets.mjs b/scripts/android-assets.mjs
new file mode 100644
index 0000000000..61931375d0
--- /dev/null
+++ b/scripts/android-assets.mjs
@@ -0,0 +1,39 @@
+import {readdirSync, readFileSync} from "node:fs";
+import path from "node:path";
+
+// Keep development output intact: open PC windows may still use older hashes.
+export function androidStageFiles(stageDir) {
+ const result = [];
+ function walk(directory, relative = "") {
+ for (const entry of readdirSync(directory, {withFileTypes: true})) {
+ const name = relative ? `${relative}/${entry.name}` : entry.name;
+ if (name === "build") continue;
+ if (entry.isDirectory()) walk(path.join(directory, entry.name), name);
+ else result.push(`stage/${name}`);
+ }
+ }
+ walk(stageDir);
+ for (const target of ["mobile", "desktop", "export"]) {
+ const directory = path.join(stageDir, "build", target);
+ const names = readdirSync(directory);
+ const required = target === "export" ? ["protyle-method.js", "base.css"] : ["index.html"];
+ for (const name of required) {
+ if (!names.includes(name)) throw new Error(`Missing Android resource: ${directory}/${name}`);
+ }
+ const hashed = names.filter(name => /\.[a-f\d]{8,}\./i.test(name) && !name.endsWith(".map"));
+ const selected = new Set(names.filter(name => !/\.[a-f\d]{8,}\./i.test(name) && !name.endsWith(".map")));
+ // Follow references from the current HTML and selected scripts/styles.
+ for (const name of selected) {
+ if (!/\.(html|js|css)$/.test(name)) continue;
+ const content = readFileSync(path.join(directory, name), "utf8");
+ for (const candidate of hashed) {
+ if (content.includes(candidate)) selected.add(candidate);
+ }
+ for (const match of content.matchAll(/(?:main|startup|base)\.[a-f\d]{8,}\.(?:js|css)\b/gi)) {
+ if (!names.includes(match[0])) throw new Error(`Missing Android resource: ${directory}/${match[0]}`);
+ }
+ }
+ for (const name of selected) result.push(`stage/build/${target}/${name}`);
+ }
+ return result.sort();
+}
diff --git a/scripts/android-dev-run.mjs b/scripts/android-dev-run.mjs
index ab63e40f8c..0d92780efe 100644
--- a/scripts/android-dev-run.mjs
+++ b/scripts/android-dev-run.mjs
@@ -4,10 +4,11 @@
// node scripts/android-dev-run.mjs [--flavor=official] [--android-dir=<路径>] [--device=<serial>] [--skip-ui] [--skip-kernel] [--skip-gradle]
// 流程: 构建 mobile 前端 -> 打包 app.zip -> gomobile 构建 kernel.aar -> gradle assemble -> adb 安装 -> 启动应用
import {spawnSync} from "node:child_process";
-import {copyFileSync, existsSync, readdirSync, readFileSync, statSync, unlinkSync} from "node:fs";
+import {copyFileSync, existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, unlinkSync, writeFileSync} from "node:fs";
import os from "node:os";
import path from "node:path";
import {fileURLToPath} from "node:url";
+import {androidStageFiles} from "./android-assets.mjs";
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const appDir = path.join(repoRoot, "app");
@@ -236,20 +237,35 @@ const zipPath = path.join(os.tmpdir(), "siyuan-android-app.zip");
if (existsSync(zipPath)) {
unlinkSync(zipPath);
}
-const tarArgs = ["-a", "-cf", zipPath,
- "-C", appDir, "appearance", "guide", "stage"];
+const tarEntries = ["appearance", "guide"];
+const stageFiles = androidStageFiles(path.join(appDir, "stage"));
const changelogsDir = path.join(appDir, "changelogs");
if (existsSync(changelogsDir)) {
- tarArgs.push("changelogs");
+ tarEntries.push("changelogs");
}
for (const file of ["LICENSE", "THIRD_PARTY_NOTICES.md"]) {
if (existsSync(path.join(repoRoot, file))) {
- tarArgs.push("-C", repoRoot, file);
+ tarEntries.push("-C", repoRoot, file);
} else {
console.log(`Warning: ${file} does not exist and will be skipped`);
}
}
-run("tar", tarArgs);
+// A file list avoids Windows command-line length limits.
+const listDir = mkdtempSync(path.join(os.tmpdir(), "siyuan-android-assets-"));
+try {
+ const listPath = path.join(listDir, "files.txt");
+ writeFileSync(listPath, stageFiles.join("\n") + "\n", "utf8");
+ // bsdtar requires -T before file operands; a later -T is treated as a filename.
+ const archive = spawnSync("tar", ["-a", "-cf", zipPath, "-C", appDir, "-T", listPath, ...tarEntries], {cwd: repoRoot, stdio: "inherit"});
+ if (archive.error || archive.status !== 0) {
+ throw archive.error || new Error(`tar failed with exit code ${archive.status}`);
+ }
+} finally {
+ if (path.dirname(path.resolve(listDir)) !== path.resolve(os.tmpdir())) {
+ throw new Error(`Unexpected temporary directory: ${listDir}`);
+ }
+ rmSync(listDir, {recursive: true, force: true});
+}
const androidAssets = path.join(androidDir, "app", "src", "main", "assets");
if (!existsSync(androidAssets)) {
fail(`Unexpected siyuan-android directory structure: ${androidAssets} does not exist. Specify the correct project path using --android-dir`);