Skip to content

Commit

Permalink
Model generator in the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
andrelmlins committed Jun 11, 2020
1 parent 1c171eb commit a027afd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/generators/ControllerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ if (controllerName) {
replaceMask(contentFile, { name: controllerName })
);

console.info(`\x1b[36mCreating a controller ${controllerName}`, '\x1b[0m');
console.info(
`\x1b[36mCreating the controller ${controllerName}.`,
'\x1b[0m'
);
console.info(`Path: ${target}`, '\x1b[0m\n');
} catch (err) {
console.log(`\x1b[31m${err}\x1b[0m`);
Expand Down
37 changes: 37 additions & 0 deletions src/generators/ModelGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fs from 'fs';
import path from 'path';
import commander from 'commander';
import capitalize from '../utils/capitalize';
import replaceMask from '../utils/replaceMask';

let modelName: string = '';

commander
.name(`recife-cli model`)
.arguments('<model-name>')
.action(name => (modelName = name))
.allowUnknownOption(false)
.parse(process.argv);

if (modelName) {
modelName = capitalize(modelName.replace(/Model|\.ts|\.js/g, ''));

modelName += 'Model';

const source = path.join(__dirname, '/../../templates/model/template');
const target = path.join(process.cwd(), 'src/models', `${modelName}.ts`);

try {
const contentFile = fs.readFileSync(source).toString();
fs.writeFileSync(target, replaceMask(contentFile, { name: modelName }));

console.info(`\x1b[36mCreating the model ${modelName}.`, '\x1b[0m');
console.info(`Path: ${target}`, '\x1b[0m\n');
} catch (err) {
console.log(`\x1b[31m${err}\x1b[0m`);
}
} else {
console.error('\x1b[31mSpecify the name model.', '\x1b[0m');
console.log(` For example: recife-cli model User`);
console.log(` Run --help for more information\n`);
}
2 changes: 1 addition & 1 deletion src/generators/ProjectGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if (projectName) {
installDependencies(target);
initializeGit(projectName);

console.info(`\x1b[36mCreating a project ${projectName}`, '\x1b[0m');
console.info(`\x1b[36mCreating the project ${projectName}.`, '\x1b[0m');
console.info(`Path: ${target}`, '\x1b[0m\n');
} catch (err) {
console.log(`\x1b[31m${err}\x1b[0m`);
Expand Down
2 changes: 1 addition & 1 deletion src/generators/ScalarGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if (scalarName) {
})
);

console.info(`\x1b[36mCreating a scalar ${scalarName}`, '\x1b[0m');
console.info(`\x1b[36mCreating the scalar ${scalarName}.`, '\x1b[0m');
console.info(`Path: ${target}`, '\x1b[0m\n');
} catch (err) {
console.log(`\x1b[31m${err}\x1b[0m`);
Expand Down
2 changes: 1 addition & 1 deletion src/generators/ValidatorGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if (validatorName) {
const contentFile = fs.readFileSync(source).toString();
fs.writeFileSync(target, replaceMask(contentFile, { name: validatorName }));

console.info(`\x1b[36mCreating a validator ${validatorName}`, '\x1b[0m');
console.info(`\x1b[36mCreating the validator ${validatorName}.`, '\x1b[0m');
console.info(`Path: ${target}`, '\x1b[0m\n');
} catch (err) {
console.log(`\x1b[31m${err}\x1b[0m`);
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ commander
'../dist/generators/ValidatorGenerator.js'
)
})
.command('model', 'Create a model', {
executableFile: path.join(__dirname, '../dist/generators/ModelGenerator.js')
})
.allowUnknownOption(false);

commander.parse(process.argv);
8 changes: 8 additions & 0 deletions templates/model/template
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Type } from 'recife';

@Type()
class [[name]] {
id?: String;
}

export default [[name]];

0 comments on commit a027afd

Please sign in to comment.