-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnativemodules.js
94 lines (71 loc) · 2.82 KB
/
nativemodules.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
"use strict";
const fs = require("fs");
const path = require("path");
const semver = require("semver");
const CodeVersion = require("./codeversion");
class NativeModules {
constructor(folder) {
this._folder = folder;
}
removeOtherSerialportVersions(platform, arch, keepVersion) {
let folders = this._getAllFolders();
let isFound = false;
folders.forEach(f => {
let cv = Object.assign(new CodeVersion(), JSON.parse(fs.readFileSync(path.join(f, "info.json"), null, "utf8")));
if (cv.platform == platform && cv.arch == arch && semver.neq(keepVersion, cv.serialport)) {
fs.rmdirSync(f, { recursive: true });
isFound = true;
}
});
return isFound;
}
removeOtherModulesVersions(platform, arch, min, max) {
let folders = this._getAllFolders();
let isFound = false;
folders.forEach(f => {
let cv = Object.assign(new CodeVersion(), JSON.parse(fs.readFileSync(path.join(f, "info.json"), null, "utf8")));
if (cv.platform == platform && cv.arch == arch && (cv.modules < min || cv.modules > max)) {
fs.rmdirSync(f, { recursive: true });
isFound = true;
}
});
return isFound;
}
exists(codeVersion) {
let folders = this._getAllFolders();
for(let f of folders) {
let cv = Object.assign(new CodeVersion(), JSON.parse(fs.readFileSync(path.join(f, "info.json"), null, "utf8")));
if (cv.modules == codeVersion.modules && cv.platform == codeVersion.platform && cv.arch == codeVersion.arch && cv.serialport == codeVersion.serialport)
return true;
}
return false;
}
static add(built_folder, modules_folder, codeVersion) {
let file = path.join(built_folder, "bindings.node");
let finalFolder = path.join(modules_folder, `node-v${codeVersion.modules}-${codeVersion.platform}-${codeVersion.arch}`);
let finalFile = path.join(finalFolder, "bindings.node");
let finalInfo = path.join(finalFolder, "info.json");
// Delete an existing target folder
if (fs.existsSync(finalFolder))
fs.rmdirSync(finalFolder, {
recursive: true,
force: true
});
fs.mkdirSync(finalFolder);
fs.copyFileSync(file, finalFile);
fs.writeFileSync(finalInfo, JSON.stringify(codeVersion));
return finalFolder;
}
_getAllFolders() {
let folders = fs.readdirSync(this._folder);
let output = [];
folders.forEach(f => {
let item = path.join(this._folder, f);
if (fs.lstatSync(item).isDirectory()) {
output.push(item);
}
});
return output;
}
}
module.exports = NativeModules;