2024-10-16 04:44:48 +00:00
|
|
|
#! /usr/bin/env node
|
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2024-10-16 08:40:21 +00:00
|
|
|
const { spawn } = require("child_process");
|
|
|
|
const handleEnvVars = require("./utils/env");
|
2024-10-16 04:44:48 +00:00
|
|
|
|
2024-10-16 07:29:28 +00:00
|
|
|
const confFileProvidedPath = process.argv[process.argv.length - 1];
|
|
|
|
|
|
|
|
if (confFileProvidedPath === "--version" || confFileProvidedPath === "-v") {
|
|
|
|
try {
|
|
|
|
const packageJson = fs.readFileSync(
|
|
|
|
path.resolve(__dirname, "package.json"),
|
|
|
|
"utf8"
|
|
|
|
);
|
|
|
|
console.log(`Turbo Sync Version: ${JSON.parse(packageJson).version}`);
|
|
|
|
} catch (error) {
|
|
|
|
console.log(
|
|
|
|
"Turbo Sync Version fetch failed! \nNo Worries, Turbo Sync is still installed properly"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-16 08:40:21 +00:00
|
|
|
process.exit(6);
|
2024-10-16 07:29:28 +00:00
|
|
|
}
|
|
|
|
|
2024-10-16 04:44:48 +00:00
|
|
|
console.log("Running Folder Sync ...");
|
|
|
|
|
|
|
|
const defaultConfigFilePath = path.resolve(
|
|
|
|
process.cwd(),
|
|
|
|
"turbosync.config.json"
|
|
|
|
);
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-10-16 11:39:14 +00:00
|
|
|
const configFinalPath = fs.existsSync(defaultConfigFilePath)
|
|
|
|
? defaultConfigFilePath
|
|
|
|
: confFileComputedPath && fs.existsSync(confFileComputedPath)
|
|
|
|
? confFileComputedPath
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const configJSON = configFinalPath
|
|
|
|
? fs.readFileSync(configFinalPath, "utf8")
|
2024-10-16 04:44:48 +00:00
|
|
|
: null;
|
|
|
|
|
|
|
|
if (!configJSON)
|
|
|
|
throw new Error(
|
|
|
|
"Config JSON could not be resolved. Please check your files."
|
|
|
|
);
|
|
|
|
|
2024-10-16 08:40:21 +00:00
|
|
|
const parsedConfigJSON = handleEnvVars({ json: configJSON });
|
|
|
|
|
2024-10-16 04:44:48 +00:00
|
|
|
/** @type {TurboSyncConfigArray} */
|
2024-10-16 08:40:21 +00:00
|
|
|
const configArray = JSON.parse(parsedConfigJSON);
|
2024-10-16 04:44:48 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2024-10-16 11:39:14 +00:00
|
|
|
|
|
|
|
// setTimeout(() => {
|
|
|
|
// if (configFinalPath) {
|
|
|
|
// fs.watchFile(configFinalPath, { interval: 2000 }, (curr, prev) => {
|
|
|
|
// process.exit(7);
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }, 1000);
|
2024-10-16 04:44:48 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(`Process Error =>`, error.message);
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
setInterval(() => {
|
2024-10-16 08:40:21 +00:00
|
|
|
console.log(
|
|
|
|
`Turbo Sync Running for ${process.uptime().toLocaleString()}s ...`
|
|
|
|
);
|
2024-10-16 04:44:48 +00:00
|
|
|
}, 60000);
|
2024-10-16 11:39:14 +00:00
|
|
|
|
|
|
|
// process.on("exit", (code) => {
|
|
|
|
// if (code === 7) {
|
|
|
|
// console.log("Config file changed. Restarting Turbo Sync service ...");
|
|
|
|
|
|
|
|
// const args = process.argv;
|
|
|
|
// const cmd = args.shift();
|
|
|
|
// if (cmd) {
|
|
|
|
// spawn(cmd, args, {
|
|
|
|
// stdio: "inherit",
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// });
|