Skip to content

Commit

Permalink
refactor: 💡 introduce commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nivekcode committed Jul 21, 2024
1 parent ecd5514 commit 288afb4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/bin/ng-parsel.bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -37,10 +38,29 @@ program
.option('-o, --out <string>', '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(
Expand All @@ -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();
30 changes: 30 additions & 0 deletions src/commands/parse.ts
Original file line number Diff line number Diff line change
@@ -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('===================================='));
}

0 comments on commit 288afb4

Please sign in to comment.