This repository has been archived by the owner on Feb 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·194 lines (165 loc) · 4.81 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// * react-apollo-magic-glue
const commander = require('commander');
const inquirer = require('inquirer');
const chalk = require('chalk');
const util = require('util');
const fs = require('fs');
const path = require('path');
const exec = util.promisify(require('child_process').exec);
const writeFile = util.promisify(fs.writeFile);
const readdir = util.promisify(fs.readdir);
const generate = require('./commands/generate');
// ** Constants
const SERVER_CFG_NAME = 'react-apollo-magic-glue-server-cfg.json';
const CLIENT_CFG_NAME = 'react-apollo-magic-glue-client-cfg.json';
const DESCRIPTION = 'Easily generate endpoints for Apollo Server along with React components that retrieve and display the data';
// ** Command Structure
commander
.version('0.0.1')
.description(DESCRIPTION);
commander
.command('generate')
.alias('g')
.description('Add a new resource with corresponding endpoints and views')
.action(runCommand(generate));
commander
.command('*', { noHelp: true })
.action(() => {
commander.help();
});
// Display help if no command is specified
commander.parse(process.argv);
if (process.argv.length < 3) {
commander.help();
}
// ** Initialization
async function runCommand(command) {
const config = await init();
await command(config);
}
async function init() {
console.log('\n');
console.log(chalk.green('react-apollo-magic-glue'));
console.log(DESCRIPTION);
console.log('\n');
return verifyConfig();
}
// *** Configuration File
/**
* Find and load a configuration file
* Or generate a new one if it is not found
*
* @returns {Object}
*/
async function verifyConfig() {
const configPath = await getConfigPath();
return configPath
? JSON.parse(fs.readFileSync(configPath))
: generateConfig();
}
/**
* If the project is a git repo, find the config at the repo root
* Otherwise look in the current directory
*
* @returns {String} or null if config not found
*/
async function getConfigPath() {
let cfgPath;
const repoPath = await getCurrentGitRepoPath();
if (repoPath) {
cfgPath = await configSearch(repoPath);
}
if (!cfgPath) {
cfgPath = await configSearch(`${process.cwd()}/`);
}
return cfgPath;
}
/**
* Get the path
* Otherwise look in the current directory
*
* @returns {String} The repo root path or null if repo not found
*/
async function getCurrentGitRepoPath() {
const { stdout } = await exec('git rev-parse --git-dir').catch(() => ({ stdout: null }));
if (!stdout || stdout.trim() === '.git') {
return null;
}
return stdout.replace('.git', '');
}
/**
* Search for a config file in the given path
*
* @returns {String} The config file path or null if config not found
*/
async function configSearch(startPath) {
if (fs.existsSync(startPath)) {
const files = await readdir(startPath);
if (files.includes(SERVER_CFG_NAME)) {
return `${startPath}${SERVER_CFG_NAME}`;
}
// If only the client config file is found, get the server path and search there
if (files.includes(CLIENT_CFG_NAME)) {
const clientConfig = JSON.parse(fs.readFileSync(`${startPath}${CLIENT_CFG_NAME}`));
if (clientConfig.serverPath) {
return configSearch(clientConfig.serverPath);
}
}
}
return null;
}
/**
* Get necessary input from the user in order to generate a config file
*
* @returns {Object} The config data from the generated file
*/
async function generateConfig() {
console.log("Can't find a valid config file. Please answer the following questions in order to create one");
const {
serverPath,
clientPath,
componentPath,
generateCSSModules,
} = await inquirer.prompt([
{
name: 'clientPath',
type: 'input',
default: '.',
message: 'What is the path to your frontend (React/Apollo Client) project?',
},
{
name: 'componentPath',
type: 'input',
default: '/components',
message: 'What directory should React components be generated in? (relative to frontend root)',
},
{
name: 'serverPath',
type: 'input',
default: '.',
message: 'What is the path to your backend (Apollo Server) project?',
},
{
name: 'generateCSSModules',
type: 'confirm',
message: 'Would you like to generate CSS modules alongside your React components?',
},
]);
const initialServerConfig = {
serverPath,
clientPath,
componentPath: `${clientPath}/${componentPath}`,
generateCSSModules,
};
const initialClientConfig = {
serverPath: path.relative(clientPath, serverPath),
};
await Promise.all([
writeFile(`${serverPath}/${SERVER_CFG_NAME}`, JSON.stringify(initialServerConfig)),
writeFile(`${clientPath}/${CLIENT_CFG_NAME}`, JSON.stringify(initialClientConfig)),
]);
console.log('\n');
console.log('Configuration files generated');
console.log('\n');
return initialServerConfig;
}