-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.ts
More file actions
52 lines (44 loc) · 1.85 KB
/
Copy pathindex.ts
File metadata and controls
52 lines (44 loc) · 1.85 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
import { promises } from "fs";
import { join } from "path";
import { parseStringPromise } from "xml2js";
import { PackageXml } from "./PackageXml";
import { WidgetXml } from "./WidgetXml";
import { generateForWidget } from "./generate";
const { mkdir, readFile, stat, writeFile } = promises;
export async function transformPackage(content: string, basePath: string) {
const contentXml = (await parseStringPromise(content)) as PackageXml;
if (!contentXml) {
throw new Error("Empty XML, please check your src folder for file package.xml");
}
const resultBasePath = join(basePath, "../typings/");
try {
await stat(resultBasePath);
} catch {
await mkdir(resultBasePath);
}
const widgetFileXmls = contentXml.package.clientModule[0].widgetFiles
.map(wf => wf.widgetFile)
.reduce((a, e) => a.concat(e), [])
.filter(wfXml => wfXml.$.path);
for (const widgetFileXml of widgetFileXmls) {
const sourcePath = widgetFileXml.$.path;
const source = await readFile(join(basePath, sourcePath), "utf-8");
let generatedContent;
try {
const sourceXml = (await parseStringPromise(source)) as WidgetXml;
generatedContent = generateForWidget(sourceXml, toWidgetName(sourcePath));
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
throw new Error(
`Incorrect widget xml file ${sourcePath}, please check Mendix Documentation: ${message}`
);
}
const resultPath = sourcePath.replace(/(\.xml)?$/, "Props.d.ts");
await writeFile(join(resultBasePath, resultPath), generatedContent);
}
}
function toWidgetName(file: string) {
file = file.replace(".xml", "");
const parts = file.split("/");
return parts.length > 0 ? parts[parts.length - 1] : "";
}