Skip to content

Commit

Permalink
refactor: 💡 let the parser parse and nothing else
Browse files Browse the repository at this point in the history
  • Loading branch information
nivekcode committed Jul 21, 2024
1 parent 288afb4 commit 3b0c055
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 101 deletions.
120 changes: 103 additions & 17 deletions src/commands/parse.ts
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('===================================='));
}
19 changes: 19 additions & 0 deletions src/parser/parse-output.model.ts
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[];
}
97 changes: 13 additions & 84 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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[] = [],
Expand Down Expand Up @@ -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,
};
}
22 changes: 22 additions & 0 deletions src/utils/config.util.ts
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);
}
}

0 comments on commit 3b0c055

Please sign in to comment.