Skip to content

Commit

Permalink
feat: generate command - [component]
Browse files Browse the repository at this point in the history
Implement a new "mantis" command within the CLI to facilitate the generation of shared components.
This command will streamline the process of creating reusable components that can be utilized across
multiple projects or parts of an application, enhancing modularity and reducing code duplication.
  • Loading branch information
babacarbasse committed Jun 19, 2024
1 parent b94413f commit 879dc03
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/actions/generate/generate.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import ora from 'ora';
import { Action } from '../abstract.action';
import { GenerateActionOptions } from './generate.types';
import { execa } from 'execa';

import fs from 'fs';
import path from 'path';

export class GenerateAction extends Action {
private options: GenerateActionOptions;

constructor(options: GenerateActionOptions) {
super('[GENERATE-ACTION]');
this.options = options;
}

updateIndexTs = async (name: string) => {
const filePath = path.join('shared-ui/src', 'index.ts');
let content = await fs.promises.readFile(filePath, 'utf8');
// add new line to the end of the file
content += `\nexport * from './lib/${name}/${name}.component';`;
await fs.promises.writeFile(filePath, content, 'utf8');
};

addNewComponentToTsConfig = async (name: string) => {
const filePath = path.join('tsconfig.base.json');
const content = await fs.promises.readFile(filePath, 'utf8');
// Update tsconfig.base.json to add the new component in compilerOptions.paths array
const json = JSON.parse(content);
const COMPONENT_PATH = `shared-ui/src/lib/${name}/${name}.component`;
json.compilerOptions.paths[`@todo/${name}`] = [COMPONENT_PATH];
await fs.promises.writeFile(
filePath,
JSON.stringify(json, null, 2),
'utf8',
);
};

generateComponent = async (name: string) => {
const spinner = ora();
spinner.start('Generating component');
// generate new shared ui component
await execa('npx', [
'nx',
'generate',
'@nx/angular:component',
name,
'--directory',
`shared-ui/src/lib/${name}`,
'--nameAndDirectoryFormat',
'as-provided',
]);
// export the component from the index.ts file
await this.updateIndexTs(name);
// add new component to tsconfig.base.json
await this.addNewComponentToTsConfig(name);
spinner.succeed();
};

async execute() {
try {
this.logger.info('Generating...');
const { type, name, filePath } = this.options;
switch (type) {
case 'component':
await this.generateComponent(name);
break;
case 'service':
this.logger.warning('Unimplemented');
break;
case 'mongo-schema':
if (!filePath) {
throw new Error('File path is required');
}
this.logger.warning('Unimplemented');
// await generateMongoSchema({
// name,
// filePath,
// });
break;
}
} catch (error) {
this.logger.error(error);
}
}
}
7 changes: 7 additions & 0 deletions src/actions/generate/generate.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type GeneratorType = 'component' | 'service' | 'mongo-schema';

export type GenerateActionOptions = {
type: GeneratorType;
name: string;
filePath?: string;
};
11 changes: 11 additions & 0 deletions src/commands/generate.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Command } from 'commander';
import { GenerateAction } from '../actions/generate/generate.actions';

export default new Command('generate')
.description('Generate a new Mantis feature')
.argument('<type>', 'The type of thing to generate')
.argument('<name>', 'The name of the thing to generate')
.option('-p, --path <path>', 'The path to the file')
.action(async (type, name, options) => {
await new GenerateAction({ type, name, filePath: options.path }).execute();
});
2 changes: 2 additions & 0 deletions src/mantis-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { welcome } from './utils/welcome.helper';
import packageJson from '../package.json';
import initCommand from './commands/init.command';
import startCommand from './commands/start.command';
import generateCommand from './commands/generate.command';

const logger = new OrbitLogger('[BOOTSTRAP]');

Expand Down Expand Up @@ -33,6 +34,7 @@ const bootstrap = async () => {

program.addCommand(initCommand);
program.addCommand(startCommand);
program.addCommand(generateCommand);

await program.parseAsync(process.argv);

Expand Down

0 comments on commit 879dc03

Please sign in to comment.