Files
galaxy/client/navigation_to_schema.mjs
T
John Chilton 9b889db565 Allow typescript unit testing with navigation YAML.
Selenium tests, JS unit tests, and the tour all speak this language. This schema generation allows TypeScript to understand it as well.

This PR migrates UserDeletion.test.js to verify this works and demonstrate how to use this component in typescript.
2023-03-11 10:28:35 -05:00

55 lines
2.0 KiB
JavaScript

import fs from "fs";
import YAML from "yaml";
const inputFilePath = "src/utils/navigation/navigation.yml";
const file = fs.readFileSync(inputFilePath, "utf8");
const rootComponent = YAML.parse(file);
function generateName(prefix, componentName) {
return `${prefix}${componentName}`;
}
let tsFile =
'import type { Component, SelectorTemplate } from "./index";\nimport { ROOT_COMPONENT as raw_root_component } from "./index";\n';
function writeComponentInterface(prefix, componentName, definition) {
const interfaceName = generateName(prefix, componentName);
let tsInterface = "";
if (typeof definition != "string") {
for (const [key, value] of Object.entries(definition)) {
if (key == "selectors") {
for (const [selectorName] of Object.entries(value)) {
tsInterface += `${selectorName}: SelectorTemplate;\n`;
}
} else if (key == "labels" || key == "text") {
// not implemented in ES client yet
} else {
const childInterfaceName = writeComponentInterface(interfaceName, key, value);
tsInterface += `${key}: ${childInterfaceName};\n`;
}
}
}
const empty = tsInterface == "";
let schemaDefinition = "";
if (!empty) {
schemaDefinition += `interface ${interfaceName} extends Component {\n`;
schemaDefinition += tsInterface;
schemaDefinition += `}\n`;
} else {
schemaDefinition += `type ${interfaceName} = Component;\n`;
}
tsFile += schemaDefinition;
return interfaceName;
}
let tsInterface = "export interface root_component {\n";
for (const [key, value] of Object.entries(rootComponent)) {
const interfaceName = writeComponentInterface("Root", key, value);
tsInterface += `${key}: ${interfaceName};\n`;
}
tsInterface += "}\n";
tsFile += tsInterface;
tsFile += "export const ROOT_COMPONENT = raw_root_component as unknown as root_component;";
const outputFilePath = "src/utils/navigation/schema.ts";
fs.writeFileSync(outputFilePath, tsFile);