-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
74 lines (64 loc) · 1.59 KB
/
main.ts
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
import { assertExists } from '@std/assert/exists';
import { parseArgs } from '@std/cli';
import { yellow } from 'jsr:@std/internal@^1.0.5/styles';
import { JsoncFormatter } from './JSONCFormatter.ts';
const usage = `
Usage: deno run mod.ts [options] <glob_pattern>
Options:
--write, -w Write formatted output back to files
--help, -h Show this help message
Examples:
deno run -R mod.ts **/*.jsonc
deno run -RW mod.ts --write **/*.jsonc
`;
export const main = (): void =>
{
const { _, help, write } = parseArgs(Deno.args, {
boolean: ['help', 'write'],
alias: { h: 'help', w: 'write' },
});
if (help || _.length === 0)
{
console.log(usage);
Deno.exit(0);
}
const opts = {
write: write ?? false,
patterns: _ as string[],
};
const changed: string[] = [];
let changedCount = 0;
const tookAction = opts.write ? 'UPDATED' : 'SKIPPED';
for (const path of opts.patterns)
{
assertExists(path);
const formatter = new JsoncFormatter();
// SOMEDAY: add async variant?
const _formatted = formatter.formatFileSync(path, { write: opts.write }, changed);
const wasChanged = changed.length > changedCount;
changedCount = changed.length;
if (wasChanged)
{
console.log(yellow(`${tookAction}: ${path}`));
}
else
{
console.log(`CONFIRMED: ${path}`);
}
}
if (changedCount === 0)
{
console.log('No changes needed.');
}
else
{
if (opts.write)
{
console.log(`${tookAction} ${changedCount} files.`);
}
else
{
console.log(`${tookAction} ${changedCount} files.`);
}
}
};