forked from horihiro/node-fitbit-livedata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postinstall.js
77 lines (69 loc) · 1.7 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
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node
if (process.platform.toLowerCase() !== 'linux') process.exit(0);
const fs = require('fs');
const TARGETS = [
'./node_modules/bleno/lib/hci-socket/bindings.js',
'../bleno/lib/hci-socket/bindings.js',
];
const findTarget = (cands) => {
let ret = '';
const res = cands.some((cand) => {
ret = cand;
return fs.existsSync(cand);
});
if (res) return ret;
return '';
};
const TARGET = findTarget(TARGETS);
if (!TARGET) process.exit(1);
const readFileAsync = path => new Promise((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data.toString());
});
});
const renameFileAsync = (oldpath, newpath) => new Promise((resolve, reject) => {
fs.rename(oldpath, newpath, (err) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
const writeFileAsync = (path, content) => new Promise((resolve, reject) => {
fs.writeFile(path, content, 'utf8', (err) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
renameFileAsync(TARGET, `${TARGET}.orig`)
.then(() => readFileAsync(`${TARGET}.orig`))
.then((content) => {
const lines = content.split(/\n/);
const heads = [];
const tails = [];
while(true) {
heads.push(lines.shift());
if (lines.length === 81) break;
}
lines.reverse();
while(true) {
tails.push(lines.shift());
if (lines.length === 4) break;
}
tails.reverse();
lines.reverse();
return heads.concat(lines.map(line => `// ${line}`)).concat(tails).join('\n');
})
.then(content => writeFileAsync(TARGET, content))
.catch((err) => {
console.error(err);
process.exit(0);
});