From 288afb4129c01de2d20c2206fd1b03660d426791 Mon Sep 17 00:00:00 2001 From: kreuzerk Date: Sun, 21 Jul 2024 17:45:51 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20introduce=20commands?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bin/ng-parsel.bin.ts | 44 +++++++++++++++++++++------------------- src/commands/parse.ts | 30 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 src/commands/parse.ts diff --git a/src/bin/ng-parsel.bin.ts b/src/bin/ng-parsel.bin.ts index 02d01d9..3ab808c 100644 --- a/src/bin/ng-parsel.bin.ts +++ b/src/bin/ng-parsel.bin.ts @@ -11,15 +11,16 @@ import { CONFIG_DEFAULT_VALUES } from '../config/config.model.js'; import { writeJson } from '../utils/write.util.js'; import { parse } from '../parser/parser.js'; import { printWelcomeMessage } from '../utils/welcome.util.js'; +import { parseCommand } from '../commands/parse.js'; const program = new Command(); const explorer = cosmiconfigSync('ng-parsel'); program.version( /* - This is very complicated and could be done way simpler by using import assertion. - But import assertions are not supported by Node 14. - */ + This is very complicated and could be done way simpler by using import assertion. + But import assertions are not supported by Node 14. + */ JSON.parse(readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), '../../package.json')).toString() as any) .version ); @@ -37,10 +38,29 @@ program .option('-o, --out ', 'Output directory for generated files') .option('--singleFile', 'Output in a single file') .action((_srcGlob, options) => { + parseCommand(options); + }); + +program.command('init').action(() => { + printWelcomeMessage(); + console.log(chalk.cyan(`ng-parsel: creating configuration file`)); + + // EXTRACT TO UTILS + writeJson('.ng-parselrc.json', CONFIG_DEFAULT_VALUES); + + console.log(chalk.green(`ng-parsel: configuration successfully written to: .ng-parselrc`)); + console.log(chalk.magenta('====================================')); +}); + +program + .command('stats') + .option('-s, --src', 'Glob pattern to search for files') + .action(() => { printWelcomeMessage(); const configObject = explorer.search(); + // TODO: extract this if (configObject) { console.log( chalk.cyan( @@ -49,25 +69,7 @@ program ) ); parse(mergeOptionalConfigWithDefaults(configObject.config)); - } else { - console.log(chalk.cyan(`ng-parsel: no configuration found. CLI arguments will be used.`)); - options.src = './test-spa'; - - parse(mergeOptionalConfigWithDefaults(options)); } - - console.log(chalk.magenta('====================================')); }); -program.command('init').action(() => { - printWelcomeMessage(); - console.log(chalk.cyan(`ng-parsel: creating configuration file`)); - - // EXTRACT TO UTILS - writeJson('.ng-parselrc.json', CONFIG_DEFAULT_VALUES); - - console.log(chalk.green(`ng-parsel: configuration successfully written to: .ng-parselrc`)); - console.log(chalk.magenta('====================================')); -}); - program.parse(); diff --git a/src/commands/parse.ts b/src/commands/parse.ts new file mode 100644 index 0000000..1374506 --- /dev/null +++ b/src/commands/parse.ts @@ -0,0 +1,30 @@ +import chalk from 'chalk'; + +import { mergeOptionalConfigWithDefaults } from '../config/config.helper.js'; +import { printWelcomeMessage } from '../utils/welcome.util.js'; +import { cosmiconfigSync } from 'cosmiconfig'; +import { parse } from '../parser/parser.js'; + +const explorer = cosmiconfigSync('ng-parsel'); + +export function parseCommand(options: { [key: string]: string }) { + printWelcomeMessage(); + + const configObject = explorer.search(); + + if (configObject) { + console.log( + chalk.cyan( + `ng-parsel: configuration found under ${configObject.filepath}. + Configuraton from config file will be used.` + ) + ); + parse(mergeOptionalConfigWithDefaults(configObject.config)); + } else { + console.log(chalk.cyan(`ng-parsel: no configuration found. CLI arguments will be used.`)); + options['src'] = './test-spa'; + + parse(mergeOptionalConfigWithDefaults(options)); + } + console.log(chalk.magenta('====================================')); +}