Skip to content

Commit a027afd

Browse files
committed
Model generator in the cli
1 parent 1c171eb commit a027afd

7 files changed

Lines changed: 55 additions & 4 deletions

File tree

src/generators/ControllerGenerator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ if (controllerName) {
3434
replaceMask(contentFile, { name: controllerName })
3535
);
3636

37-
console.info(`\x1b[36mCreating a controller ${controllerName}`, '\x1b[0m');
37+
console.info(
38+
`\x1b[36mCreating the controller ${controllerName}.`,
39+
'\x1b[0m'
40+
);
3841
console.info(`Path: ${target}`, '\x1b[0m\n');
3942
} catch (err) {
4043
console.log(`\x1b[31m${err}\x1b[0m`);

src/generators/ModelGenerator.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import commander from 'commander';
4+
import capitalize from '../utils/capitalize';
5+
import replaceMask from '../utils/replaceMask';
6+
7+
let modelName: string = '';
8+
9+
commander
10+
.name(`recife-cli model`)
11+
.arguments('<model-name>')
12+
.action(name => (modelName = name))
13+
.allowUnknownOption(false)
14+
.parse(process.argv);
15+
16+
if (modelName) {
17+
modelName = capitalize(modelName.replace(/Model|\.ts|\.js/g, ''));
18+
19+
modelName += 'Model';
20+
21+
const source = path.join(__dirname, '/../../templates/model/template');
22+
const target = path.join(process.cwd(), 'src/models', `${modelName}.ts`);
23+
24+
try {
25+
const contentFile = fs.readFileSync(source).toString();
26+
fs.writeFileSync(target, replaceMask(contentFile, { name: modelName }));
27+
28+
console.info(`\x1b[36mCreating the model ${modelName}.`, '\x1b[0m');
29+
console.info(`Path: ${target}`, '\x1b[0m\n');
30+
} catch (err) {
31+
console.log(`\x1b[31m${err}\x1b[0m`);
32+
}
33+
} else {
34+
console.error('\x1b[31mSpecify the name model.', '\x1b[0m');
35+
console.log(` For example: recife-cli model User`);
36+
console.log(` Run --help for more information\n`);
37+
}

src/generators/ProjectGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ if (projectName) {
3333
installDependencies(target);
3434
initializeGit(projectName);
3535

36-
console.info(`\x1b[36mCreating a project ${projectName}`, '\x1b[0m');
36+
console.info(`\x1b[36mCreating the project ${projectName}.`, '\x1b[0m');
3737
console.info(`Path: ${target}`, '\x1b[0m\n');
3838
} catch (err) {
3939
console.log(`\x1b[31m${err}\x1b[0m`);

src/generators/ScalarGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ if (scalarName) {
3131
})
3232
);
3333

34-
console.info(`\x1b[36mCreating a scalar ${scalarName}`, '\x1b[0m');
34+
console.info(`\x1b[36mCreating the scalar ${scalarName}.`, '\x1b[0m');
3535
console.info(`Path: ${target}`, '\x1b[0m\n');
3636
} catch (err) {
3737
console.log(`\x1b[31m${err}\x1b[0m`);

src/generators/ValidatorGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if (validatorName) {
2929
const contentFile = fs.readFileSync(source).toString();
3030
fs.writeFileSync(target, replaceMask(contentFile, { name: validatorName }));
3131

32-
console.info(`\x1b[36mCreating a validator ${validatorName}`, '\x1b[0m');
32+
console.info(`\x1b[36mCreating the validator ${validatorName}.`, '\x1b[0m');
3333
console.info(`Path: ${target}`, '\x1b[0m\n');
3434
} catch (err) {
3535
console.log(`\x1b[31m${err}\x1b[0m`);

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ commander
3636
'../dist/generators/ValidatorGenerator.js'
3737
)
3838
})
39+
.command('model', 'Create a model', {
40+
executableFile: path.join(__dirname, '../dist/generators/ModelGenerator.js')
41+
})
3942
.allowUnknownOption(false);
4043

4144
commander.parse(process.argv);

templates/model/template

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Type } from 'recife';
2+
3+
@Type()
4+
class [[name]] {
5+
id?: String;
6+
}
7+
8+
export default [[name]];

0 commit comments

Comments
 (0)