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