Skip to content

Commit

Permalink
feat(bod): rewrite bod-cli with factory pattern
Browse files Browse the repository at this point in the history
issue #15
  • Loading branch information
sabertazimi committed Aug 14, 2021
1 parent 345e375 commit 5b28228
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
52 changes: 24 additions & 28 deletions packages/bod/lib/bod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import chalk from 'chalk';
import { Command, program } from 'commander';
import consola from 'consola';
import envinfo from 'envinfo';
import fs from 'fs';
import path from 'path';
import { create } from './index';
import { BaseCommand } from './commands';
import { CommandFactory } from './index';

const createCommand = CommandFactory.get('create') as BaseCommand;
const infoCommand = CommandFactory.get('info') as BaseCommand;

const packageJsonPath = path.join(__dirname, '../package.json');
const packageJson = JSON.parse(
Expand All @@ -17,35 +20,28 @@ program.version(packageJson.version, '-v, --version');
program.usage('<command> [options]');

program
.command('create <appName>')
.description(
'create a new project powered by Create React App and @sabertazimi/react-scripts'
)
.action((appName: string) => {
create(appName);
.command(createCommand.getUsage())
.description(createCommand.getDescription())
.action(async (appName: string) => {
try {
await createCommand.run(appName);
} catch (error) {
consola.error(error);
consola.error('Bod create failed.');
program.outputHelp();
}
});

program
.command('info')
.description('print debugging information about your environment')
.action(() => {
consola.info('Environment Info:');
envinfo
.run(
{
System: ['OS', 'CPU'],
Binaries: ['Node', 'Yarn', 'npm'],
Browsers: ['Chrome', 'Edge', 'Firefox', 'Safari'],
npmPackages: '/**/{typescript,*react*,@sabertazimi/*/}',
npmGlobalPackages: ['@sabertazimi/bod'],
},
{
showNotFound: true,
duplicates: true,
fullTree: true,
}
)
.then(consola.info);
.command(infoCommand.getUsage())
.description(infoCommand.getDescription())
.action(async () => {
try {
await infoCommand.run();
} catch (error) {
consola.error(error);
program.outputHelp();
}
});

// output help information on unknown commands
Expand Down
16 changes: 7 additions & 9 deletions packages/bod/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import consola from 'consola';
import { CreateCommand } from './commands';
import { BaseCommand, CreateCommand, InfoCommand } from './commands';

const CommandFactory = new Map<string, BaseCommand>();

const createCommand = new CreateCommand();
const infoCommand = new InfoCommand();

const create = (appName: string): Promise<void> => {
return createCommand.run(appName).catch((err) => {
consola.error(err);
consola.error('\nBod create failed.');
});
};
CommandFactory.set(createCommand.getName(), createCommand);
CommandFactory.set(infoCommand.getName(), infoCommand);

export { create };
export { CommandFactory };

0 comments on commit 5b28228

Please sign in to comment.