63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import fs from "fs";
|
|
import { spawn } from "child_process";
|
|
import watchFiles from "./watch/files";
|
|
import watchFolders from "./watch/folders";
|
|
import { TurboSyncConfigObject } from "../types";
|
|
import path from "path";
|
|
|
|
const confFileProvidedJSON = process.argv[process.argv.length - 1];
|
|
|
|
global.SYNC_SUCCESS_EXIT_CODE = 32;
|
|
global.CONFIG_DIR = process.cwd();
|
|
global.SYNCING_FILE = path.join(global.CONFIG_DIR, "syncing.txt");
|
|
|
|
try {
|
|
if (fs.existsSync(global.SYNCING_FILE)) {
|
|
process.exit(0);
|
|
}
|
|
|
|
const configFileObject: TurboSyncConfigObject =
|
|
JSON.parse(confFileProvidedJSON);
|
|
|
|
console.log(`Running '${configFileObject.title}' ...`);
|
|
|
|
if (
|
|
Array.isArray(configFileObject.files) &&
|
|
Array.isArray(configFileObject.folders)
|
|
) {
|
|
throw new Error("Choose wither `files` or `folders`. Not both");
|
|
}
|
|
|
|
const files = configFileObject?.files;
|
|
const firstFile = files?.[0];
|
|
const folders = configFileObject?.folders;
|
|
const firstFolder = folders?.[0];
|
|
|
|
const options = configFileObject.options;
|
|
|
|
if (firstFile && files?.[0]) {
|
|
watchFiles({ files, options });
|
|
} else if (firstFolder && folders?.[0]) {
|
|
watchFolders({ folders, options });
|
|
}
|
|
} catch (error) {
|
|
console.log(error);
|
|
process.exit();
|
|
}
|
|
|
|
process.on("exit", (code) => {
|
|
if (code == global.SYNC_SUCCESS_EXIT_CODE) {
|
|
fs.unlinkSync(global.SYNCING_FILE);
|
|
|
|
const args = process.argv;
|
|
const cmd = args.shift();
|
|
if (cmd) {
|
|
spawn(cmd, args, {
|
|
stdio: "inherit",
|
|
});
|
|
}
|
|
} else {
|
|
process.exit(0);
|
|
}
|
|
});
|