Skip to content

Commit bbe58d7

Browse files
committed
Scalar generator in the cli
1 parent 8b71ff7 commit bbe58d7

5 files changed

Lines changed: 71 additions & 3 deletions

File tree

src/generators/ControllerGenerator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ if (controllerName) {
3838
}
3939
} else {
4040
console.error('\x1b[31mSpecify the name controller.', '\x1b[0m');
41-
console.log(` For example: recife-cli controller UserController`);
42-
console.log(` Run recife-cli --help for more information\n`);
41+
console.log(` For example: recife-cli controller User`);
42+
console.log(` Run --help for more information\n`);
4343
}

src/generators/ProjectGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ if (projectName) {
3838
} else {
3939
console.error('\x1b[31mSpecify the name project.', '\x1b[0m');
4040
console.log(` For example: recife-cli project my-project-name`);
41-
console.log(` Run recife-cli --help for more information\n`);
41+
console.log(` Run --help for more information\n`);
4242
}

src/generators/ScalarGenerator.ts

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

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ commander
2424
'../dist/generators/ControllerGenerator.js'
2525
)
2626
})
27+
.command('scalar', 'Create a scalar', {
28+
executableFile: path.join(
29+
__dirname,
30+
'../dist/generators/ScalarGenerator.js'
31+
)
32+
})
2733
.allowUnknownOption(false);
2834

2935
commander.parse(process.argv);

templates/scalar/template

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Kind, GraphQLError } from 'graphql';
2+
import ScalarType from 'recife';
3+
4+
const [[scalarName]]: ScalarType = {
5+
name: '[[name]]',
6+
description: 'A [[name]] scalar',
7+
parseValue: value => {
8+
return value;
9+
},
10+
serialize: value => {
11+
return value;
12+
},
13+
parseLiteral: ast => {
14+
if (ast.kind !== Kind.STRING) {
15+
throw new GraphQLError(`This "${ast}" is not a string`);
16+
}
17+
18+
return ast.value;
19+
}
20+
};
21+
22+
export default [[scalarName]];

0 commit comments

Comments
 (0)