Skip to content
This repository was archived by the owner on Sep 10, 2019. It is now read-only.

Commit bb48037

Browse files
committed
feat: add dev command + default gateway
- dev CLI API is in place with options according to #9 - default command (`gramps dev`) will now run a basic gateway - added `node-cleanup` to handle cleanup on process exit re #11, re #16
1 parent 88b74ad commit bb48037

File tree

6 files changed

+140
-63
lines changed

6 files changed

+140
-63
lines changed

bin/dev.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import path from 'path';
2+
import yargs from 'yargs';
3+
import startDefaultGateway from '../lib/gateway';
4+
5+
const getDirPath = dir => path.resolve(process.cwd(), dir);
6+
7+
export const command = 'dev';
8+
export const description = 'run a GraphQL gateway for local development';
9+
10+
export const builder = yargs =>
11+
yargs
12+
.group(['data-source'], 'Choose data source(s) for local development:')
13+
.option('data-source', {
14+
alias: ['data-sources', 'd'],
15+
description: 'path to one or more data sources',
16+
type: 'array',
17+
})
18+
.group(['gateway'], 'Choose a GraphQL gateway to run the data sources:')
19+
.option('gateway', {
20+
alias: 'g',
21+
description: 'path to a GraphQL gateway start script',
22+
type: 'string',
23+
})
24+
.coerce('d', srcArr => srcArr.map(getDirPath))
25+
.coerce('g', getDirPath)
26+
.group(['live', 'mock'], 'Choose real or mock data:')
27+
.options({
28+
live: {
29+
alias: 'l',
30+
conflicts: 'mock',
31+
description: 'run GraphQL with live data',
32+
},
33+
mock: {
34+
alias: 'm',
35+
conflicts: 'live',
36+
description: 'run GraphQL offline with mock data',
37+
},
38+
})
39+
.group('no-transpile', 'Turn Babel on or off:')
40+
.options({
41+
transpile: {
42+
description: 'don’t transpile data sources (point to a build dir)',
43+
type: 'boolean',
44+
default: true,
45+
},
46+
});
47+
48+
export const handler = ({
49+
dataSources = [],
50+
mock = false,
51+
gateway,
52+
transpile,
53+
}) => {
54+
console.log({
55+
dataSources,
56+
gateway,
57+
mock,
58+
transpile,
59+
});
60+
61+
if (!gateway) {
62+
startDefaultGateway({
63+
dataSources,
64+
enableMockData: mock,
65+
});
66+
}
67+
};

bin/gramps.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1+
import { EOL } from 'os';
12
import yargs from 'yargs';
2-
import * as start from './start/start';
3+
import cleanup from 'node-cleanup';
34

4-
const argv = yargs.command('start [dir]', 'Start development server', start)
5-
.argv;
5+
import { description, version } from '../package.json';
6+
import dev from './dev';
7+
// import cleanup from '../lib/cleanup';
8+
9+
yargs
10+
.command(dev)
11+
.demandCommand()
12+
.help().argv;
13+
14+
cleanup(
15+
() => {
16+
// console.log('exiting...');
17+
},
18+
{
19+
ctrl_C: `${EOL}${EOL}Thanks for using GrAMPS!`,
20+
},
21+
);

bin/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#!/usr/bin/env node
2+
23
require('reify');
34
require('./gramps');

bin/start/start.js

-60
This file was deleted.

lib/cleanup.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default (options, err) => {
2+
process.stdin.resume();
3+
4+
console.log('Buh-bye!');
5+
6+
if (err) {
7+
console.error(err);
8+
}
9+
10+
if (options.cleanup) {
11+
console.log('cleaning up');
12+
}
13+
14+
if (options.exit) {
15+
console.log('exiting...');
16+
17+
// Runs the cleanup process
18+
process.exit(); // eslint-disable-line no-process-exit
19+
}
20+
};

lib/gateway.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This is a default gateway (server) to be used during local data source
3+
* development. If the `--gateway` option is supplied, this is NOT used.
4+
*/
5+
import Express from 'express';
6+
import getPort from 'get-port';
7+
import bodyParser from 'body-parser';
8+
import gramps from '@gramps/gramps';
9+
import { GraphQLSchema } from 'graphql';
10+
import { graphqlExpress } from 'apollo-server-express';
11+
import playground from 'graphql-playground-middleware-express';
12+
13+
const GRAPHQL_ENDPOINT = '/graphql';
14+
const TESTING_ENDPOINT = '/playground';
15+
const DEFAULT_PORT = 8080;
16+
17+
async function startServer(app) {
18+
const PORT = await getPort(DEFAULT_PORT);
19+
app.listen(PORT, () => {
20+
console.log(`=> http://localhost:${PORT}${TESTING_ENDPOINT}`);
21+
});
22+
}
23+
24+
export default config => {
25+
const app = Express();
26+
const GraphQLOptions = gramps(config);
27+
28+
app.use(bodyParser.json());
29+
app.use(GRAPHQL_ENDPOINT, graphqlExpress(GraphQLOptions));
30+
app.use(TESTING_ENDPOINT, playground({ endpoint: GRAPHQL_ENDPOINT }));
31+
32+
startServer(app);
33+
};

0 commit comments

Comments
 (0)