From 7afc17ba6d0b559977087f4bee27383a3ef9c47a Mon Sep 17 00:00:00 2001 From: Mohammad Fares Date: Mon, 1 Oct 2018 12:29:54 +0300 Subject: [PATCH] Add the init command --- commands/init.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 commands/init.js diff --git a/commands/init.js b/commands/init.js new file mode 100644 index 0000000..7848366 --- /dev/null +++ b/commands/init.js @@ -0,0 +1,78 @@ +/** + * Init + * Create a global config directory for Terminalizer + * + * - Create a global config directory + * - For Windows, create it under `APPDATA` + * - For Linux and MacOS, create it under the home directory + * - Copy the default config into it + * + * @author Mohammad Fares + */ + +/** + * Executed after the command completes its task + */ +function done() { + + console.log(di.chalk.green('The global config directory is created at')); + console.log(di.chalk.magenta(di.utility.getGlobalDirectory())); + + // Terminate the app + process.exit(); + +} + +/** + * The command's main function + * + * @param {Object} argv + */ +function command(argv) { + + var globalPath = di.utility.getGlobalDirectory(); + + // Create the global directory + try { + + di.fs.mkdirSync(di.utility.getGlobalDirectory()); + + } catch (error) { + + // Ignore `already exists` error + if (error.code != 'EEXIST') { + throw error; + } + + } + + // Copy the default config file + di.fs.copySync(di.path.join(ROOT_PATH, 'config.yml'), + di.path.join(globalPath, 'config.yml'), + {overwrite: true}); + + done(); + +} + +//////////////////////////////////////////////////// +// Command Definition ////////////////////////////// +//////////////////////////////////////////////////// + +/** + * Command's usage + * @type {String} + */ +module.exports.command = 'init'; + +/** + * Command's description + * @type {String} + */ +module.exports.describe = 'Create a global config directory'; + +/** + * Command's handler function + * @type {Function} + */ +module.exports.handler = command;