-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONCFormatter.ts
40 lines (37 loc) · 1.52 KB
/
JSONCFormatter.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
import * as JSONCParser from 'jsonc-parser';
import { DefaultFormattingOptions } from './DefaultFormattingOptions.ts';
/**
This class implements the JSONC formatter. I'ts just a convenience wrapper around the [Microsoft JSONC parser](https://github.com/microsoft/node-jsonc-parser).
*/
export class JsoncFormatter
{
/**
Formats the given JSONC text, returning the formatted text. Doesn't modify the input string.
*/
format(jsonc: string): string
{
const editResult = JSONCParser.format(jsonc, undefined, DefaultFormattingOptions);
const editedText = JSONCParser.applyEdits(jsonc, editResult);
return editedText;
}
/**
Formats the given JSONC file, returning the formatted text. Modifies the file if `write` is true **and** the file contents are actually different from the formatted text.
The `changed` parameter is an optional way to track which files have been changed. If the file is actually modified, then `filePath` will be appended to the `changed` array. Management of this array is left to the caller.
*/
formatFileSync(filePath: string, options = { write: true }, changed: string[] = []): string
{
const fileContent = Deno.readFileSync(filePath);
const fileText = new TextDecoder().decode(fileContent);
const formattedText = this.format(fileText);
const isChanged = fileText !== formattedText;
if (isChanged)
{
changed.push(filePath);
if (options.write)
{
Deno.writeTextFileSync(filePath, formattedText);
}
}
return formattedText;
}
}