-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (84 loc) · 2.74 KB
/
index.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
const fs = require('fs');
const nstool = require('node-gyp-build')(__dirname)
const nodeNSTool = {
error: function(errorMessage) {
return {
error: true,
errorMessage,
};
},
run: function(options, parameters) {
if (typeof options?.showKeys !== 'undefined') {
parameters.push('--showkeys');
}
if (typeof options?.showLayout !== 'undefined') {
parameters.push('--showlayout');
}
if (typeof options?.verbose !== 'undefined') {
parameters.push('--verbose');
}
if (typeof options?.type !== 'undefined') {
parameters.push('--type');
parameters.push(options.type.toLowerCase());
}
// Make sure that the user provided a source file to process.
if (typeof options?.source === 'undefined' || typeof options.source !== 'string') {
return this.error('Provide a source file using the "source" option.');
}
// Make sure that the source file is readable.
try {
fs.accessSync(options.source, fs.constants.R_OK);
} catch {
return this.error(`The source file is not readable. Given: ${options.source}`);
}
const passing = [...parameters, options.source];
try {
const results = JSON.parse(nstool.run(...passing));
results.parameters = passing;
return results;
} catch (error) {
// Convert Napi::Error exceptions.
return this.error(error.message);
}
},
information: function(options) {
const parameters = [
'nstool',
'--json',
'--fstree',
];
return this.run(options, parameters);
},
extract: function(options) {
// Make sure that the user provided a source file to process.
if (typeof options?.outputDirectory === 'undefined') {
return this.error('Provide a full path to an output directory using the "outputDirectory " option.');
}
if (typeof options.outputDirectory !== 'string') {
return this.error('The path of the output directory must be a string.');
}
// Make sure that the output directory is writable.
try {
fs.accessSync(options.outputDirectory, fs.constants.W_OK);
} catch {
return this.error(`The output directory is not writable. Given: ${options.outputDirectory}`);
}
const parameters = [
'nstool',
'--json',
'--fstree',
'--extract',
options.outputDirectory,
];
if (typeof options?.fileName !== 'undefined') {
if (typeof options.fileName !== 'string') {
return this.error('The file name of the file you want to extract must be a string.');
}
// The user wants to extract a single file.
parameters.push('--file');
parameters.push(options.fileName);
}
return this.run(options, parameters);
},
};
module.exports = nodeNSTool;