-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.ts
105 lines (93 loc) · 2.93 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { promises as fs } from 'node:fs';
import {
DefaultLogger,
Logger,
ManualLifecycleEventEmitter,
registerCoreProfile,
validateNodeRegistry,
writeNodeSpecsToJSON
} from '@behave-graph/core';
import { DummyScene, registerSceneProfile } from '@behave-graph/scene';
import { program } from 'commander';
import { stringify } from 'csv-stringify';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const packageInfo = require('../package.json');
const { name, version } = packageInfo as { name: string; version: string };
export const main = async () => {
// Logger.onVerbose.clear();
program
.name(name)
.version(version)
.argument('<filename>', 'path to the output node-spec json export')
.option('-c, --csv', `output in CSV mode`);
program.parse(process.argv);
const programOptions = program.opts();
const outputPath = program.args[0];
if (outputPath === undefined) {
throw new Error('no path specified');
}
const registry = registerSceneProfile(
registerCoreProfile({
values: {},
nodes: {},
dependencies: {
ILogger: new DefaultLogger(),
ILifecycleEventEmitter: new ManualLifecycleEventEmitter(),
IScene: new DummyScene()
}
})
);
console.log({ registry });
const errorList: string[] = [];
errorList.push(...validateNodeRegistry(registry));
if (errorList.length > 0) {
Logger.error(`${errorList.length} errors found:`);
errorList.forEach((errorText, errorIndex) => {
Logger.error(`${errorIndex}: ${errorText}`);
});
return;
}
const nodeSpecJson = writeNodeSpecsToJSON(registry);
nodeSpecJson.sort((a, b) => a.type.localeCompare(b.type));
const jsonOutput = JSON.stringify(nodeSpecJson, null, ' ');
if (programOptions.csv) {
const csvRows: string[][] = [];
nodeSpecJson.forEach((nodeSpec) => {
const csvRow: string[] = [];
csvRow.push(nodeSpec.type, nodeSpec.category);
for (let i = 0; i < 5; i++) {
if (i < nodeSpec.inputs.length) {
const input = nodeSpec.inputs[i];
csvRow.push(input.name, input.valueType);
if (input.valueType === 'flow') {
csvRow.push('');
} else if (input.defaultValue !== undefined) {
csvRow.push(input.defaultValue.toString());
} else {
csvRow.push('', '(undefined)');
}
} else {
csvRow.push('', '', '');
}
}
for (let i = 0; i < 5; i++) {
if (i < nodeSpec.outputs.length) {
csvRow.push(nodeSpec.outputs[i].name, nodeSpec.outputs[i].valueType);
} else {
csvRow.push('', '');
}
}
csvRows.push(csvRow);
});
stringify(csvRows, async (_err, csvOutput) => {
await fs.writeFile(outputPath, csvOutput, {
encoding: 'utf-8'
});
});
} else {
await fs.writeFile(outputPath, jsonOutput, {
encoding: 'utf-8'
});
}
};