Skip to content

Commit 415658f

Browse files
authored
feat: add install script for the GraphQL CLI (#11)
* feat: add install script * feat: add prompts to update files * style: linting cleanup * fix: include package.json in file glob * fix: update placeholder to match pattern * style: remove extra space
1 parent 1076d2f commit 415658f

File tree

8 files changed

+121
-15
lines changed

8 files changed

+121
-15
lines changed

.travis.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ cache:
33
yarn: true
44
directories:
55
- node_modules
6+
env:
7+
global:
8+
# Code Climate is an excellent tool for tracking code quality.
9+
# It’s free for open source, too! Set it up here: http://bit.ly/2l0mvp9
10+
#
11+
# To track code coverage, you’ll need this test reporter ID.
12+
# Visit Settings => Test coverage on your Code Climate project dashboard
13+
- CC_TEST_REPORTER_ID=
614
node_js:
715
- 6
816
- 8
@@ -24,6 +32,3 @@ branches:
2432
only:
2533
- master
2634
- /^greenkeeper/.*$/
27-
env:
28-
global:
29-
- CC_TEST_REPORTER_ID=3f97d4ca639289c139796067359d46e7583e2d83701a315e40c1d1f58567afa8

install.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* This is a helper file for use with the GraphQL CLI. It can be ignored if
3+
* you’re not working with the GraphQL CLI.
4+
*
5+
* @see https://github.com/graphql-cli/graphql-cli
6+
*/
7+
8+
// eslint-disable-next-line import/no-extraneous-dependencies
9+
const globby = require('globby');
10+
const fs = require('fs');
11+
const pkg = require('./package.json');
12+
13+
function replaceInFile(filePath, searchValue, replaceValue) {
14+
const contents = fs.readFileSync(filePath, 'utf8');
15+
const newContents = contents.replace(
16+
new RegExp(searchValue, 'g'),
17+
replaceValue,
18+
);
19+
fs.writeFileSync(filePath, newContents);
20+
}
21+
22+
function replaceDefaultStringsInFiles(
23+
{ namespace, prefix, templateName, project },
24+
fileGlob = ['{src,test}/**/*', 'package.json'],
25+
) {
26+
const sourceFiles = globby.sync(fileGlob);
27+
const newType = `${prefix}_${namespace}`;
28+
const placeholder = `GrAMPS Data Source: ${namespace}`;
29+
30+
sourceFiles.forEach(filePath => {
31+
replaceInFile(filePath, templateName, project);
32+
replaceInFile(filePath, 'PFX_DataSourceBase', newType);
33+
replaceInFile(filePath, 'DataSourceBase', namespace);
34+
replaceInFile(filePath, 'GrAMPS GraphQL Data Source Base', placeholder);
35+
});
36+
}
37+
38+
const questions = [
39+
{
40+
type: 'input',
41+
name: 'namespace',
42+
message: 'Choose a unique namespace. Only letters and numbers are allowed.',
43+
default: 'MyDataSource',
44+
validate: input => !input.match(/\W/),
45+
},
46+
{
47+
type: 'input',
48+
name: 'prefix',
49+
message: 'Choose a short, unique prefix for your types.',
50+
default: 'PFX',
51+
},
52+
];
53+
54+
module.exports = async ({ project, context }) => {
55+
const opts = await context.prompt(questions);
56+
57+
replaceDefaultStringsInFiles({ ...opts, templateName: pkg.name, project });
58+
59+
// eslint-disable-next-line no-console
60+
console.log(`
61+
💥 You’re all set: a new GrAMPS data source has been created!
62+
63+
💡 Don’t forget to update "description", "contributors", and
64+
"repository" in \`package.json\` with your own information.
65+
66+
✅ Next steps:
67+
1. Change directory: \`cd ${project}\`
68+
2. Start local server: \`yarn dev\`
69+
3. Open the GraphQL Playground: http://localhost:8080/playground
70+
4. [BONUS] Set up Code Climate: http://bit.ly/2l0mvp9
71+
`);
72+
};

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gramps/data-source-base",
3-
"description": "Base modules for a GrAMPS GraphQL data source.",
3+
"description": "GrAMPS GraphQL Data Source Base",
44
"contributors": [
55
"Jason Lengstorf <[email protected]>"
66
],
@@ -52,9 +52,11 @@
5252
"eslint-config-prettier": "^2.9.0",
5353
"eslint-plugin-import": "^2.8.0",
5454
"eslint-plugin-prettier": "^2.4.0",
55+
"globby": "^7.1.1",
5556
"graphql": "^0.12.3",
5657
"graphql-tools": "^2.14.1",
5758
"husky": "^0.14.3",
59+
"inquirer": "^4.0.1",
5860
"jest": "^22.0.4",
5961
"prettier": "^1.9.2",
6062
"semantic-release": "^11.0.2"

src/context.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
export default {
33
getById: id => ({
44
id,
5-
name: 'GrAMPS Data Source Base',
5+
name: 'GrAMPS GraphQL Data Source Base',
66
lucky_numbers: [1, 2, 3, 5, 8, 13, 21],
77
}),
88
};

test/context.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('Data Source Context', () => {
55
it('loads data by its ID', () => {
66
expect(context.getById(123)).toEqual({
77
id: 123,
8-
name: 'GrAMPS Data Source Base',
8+
name: 'GrAMPS GraphQL Data Source Base',
99
lucky_numbers: [1, 2, 3, 5, 8, 13, 21],
1010
});
1111
});

test/index.test.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import dataSource from '../src';
22

33
// TODO: Update the data source name.
4-
const DATA_SOURCE_NAME = 'DataSourceBase';
5-
6-
describe(`Data Source: ${DATA_SOURCE_NAME}`, () => {
4+
describe(`Data Source: DataSourceBase`, () => {
75
it('contains a namespace property', () => {
8-
expect(dataSource.namespace).toBe(DATA_SOURCE_NAME);
6+
expect(dataSource.namespace).toBe('DataSourceBase');
97
});
108

119
it('contains a context property', () => {

test/mocks.test.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ import expectMockList from './helpers/expectMockList';
33
import mocks from '../src/mocks';
44

55
describe('mock resolvers', () => {
6-
/*
7-
* So, basically mock resolvers just need to return values without
8-
* exploding. To that end, we’ll just check that the mock response returns
9-
* the proper fields.
10-
*/
116
describe('PFX_DataSourceBase', () => {
127
const mockResolvers = mocks.PFX_DataSourceBase();
138

9+
// This helper creates a test to ensure each field has a mock resolver.
1410
expectMockFields(mockResolvers, ['id', 'name', 'lucky_numbers']);
11+
12+
// This helper creates a test to check that these fields return `MockList`s.
1513
expectMockList(mockResolvers, ['lucky_numbers']);
1614
});
1715
});

yarn.lock

+31
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,10 @@ chalk@^2.3.0:
12301230
escape-string-regexp "^1.0.5"
12311231
supports-color "^4.0.0"
12321232

1233+
chardet@^0.4.0:
1234+
version "0.4.2"
1235+
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.4.2.tgz#b5473b33dc97c424e5d98dc87d55d4d8a29c8bf2"
1236+
12331237
chokidar@^1.6.1:
12341238
version "1.7.0"
12351239
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468"
@@ -2048,6 +2052,14 @@ external-editor@^2.0.4:
20482052
jschardet "^1.4.2"
20492053
tmp "^0.0.33"
20502054

2055+
external-editor@^2.1.0:
2056+
version "2.1.0"
2057+
resolved "https://registry.yarnpkg.com/external-editor/-/external-editor-2.1.0.tgz#3d026a21b7f95b5726387d4200ac160d372c3b48"
2058+
dependencies:
2059+
chardet "^0.4.0"
2060+
iconv-lite "^0.4.17"
2061+
tmp "^0.0.33"
2062+
20512063
extglob@^0.3.1:
20522064
version "0.3.2"
20532065
resolved "https://registry.yarnpkg.com/extglob/-/extglob-0.3.2.tgz#2e18ff3d2f49ab2765cec9023f011daa8d8349a1"
@@ -2773,6 +2785,25 @@ inquirer@^3.0.6:
27732785
strip-ansi "^4.0.0"
27742786
through "^2.3.6"
27752787

2788+
inquirer@^4.0.1:
2789+
version "4.0.1"
2790+
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-4.0.1.tgz#b25cd541789394b4bb56f6440fb213b121149096"
2791+
dependencies:
2792+
ansi-escapes "^3.0.0"
2793+
chalk "^2.0.0"
2794+
cli-cursor "^2.1.0"
2795+
cli-width "^2.0.0"
2796+
external-editor "^2.1.0"
2797+
figures "^2.0.0"
2798+
lodash "^4.3.0"
2799+
mute-stream "0.0.7"
2800+
run-async "^2.2.0"
2801+
rx-lite "^4.0.8"
2802+
rx-lite-aggregates "^4.0.8"
2803+
string-width "^2.1.0"
2804+
strip-ansi "^4.0.0"
2805+
through "^2.3.6"
2806+
27762807
into-stream@^3.1.0:
27772808
version "3.1.0"
27782809
resolved "https://registry.yarnpkg.com/into-stream/-/into-stream-3.1.0.tgz#96fb0a936c12babd6ff1752a17d05616abd094c6"

0 commit comments

Comments
 (0)