turbo-sync/index.js

91 lines
2.3 KiB
JavaScript
Raw Normal View History

2024-10-16 04:44:48 +00:00
#! /usr/bin/env node
// @ts-check
const fs = require("fs");
const path = require("path");
const { execSync, spawn, ChildProcess } = require("child_process");
/** @type {string[]} */
let dirs = [];
console.log("Running Folder Sync ...");
const defaultConfigFilePath = path.resolve(
process.cwd(),
"turbosync.config.json"
);
const confFileProvidedPath = process.argv[process.argv.length - 1];
const confFileComputedPath =
typeof confFileProvidedPath == "string" &&
confFileProvidedPath.endsWith(".json")
? path.resolve(process.cwd(), confFileProvidedPath)
: null;
if (!fs.existsSync(defaultConfigFilePath) && !confFileComputedPath) {
console.log(
"Please Provide the path to a config file or add a config file named `turbosync.config.json` to the path you're running this program"
);
process.exit();
}
if (
!defaultConfigFilePath &&
confFileComputedPath &&
!fs.existsSync(confFileComputedPath)
) {
console.log("Config File does not exist");
process.exit();
}
// /** @type {ChildProcess[]} */
// const childProcesses = [];
try {
const configJSON = fs.existsSync(defaultConfigFilePath)
? fs.readFileSync(defaultConfigFilePath, "utf8")
: confFileComputedPath
? fs.readFileSync(confFileComputedPath, "utf8")
: null;
if (!configJSON)
throw new Error(
"Config JSON could not be resolved. Please check your files."
);
/** @type {TurboSyncConfigArray} */
const configArray = JSON.parse(configJSON);
for (let i = 0; i < configArray.length; i++) {
const config = configArray[i];
console.log(`Syncing \`${config.title} ...\``);
const childProcess = spawn(
"node",
[
path.resolve(__dirname, "./lib/sync.js"),
`${JSON.stringify(config)}`,
],
{
stdio: "inherit",
detached: false,
}
);
}
} catch (error) {
console.log(`Process Error =>`, error.message);
process.exit();
}
setInterval(() => {
console.log("Turbo Sync Running ...");
}, 60000);
// process.on("exit", () => {
// for (let i = 0; i < childProcesses.length; i++) {
// const childProcess = childProcesses[i];
// childProcess.kill("SIGTERM");
// }
// });