-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.js
208 lines (174 loc) · 4.62 KB
/
deploy.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
const chalk = require('chalk');
const deploy = require('@formspree/deploy');
const ora = require('ora');
const version = require('../../package.json').version;
const log = require('../log');
const messages = require('../messages');
const env = require('process').env;
const { traverse } = require('../traverse');
const printErrors = ({ code, errors }) => {
switch (code) {
case 'CONFIG_VALIDATION_ERROR':
console.error('');
errors.forEach((error, idx) => {
console.error(
` ${`${idx + 1})`} ${chalk.cyan(error.field)} ${error.message}`
);
});
console.error('');
break;
default:
console.error('');
errors.forEach((error, idx) => {
console.error(` ${`${idx + 1})`} ${error.message}`);
});
console.error('');
break;
}
};
const printDeployLog = ({ log }) => {
if (!log) return;
console.log('');
log.forEach((item, idx) => {
console.log(` ${`${idx + 1})`} ${item}`);
});
console.log('');
};
exports.command = 'deploy';
exports.describe = 'Deploys formspree.json';
exports.builder = yargs => {
yargs.option('config', {
alias: 'c',
describe: 'Site configuration'
});
yargs.option('key', {
alias: 'k',
describe: 'Deploy key'
});
yargs.option('endpoint', {
alias: 'e',
describe: 'API endpoint'
});
yargs.option('force', {
alias: 'f',
describe: 'Skip verifying that secrets reference environment variables',
type: 'boolean',
default: false
});
yargs.option('file', {
describe: 'Path to the local `formspree.json` file',
default: 'formspree.json'
});
};
exports.handler = async args => {
const rawConfig = args.config || deploy.getRawConfig(args);
const endpoint = args.endpoint || 'https://formspree-cli.herokuapp.com';
const userAgent = `@formspree/cli@${version}`;
const spinner = ora(chalk.gray('Deploying...'));
if (!rawConfig) {
log.error('Configuration not provided');
process.exitCode = 1;
return;
}
let parsedRawConfig;
try {
parsedRawConfig = JSON.parse(rawConfig);
} catch (err) {
log.error('Configuration could not be parsed');
process.exitCode = 1;
return;
}
// Traverse the config and validate that certain specially-named keys
// reference environment variables.
let invalidKeys = [];
const sensitiveKeys = ['apiKey', 'apiSecret', 'secretKey'];
traverse(parsedRawConfig, (key, value) => {
if (
sensitiveKeys.indexOf(key) > -1 &&
!value.match(/^\$([A-Za-z0-9_]+)$/)
) {
invalidKeys.push(key);
}
});
if (!args.force && invalidKeys.length > 0) {
log.error(
`The following properties must reference environment variables: ${invalidKeys.join(
', '
)}`
);
log.meta('To override this, use the `-f` flag.');
process.exitCode = 1;
return;
}
// Replace environment variable $-references with the actual values
// If the environment variable is not defined, store in an array an present
// an error to the user.
let undefinedEnvRefs = [];
const rawConfigWithSecrets = rawConfig.replace(
/\$([A-Za-z0-9_]+)/gi,
(_match, variableName) => {
let value = env[variableName];
if (value) return value;
undefinedEnvRefs.push(variableName);
}
);
if (undefinedEnvRefs.length > 0) {
log.error(
`The following environment variables were referenced but are not defined: ${undefinedEnvRefs.join(
', '
)}`
);
process.exitCode = 1;
return;
}
let config;
try {
config = JSON.parse(rawConfigWithSecrets);
} catch (err) {
log.error('Configuration could not be parsed');
process.exitCode = 1;
return;
}
const key = args.key || deploy.getDeployKey(args);
if (!key) {
messages.authRequired();
process.exitCode = 1;
return;
}
spinner.start();
try {
const response = await deploy.request({
endpoint,
config,
key,
userAgent
});
spinner.stop();
switch (response.status) {
case 200:
log.success(
`Deployment succeeded ${chalk.gray(`(${response.data.id})`)}`
);
printDeployLog(response.data);
return;
case 401:
log.error('Deploy key is not valid');
process.exitCode = 1;
return;
case 422:
log.error(`Deployment failed`);
printErrors(response.data);
process.exitCode = 1;
return;
default:
log.error('Deployment failed');
process.exitCode = 1;
return;
}
} catch (error) {
spinner.stop();
log.error('Deployment failed unexpectedly');
process.exitCode = 1;
return;
}
};