-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
525 lines (473 loc) · 15.4 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/env node
const path = require('path')
const yaml = require('yamljs')
const nanoid = require('nanoid')
const fs = require('fs-extra')
const inquirer = require('inquirer')
const dashify = require('dashify')
const uppercamelcase = require('uppercamelcase')
const toSnakeCase = require('just-snake-case')
const child_process = require('child_process')
const chalk = require('chalk')
const generate = require('nanoid/generate')
const repoURL = (protocol, project) =>
protocol === 'ssh'
? `[email protected]:clevertech/${project}.git`
: `https://github.com/clevertech/${project}.git`
const dirName = process.argv[2]
if (!dirName) {
console.log(chalk.red('You must specify a directory name'))
process.exit(1)
}
const basedir = path.resolve(process.cwd(), dirName)
const exec = (command, options) => {
console.log(chalk.blue(command))
return new Promise((resolve, reject) => {
const opts = Object.assign({ shell: true }, options)
child_process.exec(command, opts, (err, stdout, stderr) => {
stderr && console.error(chalk.red(stderr))
stdout && console.log(stdout)
if (err) return reject(err)
resolve(stdout)
})
})
}
const databases = {
postgres: {
port: 5432,
deps: {
knex: '^0.13.0',
pg: '^7.3.0'
}
},
mysql: {
port: 3306,
deps: {
knex: '^0.13.0',
mysql: '^2.15.0'
}
}
}
const databaseNames = Object.keys(databases)
const defaultDeatabase = databaseNames[0]
console.log('Creating a new Clevertech project in', basedir)
const cloneRepo = async (projectName, basedir) => {
console.log('Cloning repository')
try {
await exec(`git clone ${repoURL('ssh', projectName)} ${basedir} --depth 1`)
return 'ssh'
} catch (err) {
if (err.message.indexOf('Permission denied (publickey)') === -1) throw err
console.log('Cloning using SSH failed. Trying with HTTPS')
await exec(
`git clone ${repoURL('https', projectName)} ${basedir} --depth 1`
)
return 'https'
}
}
const deleteGitDir = () => {
console.log('Deleting .git dir')
return fs.remove(path.join(basedir, '.git'))
}
const deleteLicense = () => {
return fs.remove(path.join(basedir, 'LICENSE'))
}
function setImageNames(composeConfig, keys, name) {
keys.forEach(function(key) {
composeConfig.services[key].image = `${name}_${key}:latest`
})
}
const updateDockerCompose = async (answers, dbPassword) => {
const dockerComposePath = path.join(basedir, 'docker-compose.yml')
const dockerComposeSource = await fs.readFile(dockerComposePath, 'utf8')
const dockerCompose = yaml.parse(dockerComposeSource)
const dbPort = databases[answers.databaseEngine].port
const db = dockerCompose.services.db
setImageNames(
dockerCompose,
['api', 'frontend', 'redis'],
dashify(answers.projectName)
)
db.image = answers.databaseEngine
db.ports = [`${dbPort}:${dbPort}`]
const dbUser = toSnakeCase(answers.projectName)
if (answers.databaseEngine === 'postgres') {
db.environment = {
POSTGRES_PASSWORD: dbPassword,
POSTGRES_DB: dbUser + '_local',
POSTGRES_USER: dbUser
}
} else if (answers.databaseEngine === 'mysql') {
db.environment = {
MYSQL_ROOT_PASSWORD: nanoid(),
MYSQL_PASSWORD: dbPassword,
MYSQL_DATABASE: dbUser + '_local',
MYSQL_USER: dbUser
}
}
await fs.writeFile(dockerComposePath, yaml.stringify(dockerCompose, 4, 2))
}
const updateEnvFile = async (answers, dbPassword) => {
const envPath = path.join(basedir, 'api/.env.example')
const envSource = await fs.readFile(envPath, 'utf8')
const changes = {
DB_DATABASE: toSnakeCase(answers.projectName) + '_local',
DB_USER: toSnakeCase(answers.projectName),
DB_PASSWORD: dbPassword,
DB_ENGINE: answers.databaseEngine,
DB_PORT: databases[answers.databaseEngine].port,
HEALTH_CHECK_SECRET: nanoid(),
SESSION_SECRET: nanoid()
}
const envNewSource = envSource
.split('\n')
.map(line => {
for (const key of Object.keys(changes)) {
if (line.startsWith(key + '=')) {
return `${key}=${changes[key]}`
}
}
return line
})
.join('\n')
await fs.writeFile(envPath, envNewSource)
// copy to .env
await fs.copy(
path.join(basedir, 'api/.env.example'),
path.join(basedir, 'api/.env')
)
}
const updateAPIPackageJSON = async answers => {
const packageJSONPath = path.join(basedir, 'api/package.json')
const packageJSON = require(packageJSONPath)
const description = `${answers.projectName} API`
packageJSON.name = dashify(description)
packageJSON.description = description
// remove dependencies
Object.keys(databases).forEach(database => {
if (database === answers.databaseEngine) return
const deps = databases[database].deps
Object.keys(deps).forEach(dep => {
delete packageJSON.dependencies[dep]
})
})
// add dependencies for selected db
const deps = databases[answers.databaseEngine].deps
Object.keys(deps).forEach(dep => {
packageJSON.dependencies[dep] = deps[dep]
})
await fs.writeFile(packageJSONPath, JSON.stringify(packageJSON, null, 2))
}
const updateFrontendPackageJSON = async answers => {
const packageJSONPath = path.join(basedir, 'frontend/package.json')
const packageJSON = require(packageJSONPath)
const description = `${answers.projectName} Frontend`
packageJSON.name = dashify(description)
packageJSON.description = description
await fs.writeFile(packageJSONPath, JSON.stringify(packageJSON, null, 2))
}
const generateRandom = () => {
return generate('abcdefghijklmnopqrstuvwxyz', 6)
}
const SUMOLOGIC_BASE = 'https://service.us2.sumologic.com/ui/index.html'
const sumologicSearch = sourcecategory =>
`${SUMOLOGIC_BASE}#section/search/@0,0@_sourcecategory=%22${sourcecategory}%22`
const sumologicLink = (name, env, component) =>
sumologicSearch(`kubernetes/${name}/${env}/${name}/${component}`)
const updateRootPackageJSON = async answers => {
const randomDev = generateRandom()
const randomStaging = generateRandom()
const packageJSONPath = path.join(basedir, 'package.json')
const packageJSON = require(packageJSONPath)
const name = dashify(answers.projectName)
packageJSON.name = name
packageJSON.description = answers.projectName
packageJSON.scripts.browse = 'browse'
packageJSON.devDependencies['@clevertech.biz/browse'] = '^0.1.2'
packageJSON.browse = {
sentry: `https://sentry.cleverbuild.biz/clevertech/${name}-sentry/`,
development: {
servers: {
api: `https://api-${name}-dev-${randomDev}.cleverbuild.biz/`,
frontend: `https://${name}-dev-${randomDev}.cleverbuild.biz/`
},
logs: {
api: sumologicLink(name, 'development', 'api'),
frontend: sumologicLink(name, 'development', 'frontend')
}
},
staging: {
servers: {
api: `https://api-${name}-staging-${randomStaging}.cleverbuild.biz/`,
frontend: `https://${name}-staging-${randomStaging}.cleverbuild.biz/`
},
logs: {
api: sumologicLink(name, 'staging', 'api'),
frontend: sumologicLink(name, 'staging', 'frontend')
}
},
production: {
servers: {
api: `https://api.example.com`,
frontend: `https://example.com/`
},
logs: {
api: sumologicLink(name, 'production', 'api'),
frontend: sumologicLink(name, 'production', 'frontend')
}
}
}
if (answers.databaseEngine === 'mysql') {
packageJSON.betterScripts['db-client'] =
'mysql -h 127.0.0.1 -u $DB_USER -p$DB_PASSWORD $DB_DATABASE'
}
await fs.writeFile(packageJSONPath, JSON.stringify(packageJSON, null, 2))
}
const updatePrettierConfiguration = async answers => {
const filePath = path.join(basedir, '.prettierrc.json')
const config = JSON.parse(await fs.readFile(filePath, 'utf8'))
config.semi = answers.semi
await fs.writeFile(filePath, JSON.stringify(config, null, 2))
}
const useProjectName = async answers => {
const files = [
'api/Makefile',
'frontend/Makefile',
'docker/run'
]
for (const file of files) {
const filePath = path.join(basedir, file)
// Some files do not exist if you are not a Clevertech employee
if (!(await fs.exists(filePath))) continue
const source = await fs.readFile(filePath, 'utf8')
const sourceNew = source
.replace(/boilerplate/g, dashify(answers.projectName))
.replace(/Boilerplate/g, uppercamelcase(answers.projectName))
await fs.writeFile(filePath, sourceNew)
}
}
const generateHelmFrontend = async (answers, randomValue) => {
const helmFile = path.join(__dirname, 'helm/frontend.yml')
const helmFileSource = await fs.readFile(helmFile, 'utf8')
const helm = yaml.parse(helmFileSource)
helm.deployment.image.repository = helm.deployment.image.repository.replace(
/boilerplate/g,
dashify(answers.projectName)
)
helm.ingress.hosts[0].rules[0].subdomain = helm.ingress.hosts[0].rules[0].subdomain
.replace(/boilerplate/g, dashify(answers.projectName))
.replace(/randomvalue/g, randomValue)
const destFile = path.join(basedir, 'helm-frontend-development.yml')
fs.writeFile(destFile, yaml.stringify(helm, 4, 2))
}
const generateHelmAPI = async (answers, randomValue) => {
const helmFile = path.join(__dirname, 'helm/api.yml')
const helmFileSource = await fs.readFile(helmFile, 'utf8')
const helm = yaml.parse(helmFileSource)
const dbPort = databases[answers.databaseEngine].port
helm.deployment.image.repository = helm.deployment.image.repository.replace(
/boilerplate/g,
dashify(answers.projectName)
)
helm.ingress.hosts[0].rules[0].subdomain = helm.ingress.hosts[0].rules[0].subdomain
.replace(/boilerplate/g, dashify(answers.projectName))
.replace(/randomvalue/g, randomValue)
const dbName = toSnakeCase(answers.projectName)
helm.secrets[0].data = {
DB_ENGINE: answers.databaseEngine,
DB_PORT: dbPort,
DB_DATABASE: dbName + '_development',
DB_POOL_MIN: 2,
DB_POOL_MAX: 10,
DB_HOST: answers.dbhost,
DB_USER: answers.dbuser,
DB_PASSWORD: answers.dbpassword,
REDIS_HOST: answers.redishost,
REDIS_PORT: '6379',
REDIS_PREFIX: dbName + '_development',
SESSION_SECRET: nanoid(),
HEALTH_CHECK_SECRET: nanoid()
}
const destFile = path.join(basedir, 'helm-api-development.yml')
await fs.writeFile(destFile, yaml.stringify(helm, 4, 2))
}
const initGit = async answers => {
const options = { cwd: basedir }
await exec('git init', options)
await exec(`git remote add origin ${answers.gitRemote}`, options)
}
const commit = async answers => {
const options = { cwd: basedir }
await exec('git add -A', options)
await exec('git commit --no-verify -m "Boilerplate initialization"', options)
}
const runYarn = async answers => {
const options = { cwd: basedir }
await exec('yarn', options)
}
const makeAdminQuestions = async initialAnswers => {
if (!initialAnswers.admin) return
console.log(
`Creating ${chalk.cyan('helm')} files for ${chalk.cyan(
'development'
)}. Use values from ${chalk.cyan('terraform')}`
)
const answers = await inquirer.prompt([
{
name: 'dbhost',
type: 'string',
message: "What's the DB host?",
validate: Boolean
},
{
name: 'dbuser',
type: 'string',
message: "What's the DB user?",
validate: Boolean
},
{
name: 'dbpassword',
type: 'string',
message: "What's the DB password?",
validate: Boolean
},
{
name: 'redishost',
type: 'string',
message: "What's the redis host?",
validate: Boolean
}
])
const randomValue = generate('abcdefghijklmnopqrstuvwxyz', 6)
const allAnswers = Object.assign({}, initialAnswers, answers)
await Promise.all([
generateHelmFrontend(allAnswers, randomValue),
generateHelmAPI(allAnswers, randomValue)
])
}
const addExtras = async (deployMode) => {
const dir = path.join(basedir, 'extras')
await cloneRepo('boilerplate-extras', dir)
const files = ['api/Makefile', 'frontend/Makefile', 'terraform']
if (deployMode === 'k8s') {
files.push('buildspec-k8s-api.yml');
files.push('buildspec-k8s-frontend.yml');
} else {
files.push('buildspec-ecs-api.yml');
files.push('buildspec-ecs-frontend.yml');
}
// move files
await Promise.all(
files.map(filename =>
fs.move(path.join(dir, filename), path.join(basedir, filename.replace( /\-k8s|\-ecs/, '' )))
)
)
// add extra information to README
const readme = path.join(basedir, 'README.md')
const readmeExtra = path.join(dir, 'README-extra.md')
let source = await fs.readFile(readme)
source += '\n\n' + (await fs.readFile(readmeExtra))
await fs.writeFile(readme, source)
// Remove cloned extras repo
await fs.remove(dir)
}
const createRootEnvFile = async (answers) => {
const content = `COMPOSE_PROJECT_NAME=${toSnakeCase(answers.projectName)}\n`
await fs.writeFile(path.join(basedir, '.env'), content)
}
const run = async () => {
try {
const protocol = await cloneRepo('boilerplate', basedir)
const answers = await inquirer.prompt([
{
name: 'projectName',
type: 'string',
message:
"What's the official name of the project? (e.g. The New York Times)",
default: path.basename(dirName)
},
{
name: 'databaseEngine',
type: 'list',
message: 'Which database engine are you going to use?',
choices: databaseNames,
default: defaultDeatabase
},
{
name: 'gitRemote',
type: 'string',
message: "What's the Git remote URI?",
default: answers => repoURL(protocol, dashify(answers.projectName))
},
{
name: 'semi',
type: 'confirm',
message: 'Do like semicolons in code?',
default: true
},
{
name: 'employee',
type: 'confirm',
message: 'Are you a Clevertech employee?',
default: false
},
{
name: 'deployMode',
type: 'list',
message: 'What is the deploy mode?',
choices: ['k8s','ecs'],
default: 'k8s'
},
{
name: 'admin',
type: 'confirm',
message: 'Are you a Clevertech admin?',
default: false,
when: answers => answers.employee
}
])
if (answers.employee) {
await addExtras(answers.deployMode)
}
const dbPassword = nanoid()
console.log()
await Promise.all([
createRootEnvFile(answers),
updateDockerCompose(answers, dbPassword),
updateEnvFile(answers, dbPassword),
updateAPIPackageJSON(answers),
updateFrontendPackageJSON(answers),
updateRootPackageJSON(answers),
updatePrettierConfiguration(answers),
useProjectName(answers),
deleteGitDir(answers),
deleteLicense()
])
await initGit(answers)
await makeAdminQuestions(answers)
await runYarn()
await commit()
console.log()
console.log('You are almost all set! Run the application with')
console.log(chalk.cyan('🚀 docker/run'))
console.log()
console.log('Check the logs, issues and more with')
console.log(chalk.cyan('🗄 yarn run browse'))
console.log()
console.log(
'More information on https://github.com/clevertech/boilerplate#local-development'
)
if (answers.admin) {
console.log()
console.log('Use the following helm files:')
console.log(chalk.cyan(path.join(dirName, 'helm-api-development.yml')))
console.log(
chalk.cyan(path.join(dirName, 'helm-frontend-development.yml'))
)
console.log()
}
} catch (err) {
console.error(err)
}
}
run()