forked from mzehrer/node-7z
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinstaller.mjs
350 lines (324 loc) · 11.8 KB
/
installer.mjs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env node
'use strict';
import fs from 'fs-extra';
import {
fileURLToPath
} from 'url';
import {
dirname,
join,
sep,
} from 'path';
import spawn from 'cross-spawn';
import unCompress from 'all-unpacker';
import retryPromise from 'retrying-promise';
import fetching from 'node-wget-fetch';
import system_installer from 'system-installer';
import macos_release from 'macos-release';
const __filename = fileURLToPath(
import.meta.url);
const __dirname = dirname(__filename);
const versionCompare = (left, right) => {
if (typeof left + typeof right != 'stringstring')
return false;
let a = left.split('.');
let b = right.split('.');
let i = 0;
let len = Math.max(a.length, b.length);
for (; i < len; i++) {
if ((a[i] && !b[i] && parseInt(a[i]) > 0) || (parseInt(a[i]) > parseInt(b[i]))) {
return 1;
} else if ((b[i] && !a[i] && parseInt(b[i]) > 0) || (parseInt(a[i]) < parseInt(b[i]))) {
return -1;
}
}
return 0;
}
const appleOs = (process.platform == "darwin") ? macos_release.version : '99.99.99',
macOsVersion = (versionCompare(appleOs, '10.11.12') == 1) ? '10.15' : '10.11',
_7zAppUrl = 'https://7-zip.org/a/',
cwd = process.cwd(),
binaryDestination = join(__dirname, 'binaries', process.platform);
const windowsPlatform = {
source: join(cwd, '7z1900-extra.7z'),
destination: join(cwd, 'win32'),
url: 'https://d.7-zip.org/a/',
filename: '7z1900-extra.7z',
extraName: 'lzma1900.7z',
extractFolder: '',
appLocation: '',
binaryFiles: ['Far', 'x64', '7za.dll', '7za.exe', '7zxa.dll'],
binaryDestinationDir: join(__dirname, 'binaries', 'win32'),
sfxModules: ['7zr.exe', '7zS2.sfx', '7zS2con.sfx', '7zSD.sfx'],
platform: 'win32',
binary: '7za.exe',
extraSourceFile: join(cwd, 'win32', 'lzma1900.7z'),
};
const windowsOtherPlatform = {
source: join(cwd, '7z1604-extra.7z'),
destination: join(cwd, 'other32'),
url: 'https://d.7-zip.org/a/',
filename: '7z1604-extra.7z',
extraName: 'lzma1604.7z',
extractFolder: '',
appLocation: '',
binaryFiles: ['Far', 'x64', '7za.dll', '7za.exe', '7zxa.dll'],
binaryDestinationDir: join(__dirname, 'binaries', 'win32', 'other32'),
sfxModules: ['7zr.exe', '7zS2.sfx', '7zS2con.sfx', '7zSD.sfx'],
platform: 'win32',
binary: '7za.exe',
extraSourceFile: join(cwd, 'other32', 'lzma1604.7z'),
};
const linuxPlatform = {
source: join(cwd, 'p7zip_16.02_x86_linux_bin.tar.bz2'),
destination: join(cwd, 'linux'),
url: 'https://iweb.dl.sourceforge.net/project/p7zip/p7zip/16.02/',
filename: 'p7zip_16.02_x86_linux_bin.tar.bz2',
extraName: 'lzma1604.7z',
extractFolder: 'p7zip_16.02',
appLocation: 'bin',
binaryFiles: ['7z', '7z.so', '7za', '7zCon.sfx', '7zr', 'Codecs'],
binaryDestinationDir: join(__dirname, 'binaries', 'linux'),
sfxModules: ['7zS2.sfx', '7zS2con.sfx', '7zSD.sfx'],
platform: 'linux',
binary: '7za',
extraSourceFile: join(cwd, 'linux', 'lzma1604.7z'),
};
const macVersion = (macOsVersion == '10.15') ? 'p7zip-16.02-macos10.15.pkg' : 'p7zip-16.02-macos10.11.pkg';
const appleMacPlatform = {
source: join(cwd, macVersion),
destination: join(cwd, 'darwin'),
url: 'https://raw.githubusercontent.com/rudix-mac/packages/master/',
filename: macVersion,
extraName: 'lzma1604.7z',
extractFolder: '',
appLocation: 'usr/local/lib/p7zip',
binaryFiles: ['7z', '7z.so', '7za', '7zCon.sfx', '7zr', 'Codecs'],
binaryDestinationDir: join(__dirname, 'binaries', 'darwin'),
sfxModules: ['7zS2.sfx', '7zS2con.sfx', '7zSD.sfx'],
platform: 'darwin',
binary: '7za',
extraSourceFile: join(cwd, 'darwin', 'lzma1604.7z'),
};
function retrieve(path = {
url: '',
dest: ''
}) {
console.log('Downloading ' + path.url);
return new Promise((resolve, reject) => {
fetching.wget(path.url, path.dest)
.then((info) => resolve(info))
.catch((err) => reject('Error downloading file: ' + err));
});
}
function platformUnpacker(platformData = windowsPlatform) {
return new retryPromise({
retries: 5
}, (resolve, retry) => {
retrieve({
url: platformData.url + platformData.filename,
dest: platformData.source
}).then(() => {
console.log('Extracting: ' + platformData.filename);
if (platformData.platform == 'darwin') {
let destination = platformData.destination;
if (process.platform == 'win32') {
macUnpack(platformData)
.then(() => {
return resolve('darwin');
}).catch((err) => retry(err));
} else {
unpack(platformData.source, destination)
.then((data) => {
console.log('Decompressing: p7zipinstall.pkg/Payload');
unpack(join(destination, 'p7zipinstall.pkg', 'Payload'), destination).then(() => {
console.log('Decompressing: Payload');
unpack(join(destination, 'Payload'), destination, platformData.appLocation + sep + '*').then(() => {
return resolve('darwin');
})
.catch((err) => retry(err));
})
.catch((err) => retry(err));
})
.catch((err) => retry(err));
}
} else if (platformData.platform == 'win32') {
unpack(platformData.source, platformData.destination)
.then(() => {
return resolve('win32');
})
.catch((err) => retry(err));
} else if (platformData.platform == 'linux') {
unpack(platformData.source, platformData.destination)
.then(() => {
const system = system_installer.packager();
const toInstall = (system.packager == 'yum' || system.packager == 'dnf') ?
'glibc.i686' : 'libc6-i386';
if (process.platform == 'linux')
system_installer.installer(toInstall).then(() => {
return resolve('linux');
});
else
return resolve('linux');
})
.catch((err) => retry(err));
} else if (fetching.isString(platformData.platform)) {
unpack(platformData.source, platformData.destination)
.then(() => {
return resolve(platformData.platform);
})
.catch((err) => retry(err));
}
}).catch((err) => retry(err));
}).catch((err) => console.error(err));
}
function unpack(source, destination, toCopy) {
return new Promise((resolve, reject) => {
return unCompress.unpack(
source, {
files: (toCopy == null ? '' : toCopy),
targetDir: destination,
forceOverwrite: true,
noDirectory: true,
quiet: true,
},
(err, files, text) => {
if (err)
return reject(err);
console.log(text);
return resolve(files);
}
);
});
}
function extraUnpack(cmd = '', source = '', destination = '', toCopy = []) {
let args = ['e', source, '-o' + destination];
let extraArgs = args.concat(toCopy).concat(['-r', '-aos']);
console.log('Running: ' + cmd + ' ' + extraArgs);
return spawnSync(cmd, extraArgs);
}
function macUnpack(dataFor = appleMacPlatform, dataForOther = windowsOtherPlatform) {
return new Promise((resolve, reject) => {
retrieve({
url: dataForOther.url + '7z1805-extra.7z',
dest: '.' + sep + '7z-extra.7z'
})
.then(() => {
let destination = join(cwd, 'other');
unpack(join(__dirname, '7z-extra.7z'), destination)
.then(() => {
extraUnpack(join(__dirname, 'other', '7za.exe'), dataFor.source, dataFor.destination);
console.log('Decompressing: ' + 'p7zip-16.02-macos10.15');
unpack(join(dataFor.destination, 'p7zip-16.02-macos10.15'), dataFor.destination)
.then(() => {
fs.emptyDir(destination).then(() => {
fs.unlink(join(__dirname, '7z-extra.7z')).then(() => {
fs.removeSync(destination);
return resolve('darwin');
});
});
})
.catch(() => {
fs.emptyDir(destination).then(() => {
fs.unlink(join(__dirname, '7z-extra.7z')).then(() => {
fs.removeSync(destination);
return resolve('darwin');
});
});
});
}).catch((err) => reject);
}).catch((err) => reject);
});
}
function spawnSync(spCmd = '', spArgs = []) {
let doUnpack = spawn.sync(spCmd, spArgs, {
stdio: 'pipe'
});
if (doUnpack.error) {
console.error('Error 7za exited with code ' + doUnpack.error);
console.error('resolve the problem and re-install using:');
console.error('npm install');
}
return doUnpack;
}
function makeExecutable(binary = [], binaryFolder = '') {
binary.forEach((file) => {
try {
if (file == 'Codecs')
file = 'Codecs' + sep + 'Rar.so'
fs.chmodSync(join(binaryFolder, file), 755);
} catch (err) {
console.error(err);
}
});
}
[windowsOtherPlatform, windowsPlatform, linuxPlatform, appleMacPlatform].forEach((dataFor) => {
fs.mkdir(dataFor.destination, (err) => {
if (err) {}
retrieve({
url: _7zAppUrl + dataFor.extraName,
dest: dataFor.extraSourceFile
})
.then(() => {
platformUnpacker(dataFor)
.then(() => {
dataFor.binaryFiles.forEach((file) => {
try {
let from = join(dataFor.destination, dataFor.extractFolder, dataFor.appLocation, file);
let to = join(dataFor.binaryDestinationDir, file);
if (file == '7zCon.sfx') {
file = '7zCon' + dataFor.platform + '.sfx';
let location = join(binaryDestination, (process.platform == 'win32' ? 'other32' : ''));
to = join(location, file);
fs.moveSync(from, to, {
overwrite: true
});
makeExecutable([file], location);
console.log('Sfx module ' + file + ' copied successfully!');
} else if (dataFor.platform == process.platform) {
fs.moveSync(from, to, {
overwrite: true
});
if (dataFor.platform != 'win32')
makeExecutable([file], dataFor.binaryDestinationDir);
}
} catch (err) {
console.error(err);
}
});
console.log('Binaries copied successfully!');
fs.unlinkSync(dataFor.source);
if (process.platform == dataFor.platform) {
setTimeout(() => {
extraUnpack(join(dataFor.binaryDestinationDir, dataFor.binary),
dataFor.extraSourceFile,
binaryDestination,
dataFor.sfxModules
);
fs.unlink(dataFor.extraSourceFile, (err) => {
if (err) throw err;
dataFor.sfxModules.forEach((file) => {
let name = file.replace(/.sfx/g, (dataFor.destination.includes('win32') ? '' : 'other32') + '.sfx');
let to = join(binaryDestination, ((name.includes('other32') && process.platform == 'win32') ? 'other32' : ''), name);
if (name.includes('other32')) {
fs.renameSync(join(binaryDestination, file), to);
}
console.log('Sfx module ' + name + ' copied successfully!');
});
fs.removeSync(dataFor.destination);
});
}, 5000);
} else {
fs.removeSync(dataFor.destination);
}
})
.catch((err) => {
console.error('Unpacking for platform failed.');
throw (err);
});
})
.catch((err) => {
console.error('Error downloading file: ' + err);
});
});
});