Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const exitWithSuccess = async message => {
if (message) {
console.log(chalk.green(message)); // eslint-disable-line no-console
}
await logger.close();
await killAllChildProcesses();
process.exit(0);
};
const exitWithError = async error => {
if (error instanceof UserError) {
const coloredErrorMessage = colorRedIfNotYetColored(error.message);
console.error(coloredErrorMessage); // eslint-disable-line no-console
} else if (error instanceof EditorError) {
logger.error(error);
if (error.userMessage) {
const coloredErrorMessage = colorRedIfNotYetColored(error.userMessage);
console.error(coloredErrorMessage); // eslint-disable-line no-console
}
} else {
logger.error(error);
}
await logger.close();
await killAllChildProcesses();
process.exit(1);
};
async function startServer(siteRootPath, options) {
logger.info(
getMessage("Server_Start_Log", { path: path.resolve(siteRootPath) })
);
const siteSrcPath = projectPaths.siteSrcPath(siteRootPath);
await fs.ensureDir(siteSrcPath);
const isEmpty = await isEmptyDir(siteSrcPath);
const isWix = await fs.exists(
path.join(siteRootPath, ".corvid", "corvidrc.json")
); // TEMPORARY
const siteBackupPath = projectPaths.backupPath(siteRootPath);
const hasBackup = await fs.exists(siteBackupPath);
if (hasBackup) {
logger.info(getMessage("Server_Backup_Found_Log"));
throw new UserError("BACKUP_FOLDER_EXISTS");
}
if (isEdit(options)) {
if (!isWix) {
logger.info(getMessage("Server_Edit_Project_Not_Found_Log"));
throw new UserError("CAN_NOT_EDIT_NON_WIX_SITE");
}
if (isEmpty) {
logger.info(getMessage("Server_Edit_Empty_Site_Log"));
throw new UserError("CAN_NOT_EDIT_EMPTY_SITE");
}
}
if (isClone(options)) {
if (!isWix) {
logger.info(getMessage("Server_Clone_Project_Not_Found_Log"));
const isEmpty = await isEmptyDir(siteSrcPath);
const isWix = await fs.exists(
path.join(siteRootPath, ".corvid", "corvidrc.json")
); // TEMPORARY
const siteBackupPath = projectPaths.backupPath(siteRootPath);
const hasBackup = await fs.exists(siteBackupPath);
if (hasBackup) {
logger.info(getMessage("Server_Backup_Found_Log"));
throw new UserError("BACKUP_FOLDER_EXISTS");
}
if (isEdit(options)) {
if (!isWix) {
logger.info(getMessage("Server_Edit_Project_Not_Found_Log"));
throw new UserError("CAN_NOT_EDIT_NON_WIX_SITE");
}
if (isEmpty) {
logger.info(getMessage("Server_Edit_Empty_Site_Log"));
throw new UserError("CAN_NOT_EDIT_EMPTY_SITE");
}
}
if (isClone(options)) {
if (!isWix) {
logger.info(getMessage("Server_Clone_Project_Not_Found_Log"));
throw new UserError("CAN_NOT_CLONE_NON_WIX_SITE");
}
if (!isEmpty) {
logger.info(getMessage("Server_Clone_Project_Not_Empty_Site_Log"));
throw new UserError("CAN_NOT_PULL_NON_EMPTY_SITE");
}
const { killAllChildProcesses } = require("./utils/electron");
notifyOnUpdatedPackages();
// eslint-disable-next-line no-console
console.log(chalk.yellow(getMessage("Cli_Description_Yellow")));
// eslint-disable-next-line no-console
console.log(getMessage("Cli_Description"));
process.on("exit", () => killAllChildProcesses());
process.on("SIGINT", () => killAllChildProcesses());
process.on("SIGTERM", () => killAllChildProcesses());
const fullCommand = process.argv.slice(2).join(" ");
logger.info(`running [${fullCommand}]`);
logger.addSessionData({
command: process.argv[2],
fullCommand
});
sessionData.on(["msid", "uuid"], (metasiteId, userId) =>
logger.addSessionData({ metasiteId, userId })
);
require("yargs")
.usage("Usage: $0 [options]")
.commandDir("commands")
.help("help")
.strict()
.demandCommand().argv;
const siteBackupPath = projectPaths.backupPath(siteRootPath);
const hasBackup = await fs.exists(siteBackupPath);
if (hasBackup) {
logger.info(getMessage("Server_Backup_Found_Log"));
throw new UserError("BACKUP_FOLDER_EXISTS");
}
if (isEdit(options)) {
if (!isWix) {
logger.info(getMessage("Server_Edit_Project_Not_Found_Log"));
throw new UserError("CAN_NOT_EDIT_NON_WIX_SITE");
}
if (isEmpty) {
logger.info(getMessage("Server_Edit_Empty_Site_Log"));
throw new UserError("CAN_NOT_EDIT_EMPTY_SITE");
}
}
if (isClone(options)) {
if (!isWix) {
logger.info(getMessage("Server_Clone_Project_Not_Found_Log"));
throw new UserError("CAN_NOT_CLONE_NON_WIX_SITE");
}
if (!isEmpty) {
logger.info(getMessage("Server_Clone_Project_Not_Empty_Site_Log"));
throw new UserError("CAN_NOT_PULL_NON_EMPTY_SITE");
}
}
if (isPullForce(options)) {
if (!isWix) {
client.on("user-message", message => {
logger.info(getMessage("Pull_User_Message_Log", { message }));
console.log(message);
});
client.on("user-message", message => {
logger.info(getMessage("OpenEditor_User_Message_Log", { message }));
console.log(message);
});
throw new UserError("CAN_NOT_EDIT_NON_WIX_SITE");
}
if (isEmpty) {
logger.info(getMessage("Server_Edit_Empty_Site_Log"));
throw new UserError("CAN_NOT_EDIT_EMPTY_SITE");
}
}
if (isClone(options)) {
if (!isWix) {
logger.info(getMessage("Server_Clone_Project_Not_Found_Log"));
throw new UserError("CAN_NOT_CLONE_NON_WIX_SITE");
}
if (!isEmpty) {
logger.info(getMessage("Server_Clone_Project_Not_Empty_Site_Log"));
throw new UserError("CAN_NOT_PULL_NON_EMPTY_SITE");
}
}
if (isPullForce(options)) {
if (!isWix) {
logger.info(getMessage("Server_PullForce_Project_Not_Found_Log"));
throw new UserError("CAN_NOT_PULL_NON_WIX_SITE");
}
await fs.emptyDir(siteSrcPath);
}
if (isPullMove(options)) {
if (!isWix) {
logger.info(getMessage("Server_PullMove_Project_Not_Found_Log"));
throw new UserError("CAN_NOT_PULL_NON_WIX_SITE");
}
fs.readFile(configFilePath(dir), (exc, config) => {
if (exc) {
if (exc.code === "ENOENT") {
reject(
new UserError(
getMessage("CorvidConfig_No_Project_Error", {
dir: path.resolve(dir)
})
)
);
} else {
reject(exc);
}
} else {
resolve(config);
}
});
});