-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdataset-update.ts
174 lines (142 loc) · 5.02 KB
/
dataset-update.ts
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
import * as os from 'os';
import * as fs from 'fs';
import * as fse from 'fs-extra';
import * as net from 'net';
import * as path from 'path';
import * as events from 'events';
import * as urlLib from 'url';
import * as semverSort from 'semver-sort';
import { getRemoteInfo } from 'isomorphic-git';
import httpGit from "isomorphic-git/http/node";
import * as extract from 'extract-zip';
import * as DecompressZip from 'decompress-zip';
import { http, https } from 'follow-redirects';
export async function updateDataset(tag: any, destDir: string, userDataPath: string, em?: events.EventEmitter) {
const dsFeedUrl = `https://github.com/open-numbers/${tag.path}/archive/v${tag.version}.zip`;
const tempPath = path.resolve(userDataPath, `gapminder-offline-dataset-temp_${tag.ds}`);
await removeDir(tempPath);
if (em) {
em.emit('ds-update-status', `Downloading ${tag.path} dataset archive...`);
}
const dlFile = await download({url: dsFeedUrl, path: tempPath, file: 'dl.zip'});
const unpackFun = os.platform() === 'win32' ? unpackWin : unpackNix;
if (em) {
em.emit('ds-update-status', `Unpacking ${tag.path} dataset archive...`);
}
const unpacked = await unpackFun({fullPath: dlFile, target: path.resolve(tempPath, 'unpacked')});
const contentDir = await getFirstDir(unpacked);
if (em) {
em.emit('ds-update-status', `Updating existing ${tag.path} dataset...`);
}
await removeDir(path.resolve(contentDir,'etl'));
await removeDir(path.resolve(contentDir,'.gitignore'));
await removeDir(path.resolve(contentDir,'.gitattributes'));
await removeDir(path.resolve(contentDir,'.gitmodules'));
await copy(contentDir, destDir);
await removeDir(tempPath);
}
async function copy(source: string, dest: string): Promise<void> {
return new Promise<void>((resolve: Function, reject: Function) => {
fse.copy(source, dest, err => {
if (err) {
return reject(err);
}
resolve();
});
});
}
async function getFirstDir(baseDir: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
fs.readdir(baseDir, (err, files) => {
if (err) {
return reject(err);
}
if (files.length !== 1) {
return reject();
}
resolve(path.resolve(baseDir, files[0]));
});
});
}
export async function getLatestGithubTag(inputParam: string): Promise<string> {
return new Promise<string>((resolve: Function, reject: Function) => {
const input = inputParam.replace(/^(?!(?:https|git):\/\/)/, 'https://');
getRemoteInfo({ http: httpGit, url: input }).then(info => {
const tags = info?.refs?.tags;
if (tags) {
resolve(semverSort.desc(Object.keys(tags).map(tag => tag.replace(/\^\{\}$/, '').substr(1)))[0]);
}
return reject('Tags are missing');
});
});
}
async function removeDir(what: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
fse.remove(what, err => {
if (err) {
return reject(err);
}
resolve();
});
});
}
async function unpackWin(options: { fullPath: string, target: string }): Promise<string> {
const unzipper = new DecompressZip(options.fullPath);
const targetPath = path.resolve(options.target);
return new Promise<string>((resolve: Function, reject: Function) => {
unzipper.on('error', reject);
unzipper.on('extract', () => {
resolve(targetPath);
});
unzipper.extract({
path: targetPath,
follow: true
});
});
}
async function unpackNix(options): Promise<string> {
const targetPath = path.resolve(options.target);
return new Promise<string>((resolve: Function, reject: Function) => {
extract(options.fullPath, {dir: targetPath}, err => {
if (err) {
return reject(err);
}
resolve(targetPath);
});
});
}
async function download(options: { url: string, path: string, file: string }): Promise<string> {
const TIMEOUT = 240000;
const mode = parseInt('0777', 8) & (~process.umask());
return new Promise<string>((resolve: Function, reject: Function) => {
fs.mkdir(options.path, mode, pathErr => {
if (pathErr) {
return reject(pathErr);
}
const filePath = path.resolve(options.path, options.file);
const file = fs.createWriteStream(filePath);
const timeoutWrapper = req => () => {
req.abort();
reject('File transfer timeout!');
};
const relatedLib = options.url.indexOf('https://') === 0 ? https : http;
const request = relatedLib.get(options.url)
.on('response', res => {
res.on('data', chunk => {
file.write(chunk);
clearTimeout(timeoutId);
timeoutId = setTimeout(timeoutAction, TIMEOUT);
}).on('end', () => {
clearTimeout(timeoutId);
file.end();
resolve(filePath);
}).on('error', err => {
clearTimeout(timeoutId);
reject(err);
});
});
const timeoutAction = timeoutWrapper(request);
let timeoutId = setTimeout(timeoutAction, TIMEOUT);
});
});
}