-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync-apps.js
78 lines (69 loc) · 2.31 KB
/
sync-apps.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
const https = require('https');
const tar = require('tar');
const fs = require('fs-extra');
const path = require('path');
const appBuilds = require('./app-builds');
const downloadAndExtract = async (key, url, tag) => {
const finalUrl = url.replace('{{tag}}', tag);
console.log(`Downloading ${key} from ${finalUrl}`);
const appPath = path.join(__dirname, 'public/apps', key);
// Create the folder if it doesn't exist
fs.mkdirSync(appPath, { recursive: true });
const downloadFile = (url, redirections = 5) => {
return new Promise((resolve, reject) => {
if (redirections < 1) {
reject(new Error('Too many redirections'));
}
https.get(url, (response) => {
if (response.statusCode === 200) {
resolve(response);
} else if (response.statusCode === 302 || response.statusCode === 301) {
console.log(`Redirecting to ${response.headers.location}`);
downloadFile(response.headers.location, redirections - 1).then(resolve);
} else {
reject(`Failed to download file, status code: ${response.statusCode}`);
}
});
});
};
await downloadFile(finalUrl)
.then((response) => {
const extraction = tar.x({ cwd: appPath });
response.pipe(extraction);
return new Promise((resolve, reject) => {
extraction.on('finish', resolve);
extraction.on('error', reject);
});
})
.then(() => {
const buildPath = path.join(appPath, 'build');
if (fs.existsSync(buildPath)) {
return fs
.copy(buildPath, appPath)
.then(() => fs.remove(buildPath))
.then(() => console.log(`Moved files from ${buildPath} to ${appPath}`));
} else {
throw new Error(`No build directory in ${key} build path`);
}
})
.catch((error) => {
throw new Error(`Failed to download and extract ${key}: ${error}`);
});
};
const deleteAppsDir = async () => {
const appsDirPath = path.join(__dirname, 'public/apps');
await fs.remove(appsDirPath);
};
const main = async () => {
await deleteAppsDir();
try {
for (const key in appBuilds) {
const { tag, url } = appBuilds[key];
await downloadAndExtract(key, url, tag);
}
} catch (error) {
console.error(`Error getting child apps: ${error}`);
await deleteAppsDir();
}
};
main();