type PathRole = "app-bundle" | "asar" | "extra-resource" | "user-data" | "temp" | "logs"; type Classification = { role: PathRole; writable: boolean; note: string; }; function classify(pathValue: string): Classification { if (pathValue.includes(".asar/") || pathValue.endsWith(".asar")) { return { role: "asar", writable: false, note: "ASAR 内只读,写入会在运行时失败", }; } if (pathValue.includes("/Contents/Resources/")) { return { role: "extra-resource", writable: false, note: "随包资源,升级会被覆盖,不能当用户数据", }; } if (/\/(User Data|Application Support)\//.test(pathValue)) { return { role: "user-data", writable: true, note: "按用户隔离,适合数据库与设置", }; } if (pathValue.startsWith("/tmp/") || pathValue.includes("/T/")) { return { role: "temp", writable: true, note: "可随时清理,不要放需要保留的数据", }; } if (pathValue.includes("/Logs/")) { return { role: "logs", writable: true, note: "日志目录,按大小滚动", }; } return { role: "app-bundle", writable: false, note: "应用包目录,开发态可能可写,打包后只读", }; } const rows: Array<[string, string]> = [ ["app", "/Applications/PathsAndAssets.app/Contents/Resources/app.asar"], ["extra", "/Applications/PathsAndAssets.app/Contents/Resources/extra/a.txt"], ["userData", "/Users/demo/Library/Application Support/PathsAndAssets"], ["temp", "/tmp/PathsAndAssets"], ["logs", "/Users/demo/Library/Logs/PathsAndAssets"], ["dev", "/Users/demo/project/app/.vite/build/assets/notice.txt"], ]; const results = rows.map(([label, value]) => ({ label, value, ...classify(value), })); const byLabel = Object.fromEntries(results.map((row) => [row.label, row])); const ok = byLabel.app?.role === "asar" && byLabel.app?.writable === false && byLabel.extra?.role === "extra-resource" && byLabel.extra?.writable === false && byLabel.userData?.role === "user-data" && byLabel.userData?.writable === true && byLabel.temp?.role === "temp" && byLabel.temp?.writable === true && byLabel.logs?.role === "logs" && byLabel.logs?.writable === true && byLabel.dev?.role === "app-bundle" && byLabel.dev?.writable === false; console.log(JSON.stringify({ evt: "smoke", ok, results }));