Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate config files on build time #218

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
generate config files on build time
  • Loading branch information
milahu committed Apr 1, 2024
commit 548f6ec2366713a4b3aa733a7164e668b9818c96
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);
}
}