forked from realdecoy/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.ts
48 lines (40 loc) · 1.67 KB
/
component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Command, flags } from '@oclif/command';
import chalk from 'chalk';
import { toKebabCase } from '@rdfrontier/stdlib';
import { checkProjectValidity, parseComponentName } from '../../../utils/utilities';
import { CLI_COMMANDS, CLI_STATE, DOCUMENTATION_LINKS } from '../../../utils/constants';
import { addElementFunction } from '../../../functions/addElement';
import { invalidProject } from '@rdfrontier/plugin-shared'
import { catchError } from '@rdfrontier/plugin-shared';
const TEMPLATE_FOLDERS = ['component'];
/**
* Class representing a component.
* @extends Command
*/
export default class Component extends Command {
static description = 'add a new Component module.'
static flags = {
help: flags.help({ char: 'h' }),
}
static args = [
{ name: 'name', description: 'name of new component' },
]
// override Command class error handler
catch(error: Error): Promise<any> {
return catchError(error, CLI_STATE);
}
async run(): Promise<void> {
const { isValid: isValidProject, projectRoot } = checkProjectValidity();
// block command unless being run within an rdvue project
if (isValidProject === false) {
invalidProject(CLI_COMMANDS.AddComponent, "rdvue");
}
const { args } = this.parse(Component);
// retrieve component name
const componentName = await parseComponentName(args);
// parse config files required for scaffolding this module
addElementFunction(TEMPLATE_FOLDERS, projectRoot, componentName)
this.log(`${CLI_STATE.Success} component added: ${toKebabCase(componentName)}`);
this.log(`\n Visit the documentation page for more info:\n ${chalk.yellow(DOCUMENTATION_LINKS.Component)}\n`);
}
}