-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmain.ts
36 lines (31 loc) · 1011 Bytes
/
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
import * as TopLevel from "./TopLevel";
import fs from "fs";
import process from "process";
import * as Schema from "effect/Schema";
const sample = process.argv[2];
const json = fs.readFileSync(sample);
const value = JSON.parse(json.toString());
let schema = TopLevel.TopLevel ?? TopLevel.TopLevelElement;
if (!schema) {
// Sometimes key is prefixed with funPrefixes (e.g. 2df80.json)
Object.keys(TopLevel).some(key => {
if (key.endsWith("TopLevel") || key.endsWith("TopLevelElement")) {
schema = TopLevel[key];
return true;
}
});
}
if (!schema) {
throw new Error("No schema found");
}
let backToJson: string;
if (Array.isArray(value)) {
const parsedValue = value.map(v => {
return Schema.decodeUnknownSync(schema)(v);
});
backToJson = JSON.stringify(parsedValue, null, 2);
} else {
const parsedValue = Schema.decodeUnknownSync(schema)(value);
backToJson = JSON.stringify(parsedValue, null, 2);
}
console.log(backToJson);