-
Notifications
You must be signed in to change notification settings - Fork 2
/
package.js
174 lines (159 loc) · 6.4 KB
/
package.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
/*
* Copyright (c) 2019 - 2022 Geode-solutions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
// require modules
var fs = require('fs');
var path = require('path');
var archiver = require('archiver');
var version = require('./version.json');
const fetch = require('node-fetch');
const {Octokit} = require('@octokit/rest');
const extract = require('extract-zip')
const {execSync} = require('child_process');
const rimraf = require('rimraf').sync;
function pip(pipDestination, extractedDirectory) {
console.log('PIP to:', pipDestination);
execSync(
'python -m pip install --upgrade -r ' +
path.join(
extractedDirectory,
'server/requirements.txt') +
' -t ' + pipDestination,
{stdio: 'inherit'});
}
const dir = 'GeodePackage' +
'-' + process.argv[2] + '-' + process.argv[3];
console.log(dir);
fs.mkdirSync(dir);
const pipDestination = path.join(dir, 'server');
fs.mkdirSync(pipDestination);
const owner = 'Geode-solutions'
var octokit = new Octokit({auth: process.env.TOKEN});
function getRelease(repo, version, isModule) {
const outputDirectory = isModule ? path.join(dir, 'modules') : dir;
return new Promise((resolve, reject) => {
console.log(repo, version);
const tag = 'v' + version;
octokit.repos.getReleaseByTag({owner, repo, tag})
.then(release => {
const release_id = release.data.id;
console.log(release.data);
octokit.repos.listReleaseAssets({owner, repo, release_id})
.then(assets => {
console.log(assets.data);
const asset = assets.data.find(
asset => asset.name.includes(process.argv[3]));
console.log('Asset name:', asset.name);
let assetUrl = asset.url;
fetch(assetUrl, {
headers: {accept: 'application/octet-stream'}
}).then(response => {
const outputFile = repo.concat('.zip');
console.log('Downloading to:', outputFile);
response.body.pipe(fs.createWriteStream(outputFile))
.on('finish', function() {
console.log('Unzipping', outputFile);
try {
extract(outputFile, {
dir: path.resolve(outputDirectory)
}).then(() => {
if (isModule) {
const extractedName = asset.name.slice(0, -4);
const extractedDirectory =
path.join(outputDirectory, repo);
rimraf(extractedDirectory);
fs.renameSync(
path.join(outputDirectory, extractedName),
extractedDirectory);
}
console.log('Unzip to:', repo);
resolve();
});
} catch (error) {
reject(error);
}
});
});
});
})
.catch((error) => {
console.log(error);
reject(error);
});
});
}
let promises = [];
let config = {modules: []};
promises.push(getRelease('Geode', version.geode, false));
for (let [repo, tag] of Object.entries(version.modules)) {
const repoGeode = repo.concat('.geode');
config.modules.push(path.join('modules', repoGeode, 'config.json'));
promises.push(getRelease(repoGeode, tag, true));
}
fs.writeFileSync(path.join(dir, 'config.json'), JSON.stringify(config));
Promise.all(promises).then(() => {
pip(pipDestination, dir);
for (let [repo, tag] of Object.entries(version.modules)) {
const repoGeode = repo.concat('.geode');
pip(pipDestination, path.join(dir, 'modules', repoGeode));
}
// create a file to stream archive data to.
const outputName = path.join(__dirname, dir + '.zip');
console.log('Output: ', outputName);
var output = fs.createWriteStream(outputName);
var archive = archiver('zip');
// listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log(
'archiver has been finalized and the output file descriptor has closed.');
});
// This event is fired when the data source is drained no matter what was the
// data source. It is not part of this library but rather from the NodeJS
// Stream API.
// @see: https://nodejs.org/api/stream.html#stream_event_end
output.on('end', function() {
console.log('Data has been drained');
});
// good practice to catch warnings (ie stat failures and other non-blocking
// errors)
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
// log warning
} else {
// throw error
throw err;
}
});
// good practice to catch this error explicitly
archive.on('error', function(err) {
throw err;
});
// pipe archive data to the file
archive.pipe(output);
archive.directory(dir);
// finalize the archive (ie we are done appending files but streams have to
// finish yet) 'close', 'end' or 'finish' may be fired right after calling
// this method so register to them beforehand
archive.finalize();
});