forked from microsoft/fast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-schemas.js
59 lines (52 loc) · 1.75 KB
/
convert-schemas.js
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
/**
* Utility for copying schemas to their dist folders.
* Usage: node build/convert-schemas.js
*/
const path = require("path");
const fs = require("fs");
const glob = require("glob");
const rootDir = path.resolve(process.cwd());
const srcSchemaPaths = "src/**/*.schema.ts";
const destDir = "dist";
let exit = 0;
/**
* Function to convert and copy schema files to their dist folder
*/
function copySchemaFiles() {
const resolvedSrcSchemaPaths = path.resolve(rootDir, srcSchemaPaths);
glob.sync(resolvedSrcSchemaPaths, void 0).forEach(function(filePath) {
let destSchemaPath = filePath.replace(/(\bsrc\b)(?!.*\1)/, destDir);
destSchemaPath =
destSchemaPath.substr(0, destSchemaPath.lastIndexOf(".ts")) + ".json";
fs.copyFileSync(filePath, destSchemaPath);
const fileContents = fs.readFileSync(destSchemaPath).toString();
const commonJS = "module.exports = ".concat(
fileContents.substring(fileContents.indexOf("{"))
);
const tempPath = path.resolve(
path.dirname(filePath),
`.tmp/${path.basename(filePath)}.js`
);
try {
if (!fs.existsSync(path.dirname(tempPath))) {
fs.mkdirSync(path.dirname(tempPath));
}
fs.writeFileSync(tempPath, commonJS);
const schemaData = require(tempPath);
const json = JSON.stringify(schemaData, null, 2);
fs.writeFileSync(destSchemaPath, json);
} catch (e) {
console.error(e);
exit = 1;
} finally {
if (fs.existsSync(tempPath)) {
fs.unlinkSync(tempPath);
}
}
});
process.exit(exit);
}
/**
* Convert and Copy all files
*/
copySchemaFiles();