Skip to content

Commit 5a17d78

Browse files
committed
Validator generator in the cli
1 parent bbe58d7 commit 5a17d78

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

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

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ commander
3030
'../dist/generators/ScalarGenerator.js'
3131
)
3232
})
33+
.command('validator', 'Create a validator', {
34+
executableFile: path.join(
35+
__dirname,
36+
'../dist/generators/ValidatorGenerator.js'
37+
)
38+
})
3339
.allowUnknownOption(false);
3440

3541
commander.parse(process.argv);

templates/validator/template

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class [[name]] {
2+
constructor() {
3+
//
4+
}
5+
}
6+
7+
export default [[name]];

0 commit comments

Comments
 (0)