-
Notifications
You must be signed in to change notification settings - Fork 208
/
SchemaXml.ts
59 lines (51 loc) · 1.98 KB
/
SchemaXml.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Utils
*/
import * as fs from "fs-extra";
import * as path from "path";
import { DOMParser, XMLSerializer } from "@xmldom/xmldom";
import { Schema } from "@itwin/ecschema-metadata";
/**
* Utility class to assist in creating serialized EC Schemas on the file system.
* @beta
*/
export namespace SchemaXml {
/**
* Writes a Schema to an xml file to the specified output path.
* @param schema The Schema to serialize.
* @param outputPath The directory in which to create the file.
*/
export async function writeFile(schema: Schema, outputPath: string) {
const xml = await writeString(schema);
const baseFile = getSchemaPath(schema, outputPath);
try {
await fs.writeFile(baseFile, xml);
} catch (err: any) {
const msg = `An error occurred writing to file '${baseFile}': ${err.message}`;
throw new Error(msg);
}
}
/**
* Writes a Schema to an xml string.
* @param schema The Schema to serialize.
*/
export async function writeString(schema: Schema): Promise<string> {
let xmlDoc = new DOMParser().parseFromString(`<?xml version="1.0" encoding="UTF-8"?>`, "application/xml");
xmlDoc = await schema.toXml(xmlDoc);
const serializer = new XMLSerializer();
return serializer.serializeToString(xmlDoc);
}
function getSchemaPath(schema: Schema, outputPath: string): string {
const realDir = path.normalize(outputPath);
const test = fs.pathExistsSync(realDir);
if (!test) {
const msg = `The output directory '${realDir}' does not exist.`;
throw new Error(msg);
}
return path.resolve(realDir, `${schema.name}.ecschema.xml`);
}
}