-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.ts
109 lines (87 loc) · 2.51 KB
/
parse.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
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
import * as path from "https://deno.land/std@0.204.0/path/mod.ts";
const exists = async (filename: string): Promise<boolean> => {
try {
await Deno.stat(filename);
// successful, file or directory must exist
return true;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// file or directory does not exist
return false;
} else {
// unexpected error, maybe permissions, pass it along
throw error;
}
}
};
export async function parseToCreatorJSON(targetPath?: string) {
const currentDir = path.dirname(path.fromFileUrl(import.meta.url));
const [givenFilePath = targetPath, comment = "Migration file"] = Deno.args;
if (!givenFilePath) {
throw new TypeError("No file path given");
}
const targetFilePath = path.isAbsolute(givenFilePath)
? givenFilePath
: path.resolve(givenFilePath);
const fileName = path.basename(targetFilePath);
const fileContent = await Deno.readTextFile(targetFilePath);
const parsedContent: {
ResourceRecordSets: Array<{
Name: string;
Type: string;
TTL: number;
ResourceRecords: Array<{
Value: string;
}>;
}>;
} = JSON.parse(fileContent);
const outputFileName = "parsed-".concat(fileName);
const outputFileDirPath = path.resolve(currentDir, "parsed");
const outputFilePath = path.join(
outputFileDirPath,
outputFileName,
);
if (!(await exists(outputFileDirPath)))
await Deno.mkdir(outputFileDirPath, { recursive: true });
if (await exists(outputFilePath)) {
await Deno.remove(outputFilePath);
} else {
await Deno.create(outputFilePath);
}
const notesFile = await Deno.open(outputFilePath, {
write: true,
create: true,
truncate: true,
});
const encoder = new TextEncoder();
const output = {
Comment: comment,
Changes: [] as Array<{
Action: string;
ResourceRecordSet: {
Name: string;
Type: string;
TTL: number;
ResourceRecords: Array<{
Value: string;
}>;
};
}>,
};
const excludedTypes = ["NS", "SOA"];
for (const record of parsedContent.ResourceRecordSets) {
if (!excludedTypes.includes(record.Type)) {
output.Changes.push({
Action: "CREATE",
ResourceRecordSet: record,
});
}
}
const notes = new Uint8Array([
...encoder.encode(JSON.stringify(output, null, 2)),
...encoder.encode("\n"),
]);
await notesFile.write(notes);
notesFile.close();
}
if (import.meta.main) await parseToCreatorJSON();