From e71220ef5ba839a96b03ed46f3145a781eca1bc6 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Fri, 13 Oct 2017 19:54:06 -0700 Subject: [PATCH] Add generator script --- .gitignore | 1 + README.md | 9 +++++- package-lock.json | 48 ++++++++++++++++++++++++-------- package.json | 11 ++++++-- scripts/generate.js | 68 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+), 15 deletions(-) create mode 100644 scripts/generate.js diff --git a/.gitignore b/.gitignore index ba2a97b..0972d12 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules coverage +package-lock.json diff --git a/README.md b/README.md index e05ca19..62bc2b0 100644 --- a/README.md +++ b/README.md @@ -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 -- '' +``` +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! diff --git a/package-lock.json b/package-lock.json index 750544e..421fc7e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1721,7 +1721,7 @@ "requires": { "ansi-escapes": "2.0.0", "chalk": "2.1.0", - "log-symbols": "2.0.0", + "log-symbols": "2.1.0", "plur": "2.1.2", "string-width": "2.1.1" }, @@ -2189,13 +2189,13 @@ } }, "fs-extra": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-4.0.1.tgz", - "integrity": "sha1-f8DGyJV/mD9X8waiTlud3Y0N2IA=", + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-4.0.2.tgz", + "integrity": "sha1-+RcExT0bRh+JNFKwwwfZmXZHq2s=", "dev": true, "requires": { "graceful-fs": "4.1.11", - "jsonfile": "3.0.1", + "jsonfile": "4.0.0", "universalify": "0.1.1" } }, @@ -4401,9 +4401,9 @@ "dev": true }, "jsonfile": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-3.0.1.tgz", - "integrity": "sha1-pezG9l9T9mLEQVx2daAzHQmS7GY=", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", + "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", "dev": true, "requires": { "graceful-fs": "4.1.11" @@ -4497,7 +4497,7 @@ "dedent": "0.7.0", "execa": "0.8.0", "find-up": "2.1.0", - "fs-extra": "4.0.1", + "fs-extra": "4.0.2", "get-port": "3.2.0", "glob": "7.1.2", "glob-parent": "3.1.0", @@ -4834,9 +4834,9 @@ "dev": true }, "log-symbols": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-2.0.0.tgz", - "integrity": "sha512-ValCSal2pRxRbet7O69a/1g5fZ2MLxf1YXIslNrdJF42ofY9zVf6MTqTwfhG+2x168xrbZATCgFQfXAwdNHv+w==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-2.1.0.tgz", + "integrity": "sha512-zLeLrzMA1A2vRF1e/0Mo+LNINzi6jzBylHj5WqvQ/WK/5WCZt8si9SyN4p9llr/HRYvVR1AoXHRHl4WTHyQAzQ==", "dev": true, "requires": { "chalk": "2.1.0" @@ -6472,6 +6472,30 @@ "integrity": "sha1-uDVx+k2MJbguIxsG46MFXeTKGkc=", "dev": true }, + "to-no-case": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/to-no-case/-/to-no-case-1.0.2.tgz", + "integrity": "sha1-xyKQcWTvaxeBMsjmmTAhLRtKoWo=", + "dev": true + }, + "to-slug-case": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/to-slug-case/-/to-slug-case-1.0.0.tgz", + "integrity": "sha1-kqzeU0cQVaM8gwcZ6Ot4OHIG2BM=", + "dev": true, + "requires": { + "to-space-case": "1.0.0" + } + }, + "to-space-case": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/to-space-case/-/to-space-case-1.0.0.tgz", + "integrity": "sha1-sFLar7Gysp3HcM6gFj5ewOvJ/Bc=", + "dev": true, + "requires": { + "to-no-case": "1.0.2" + } + }, "tough-cookie": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.3.2.tgz", diff --git a/package.json b/package.json index 3e5d294..28e346b 100644 --- a/package.json +++ b/package.json @@ -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", @@ -16,9 +17,12 @@ "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": { @@ -26,7 +30,10 @@ "env": [ "node", "jest" - ] + ], + "rules": { + "unicorn/explicit-length-check": "off" + } }, "repository": { "type": "git", diff --git a/scripts/generate.js b/scripts/generate.js new file mode 100644 index 0000000..0e23000 --- /dev/null +++ b/scripts/generate.js @@ -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 -- `') + +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')