-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
103 lines (81 loc) · 2.9 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
const path = require('path');
const storage = require('@google-cloud/storage');
const fsp = require('fs-promise');
const {exec} = require('mz/child_process');
function fetchCurrentVersion(ui, configFile) {
ui.writeLine(`fetching current app version from ${configFile.bucket.name}/${configFile.name}`);
return configFile.download().then(([buffer]) => {
const config = JSON.parse(buffer.toString());
ui.writeLine('got config', config);
return config;
});
}
function removeOldApp(ui, outputPath) {
ui.writeLine(`removing ${outputPath}`);
return fsp.remove(outputPath);
}
function downloadAppZip(ui, zipFile, zipPath) {
ui.writeLine(`saving Cloud Storage object ${zipFile.bucket.name}/${zipFile.name} to ${zipPath}`);
return zipFile.download({destination: zipPath});
}
function unzipApp(ui, zipPath) {
return execute(ui, `unzip ${zipPath}`).then(() => {
ui.writeLine(`unzipped ${zipPath}`);
});
}
function installDependencies(ui, outputPath) {
return fsp.access(path.join(outputPath, 'yarn.lock')).then(() => {
return execute(ui, `cd ${outputPath} && yarn install --prefer-offline`);
}, () => {
return execute(ui, `cd ${outputPath} && npm install`);
}).then(() => {
ui.writeLine('installed dependencies');
}).catch(() => {
ui.writeError('unable to install dependencies');
});
}
function execute(ui, command) {
return exec(command).catch((stdout, stderr) => {
ui.writeError(`error running command ${command}`);
ui.writeError(stderr);
});
}
function outputPathFor(zipPath) {
const name = path.basename(zipPath, '.zip');
// Remove MD5 hash
return name.split('-').slice(0, -1).join('-');
}
class AppNotFoundError extends Error {
constructor() {
super(...arguments);
this.name = 'AppNotFoundError';
}
}
class GCSDownloader {
constructor({bucket, key, authentication} = {}) {
this.configBucket = bucket;
this.configKey = key || 'fastboot-deploy-info.json';
this.authentication = authentication;
}
download() {
if (!this.configBucket || !this.configKey) {
this.ui.writeError('no Cloud Storage bucket or key provided; not downloading app');
return Promise.reject(new AppNotFoundError());
}
const gcs = storage(this.authentication);
const configFile = gcs.bucket(this.configBucket).file(this.configKey);
return fetchCurrentVersion(this.ui, configFile).then(({bucket: appBucket, key: appKey}) => {
const zipFile = gcs.bucket(appBucket).file(appKey);
const zipPath = path.basename(zipFile.name);
const outputPath = outputPathFor(zipPath);
return removeOldApp(this.ui, outputPath).then(() => {
return downloadAppZip(this.ui, zipFile, zipPath);
}).then(() => {
return unzipApp(this.ui, zipPath);
}).then(() => {
return installDependencies(this.ui, outputPath);
}).then(() => outputPath);
});
}
}
module.exports = GCSDownloader;