From 3b0c0551dbc71bed42724580cd051864add1c084 Mon Sep 17 00:00:00 2001 From: kreuzerk Date: Sun, 21 Jul 2024 18:08:05 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20let=20the=20parser?= =?UTF-8?q?=20parse=20and=20nothing=20else?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/parse.ts | 120 ++++++++++++++++++++++++++----- src/parser/parse-output.model.ts | 19 +++++ src/parser/parser.ts | 97 ++++--------------------- src/utils/config.util.ts | 22 ++++++ 4 files changed, 157 insertions(+), 101 deletions(-) create mode 100644 src/parser/parse-output.model.ts create mode 100644 src/utils/config.util.ts diff --git a/src/commands/parse.ts b/src/commands/parse.ts index 1374506..6b8306d 100644 --- a/src/commands/parse.ts +++ b/src/commands/parse.ts @@ -1,30 +1,116 @@ import chalk from 'chalk'; +import { existsSync, mkdirSync } from 'fs'; -import { mergeOptionalConfigWithDefaults } from '../config/config.helper.js'; import { printWelcomeMessage } from '../utils/welcome.util.js'; -import { cosmiconfigSync } from 'cosmiconfig'; import { parse } from '../parser/parser.js'; +import { generateSpinner } from '../utils/spinner.util.js'; +import { NgParselConfig } from '../config/config.model.js'; +import { NgParselComponent } from '../parser/component/component.model.js'; +import { NgParselService } from '../parser/services/service.model.js'; +import { NgParselDirective } from '../parser/directive/directive.model.js'; +import { NgParselModule } from '../parser/module/module.model.js'; +import { NgParselSpec } from '../parser/spec/spec.model.js'; +import { NgParselHarness } from '../parser/harness/harness.model.js'; +import { NgParselPipe } from '../parser/pipe/pipe.model.js'; +import { NgParselValidtor } from '../parser/validator/validator.model.js'; +import { writeJson } from '../utils/write.util.js'; +import { NgParselOutput } from '../parser/parse-output.model.js'; +import { loadAndMergeConfig } from '../utils/config.util.js'; -const explorer = cosmiconfigSync('ng-parsel'); - -export function parseCommand(options: { [key: string]: string }) { +export function parseCommand(cliArgs: { [key: string]: string }) { printWelcomeMessage(); - const configObject = explorer.search(); + const config = loadAndMergeConfig(cliArgs); + + // TODO: clean this up later + config['src'] = './test-spa'; + + const parseOutput = parse(config); + writeParsedOutputToDisk(config, parseOutput); + + console.log(chalk.magenta('====================================')); +} + +function writeParsedOutputToDisk(config: NgParselConfig, parseOutput: NgParselOutput) { + const writeOutputSpinner = generateSpinner('Write output files'); + try { + writeOutputSpinner.start(); - if (configObject) { - console.log( - chalk.cyan( - `ng-parsel: configuration found under ${configObject.filepath}. - Configuraton from config file will be used.` - ) + writeOutputFiles( + config, + parseOutput.ngParselComponents, + parseOutput.ngParselServices, + parseOutput.ngParselDirectives, + parseOutput.ngParselModules, + parseOutput.ngParselSpecs, + parseOutput.ngParselHarnesses, + parseOutput.ngParselPipes, + parseOutput.ngParselValidators ); - parse(mergeOptionalConfigWithDefaults(configObject.config)); + + writeOutputSpinner.succeed(`Files successfully written to ${config.out}`); + } catch (e) { + writeOutputSpinner.fail(`Failed to write output files: ${e}`); + } +} + +function writeOutputFiles( + config: NgParselConfig, + ngParselComponents: NgParselComponent[], + ngParselServices: NgParselService[], + ngParselDirectives: NgParselDirective[], + ngParselModules: NgParselModule[], + ngParselSpecs: NgParselSpec[], + ngParselHarnesses: NgParselHarness[], + ngParselPipes: NgParselPipe[], + ngParselValidators: NgParselValidtor[] +): void { + if (!existsSync(config.out as string)) { + mkdirSync(config.out as string, { recursive: true }); + } + + if (config.singleFile) { + writeJson(`${config.out}/ng-parsel.json`, [ + ...ngParselComponents, + ...ngParselServices, + ...ngParselModules, + ...ngParselDirectives, + ...ngParselSpecs, + ...ngParselHarnesses, + ...ngParselPipes, + ...ngParselValidators, + ]); } else { - console.log(chalk.cyan(`ng-parsel: no configuration found. CLI arguments will be used.`)); - options['src'] = './test-spa'; + if (ngParselComponents.length > 0) { + writeJson(`${config.out}/ng-parsel-components.json`, ngParselComponents); + } + + if (ngParselServices.length > 0) { + writeJson(`${config.out}/ng-parsel-services.json`, ngParselServices); + } + + if (ngParselModules.length > 0) { + writeJson(`${config.out}/ng-parsel-modules.json`, ngParselModules); + } - parse(mergeOptionalConfigWithDefaults(options)); + if (ngParselDirectives.length > 0) { + writeJson(`${config.out}/ng-parsel-directives.json`, ngParselDirectives); + } + + if (ngParselPipes.length > 0) { + writeJson(`${config.out}/ng-parsel-pipes.json`, ngParselPipes); + } + + if (ngParselSpecs.length > 0) { + writeJson(`${config.out}/ng-parsel-specs.json`, ngParselSpecs); + } + + if (ngParselHarnesses.length > 0) { + writeJson(`${config.out}/ng-parsel-harnesses.json`, ngParselHarnesses); + } + + if (ngParselValidators.length > 0) { + writeJson(`${config.out}/ng-parsel-validators.json`, ngParselValidators); + } } - console.log(chalk.magenta('====================================')); } diff --git a/src/parser/parse-output.model.ts b/src/parser/parse-output.model.ts new file mode 100644 index 0000000..ed4161e --- /dev/null +++ b/src/parser/parse-output.model.ts @@ -0,0 +1,19 @@ +import { NgParselComponent } from './component/component.model.js'; +import { NgParselSpec } from './spec/spec.model.js'; +import { NgParselValidtor } from './validator/validator.model.js'; +import { NgParselHarness } from './harness/harness.model.js'; +import { NgParselPipe } from './pipe/pipe.model.js'; +import { NgParselModule } from './module/module.model.js'; +import { NgParselDirective } from './directive/directive.model.js'; +import { NgParselService } from './services/service.model.js'; + +export interface NgParselOutput { + ngParselComponents: NgParselComponent[]; + ngParselSpecs: NgParselSpec[]; + ngParselValidators: NgParselValidtor[]; + ngParselHarnesses: NgParselHarness[]; + ngParselPipes: NgParselPipe[]; + ngParselModules: NgParselModule[]; + ngParselDirectives: NgParselDirective[]; + ngParselServices: NgParselService[]; +} diff --git a/src/parser/parser.ts b/src/parser/parser.ts index 26b2e10..2376308 100644 --- a/src/parser/parser.ts +++ b/src/parser/parser.ts @@ -1,10 +1,9 @@ import { tsquery } from '@phenomnomnominal/tsquery'; -import { existsSync, mkdirSync, readFileSync } from 'fs'; +import { readFileSync } from 'fs'; import glob from 'glob'; import { investigateType } from '../investigator/investigator.js'; import { generateSpinner } from '../utils/spinner.util.js'; -import { writeJson } from '../utils/write.util.js'; import { parseSpec } from './spec/spec.parser.js'; import { parsePipe } from './pipe/pipe.parser.js'; @@ -24,8 +23,9 @@ import { parseValidator } from './validator/validator.parser.js'; import { NgParselValidtor } from './validator/validator.model.js'; import { NgParselService } from './services/service.model.js'; import { parseService } from './services/service.parser.js'; +import { NgParselOutput } from './parse-output.model.js'; -export function parse(configuration: NgParselConfig): void { +export function parse(configuration: NgParselConfig): NgParselOutput { const directoryGlob = `${configuration.src}/**/*.{ts,html,scss,css,less}`; let ngParselComponents: NgParselComponent[] = [], @@ -83,85 +83,14 @@ export function parse(configuration: NgParselConfig): void { parseSpinner.fail(`Failed to parse files: ${e}`); } - const writeOutputSpinner = generateSpinner('Write output files'); - try { - writeOutputSpinner.start(); - - writeOutputFiles( - configuration, - ngParselComponents, - ngParselServices, - ngParselDirectives, - ngParselModules, - ngParselSpecs, - ngParselHarnesses, - ngParselPipes, - ngParselValidators - ); - - writeOutputSpinner.succeed(`Files successfully written to ${configuration.out}`); - } catch (e) { - writeOutputSpinner.fail(`Failed to write output files: ${e}`); - } -} - -function writeOutputFiles( - config: NgParselConfig, - ngParselComponents: NgParselComponent[], - ngParselServices: NgParselService[], - ngParselDirectives: NgParselDirective[], - ngParselModules: NgParselModule[], - ngParselSpecs: NgParselSpec[], - ngParselHarnesses: NgParselHarness[], - ngParselPipes: NgParselPipe[], - ngParselValidators: NgParselValidtor[] -): void { - if (!existsSync(config.out as string)) { - mkdirSync(config.out as string, { recursive: true }); - } - - if (config.singleFile) { - writeJson(`${config.out}/ng-parsel.json`, [ - ...ngParselComponents, - ...ngParselServices, - ...ngParselModules, - ...ngParselDirectives, - ...ngParselSpecs, - ...ngParselHarnesses, - ...ngParselPipes, - ...ngParselValidators, - ]); - } else { - if (ngParselComponents.length > 0) { - writeJson(`${config.out}/ng-parsel-components.json`, ngParselComponents); - } - - if (ngParselServices.length > 0) { - writeJson(`${config.out}/ng-parsel-services.json`, ngParselServices); - } - - if (ngParselModules.length > 0) { - writeJson(`${config.out}/ng-parsel-modules.json`, ngParselModules); - } - - if (ngParselDirectives.length > 0) { - writeJson(`${config.out}/ng-parsel-directives.json`, ngParselDirectives); - } - - if (ngParselPipes.length > 0) { - writeJson(`${config.out}/ng-parsel-pipes.json`, ngParselPipes); - } - - if (ngParselSpecs.length > 0) { - writeJson(`${config.out}/ng-parsel-specs.json`, ngParselSpecs); - } - - if (ngParselHarnesses.length > 0) { - writeJson(`${config.out}/ng-parsel-harnesses.json`, ngParselHarnesses); - } - - if (ngParselValidators.length > 0) { - writeJson(`${config.out}/ng-parsel-validators.json`, ngParselValidators); - } - } + return { + ngParselComponents, + ngParselServices, + ngParselDirectives, + ngParselModules, + ngParselSpecs, + ngParselHarnesses, + ngParselPipes, + ngParselValidators, + }; } diff --git a/src/utils/config.util.ts b/src/utils/config.util.ts new file mode 100644 index 0000000..e742073 --- /dev/null +++ b/src/utils/config.util.ts @@ -0,0 +1,22 @@ +import { cosmiconfigSync } from 'cosmiconfig'; +import chalk from 'chalk'; +import { mergeOptionalConfigWithDefaults } from '../config/config.helper.js'; + +const explorer = cosmiconfigSync('ng-parsel'); + +export function loadAndMergeConfig(cliArgsConfig: { [key: string]: string }) { + const configObject = explorer.search(); + + if (configObject) { + console.log( + chalk.cyan( + `ng-parsel: configuration found under ${configObject.filepath}. + Configuraton from config file will be used.` + ) + ); + return mergeOptionalConfigWithDefaults(configObject.config); + } else { + console.log(chalk.cyan(`ng-parsel: no configuration found. CLI arguments will be used.`)); + return mergeOptionalConfigWithDefaults(cliArgsConfig); + } +}