-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
114 lines (108 loc) · 4.33 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
103
104
105
106
107
108
109
110
111
112
113
114
// @ts-check
const xbw = require('node-gyp-build')(__dirname);
const { lstatSync, readdirSync, existsSync } = require('fs');
const { join } = require('path');
/** @param {string[]} args */
const getIsoFilesFromArgs = (args = []) => {
const files = [];
args
.filter(arg => !arg.startsWith('-'))
.forEach(arg => {
if (existsSync(arg) && lstatSync(arg).isDirectory()) {
files.push(...readdirSync(arg).reduce((files, itemName) => {
const itemPath = join(arg, itemName);
if (lstatSync(itemPath).isDirectory()) {
return [...files, ...getIsoFilesFromArgs([itemPath])];
}
return /** @type {string[]} */(itemName.endsWith('.iso') ? [...files, itemPath] : files);
}, /** @type {string[]} */([])));
} else {
files.push(arg);
}
});
return files;
};
/** @type {import('./index').verifyWithAbgx360} */
const verifyWithAbgx360 = (args = [], options, onProgress) => {
if (Array.isArray(args)) {
const isoPaths = args.filter(arg => !arg.startsWith('-'));
const files = getIsoFilesFromArgs(isoPaths);
if (isoPaths.length && !files.length) {
throw new Error('Error: xbw.verifyWithAbgx360: no ISO files were found');
}
let abgxOptions = ['--help'];
if (files.length) {
abgxOptions = args.filter(arg => arg.startsWith('-'));
if (options) {
/**
* TypeScript is not smart enough to figure out what filter() does.
* So we need to cast the result as being string[] and not (string | undefined)[].
*/
const items = /** @type {string[]} */(Object
.keys(options)
.map(key => {
if (typeof options[key] === 'string') {
return `--${key} ${options[key]}`;
} else if (typeof options[key] === 'number') {
return `--${key} ${String(options[key])}`;
} else if (options[key] === false) {
return undefined; // no-op
}
return `--${key}`;
})
.filter(option => option)); // filter out the no-op
abgxOptions.push(...items);
}
}
return new Promise((resolve, reject) => {
xbw.verifyWithAbgx360(
abgxOptions.concat(files),
(/** @type {any} */ error, /** @type {any[]} */ results, /** @type {string} */ progress) => {
if (progress && onProgress) {
if (progress.includes('<loader>')) {
progress = progress.replace(' ', ' ');
}
onProgress(progress);
}
if (error) {
return reject(error);
}
if (results) {
return resolve(files.map((file, index) => {
return { file, status: results?.[index] ?? -1 };
}));
}
}
);
});
} else {
throw new Error('Error: xbw.verifyWithAbgx360: `args` parameter should be of type Array');
}
};
/** @type {import('./index').getIsosInfo} */
const getIsosInfo = (isoPaths = []) => {
if (Array.isArray(isoPaths)) {
const files = getIsoFilesFromArgs(isoPaths);
if (files.length) {
return files.map(file => {
try {
return {
...xbw.getIsoInfo(file),
isValid: true
};
} catch (error) {
console.error(`Error: xbw.getIsosInfo: ${error.message}\n for file: ${file}`);
return { file, isValid: false };
}
});
} else {
throw new Error('Error: xbw.getIsosInfo: no ISO files were found');
}
} else {
throw new Error('Error: xbw.getIsosInfo: `isoPaths` parameter should be of type Array');
}
};
module.exports = {
verifyWithAbgx360,
getIsosInfo
};