ervu-eks/frontend/save.ts.metadata.js

102 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-11-21 10:29:18 +03:00
#!/usr/bin/env node
var tsp = require("typescript-parser");
var fs = require('fs');
var path = require('path');
var ts = require("typescript");
var parser = new tsp.TypescriptParser();
var excludedDirs = [
2025-10-17 16:56:48 +03:00
path.resolve(__dirname, "modules", "generated")
2024-11-21 10:29:18 +03:00
];
var walkFileTree = function (dir, action) {
if (typeof action !== "function") {
return;
}
fs.readdirSync(dir).forEach(function (file) {
2025-10-17 16:56:48 +03:00
var filePath = dir + path.sep + file;
var stat = fs.statSync(filePath);
2024-11-21 10:29:18 +03:00
var extension = ".ts";
2025-10-17 16:56:48 +03:00
if (stat && stat.isDirectory() && excludedDirs.indexOf(filePath) === -1) {
walkFileTree(filePath, action);
2024-11-21 10:29:18 +03:00
}
2025-10-17 16:56:48 +03:00
else if (filePath.indexOf(extension, filePath.length - extension.length) !== -1) {
action(null, filePath);
2024-11-21 10:29:18 +03:00
}
});
};
var dateInLong = Date.now();
var arr = [];
2025-10-17 16:56:48 +03:00
function parseTsMeta(file,childModule) {
2024-11-21 10:29:18 +03:00
var content = fs.readFileSync(file).toString();
var jsonStructure = parser.parseTypescript(ts.createSourceFile(
2025-10-17 16:56:48 +03:00
file,
content,
ts.ScriptTarget.Latest,
true,
ts.ScriptKind.TS
2024-11-21 10:29:18 +03:00
),
'/');
2025-10-17 16:56:48 +03:00
if (childModule) {
let moduleRelativePath = path.relative(path.resolve(__dirname, "modules"), file);
let pathElements = moduleRelativePath.split(path.sep);
jsonStructure['moduleName'] = pathElements.shift();
pathElements.shift(); //remove src folder
pathElements.shift(); //remove lib folder
jsonStructure['packageName'] = pathElements.join(path.sep);
}else {
jsonStructure['packageName'] = path.relative(path.resolve(__dirname, "src", "ts"), jsonStructure['filePath']);
}
2024-11-21 10:29:18 +03:00
jsonStructure['imports'].forEach( function (val) {
2025-10-17 16:56:48 +03:00
2024-11-21 10:29:18 +03:00
if (val.libraryName.startsWith(".")) {
val['libraryName'] = path.resolve(path.dirname(jsonStructure['filePath']), val['libraryName']);
2025-10-17 16:56:48 +03:00
if (childModule) {
// let subPath = "modules" + path.sep + jsonStructure['moduleName'] + path.sep + "src" + path.sep + "lib";
val['libraryName'] = path.relative(path.resolve(__dirname, "modules", jsonStructure['moduleName'], "src", "lib"), val['libraryName']);
}
else {
val['libraryName'] = path.relative(path.resolve(__dirname, "src", "ts"), val['libraryName']);
}
2024-11-21 10:29:18 +03:00
val['libraryName'] = path.dirname(val['libraryName']).split(path.sep).join(".");
}
});
delete jsonStructure['filePath'];
jsonStructure['packageName'] = path.dirname(jsonStructure['packageName']).split(path.sep).join( ".");
arr.push(jsonStructure);
2025-10-17 16:56:48 +03:00
}
var basePath = path.resolve(__dirname, "src", "ts");
walkFileTree(basePath, function (err, file) {
parseTsMeta(file, false);
});
walkFileTree(path.resolve(__dirname, "modules"), function (err, file) {
parseTsMeta(file,true);
2024-11-21 10:29:18 +03:00
});
var cache = [];
fs.writeFileSync("./../.studio/typescript.metadata.json",
JSON.stringify(arr, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
}));
cache = null;
2025-10-17 16:56:48 +03:00
console.log("typescript parse time = " + (Date.now() - dateInLong));