generated from parzh/package-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-grouped-by-data-files.ts
221 lines (176 loc) · 5.31 KB
/
build-grouped-by-data-files.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import fs from "fs";
import path from "path";
import packageJson from "./package.json";
import type { JSXml, Primitive } from "./js-xml.type";
import json from "./data.json";
import { writeJsonToFile } from "./write-to-file";
/** @private */
const envVarKeyPrefix = "ISO4217_JSON_BUILD_DATA_GROUPED_BY_";
/** @private */
const grouping = {
COUNTRY_NAME: {
tagName: "CtryNm",
fileName: "data-grouped-by-country-name.json",
},
CURRENCY_NAME: {
tagName: "CcyNm",
fileName: "data-grouped-by-currency-name.json",
},
CURRENCY_CODE: {
tagName: "Ccy",
fileName: "data-grouped-by-currency-code.json",
},
CURRENCY_NUMBER: {
tagName: "CcyNbr",
fileName: "data-grouped-by-currency-number.json",
},
CURRENCY_MINOR_UNITS: {
tagName: "CcyMnrUnts",
fileName: "data-grouped-by-currency-minor-units.json",
},
} as const;
/** @private */
type Grouping = typeof grouping;
/** @private */
type EnvVarKeyPostfix = keyof Grouping;
/** @private */
type EnvVarKey = `${typeof envVarKeyPrefix}${EnvVarKeyPostfix}`;
declare global {
namespace NodeJS {
interface ProcessEnv extends Partial<Record<EnvVarKey, string>> {}
}
}
/** @private */
const enum BuildStrategy {
Never = "never",
Always = "always",
IfNotExists = "if-not-exists",
IfExists = "if-exists",
}
/** @private */
const aliasExactToBuildStrategyMap = {
never: BuildStrategy.Never,
always: BuildStrategy.Always,
"if-not-exists": BuildStrategy.IfNotExists,
"if-exists": BuildStrategy.IfExists,
} as const;
/** @private */
const aliasBooleanToBuildStrategyMap = {
false: BuildStrategy.Never,
true: BuildStrategy.Always,
} as const;
/** @private */
const aliasNumberToBuildStrategyMap = {
0: BuildStrategy.Never,
1: BuildStrategy.Always,
2: BuildStrategy.IfNotExists,
3: BuildStrategy.IfExists,
} as const;
/** @private */
const aliasToBuildStrategyMap = {
...aliasExactToBuildStrategyMap,
...aliasBooleanToBuildStrategyMap,
...aliasNumberToBuildStrategyMap,
hard: BuildStrategy.Always,
soft: BuildStrategy.IfNotExists,
"unless-exists": BuildStrategy.IfNotExists,
} as const;
/** @private */
type BuildStrategyAlias = keyof typeof aliasToBuildStrategyMap;
/** @private */
function toBuildStrategyAlias(value: unknown): BuildStrategyAlias {
if (typeof value === "string" && value && value in aliasToBuildStrategyMap) {
return value as BuildStrategyAlias
}
const booleanAlias = String(!!value) as `${boolean}`
return booleanAlias
}
/** @private */
function getBuildStrategy(envVarValue: string | undefined): BuildStrategy {
const alias = toBuildStrategyAlias(envVarValue)
const strategy = aliasToBuildStrategyMap[alias]
return strategy
}
/** @private */
type Entry = JSXml<JSXml[]>;
/** @private */
type EntryDataItem = JSXml<Primitive>;
/** @private */
const entries: Entry[] = json.$data[0].$data;
/** @private */
function getEntryDataItem(entry: Entry, itemName: Grouping[EnvVarKeyPostfix]["tagName"]): EntryDataItem | null {
for (const item of entry.$data) {
if (item.$name === itemName) {
return item as EntryDataItem;
}
}
return null;
}
/** @private */
function getEntriesGroupedBy(envVarKeyPostfix: EnvVarKeyPostfix): Record<string, Entry[]> {
const { tagName } = grouping[envVarKeyPostfix];
const grouped = Object.create(null) as Record<string, Entry[]>;
for (const entry of entries) {
const dataItem = getEntryDataItem(entry, tagName);
if (dataItem == null) {
continue;
}
const groupName = String(dataItem.$data);
grouped[groupName] ??= [];
grouped[groupName].push(entry);
}
return grouped;
}
/** @private */
const logPrefix = `${packageJson.name}:`;
/** @private */
function log(level: "info" | "error", ...values: unknown[]) : void {
console[level](logPrefix, ...values);
}
/** @private */
async function writeGroupedDataToFile(
dataGrouped: Record<string, Entry[]>,
filePathAbsolute: string,
filePathRelative: string,
) {
try {
await writeJsonToFile(filePathAbsolute, dataGrouped);
log("info", `File "${filePathRelative}" is built successfully`);
} catch (error) {
log("error", error);
log("error", `An error encountered while building "${filePathRelative}" (see above)`);
}
}
export default async function buildGroupedByDataFiles() {
const jobs: Promise<void>[] = [];
for (const key in grouping) {
const envVarKeyPostfix = key as EnvVarKeyPostfix;
const { fileName } = grouping[envVarKeyPostfix];
const envVarKey = `${envVarKeyPrefix}${envVarKeyPostfix}` as const;
const envVarValue = process.env[envVarKey];
const strategy = getBuildStrategy(envVarValue);
if (strategy === BuildStrategy.Never)
continue;
const filePathAbsolute = path.resolve(__dirname, fileName);
const filePathRelative = path.relative(process.cwd(), filePathAbsolute);
if (strategy !== BuildStrategy.Always) {
const isIfExists = strategy === BuildStrategy.IfExists;
const fileExists = fs.existsSync(filePathAbsolute);
if (isIfExists !== fileExists) {
log("info", `Skipping file "${filePathRelative}" (strategy "${strategy}")`);
continue;
}
}
log("info", `Building file "${filePathRelative}" ...`);
const dataGrouped = getEntriesGroupedBy(envVarKeyPostfix);
const job = writeGroupedDataToFile(dataGrouped, filePathAbsolute, filePathRelative);
jobs.push(job);
}
if (jobs.length === 0)
log("info", "No groupings to generate");
else
await Promise.all(jobs);
}
if (require.main === module) {
buildGroupedByDataFiles();
}