-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
196 lines (166 loc) · 4.82 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env node
// Source copied from Prettier's CLI! (MIT License)
// https://github.com/prettier/prettier/blob/master/bin/prettier.js
"use strict";
const USAGE = `
Usage: parindent [opts] [filename ...]
A minimal indenter for Lisp code (e.g. Clojure)
Available options:
--write Edit the file in-place. (Beware!)
--list-different or -l Print filenames of files that are different from Parindent formatting.
--stdin Read input from stdin.
--version or -v Print Parindent version.
`;
const fs = require("fs");
const getStdin = require("get-stdin");
const glob = require("glob");
const chalk = require("chalk");
const minimist = require("minimist");
const readline = require("readline");
const { fixIndent } = require("./main.js");
const pkg = require("./package.json");
const version = pkg.version;
const argv = minimist(process.argv.slice(2), {
boolean: ["write", "stdin", "help", "version", "list-different"],
alias: { help: "h", version: "v", "list-different": "l" },
unknown: param => {
if (param.startsWith("-")) {
console.warn("Ignored unknown option: " + param + "\n");
return false;
}
}
});
if (argv["version"]) {
console.log("Parindent " + version);
process.exit(0);
}
const filepatterns = argv["_"];
const write = argv["write"];
const stdin = argv["stdin"] || (!filepatterns.length && !process.stdin.isTTY);
const globOptions = {
dot: true
};
function getLineEnding(text) {
// NOTE: We assume that if the CR char "\r" is used anywhere,
// then we should use CRLF line-endings after every line.
var i = text.search("\r");
if (i !== -1) {
return "\r\n";
}
return "\n";
}
function format(input) {
const result = fixIndent(input);
if (!result.success) {
throw result.error;
}
const output = result.text;
return output;
}
function handleError(filename, e) {
console.error(
filename + ":" + (e.lineNo + 1) + ":" + e.x + " - " + e.message
);
// Don't exit the process if one file failed
process.exitCode = 2;
}
if (argv["help"] || (!filepatterns.length && !stdin)) {
console.log(USAGE);
process.exit(argv["help"] ? 0 : 1);
}
if (stdin) {
getStdin().then(input => {
try {
writeOutput(format(input));
} catch (e) {
handleError("stdin", e);
return;
}
});
} else {
eachFilename(filepatterns, filename => {
if (write) {
// Don't use `console.log` here since we need to replace this line.
process.stdout.write(filename);
}
let input;
try {
input = fs.readFileSync(filename, "utf8");
} catch (e) {
// Add newline to split errors from filename line.
process.stdout.write("\n");
console.error("Unable to read file: " + filename + "\n" + e);
// Don't exit the process if one file failed
process.exitCode = 2;
return;
}
const start = Date.now();
let output;
try {
output = format(input);
} catch (e) {
// Add newline to split errors from filename line.
process.stdout.write("\n");
handleError(filename, e);
return;
}
if (argv["list-different"]) {
if (output !== input) {
if (!write) {
console.log(filename);
}
process.exitCode = 1;
}
}
if (write) {
// Remove previously printed filename to log it with duration.
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
// Don't write the file if it won't change in order not to invalidate
// mtime based caches.
if (output === input) {
if (!argv["list-different"]) {
console.log(chalk.grey("%s %dms"), filename, Date.now() - start);
}
} else {
if (argv["list-different"]) {
console.log(filename);
} else {
console.log("%s %dms", filename, Date.now() - start);
}
try {
fs.writeFileSync(filename, output, "utf8");
} catch (err) {
console.error("Unable to write file: " + filename + "\n" + err);
// Don't exit the process if one file failed
process.exitCode = 2;
}
}
} else if (!argv["list-different"]) {
writeOutput(output);
}
});
}
function writeOutput(text) {
// Don't use `console.log` here since it adds an extra newline at the end.
process.stdout.write(text);
}
function eachFilename(patterns, callback) {
patterns.forEach(pattern => {
if (!glob.hasMagic(pattern)) {
callback(pattern);
return;
}
glob(pattern, globOptions, (err, filenames) => {
if (err) {
console.error("Unable to expand glob pattern: " + pattern + "\n" + err);
// Don't exit the process if one pattern failed
process.exitCode = 2;
return;
}
filenames.forEach(filename => {
callback(filename);
});
});
});
}