-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 💡 let the parser parse and nothing else
- Loading branch information
Showing
4 changed files
with
157 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('====================================')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |