-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
executable file
·42 lines (36 loc) · 830 Bytes
/
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
#!/usr/bin/env node
import fs from 'node:fs';
import process from 'node:process';
import meow from 'meow';
import stdin from 'get-stdin';
import detectIndent from 'detect-indent';
const cli = meow(`
Usage
$ detect-indent <file>
echo <string> | detect-indent
Example
$ echo ' foo\\n bar' | detect-indent | wc --chars
2
`, {
importMeta: import.meta,
});
function init(data) {
const {indent} = detectIndent(data);
if (indent === undefined) {
console.error('Indentation could not be detected');
process.exitCode = 2;
} else {
process.stdout.write(indent);
}
}
const [input] = cli.input;
if (!input && process.stdin.isTTY) {
console.error('Specify a filepath');
process.exitCode = 1;
} else if (input) {
init(fs.readFileSync(input, 'utf8'));
} else {
(async () => {
init(await stdin());
})();
}