Skip to content

Commit

Permalink
Add generator script
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix22 committed Oct 14, 2017
1 parent 359806e commit e71220e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
coverage
package-lock.json
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ Each of these types can be installed individually using there 'Package Name' sho
| USState | `graphql-types-us-state` | `US`, `CA`, `DE` `...` |

## Contributing
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.
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.

### Creating a new type
First run:
```shell
$ npm run generate -- '<your type name>'
```
to get started. A folder with `index.js`, `index.spec.js` (your test), and a `package.json` will be created for you!

If you have any idea for new types, please submit an issue or PR!
48 changes: 36 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"lint": "xo",
"test": "jest",
"link": "lerna bootstrap",
"publish": "npm run lint && npm test && lerna publish"
"publish": "npm run lint && npm test && lerna publish",
"generate": "node ./scripts/generate"
},
"keywords": [
"graphql",
Expand All @@ -16,17 +17,23 @@
"license": "MIT",
"devDependencies": {
"eslint-config-prettier": "^2.4.0",
"fs-extra": "^4.0.2",
"jest": "^21.0.2",
"lerna": "^2.1.2",
"log-symbols": "^2.1.0",
"prettier": "^1.6.1",
"to-slug-case": "^1.0.0",
"xo": "^0.18.2"
},
"xo": {
"extends": "prettier",
"env": [
"node",
"jest"
]
],
"rules": {
"unicorn/explicit-length-check": "off"
}
},
"repository": {
"type": "git",
Expand Down
68 changes: 68 additions & 0 deletions scripts/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env node
const fs = require('fs-extra')
const slug = require('to-slug-case')
const logSymbols = require('log-symbols');

const args = process.argv.slice(2)

if (!args.length) throw new Error('`type` is required. Try `npm run generate -- <your type name>`')

const type = args.join(' ')

const path = `./packages/${slug(type)}`

fs.mkdirSync(path)

const cap = s => s[0].toUpperCase() + s.slice(1).toLowerCase()

const pkgJsonTemplate = {
name: `graphql-types-${slug(type)}`,
version: '0.0.0',
description: `GraphQL Normalized ${args.map(cap).join(' ')} Type`,
main: 'index.js',
keywords: [
'graphql',
'normalized',
'types',
'scalar',
'gin-n-tonic',
type
],
license: 'MIT',
dependencies: {
},
peerDependencies: {
'graphql': '*'
}
}

const indexTemplate =
`const { GraphQLScalarType } = require('graphql')
const parse = v => {
// Feel free to handle this however you would like!
}
module.exports = new GraphQLScalarType({
name: '${args.map(cap).join('')}',
description: '',
serialize: parse,
parseValue: parse,
parseLiteral(ast) {
return parse(ast.value);
}
})`

const indexSpecTemplate =
`const type = require('.')
test('${type}', () => {
expect(dl._scalarConfig.parseValue())
})`


fs.writeFileSync(`${path}/index.js`, indexTemplate)
fs.writeFileSync(`${path}/index.spec.js`, indexSpecTemplate)
fs.writeJsonSync(`${path}/package.json`, pkgJsonTemplate, {spaces: 2})

console.log('\n', logSymbols.success, args.map(cap).join(''), 'type created!', '\n')

0 comments on commit e71220e

Please sign in to comment.