-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathconfig.ts
More file actions
122 lines (110 loc) · 3.48 KB
/
Copy pathconfig.ts
File metadata and controls
122 lines (110 loc) · 3.48 KB
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
import { parseJSON, parseJSON5, parseJSONC, parseYAML, parseTOML, parseINI, stringifyJSON, stringifyJSON5, stringifyJSONC, stringifyYAML, stringifyTOML, stringifyINI } from "confbox";
import CommonFormats, { Category } from "src/CommonFormats.ts";
import { FormatDefinition, type FileData, type FileFormat, type FormatHandler } from "../FormatHandler.ts";
const JSON5_FORMAT = new FormatDefinition(
"JSON5",
"json5",
"json5",
"application/json5",
Category.DATA
);
const JSONC_FORMAT = new FormatDefinition(
"JSON Comments",
"jsonc",
"jsonc",
"application/jsonc",
Category.DATA
);
const TOML_FORMAT = new FormatDefinition(
"Tom's Obvious, Minimal Language",
"toml",
"toml",
"application/toml",
Category.DATA
);
const INI_FORMAT = new FormatDefinition(
"Initialization file",
"ini",
"ini",
"text/plain",
Category.DATA
);
class configHandler implements FormatHandler {
public name: string = "config";
public ready: boolean = true;
public supportedFormats: FileFormat[] = [
// JSON maintains exact data equivalence to JS Objects natively
CommonFormats.JSON.builder("json").allowFrom().allowTo().markLossless(true),
// JSON5, YAML, and TOML have comments and other features lost when parsed to JS Objects
JSON5_FORMAT.builder("json5").allowFrom().allowTo().markLossless(false),
JSONC_FORMAT.builder("jsonc").allowFrom().allowTo().markLossless(false),
CommonFormats.YML.builder("yaml").allowFrom().allowTo().markLossless(false),
CommonFormats.YML.builder("yaml").withExt("yaml").allowFrom().allowTo().markLossless(false),
TOML_FORMAT.builder("toml").allowFrom().allowTo().markLossless(false),
INI_FORMAT.builder("ini").allowFrom().allowTo().markLossless(false),
];
async init() {
this.ready = true;
}
async doConvert(
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat,
): Promise<FileData[]> {
return inputFiles.map(file => {
const baseName = file.name.split(".").slice(0, -1).join(".");
const text = new TextDecoder().decode(file.bytes);
let object: any;
switch (inputFormat.internal) {
case "json":
object = parseJSON(text);
break;
case "json5":
object = parseJSON5(text);
break;
case "jsonc":
object = parseJSONC(text);
break;
case "yaml":
object = parseYAML(text);
break;
case "toml":
object = parseTOML(text);
break;
case "ini":
object = parseINI(text);
break;
default:
throw new TypeError(`Unsupported input internal format: ${inputFormat.internal}`);
}
let outText = "";
switch (outputFormat.internal) {
case "json":
outText = stringifyJSON(object);
break;
case "json5":
outText = stringifyJSON5(object);
break;
case "jsonc":
outText = stringifyJSONC(object);
break;
case "yaml":
outText = stringifyYAML(object);
break;
case "toml":
outText = stringifyTOML(object);
break;
case "ini":
outText = stringifyINI(object);
break;
default:
throw new TypeError(`Unsupported output internal format: ${outputFormat.internal}`);
}
return {
name: `${baseName}.${outputFormat.extension}`,
bytes: new TextEncoder().encode(outText),
};
});
}
}
export default configHandler;