Skip to content

Commit e71220e

Browse files
committed
Add generator script
1 parent 359806e commit e71220e

File tree

5 files changed

+122
-15
lines changed

5 files changed

+122
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
coverage
3+
package-lock.json

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ Each of these types can be installed individually using there 'Package Name' sho
4545
| USState | `graphql-types-us-state` | `US`, `CA`, `DE` `...` |
4646

4747
## Contributing
48-
Contributions are more than welcome! This repo is not meant to be owned by me (and if there is a more suitable owner please [let me know](https://github.com/mfix22/gnt/issues)), but rather by the commuity.
48+
Contributions are more than welcome! This repo is not meant to be owned by me (and if there is a more suitable owner please [let me know](https://github.com/mfix22/gnt/issues)), but rather by the commuity.
49+
50+
### Creating a new type
51+
First run:
52+
```shell
53+
$ npm run generate -- '<your type name>'
54+
```
55+
to get started. A folder with `index.js`, `index.spec.js` (your test), and a `package.json` will be created for you!
4956

5057
If you have any idea for new types, please submit an issue or PR!

package-lock.json

+36-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"lint": "xo",
44
"test": "jest",
55
"link": "lerna bootstrap",
6-
"publish": "npm run lint && npm test && lerna publish"
6+
"publish": "npm run lint && npm test && lerna publish",
7+
"generate": "node ./scripts/generate"
78
},
89
"keywords": [
910
"graphql",
@@ -16,17 +17,23 @@
1617
"license": "MIT",
1718
"devDependencies": {
1819
"eslint-config-prettier": "^2.4.0",
20+
"fs-extra": "^4.0.2",
1921
"jest": "^21.0.2",
2022
"lerna": "^2.1.2",
23+
"log-symbols": "^2.1.0",
2124
"prettier": "^1.6.1",
25+
"to-slug-case": "^1.0.0",
2226
"xo": "^0.18.2"
2327
},
2428
"xo": {
2529
"extends": "prettier",
2630
"env": [
2731
"node",
2832
"jest"
29-
]
33+
],
34+
"rules": {
35+
"unicorn/explicit-length-check": "off"
36+
}
3037
},
3138
"repository": {
3239
"type": "git",

scripts/generate.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env node
2+
const fs = require('fs-extra')
3+
const slug = require('to-slug-case')
4+
const logSymbols = require('log-symbols');
5+
6+
const args = process.argv.slice(2)
7+
8+
if (!args.length) throw new Error('`type` is required. Try `npm run generate -- <your type name>`')
9+
10+
const type = args.join(' ')
11+
12+
const path = `./packages/${slug(type)}`
13+
14+
fs.mkdirSync(path)
15+
16+
const cap = s => s[0].toUpperCase() + s.slice(1).toLowerCase()
17+
18+
const pkgJsonTemplate = {
19+
name: `graphql-types-${slug(type)}`,
20+
version: '0.0.0',
21+
description: `GraphQL Normalized ${args.map(cap).join(' ')} Type`,
22+
main: 'index.js',
23+
keywords: [
24+
'graphql',
25+
'normalized',
26+
'types',
27+
'scalar',
28+
'gin-n-tonic',
29+
type
30+
],
31+
license: 'MIT',
32+
dependencies: {
33+
},
34+
peerDependencies: {
35+
'graphql': '*'
36+
}
37+
}
38+
39+
const indexTemplate =
40+
`const { GraphQLScalarType } = require('graphql')
41+
42+
const parse = v => {
43+
// Feel free to handle this however you would like!
44+
}
45+
46+
module.exports = new GraphQLScalarType({
47+
name: '${args.map(cap).join('')}',
48+
description: '',
49+
serialize: parse,
50+
parseValue: parse,
51+
parseLiteral(ast) {
52+
return parse(ast.value);
53+
}
54+
})`
55+
56+
const indexSpecTemplate =
57+
`const type = require('.')
58+
59+
test('${type}', () => {
60+
expect(dl._scalarConfig.parseValue())
61+
})`
62+
63+
64+
fs.writeFileSync(`${path}/index.js`, indexTemplate)
65+
fs.writeFileSync(`${path}/index.spec.js`, indexSpecTemplate)
66+
fs.writeJsonSync(`${path}/package.json`, pkgJsonTemplate, {spaces: 2})
67+
68+
console.log('\n', logSymbols.success, args.map(cap).join(''), 'type created!', '\n')

0 commit comments

Comments
 (0)