Major update: Add Environment variables support

This commit is contained in:
Benjamin Toby 2024-10-16 09:40:21 +01:00
parent ed5248a43f
commit f56a63b2f2
8 changed files with 110 additions and 22 deletions

View File

@ -73,3 +73,14 @@ The config file is a json file that contains all the information needed to run t
}
]
```
You can also use environment variables in the config file. Example:
```json
[
{
"title": "Sync Folders",
"folders": ["$FOLDER_1", "$FOLDER_2"]
}
]
```

View File

@ -3,10 +3,8 @@
const fs = require("fs");
const path = require("path");
const { execSync, spawn, ChildProcess } = require("child_process");
/** @type {string[]} */
let dirs = [];
const { spawn } = require("child_process");
const handleEnvVars = require("./utils/env");
const confFileProvidedPath = process.argv[process.argv.length - 1];
@ -23,7 +21,7 @@ if (confFileProvidedPath === "--version" || confFileProvidedPath === "-v") {
);
}
process.exit();
process.exit(6);
}
console.log("Running Folder Sync ...");
@ -56,9 +54,6 @@ if (
process.exit();
}
// /** @type {ChildProcess[]} */
// const childProcesses = [];
try {
const configJSON = fs.existsSync(defaultConfigFilePath)
? fs.readFileSync(defaultConfigFilePath, "utf8")
@ -71,8 +66,10 @@ try {
"Config JSON could not be resolved. Please check your files."
);
const parsedConfigJSON = handleEnvVars({ json: configJSON });
/** @type {TurboSyncConfigArray} */
const configArray = JSON.parse(configJSON);
const configArray = JSON.parse(parsedConfigJSON);
for (let i = 0; i < configArray.length; i++) {
const config = configArray[i];
@ -96,12 +93,7 @@ try {
}
setInterval(() => {
console.log("Turbo Sync Running ...");
console.log(
`Turbo Sync Running for ${process.uptime().toLocaleString()}s ...`
);
}, 60000);
// process.on("exit", () => {
// for (let i = 0; i < childProcesses.length; i++) {
// const childProcess = childProcesses[i];
// childProcess.kill("SIGTERM");
// }
// });

View File

@ -2,9 +2,7 @@
// @ts-check
const fs = require("fs");
const path = require("path");
const { execSync, spawn } = require("child_process");
const { spawn } = require("child_process");
const watchFiles = require("./watch/files");
const watchFolders = require("./watch/folders");

View File

@ -111,6 +111,14 @@ function sync({ options, filePath, files }) {
const dstFl = destFiles[j];
if (typeof dstFl == "string") {
if (!fs.existsSync(dstFl)) continue;
if (filePath === dstFl) {
console.log(
`You can't sync the same paths. Please check your configuration and resolve duplicate paths`
);
process.exit(6);
}
cmdArray.push(filePath, dstFl);
const cmd = cmdArray.join(" ");
execSync(cmd, {
@ -119,6 +127,13 @@ function sync({ options, filePath, files }) {
} else if (dstFl.path) {
if (!dstFl.host && !fs.existsSync(dstFl.path)) continue;
if (filePath === dstFl.path) {
console.log(
`You can't sync the same paths. Please check your configuration and resolve duplicate paths`
);
process.exit(6);
}
if (dstFl.host && dstFl.ssh_key && dstFl.user) {
cmdArray.push("-e", `'ssh -i ${dstFl.ssh_key}'`);
cmdArray.push(

View File

@ -120,6 +120,14 @@ function sync({ options, dirs, dirPath, init }) {
const dstDr = dstDirs[j];
if (typeof dstDr == "string") {
if (!fs.existsSync(dstDr)) continue;
if (dirPath === dstDr) {
console.log(
`You can't sync the same paths. Please check your configuration and resolve duplicate paths`
);
process.exit(6);
}
cmdArray.push(
path.normalize(dirPath) + "/",
path.normalize(dstDr) + "/"
@ -131,6 +139,13 @@ function sync({ options, dirs, dirPath, init }) {
} else if (dstDr.path) {
if (!dstDr.host && !fs.existsSync(dstDr.path)) continue;
if (dirPath === dstDr.path) {
console.log(
`You can't sync the same paths. Please check your configuration and resolve duplicate paths`
);
process.exit(6);
}
if (dstDr.host && dstDr.ssh_key && dstDr.user) {
cmdArray.push("-e", `'ssh -i ${dstDr.ssh_key}'`);
cmdArray.push(

View File

@ -53,3 +53,8 @@
* @property {string} dirPath
* @property {boolean} [init] - is this an initialization phase?
*/
/**
* @typedef {object} HandleEnvVarsFnParams
* @property {string} json
*/

View File

@ -5,11 +5,11 @@
* @param {number} [time]
* @returns
*/
async function delay(time) {
async function delay(time = 500) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, time || 500);
}, time);
});
}

52
utils/env.js Normal file
View File

@ -0,0 +1,52 @@
// @ts-check
const fs = require("fs");
const path = require("path");
/**
*
* @param {HandleEnvVarsFnParams} param0
* @returns {string}
*/
function handleEnvVars({ json }) {
let newJson = json;
try {
let envVars = { ...process.env };
const localEnvFilePath = path.resolve(process.cwd(), "./.env");
if (fs.existsSync(localEnvFilePath)) {
const localEnvText = fs.readFileSync(localEnvFilePath, "utf8");
const localEnvKeyPairArray = localEnvText
.split("\n")
.filter(
(keyPair) =>
keyPair &&
keyPair.match(/.{3,}/) &&
!keyPair.match(/^\#/)
)
.map((keyPair) => keyPair.trim());
localEnvKeyPairArray.forEach((keyPair) => {
let keyPairArray = keyPair.split("=");
const key = keyPairArray.shift();
const value = keyPairArray.join("=");
const newEnvObject = {};
newEnvObject[key] = value;
envVars = { ...envVars, ...newEnvObject };
});
}
for (let key in envVars) {
newJson = newJson.replaceAll(`$${key}`, String(envVars[key]));
}
} catch (error) {
console.log(`Error replacing Environment variables`, error.message);
return json;
}
return newJson;
}
module.exports = handleEnvVars;