-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostinstall.js
65 lines (55 loc) · 1.82 KB
/
postinstall.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
const https = require("follow-redirects").https;
const unzipper = require("unzipper");
const q = require("q");
const fs = require("fs-extra");
const path = require("path");
const uri = require("url");
const releasesUrl = 'https://api.github.com/repos/geeklearningio/gl-docs-theme/releases';
function downloadJson(url, dest) {
var details = uri.parse(url)
let deferal = q.defer();
let content = '';
let request = https.get({
protocol: 'https:',
hostname : details.host,
path: details.path,
headers:{
'Accept' : ' application/vnd.github.v3+json' ,
'User-Agent' : 'YarnPostInstall'
}
}, (response) => {
response.on("end", () => {
deferal.resolve(JSON.parse(content));
}).on('data', function (chunk) {
content += chunk.toString('utf8');
});
}).on("error", (err) => {
deferal.reject(err);
});
return deferal.promise;
}
function downloadFile(url, dest) {
let deferal = q.defer();
let file = fs.createWriteStream(dest);
let request = https.get(url, (response) => {
response.pipe(file);
file.on("finish", () => {
deferal.resolve();
});
}).on("error", (err) => {
deferal.reject(err);
});
return deferal.promise;
}
async function run() {
var releases = await downloadJson(releasesUrl)
console.log(releases[0].assets[0].browser_download_url);
await downloadFile(
releases[0].assets[0].browser_download_url,
'./gl-template.zip');
fs.ensureDirSync('./docfx_project/gl-template');
await fs.createReadStream('./gl-template.zip')
.pipe(unzipper.Extract({ path: path.join(__dirname, './docfx_project/gl-template') })).promise()
.then((ok) => console.log('ok'), (err) => console.log('ko', err));
}
run();