-
Notifications
You must be signed in to change notification settings - Fork 2
/
preinstall.js
141 lines (126 loc) · 4.04 KB
/
preinstall.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
const { promisify } = require('util');
const zlib = require('zlib');
const tar = require('tar');
const Gauge = require('gauge');
const path = require('path');
const http = require('https');
const writeFile = promisify(require('fs').writeFile);
const link = promisify(require('fs').link);
const rimraf = promisify(require('rimraf'));
const mkdirp = promisify(require('mkdirp'));
const decompress = require('decompress');
const package = require('./package.json');
const VERSION_BASE = package['cmake_version_base'];
const VERSION_REV = package['cmake_version_rev'];
const VERSION = `${VERSION_BASE}.${VERSION_REV}`;
const tempPath = path.join(__dirname, 'temp');
const linkPath = path.join(__dirname, 'bin');
const outPath = path.join(__dirname, 'bin2');
const archives = [
'win32-x86.zip',
'win64-x64.zip',
'Linux-x86_64.tar.gz',
'Darwin-x86_64.tar.gz',
];
function init() {
return Promise.resolve();
}
function archiveToUrl(arch) {
return `https://cmake.org/files/v${VERSION_BASE}/cmake-${VERSION}-${arch}`;
}
let archive;
if (process.platform === 'win32') {
if (process.arch == 'x86') {
archive = archives[0];
} else if (process.arch === 'x64') {
archive = archives[1];
} else {
throw new Error('unsupported architecture');
}
} else if (process.platform === 'linux') {
archive = archives[2];
} else if (process.platform === 'darwin') {
archive = archives[3];
} else {
throw new Error('unsupported platform');
}
/* Fetch a file as a buffer, and display a progress bar */
function fetchBuffer(url) {
return new Promise((resolve, reject) => {
http.get(url, res => {
const totalSize = parseInt(res.headers['content-length'], 10);
const gauge = new Gauge();
let currentSize = 0;
const buffers = [];
res.on('data', data => {
currentSize += data.length;
buffers.push(data);
const progress = currentSize / totalSize;
gauge.show(`${(progress * 100).toFixed(0)}%`, progress);
gauge.pulse(url);
});
res.on('end', () => {
const b = Buffer.concat(buffers, totalSize);
gauge.hide();
resolve(b);
});
res.on('error', reject);
});
});
}
function makeEmptyFile(path) {
return writeFile(path, '');
}
/* will make hard links of binaries, so npm can correctly install the binaries as per the package.json */
function makeLinks() {
const exePaths = require('./index.js');
const promises = [];
for (const _name of Object.keys(exePaths)) {
let name = _name;
if (name === 'cmakeGui') name = 'cmake-gui';
try {
const exe = exePaths[_name]();
if (process.platform === 'win32') {
// npm on windows is stupid, you need both an exe and a file without extension for it to install.
const emptyFilePromise = makeEmptyFile(path.join(linkPath, name));
promises.push(emptyFilePromise);
name = name + '.exe';
}
const linkPromise = link(exe, path.join(linkPath, name));
promises.push(linkPromise);
} catch (e) {
if (e && e.message && e.message.includes('is only supported on')) {
// create an error script.
const file = `#!/usr/bin/env node
console.error("${e.message}");
process.exit(1);
`;
const writePromise = writeFile(path.join(linkPath, name), file);
promises.push(writePromise);
} else {
throw e;
}
}
}
return Promise.all(promises);
}
/* Anybody wants to make nice package to decompress zip and tar? thanks */
const isTar = archive.includes('tar.gz');
const unzip = isTar ?
(input, output) => tar.x({ file: input, cwd: output, strip: 1 })
:(input, output) => decompress(input, output, { strip: 1 })
const url = archiveToUrl(archive);
init()
.then(() => mkdirp(outPath))
.then(() => mkdirp(linkPath))
.then(() => rimraf(path.join(outPath, '*')))
.then(() => fetchBuffer(url))
.then(b => writeFile(tempPath, b, 'binary'))
.then(() => unzip(tempPath, outPath))
.then(() => rimraf(tempPath))
.then(makeLinks)
.then(() => console.log('done!'))
.catch(e => {
console.error(e);
process.exit(1);
});