-
Notifications
You must be signed in to change notification settings - Fork 19
/
postsetup.js
77 lines (70 loc) · 2.18 KB
/
postsetup.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
// Check to see if the user has Docker installed and which version of Python they prefer.
'use strict';
var inquirer = require('inquirer');
var chalk = require('chalk');
var YAML = require('js-yaml');
var fs = require('fs');
console.log(chalk.yellow('Hi, a few quick questions before we start:'));
var questions = [
{
type: 'list',
name: 'python',
message: 'What Python version?',
choices: ['python2.7', 'python3.6'],
filter: function (val) {
return val.toLowerCase();
}
},
{
type: 'confirm',
name: 'docker',
message: 'Do you have Docker installed? Recommended, but not required.',
default: true
},
{
type: 'confirm',
name: 'wantsDomain',
message: 'Do you want to set up a custom domain? e.g. api.mycompany.com? Requires a domain in Route53.',
default: false
},
{
type: 'input',
name: 'domainName',
message: 'What is your domain name? e.g. api.mycompany.com',
when: function(answers) {
return answers.wantsDomain
}
},
]
inquirer.prompt(questions).then(function (answers) {
var doc = YAML.safeLoad(fs.readFileSync('serverless.yml', 'utf8'));
doc.custom.pythonRequirements.dockerizePip = answers.docker;
doc.provider.runtime = answers.python;
if (answers.wantsDomain) {
doc.plugins.push('serverless-domain-manager');
doc.custom.customDomain = createCustomDomain(answers.domainName);
}
saveServerlessYml(doc);
console.log(chalk.yellow("All set! Run `sls deploy` to send your code to the cloud"));
if (answers.wantsDomain) {
console.log(chalk.yellow("Run `sls create_domain` to set up your custom domain."))
}
});
const createCustomDomain = (domainName) => {
return {
domainName,
basePath: '',
stage: '${self:provider.stage}',
createRoute53Record: true
}
}
const saveServerlessYml = (config) => {
/*
When dumping to yml, it puts quotation marks around Serverless variables. This
breaks variable resolution, so I clean it out after dumping to yml but before
writing to the config file.
*/
const dumped = YAML.dump(config);
const cleaned = dumped.replace("\'${self:provider.stage}\'", "${self:provider.stage}")
fs.writeFileSync('serverless.yml', cleaned);
}