forked from wilix-team/iohook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
137 lines (127 loc) · 3.47 KB
/
build.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
const upload = require('prebuild/upload');
const spawn = require('child_process').spawn;
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const tar = require('tar-stream');
const zlib = require('zlib');
const pkg = require('./package.json');
function mode(octal) {
return parseInt(octal, 8)
}
let arch = process.env.ARCH
? process.env.ARCH
.replace('i686', 'ia32')
.replace('x86_64', 'x64')
: process.arch;
let cmakeJsPath = path.join(
__dirname,
'node_modules',
'.bin',
process.platform === 'win32' ? 'cmake-js.cmd' : 'cmake-js'
);
let support = require('./support');
let targets = support.targets;
let abis = support.abis;
let files = [];
let chain = Promise.resolve();
targets.forEach(parts => {
let runtime = parts[0];
let version = parts[1];
chain = chain
.then(function () {
return build(runtime, version)
})
.then(function () {
return tarGz(runtime, version)
})
.catch(err => {
console.error(err);
process.exit(1);
})
});
chain = chain.then(function () {
return uploadFiles(files)
});
function build(runtime, version) {
return new Promise(function (resolve, reject) {
let args = [
'rebuild',
'--runtime-version=' + version,
'--target_arch=' + arch,
'--runtime=' + runtime
];
let proc = spawn(cmakeJsPath, args, {
env: process.env
});
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
proc.on('exit', function (code, sig) {
if (code === 1) {
return reject(new Error('Failed to build...'))
}
resolve()
})
})
}
function tarGz(runtime, version) {
return new Promise(function (resolve) {
let filename = 'build/Release/iohook.node';
let abi = abis[runtime][version];
let tarPath = 'prebuilds/' + pkg.name + '-v' + pkg.version + '-' + runtime + '-v' + abi + '-' + process.platform + '-' + arch + '.tar.gz';
files.push(tarPath);
mkdirp(path.dirname(tarPath), function () {
fs.stat(filename, function (err, st) {
if (err) {
return reject(err);
}
let tarStream = tar.pack();
let ws = fs.createWriteStream(tarPath);
let stream = tarStream.entry({
name: filename.replace(/\\/g, '/').replace(/:/g, '_'),
size: st.size,
mode: st.mode | mode('444') | mode('222'),
gid: st.gid,
uid: st.uid
});
fs.createReadStream(filename)
.pipe(stream)
.on('finish', function () {
tarStream.finalize()
});
tarStream
.pipe(zlib.createGzip())
.pipe(ws)
.on('close', resolve)
})
})
})
}
function uploadFiles (files) {
return new Promise(function (resolve, reject) {
console.log('Uploading ' + files.length + ' prebuilds(s) to Github releases');
let opts = {
pkg: pkg,
files: files,
upload: process.env.GITHUB_ACCESS_TOKEN
};
upload(opts, function (err, result) {
if (err) {
return reject(err);
}
console.log('Found ' + result.old.length + ' prebuild(s) on Github');
if (result.old.length) {
result.old.forEach(function (build) {
console.log('-> ' + build)
})
}
console.log('Uploaded ' + result.new.length + ' new prebuild(s) to Github');
if (result.new.length) {
result.new.forEach(function (build) {
console.log('-> ' + build)
})
}
resolve()
})
})
}