-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate command - [component]
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
1 parent
b94413f
commit 879dc03
Showing
4 changed files
with
106 additions
and
0 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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
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,7 @@ | ||
export type GeneratorType = 'component' | 'service' | 'mongo-schema'; | ||
|
||
export type GenerateActionOptions = { | ||
type: GeneratorType; | ||
name: string; | ||
filePath?: string; | ||
}; |
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,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(); | ||
}); |
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