-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·156 lines (120 loc) · 3.71 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const arg = require('arg');
const { ModuleInfo, removeModuleExtensions, listModuleExtensions } = require('binary-version-reader');
const fs = require('fs').promises;
const package = require('./package.json');
let args = undefined
try {
args = arg({
// Types
'--help': Boolean,
'--verbose': arg.COUNT, // Counts the number of times --verbose is passed
'--input': String, // --input <number> or --input=<number>
'--output': String, // --output <string> or --output=<string>
// Aliases
'-v': '--verbose'
});
//console.log(args);
} catch (err) {
if (err.code === 'ARG_UNKNOWN_OPTION') {
console.log(err.message);
} else {
throw err;
}
}
function showHelp() {
console.log(`
Usage: --input <input file>
--output <output file>
--help Show this help
<command> [options]
Commands:
strip --input blah --output blah
list --input blah
version
`);
}
async function checkInput() {
//if input and output are missing, error. given individual error messages for each
if (args['--input'] == undefined) {
console.error('Input file is required. --input <file>');
showHelp();
//exit process with error -1
process.exit(-1);
}
}
async function checkOutput() {
if (args['--output'] == undefined) {
console.error('Output file is required. --output <file>');
showHelp();
//exit process with error -1
process.exit(-1);
}
}
async function stripAssets(input, output) {
console.log('strip: Stripping assets from binary file')
console.log(`Reading: ${input}`);
const file = await fs.readFile(input);
//print file size
console.log(`File length read: ${file.length}`);
const appWithAssets = Buffer.from(file);
const appWithAssetsRemoved = await removeModuleExtensions({
module: appWithAssets,
exts: [ModuleInfo.ModuleInfoExtension.ASSET_DEPENDENCY]
});
console.log(`Writing: ${output}`);
await fs.writeFile(output, appWithAssetsRemoved);
}
async function listAssets(input) {
console.log('list: Listing assets in binary file')
console.log(`Reading: ${input}`);
const file = await fs.readFile(input);
//print file size
console.log(`File length read: ${file.length}`);
const appWithAssets = Buffer.from(file);
const assets = await listModuleExtensions({
module: appWithAssets,
exts: [ModuleInfo.ModuleInfoExtension.ASSET_DEPENDENCY]
});
//if no assets, print no assets
if (assets.length === 0) {
console.log('No assets found');
}
//loop over assets and print the name and length
for (let i = 0; i < assets.length; i++) {
//print: Name: <name> Length: <length>
console.log(`Asset ${i}: ${assets[i].name} Length: ${assets[i].data.length}`);
}
}
(async () => {
console.log(`${package.name}. Particle Inc, 2024`)
//if help is passed, show help
if (args['--help']) {
showHelp();
return;
}
//check the command is valid and there is only one command
if (args._.length !== 1) {
console.error('Invalid command');
showHelp();
return;
}
const command = args._[0];
//interpret the command
if (command === 'strip') {
//strip assets
await checkInput();
await checkOutput();
await stripAssets(args['--input'], args['--output']);
} else if (command === 'version') {
//print version
console.log(`Version: ${package.version}`)
} else if (command === 'list') {
//list assets
await checkInput();
await listAssets(args['--input']);
} else {
console.error('Invalid command');
showHelp();
return;
}
})();