86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
import { spawn } from "child_process";
|
|
import watchFiles from "./watch/files";
|
|
import watchFolders from "./watch/folders";
|
|
import { TurboSyncConfigObject } from "../types";
|
|
import getLatestSource from "../utils/get-last-edited-src";
|
|
import fldFileToStrArr, {
|
|
fldFileToStr,
|
|
} from "../utils/grab-folders-files-string-paths";
|
|
|
|
const confFileProvidedJSON = process.argv[process.argv.length - 1];
|
|
|
|
global.SYNC_SUCCESS_EXIT_CODE = 32;
|
|
global.CONFIG_DIR = process.cwd();
|
|
global.SYNCING = false;
|
|
|
|
const REWATCH_TIMEOUT = 1000;
|
|
|
|
try {
|
|
const configFileObject: TurboSyncConfigObject =
|
|
JSON.parse(confFileProvidedJSON);
|
|
|
|
const lastUpdated = getLatestSource({
|
|
dirs: fldFileToStrArr(configFileObject.folders),
|
|
files: fldFileToStrArr(configFileObject.files),
|
|
});
|
|
|
|
console.log(`Running '${configFileObject.title}' ...`);
|
|
console.log(`Last Updated Path => '${lastUpdated || "N/A"}' ...`);
|
|
|
|
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;
|
|
|
|
const sortedFoldersByLastUpdated =
|
|
folders?.[0] && lastUpdated
|
|
? [
|
|
lastUpdated,
|
|
...(folders?.filter(
|
|
(fl) => fldFileToStr(fl) !== lastUpdated
|
|
) || []),
|
|
]
|
|
: folders;
|
|
|
|
const sortedFilesByLastUpdated =
|
|
files?.[0] && lastUpdated
|
|
? [
|
|
lastUpdated,
|
|
...(files?.filter((fl) => fldFileToStr(fl) !== lastUpdated) ||
|
|
[]),
|
|
]
|
|
: files;
|
|
|
|
if (firstFile && sortedFilesByLastUpdated?.[0]) {
|
|
watchFiles({ files: sortedFilesByLastUpdated, options });
|
|
} else if (firstFolder && sortedFoldersByLastUpdated?.[0]) {
|
|
watchFolders({ folders: sortedFoldersByLastUpdated, options });
|
|
}
|
|
} catch (error) {
|
|
console.log(error);
|
|
process.exit();
|
|
}
|
|
|
|
process.on("exit", (code) => {
|
|
if (code == global.SYNC_SUCCESS_EXIT_CODE) {
|
|
const args = process.argv;
|
|
const cmd = args.shift();
|
|
if (cmd) {
|
|
spawn(cmd, args, {
|
|
stdio: "inherit",
|
|
});
|
|
}
|
|
} else {
|
|
process.exit(0);
|
|
}
|
|
});
|