-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
86 lines (82 loc) · 2.48 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
const { getAgent } = require('@tigojs/api-request');
const inquirer = require('inquirer');
const path = require('path');
const fs = require('fs');
async function sendSaveRequest(devConfigPath) {
// read dev config
if (!fs.existsSync(devConfigPath)) {
throw new Error('Cannot find development configuration.');
}
const devConfig = JSON.parse(fs.readFileSync(devConfigPath, { encoding: 'utf-8' }));
const deployConfig = devConfig?.deploy;
if (!deployConfig) {
throw new Error('Cannot find deploy configuration in the .tigodev.');
}
// read bundled script
const outputPath = devConfig?.rollup?.output || './dist/bundled.js';
const BUNDLED_PATH = path.resolve(process.cwd(), outputPath);
if (!fs.existsSync(BUNDLED_PATH)) {
throw new Error('Cannot find bundled script.');
}
const bundledScript = fs.readFileSync(BUNDLED_PATH, { encoding: 'utf-8' });
// check upload config
const shouldNotBeEmpty = ['host', 'https', 'base', 'accessKey', 'secretKey'];
shouldNotBeEmpty.forEach((key) => {
if (!deployConfig[key]) {
throw new Error(`Option "${key}" in upload configuration is necessary, please set it first.`);
}
});
// check name
let name = deployConfig.name;
let isNew = false;
if (!name) {
isNew = true;
const answers = await inquirer.prompt([
{
type: 'input',
message: 'This seems like a new script, you should give it a name.',
default: '',
validate: (input) => {
if (!input.trim()) {
return 'Name is empty.';
}
return true;
},
},
]);
name = answers.name.trim();
if (!name) {
throw new Error('Cannot get the name of script.');
}
}
const agent = getAgent(deployConfig);
const action = isNew ? 'add' : 'edit';
const params = {
action,
name,
content: Buffer.from(bundledScript, 'utf-8').toString('base64'),
};
if (action === 'edit') {
Object.assign(params, id);
}
const env = devConfig?.lambda?.env;
if (env) {
Object.assign(params, {
env,
});
}
const policy = devCOnfig?.lambda?.policy;
if (policy) {
Object.assign(params, {
policy,
});
}
const res = await agent.post('/faas/save').send(params);
if (res.status !== 200) {
throw new Error(`Upload failed.${` ${res.body.message}`}`);
}
devConfig.deploy.name = name;
// write dev config
fs.writeFileSync(CONFIG_PATH, JSON.stringify(devConfig, null, ' '), { encoding: 'utf-8' });
}
module.exports = sendSaveRequest;