Skip to content

Commit

Permalink
generate config files on build time
Browse files Browse the repository at this point in the history
  • Loading branch information
milahu committed Apr 1, 2024
1 parent 4c1a6ff commit 4d65a33
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions app/src/native-autoinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ async function PrepareFlatpak() {
}

async function install_uninstall(uninstall = false) {
if (process.env.VDHCOAPP_INSTALL_ON_BUILDTIME != "1") {
// VDHCOAPP_INSTALL_ON_BUILDTIME=1 vdhcoapp install
return await install_uninstall_on_runtime(uninstall);
}
let platform = os.platform();
if (platform == "darwin") {
let mode = GetMode();
Expand All @@ -182,3 +186,68 @@ export const uninstall = async () => {
console.log("Uninstalling…");
await install_uninstall(true);
};

async function fs_exists(path) {
try {
await fs.access(path, fs.constants.F_OK);
return true;
}
catch (e) {
if (e.errno == -2) {
return false;
}
throw e;
}
}

async function install_uninstall_on_runtime(uninstall) {
const glob = (await import('glob')).default;
const srcMainJsPath = await fs.realpath(process.argv[1]);
const srcConfigDirPath = path.join(path.dirname(srcMainJsPath), "../config", os.platform());
if (!await fs_exists(srcConfigDirPath)) {
console.error(`error: not found config dir: ${srcConfigDirPath}`);
process.exit(1);
}
const pattern = "**/*.json";
const options = {
cwd: srcConfigDirPath,
nodir: true, // Do not match directories, only files
follow: true, // Follow symlinked directories when expanding ** patterns
dot: true, // match hidden paths
//realpath: true, // call fs.realpath on all of the results
};
const homedir = os.homedir();
for (const configPath of glob.sync(pattern, options)) {
const dstConfigPath = path.join(homedir, configPath);
const dstConfigDirPath = path.join(homedir, path.dirname(configPath), "..");
if (!await fs_exists(dstConfigDirPath)) {
console.log(`missing ${dstConfigDirPath}`);
continue;
}
const srcConfigPath = await fs.realpath(path.join(srcConfigDirPath, configPath));
if (uninstall) {
if (await fs_exists(dstConfigPath)) {
console.log(`deleting ${dstConfigPath}`);
await fs.unlink(dstConfigPath);
}
continue;
}
if (!await fs_exists(dstConfigPath)) {
console.log(`creating ${dstConfigPath}`);
await fs.mkdir(path.dirname(dstConfigPath), { recursive: true });
await fs.symlink(srcConfigPath, dstConfigPath);
continue;
}
// check if file is up to date
const oldLinkTarget = await fs.realpath(dstConfigPath);
if (oldLinkTarget == srcConfigPath) {
// file is up to date
console.log(`keeping ${dstConfigPath}`);
continue;
}
// found different file. delete the old file
console.log(`replacing ${dstConfigPath}`);
await fs.unlink(dstConfigPath);
await fs.symlink(srcConfigPath, dstConfigPath);
}
}

0 comments on commit 4d65a33

Please sign in to comment.