-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
101 lines (74 loc) · 3.2 KB
/
cli.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
#!/usr/bin/env node
// fwg
// https://github.com/waymondrang/fwg
const { spawn, spawnSync } = require('child_process');
const path = require('path');
const fs = require("fs");
if (process.platform !== "win32") {
console.error('Oops! Sorry, this script only works on Windows');
process.exit(1);
}
var ps_version = spawnSync('$PSVersionTable.PSVersion', [], { shell: 'powershell.exe' });
if (!ps_version.stdout && ps_version.error) {
console.log('Powershell is required to run this script');
process.exit(1);
}
var args = process.argv.slice(2);
var target = args[0];
if (!target) {
console.error('Specify a target directory. Type \"fwg help\" for more info');
process.exit(1);
}
if (target.startsWith("help")) {
console.log(`Usage\n\tfwg TARGET-DIRECTORY [-r -l] FILE-TARGETS...\n\nArguments\n\t${"TARGET-DIRECTORY".padEnd(20)}The directory in which to search for files\n\t${"OPTIONS".padEnd(20)}\n\t${"-r".padStart(10).padEnd(20)}Remove generated rules\n\t${"-l".padStart(10).padEnd(20)}Generate and save log file\n\t${"FILE-TARGETS".padEnd(20)}The file target rules. Default is *.exe\n\nExamples\n\tfwg ~/Downloads -l *.exe *.msi *.msp\n\tfwg ~/Documents -r *Launcher.exe\n`);
process.exit(0);
}
var quotation_index = args[0].indexOf('"');
if (args.length == 1 && quotation_index != -1) {
target = args[0].substring(0, quotation_index);
args.push(...args[0].substring(quotation_index + 1).split(" ").map(e => e.trim()));
}
if (target.slice(-1) == "\"") {
target = target.substring(0, target.length - 1);
}
if (target.slice(-1) != "\\" && target.slice(-1) != "/") {
target += '\\';
}
var create_log = args.slice(1).some(e => /(\B-\bl\b)/gm.test(e.trim()));
var parsed_args = args.slice(1).map(e => e.replace(/(\B-\bl\b)/gm, '').trim()).filter(e => e != "");
// console.log('powershell', ['-c', `\"& \'${path.join(__dirname, 'fwg.ps1')}\' \'${target}\' ${args.slice(1).join(" ")}\"`]);
var start_time = new Date();
var file_path = path.join(__dirname, "/logs/" + "fwg-" + start_time.toISOString().replace(/[:.]/g, "-") + '.log');
function log_to_file(data) {
if (!create_log)
return;
if (!fs.existsSync(__dirname + "/logs/"))
fs.mkdirSync(__dirname + "/logs/");
try {
fs.appendFileSync(file_path, data, function (error) {
if (error) console.error(error);
})
} catch {
console.error("Could not write to log file");
}
}
var ps = spawn('powershell', ['-c', `\"& \'${path.join(__dirname, 'fwg.ps1')}\' \'${target}\' ${parsed_args.join(" ")}\"`], { shell: 'powershell.exe' });
ps.on("spawn", function () {
console.log(`Starting fwg version ${require("./package.json").version}`);
})
ps.stdout.on('data', (data) => {
process.stdout.write(data);
log_to_file("[stdout] " + data);
});
ps.stderr.on('data', (data) => {
process.stderr.write(data);
log_to_file("[stderr] " + data);
});
ps.on("exit", function () {
if (create_log)
console.log(`Created log file at ${file_path}`);
var exit_message = `Process ended in ${((new Date() - start_time) / 1000).toFixed(2)} seconds with exit code ${ps.exitCode}`;
log_to_file("[exit] " + exit_message);
console.log(exit_message);
process.exit(0);
})