-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconvertToAST.js
More file actions
44 lines (35 loc) · 1.23 KB
/
convertToAST.js
File metadata and controls
44 lines (35 loc) · 1.23 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
import fs from 'fs';
import path from 'path';
import { Parser } from '@accordproject/concerto-cto';
const BASE_DIR = path.resolve('semantic/specifications');
function getAllCTOFiles(dir) {
let files = [];
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
files = files.concat(getAllCTOFiles(fullPath));
} else if (entry.isFile() && fullPath.endsWith('.cto')) {
files.push(fullPath);
}
}
return files;
}
function convertAndWriteAST(ctoPath) {
try {
const modelContent = fs.readFileSync(ctoPath, 'utf8');
const ast = Parser.parse(modelContent, ctoPath);
const outFileName = path.basename(ctoPath).replace('.cto', '.json');
const outDir = path.dirname(ctoPath); // same directory as .cto
const outFilePath = path.join(outDir, outFileName);
fs.writeFileSync(outFilePath, JSON.stringify(ast, null, 2), 'utf8');
console.log(`✅ Converted: ${ctoPath} → ${outFilePath}`);
} catch (error) {
console.error(`❌ Failed to convert: ${ctoPath}`);
console.error(error.message);
}
}
function run() {
const allCTOs = getAllCTOFiles(BASE_DIR);
allCTOs.forEach(convertAndWriteAST);
}
run();