Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 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) {
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);
}
});
});
const startInCloneMode = (
siteRootPath,
options = { override: false, move: false }
) => {
if (options.override && options.move) {
throw new UserError(getMessage("Server_Override_And_Move_Error"));
}
let type = "CLONE";
if (options.move) {
type = "MOVE_PULL";
}
if (options.override) {
type = "FORCE_PULL";
}
return startServer(siteRootPath, {
type
});
};
const startInEditMode = siteRootPath =>
error: error => {
spinner.fail();
const errorMessage = getMessage(error);
if (errorMessage) {
if (error === "CAN_NOT_PULL_NON_EMPTY_SITE") {
reject(
new UserError(`${chalk.red(
getMessage("Pull_Command_Not_Empty_Red_Log")
)}
${chalk(getMessage("Pull_Command_Not_Empty_Log"))}`)
);
} else {
reject(new UserError(errorMessage));
}
} else {
reject(new Error(error));
}
}
},
error: error => {
spinner.fail();
const errorMessage = getMessage(error);
if (errorMessage) {
if (error === "CAN_NOT_PULL_NON_EMPTY_SITE") {
reject(
new UserError(`${chalk.red(
getMessage("Pull_Command_Not_Empty_Red_Log")
)}
${chalk(getMessage("Pull_Command_Not_Empty_Log"))}`)
);
} else {
reject(new UserError(errorMessage));
}
} else {
reject(new Error(error));
}
}
},
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");
}
const snapshotFolder = path.join(
siteRootPath,
".corvid",
"snapshots",
Date.now().toString()
);