-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (80 loc) · 2.61 KB
/
Copy pathindex.js
File metadata and controls
83 lines (80 loc) · 2.61 KB
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
#!/bin/env node
/* AADecode - Decode encoded-as-aaencode JavaScript program.
*
* Copyright (C) 2010,2016 @cat_in_136
*
* This software is released under the MIT License.
* http://opensource.org/licenses/mit-license.php
*/
const fs = require("fs");
const path = require("path");
const minimist = require("minimist");
const AADecode = require("./aadecode.js");
var argv = minimist(process.argv.slice(2), {
"string": ["output"],
"boolean": ["help", "version"],
"alias": {
h: "help",
o: "output",
v: "version"
}
});
if (argv.help) {
process.stdout.write("Usage: aadecode [OPTION]... [FILE]\n");
process.stdout.write(" -o, --output=FILE output to FILE\n");
process.stdout.write(" -h, --help display this help and exit\n");
process.stdout.write(" -v, --version output version information and exit\n");
process.exit(0);
} else if (argv.version) {
const pkg = require("./package.json");
process.stdout.write(pkg.name + " " + pkg.version + "\n");
process.exit(0);
} else {
(function () {
if (argv._.length == 0) {
return new Promise(function (resolve, reject) {
var text = "";
process.stdin.setEncoding("utf8");
process.stdin.on("readable", function () {
text += process.stdin.read() || "";
});
process.stdin.on("end", function () {
resolve(text);
});
process.stdin.on("error", function (error) {
reject(error);
});
});
} else {
return new Promise(function (resolve, reject) {
fs.readFile(path.resolve(argv._[0]), function (error, text) {
if (error) {
reject(error);
} else {
resolve(text.toString());
}
});
});
}
})().then(function (text) {
if (argv.output) {
return new Promise(function (resolve, reject) {
fs.writeFile(argv.output, AADecode.decode(text), function (error) {
if (error) {
reject(error);
} else {
resolve();
}
});
});
} else {
process.stdout.write(AADecode.decode(text));
return Promise.resolve();
}
}).then(function () {
process.exit();
}).catch(function (error) {
console.error(error);
process.exit(1);
});
}