Convert Windows app to Electron and add Android Capacitor app with CI builds
This commit is contained in:
95
android-app/scripts/sync-ui.js
Normal file
95
android-app/scripts/sync-ui.js
Normal file
@@ -0,0 +1,95 @@
|
||||
import fs from "fs-extra";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const root = path.resolve(__dirname, "..", "..");
|
||||
const source = path.join(root, "chat", "public");
|
||||
const dest = path.resolve(__dirname, "..", "www");
|
||||
|
||||
async function copyUi() {
|
||||
if (!(await fs.pathExists(source))) {
|
||||
throw new Error(`Source UI folder not found: ${source}`);
|
||||
}
|
||||
|
||||
await fs.emptyDir(dest);
|
||||
await fs.copy(source, dest, {
|
||||
filter: (src) => !src.endsWith(".map") && !src.includes(".DS_Store"),
|
||||
overwrite: true,
|
||||
});
|
||||
}
|
||||
|
||||
async function findHtmlFiles(dir) {
|
||||
const entries = await fs.readdir(dir, { withFileTypes: true });
|
||||
const files = await Promise.all(
|
||||
entries.map(async (entry) => {
|
||||
const full = path.join(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
return findHtmlFiles(full);
|
||||
}
|
||||
return entry.isFile() && entry.name.endsWith(".html") ? [full] : [];
|
||||
})
|
||||
);
|
||||
return files.flat();
|
||||
}
|
||||
|
||||
async function injectBridge() {
|
||||
const bridgeContent = `
|
||||
<script>
|
||||
window.nativeBridge = {
|
||||
async saveApiKey(token) {
|
||||
const { Preferences } = await import('./capacitor-bridge.js');
|
||||
return Preferences.set({ key: 'opencode_api_key', value: token });
|
||||
},
|
||||
async persistApp(app) {
|
||||
const { Preferences } = await import('./capacitor-bridge.js');
|
||||
return Preferences.set({ key: 'app_' + app.id, value: JSON.stringify(app) });
|
||||
},
|
||||
async listApps() {
|
||||
const { Preferences } = await import('./capacitor-bridge.js');
|
||||
const keys = await Preferences.keys();
|
||||
const apps = [];
|
||||
for (const key of keys.keys) {
|
||||
if (key.startsWith('app_')) {
|
||||
const { value } = await Preferences.get({ key });
|
||||
if (value) apps.push(JSON.parse(value));
|
||||
}
|
||||
}
|
||||
return apps;
|
||||
},
|
||||
async syncApp(appId) {
|
||||
const { syncApp } = await import('./capacitor-bridge.js');
|
||||
return syncApp(appId);
|
||||
},
|
||||
async runOpencodeTask(appId, taskName, args) {
|
||||
const { runOpencodeTask } = await import('./capacitor-bridge.js');
|
||||
return runOpencodeTask(appId, taskName, args);
|
||||
}
|
||||
};
|
||||
</script>
|
||||
`;
|
||||
|
||||
const files = await findHtmlFiles(dest);
|
||||
await Promise.all(
|
||||
files.map(async (fullPath) => {
|
||||
const html = await fs.readFile(fullPath, "utf8");
|
||||
if (html.includes("capacitor-bridge.js")) return;
|
||||
|
||||
const injection = bridgeContent + '\n </head>';
|
||||
const updated = html.replace(/<\/head>/i, injection);
|
||||
await fs.writeFile(fullPath, updated, "utf8");
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await copyUi();
|
||||
await injectBridge();
|
||||
console.log(`UI prepared in ${dest}`);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user