diff --git a/src/bin/commands/init.js b/src/bin/commands/init.js index 5159a0ac..f4fdbcf4 100644 --- a/src/bin/commands/init.js +++ b/src/bin/commands/init.js @@ -2,7 +2,8 @@ import { cwd } from "node:process"; import path from "node:path"; -import { writeFile } from "node:fs/promises"; +import { existsSync } from "node:fs"; +import { writeFile, rename } from "node:fs/promises"; import { styleText } from "node:util"; import { DEFAULT_VUELESS_CONFIG_CONTNET } from "../constants.js"; @@ -24,6 +25,21 @@ export async function vuelessInit(options) { ext: fileExt, }); + if (existsSync(formattedDestPath)) { + const timestamp = new Date().valueOf(); + const renamedTarget = `${VUELESS_CONFIG_FILE_NAME}-backup-${timestamp}${fileExt}`; + + await rename(formattedDestPath, renamedTarget); + + const warnMessage = styleText( + "yellow", + // eslint-disable-next-line vue/max-len + `Current Vueless config backed into: '${path.basename(renamedTarget)}' folder. Don't forget to remove it before commit.`, + ); + + console.warn(warnMessage); + } + await writeFile(formattedDestPath, DEFAULT_VUELESS_CONFIG_CONTNET, "utf-8"); const successMessage = styleText( diff --git a/src/bin/index.js b/src/bin/index.js index afb1257f..a889925a 100644 --- a/src/bin/index.js +++ b/src/bin/index.js @@ -2,6 +2,8 @@ /* eslint-disable no-console */ +import { styleText } from "node:util"; + import { commands } from "./commands/index.js"; import { DEFAULT_EXIT_CODE, FAILURE_CODE } from "../constants.js"; @@ -16,9 +18,9 @@ try { if (command in commands) { commands[command](options); } else { - throw new Error(`There is no such command: ${command}`); + throw new Error(styleText("red", `There is no such command: ${command}`)); } } catch (error) { - console.error(error.message); + console.error(styleText("red", error.message)); process.exit(error.code || FAILURE_CODE); }