forked from kriasoft/graphql-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
184 lines (174 loc) · 5.5 KB
/
setup.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
/**
* This script helps to configure all the required
* environment variables such as application URL (origin),
* Google Cloud project IDs, database name, etc. Usage example:
*
* $ yarn setup
*
* @copyright 2016-present Kriasoft (https://git.io/vMINh)
*/
const fs = require("fs");
const crypto = require("crypto");
const dotenv = require("dotenv");
const spawn = require("cross-spawn");
const inquirer = require("inquirer");
const environments = {
prod: "production",
test: "test (QA)",
dev: "development",
};
function replace(filename, searchValue, replaceValue) {
let text = fs.readFileSync(filename, "utf8");
if (text.match(searchValue)) {
text = text.replace(searchValue, replaceValue);
fs.writeFileSync(filename, text, "utf8");
return true;
} else {
return `Failed to find ${searchValue} in ${filename}`;
}
}
const questions = [
{
type: "confirm",
name: "setup",
message:
"Configure this project for production, test (QA), and shared development environments?",
default: true,
},
{
type: "input",
name: "domain",
message: "Domain name where the app will be hosted:",
when: (answers) => answers.setup,
default() {
const { parsed } = dotenv.config({ path: "env/.env.prod" });
return new URL(parsed.APP_ORIGIN).hostname;
},
validate(domain) {
if (!domain.match(/^\w[\w-.]{0,61}\w\.[\w]{2,}$/)) {
return "Requires a valid domain name.";
}
const appOrigin = /^(APP_ORIGIN)=.*$/m;
const appName = /^(APP_NAME)=.*$/m;
const appNameValue = domain
.substring(0, domain.lastIndexOf("."))
.replace(/\./g, "_");
return (
replace("env/.env.prod", appOrigin, `$1=https://${domain}`) &&
replace("env/.env.test", appOrigin, `$1=https://test.${domain}`) &&
replace("env/.env.dev", appOrigin, `$1=https://dev.${domain}`) &&
replace("env/.env", appName, `$1=${appNameValue}`)
);
},
},
{
type: "input",
name: "pkg",
message: "GCS bucket for the app bundles:",
when: (answers) => answers.setup,
default: ({ domain }) => `pkg.${domain}`,
validate(value) {
if (!value.match(/^\w[\w-.]*\w$/)) {
return "Requires a valid GCS bucket name.";
}
const search = /^(PKG_BUCKET)=.*/m;
return replace("env/.env", search, `$1=${value}`);
},
},
{
type: "input",
name: "storage",
message: "GCS bucket for user uploaded content:",
when: (answers) => answers.setup,
default: ({ domain }) => `s.${domain}`,
validate(value) {
if (!value.match(/^\w[\w-.]*\w$/)) {
return "Requires a valid GCS bucket name.";
}
const search = /^(STORAGE_BUCKET)=.*/m;
return (
replace("env/.env.prod", search, `$1=${value}`) &&
replace("env/.env.test", search, `$1=test-${value}`) &&
replace("env/.env.dev", search, `$1=dev-${value}`) &&
replace("env/.env.local", search, `$1=dev-${value}`)
);
},
},
...Object.keys(environments).map((env) => ({
type: "input",
name: `gcp_project_${env}`,
message: `GCP project ID for ${environments[env]} (${env}):`,
when: (answers) => answers.setup,
default: ({ domain }) =>
domain
.substring(0, domain.lastIndexOf("."))
.replace(/\./g, "-")
.toLowerCase() + `-${env}`,
validate(value) {
const gcp = /^(GOOGLE_CLOUD_PROJECT)=.*/gm;
const db = /^(PGDATABASE)=.*/gm;
const dbName = value.replace(/-/g, "_");
const dbServer = /(PGSERVERNAME)=.*/gm;
const localDb = dbName.replace(/_(dev|development)/, "_local");
return (
replace(`env/.env.${env}`, gcp, `$1=${value}`) &&
replace(`env/.env.${env}`, db, `$1=${dbName}`) &&
replace(`env/.env.${env}`, dbServer, `$1=${value}:pg12`) &&
(env === "dev"
? replace(`env/.env.local`, gcp, `$1=${value}`) &&
replace(`env/.env.local`, db, `$1=${localDb}`)
: true)
);
},
})),
{
type: "confirm",
name: "clean",
message: "Do you want to remove this setup script?",
when: (answers) => answers.setup,
default: false,
},
];
async function done(answers) {
// Remove this script
if (answers.clean) {
fs.unlinkSync("./setup.js");
let text = fs.readFileSync("./package.json", "utf8");
text = text.replace(/\n\s+"setup": ".*?\n/s, "\n");
fs.writeFileSync("./package.json", text, "utf8");
spawn.sync("yarn", ["remove", "inquirer", "cross-spawn", "dotenv"], {
stdio: "inherit",
});
} else {
spawn.sync("yarn", ["install"], { stdio: "inherit" });
}
if (answers.setup) {
// Generate JWT secret(s)
Object.keys(environments).forEach((env) => {
let text = fs.readFileSync(`env/.env.${env}`, "utf8");
let [, secret] = text.match(/^JWT_SECRET=(.*)/m) || [];
if (secret === "n2127bOgmzao67RiW3umlVs16GL9fEj+JQRDaaN5E9G7yC/b") {
secret = crypto.randomBytes(36).toString("base64");
text = text.replace(/^(JWT_SECRET)=.*/m, `$1=${secret}`);
fs.writeFileSync(`env/.env.${env}`, text, "utf8");
}
});
console.log(` `);
console.log(
` Done! Now you can migrate the database and launch the app by running:`,
);
console.log(` `);
console.log(` $ yarn db:reset`);
console.log(` $ yarn api:start`);
console.log(` `);
} else {
console.log(` No problem. You can run this script at any time later.`);
}
}
inquirer
.prompt(questions)
.then(done)
.catch((err) => {
console.error(err);
process.exit(1);
});