From 68558c34ad8e5a4e54c36fd72bb52f7392cc3374 Mon Sep 17 00:00:00 2001 From: gitgrimbo Date: Fri, 5 Jun 2020 10:10:17 +0100 Subject: [PATCH] async config --- bin/cli.js | 76 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 4f0dad8..3084c4e 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -11,50 +11,54 @@ const combineConfig = require('../lib/helpers/combine-config'); const errorHandler = require('../lib/helpers/error-handler'); const successHandler = require('../lib/helpers/success-handler'); -//Extract parameters -const {configFile} = argv; +async function main() { + //Extract parameters + const {configFile} = argv; -//Verify arguments -if (argv._.length < 3 && !configFile) { - errorHandler('Replace in file needs at least 3 arguments'); -} + //Verify arguments + if (argv._.length < 3 && !configFile) { + errorHandler('Replace in file needs at least 3 arguments'); + } -//Load config and combine with passed arguments -const config = loadConfig(configFile); -const options = combineConfig(config, argv); + //Load config and combine with passed arguments + const config = await loadConfig(configFile); + const options = combineConfig(config, argv); -//Extract settings -const {from, to, files, isRegex, verbose, quiet} = options; + //Extract settings + const {from, to, files, isRegex, verbose, quiet} = options; -//Single star globs already get expanded in the command line -options.files = files.reduce((files, file) => { - return files.concat(file.split(',')); -}, []); + //Single star globs already get expanded in the command line + options.files = files.reduce((files, file) => { + return files.concat(file.split(',')); + }, []); + + //If the isRegex flag is passed, convert the from parameter to a RegExp object + if (isRegex) { + const flags = from.replace(/.*\/([gimyus]*)$/, '$1'); + const pattern = from.replace(new RegExp(`^/(.*?)/${flags}$`), '$1'); + try { + options.from = new RegExp(pattern, flags); + } + catch (error) { + errorHandler(error, 'Error creating RegExp from `from` parameter'); + } + } + + //Log + if (!quiet) { + console.log(`Replacing '${from}' with '${to}'`); + } -//If the isRegex flag is passed, convert the from parameter to a RegExp object -if (isRegex) { - const flags = from.replace(/.*\/([gimyus]*)$/, '$1'); - const pattern = from.replace(new RegExp(`^/(.*?)/${flags}$`), '$1'); + //Replace try { - options.from = new RegExp(pattern, flags); + const results = replace.sync(options); + if (!quiet) { + successHandler(results, verbose); + } } catch (error) { - errorHandler(error, 'Error creating RegExp from `from` parameter'); + errorHandler(error); } } -//Log -if (!quiet) { - console.log(`Replacing '${from}' with '${to}'`); -} - -//Replace -try { - const results = replace.sync(options); - if (!quiet) { - successHandler(results, verbose); - } -} -catch (error) { - errorHandler(error); -} +main().catch((error) => errorHandler(error));