-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap.js
83 lines (74 loc) · 2.25 KB
/
bootstrap.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
const path = require('path');
const fs = require('fs-extra');
const { PATHS, IS_PACKAGED } = require('./consts.js');
console.log(IS_PACKAGED ? 'Packaged version' : 'Dev version');
function initialize() {
if (IS_PACKAGED) {
if (process.argv.includes('--cleanup')) {
// Clean up our temporary data. This can only be done when processlist.node
// was not required yet. Hence - right at the beginning.
let retries = 0;
const deleteTemporaryData = () => {
try {
fs.removeSync(PATHS.APPDATA);
process.exit();
} catch (e) {
retries++;
if (retries > 20) {
// ~10 seconds
fs.writeFileSync(
path.join(
path.dirname(process.execPath),
'spotify-ad-blocker_error.log'
),
`Could not clean up ${PATHS.APPDATA}, please delete it manually.`
);
process.exit(1);
} else {
setTimeout(deleteTemporaryData, 500);
}
}
};
deleteTemporaryData();
return false;
}
/**
* Native dependencies have to be extracted to the file system so that they can be spawned.
* Since pkg doesn't allow for inclusion of .node files (their philosophy is "deliver them with the .exe"),
* they have to be renamed to .foolkpkg before packaging.
*
* @param {Array<string>} deps file paths relative to __dirname of this file
*/
const extractNativeDeps = (deps) => {
deps.forEach((dep) => {
fs.writeFileSync(
path.join(PATHS.APPDATA, path.basename(dep)),
fs.readFileSync(
path.join(
__dirname,
IS_PACKAGED ? dep.replace('.node', '.foolpkg') : dep
) // pkg
// path.join(__dirname, dep) // nexe
)
);
});
};
// Needs to be done right at the the beginning so that we can put
// native addons where we want them and make pkg look there
// (by setting cwd to PATHS.APPDATA), as they need to be available
// for require() calls that follow.
fs.ensureDirSync(PATHS.APPDATA);
process.chdir(PATHS.APPDATA);
extractNativeDeps([
'../assets/spotify-ad-blocker.ico',
'../build/Release/spotify-ad-blocker_detection.exe',
'../build/Release/tray.node',
'../build/Release/volumectrl.node',
'../node_modules/process-list/build/Release/processlist.node',
]);
}
return true;
}
module.exports = {
initialize,
};