-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile
127 lines (111 loc) · 3.43 KB
/
compile
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
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const getOpts = require("get-options");
const IconCompiler = require("../lib/icons/icon-compiler.js");
const {options, argv} = getOpts(process.argv.slice(2), {
"-h, -?, --help": "",
"-v, --version": "",
"--verbose": "",
});
if(options.help){
printHelp();
process.exit(0);
}
if(options.version){
const {version} = require("../package.json");
process.stdout.write(`v${version.replace(/^v/i, "")}\n`);
process.exit(0);
}
const unrecognisedOption = argv.find(a => /^-/.test(a));
if(unrecognisedOption){
process.stderr.write(makeSpiffy("Unknown option: ") + unrecognisedOption + "\n");
printHelp(true);
process.exit(1);
}
// Bail out if needed dependencies are missing
if(!fs.existsSync(path.resolve(__dirname, "..", "node_modules", "coffee-script"))){
process.stderr.write("devDependencies are missing. Run `apm install file-icons`, then try again\n");
process.exit(1);
}
// No arguments passed or input piped through; recompile package config automatically
if(!argv[0] && process.stdin.isTTY){
debug("No arguments passed; using sane defaults");
new Promise((resolve, reject) => {
const inputPath = path.resolve(__dirname, "..", "config.cson");
debug(`Reading input from ${inputPath}`);
fs.readFile(inputPath, (error, data) => error
? reject(error)
: resolve(data.toString()));
}).then(data => {
const outputData = IconCompiler.compileConfigData(data, true);
const outputPath = path.resolve(__dirname, "..", "lib", "icons", ".icondb.js");
debug(`Writing output to ${outputPath}`);
return new Promise((resolve, reject) => {
fs.writeFile(outputPath, outputData, error => {
error ? reject(error) : resolve();
});
});
}).catch(error => {
console.error(error);
process.exit(1);
});
}
else new Promise((resolve, reject) => {
if(!process.stdin.isTTY){
debug("Reading config from standard input");
let input = "";
process.stdin.setEncoding("UTF8");
process.stdin.on("readable", () => {
const chunk = process.stdin.read();
null !== chunk ? input += chunk : resolve(input);
});
}
else{
debug(`Reading config from ${argv[0]}`);
fs.readFile(argv[0], (error, data) => error
? reject(error)
: resolve(data.toString()));
}
}).then(data => {
debug("Writing result to standard output");
const output = IconCompiler.compileConfigData(data, true);
process.stdout.write(output);
}).catch(error => {
console.error(error);
process.exit(1);
});
function printHelp(short = false){
if(short){
const usage = "Usage: compile [--help] input.cson > output.js\n";
process.stderr.write(makeSpiffy(usage));
return;
}
const help = `
Usage: compile [-h|-?|--help] [--verbose] <input>
Examples:
compile
compile config.cson
compile < config.cson > output.js`
.replace(/^\n+/, "")
.replace(/\t+/gm, "")
.replace(/^(?=compile)/gm, " ")
+ "\n";
process.stderr.write(makeSpiffy(help));
}
function makeSpiffy(input){
if(!process.stderr.isTTY)
return input;
const bold = "\x1B[1m";
const plain = "\x1B[0m";
const italic = "\x1B[4m";
return input
.replace(/^([^:]+:\s+)/gm, `${bold}$1${plain}`)
.replace(/(\s+|\||\[)(-\?|-{1,2}\w+[-\w]*)(?=[\s|\]]|$)/g, `$1${bold}$2${plain}`)
.replace(/<([-\w]+)>/g, `<${italic}$1${plain}>`)
.replace(/(\s+)(\S+\.(?:cson|js)\b)/g, `$1${italic}$2${plain}`);
}
function debug(...args){
options.verbose && process.stderr.write(args.join(" ") + "\n");
}