Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions converters/BUILD
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@rules_player//javascript:defs.bzl", "js_pipeline")
load("//helpers:defs.bzl", "tsup_config", "vitest_config")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_library")

npm_link_all_packages(name = "node_modules")

tsup_config(name = "tsup_config")

vitest_config(name = "vitest_config")

js_library(
name = "entrypoint",
srcs = glob(["bin/*"]),
deps = [],
)

js_pipeline(
package_name = "@xlr-lib/xlr-converters",
peer_deps = [
Expand All @@ -23,4 +30,10 @@ js_pipeline(
":node_modules/@xlr-lib/xlr",
":node_modules/@xlr-lib/xlr-utils",
],
include_packaging_targets = [
":entrypoint"
],
create_package_json_args = {
"custom_entrypoints": True
}
)
8 changes: 8 additions & 0 deletions converters/bin/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env node
const converter = require("../dist/index.mjs")
const input = process.argv[2]
const output = process.argv[3]
const primitives = process.argv[4].split(',')
console.log(primitives)
console.log(typeof primitives)
converter.converter(input, output, primitives)
3 changes: 3 additions & 0 deletions converters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
},
"devDependencies": {
"@xlr-lib/test-utils": "workspace:*"
},
"bin": {
"xlr": "bin/run"
}
}
2 changes: 1 addition & 1 deletion converters/src/__tests__/common-to-ts.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, describe, expect, vi } from "vitest";
import type { NamedType } from "@xlr-lib/xlr";
import ts from "typescript";
import { TSWriter } from "../xlr-to-ts";
import { TSWriter } from "../ts/xlr-to-ts";

describe("Type Exports", () => {
vi.setConfig({
Expand Down
44 changes: 44 additions & 0 deletions converters/src/helpers/converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Manifest } from "@xlr-lib/xlr";
import ts from "typescript";
import path from "path";
import fs from "fs";
import { TsConverter } from "../ts/ts-to-xlr";
import { writeManifest } from "./writeManifest";

/** Basic Exporter to export a self contained type file */
export function converter(
sourceFile: string,
outputDirectory: string,
customPrimitives: string[],
tsOptions: ts.CompilerOptions = {},
): Manifest | undefined {
const program = ts.createProgram([sourceFile], tsOptions);
fs.mkdirSync(outputDirectory, { recursive: true });

// Get the checker, we will use it to find more about classes
const checker = program.getTypeChecker();

const converter = new TsConverter(checker, customPrimitives);
const convertedTypes = converter.convertSourceFile(
program.getSourceFiles()[0],
);

if (convertedTypes.data.types.length === 0) {
return undefined;
}

convertedTypes.data.types.forEach((type) => {
fs.writeFileSync(
path.join(outputDirectory, `${type.name}.json`),
JSON.stringify(type, undefined, 4),
);
});

const manifest: Manifest = {
pluginName: "Types",
capabilities: new Map([["Types", convertedTypes.convertedTypes]]),
customPrimitives: customPrimitives,
};

writeManifest(manifest, outputDirectory);
}
45 changes: 45 additions & 0 deletions converters/src/helpers/writeManifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Manifest } from "@xlr-lib/xlr";
import path from "path";
import fs from "fs";

/** Serializes ES6 Maps */
function replacer(key: any, value: any) {
if (value instanceof Map) {
return Object.fromEntries(value.entries());
}

return value;
}

export function writeManifest(
capabilities: Manifest,
outputDirectory: string,
): void {
// print out the manifest files
const jsonManifest = JSON.stringify(capabilities, replacer, 4);
fs.writeFileSync(path.join(outputDirectory, "manifest.json"), jsonManifest);

const tsManifestFile = `${[...(capabilities.capabilities?.values() ?? [])]
.flat(2)
.map((capability) => {
return `const ${capability} = require("./${capability}.json")`;
})
.join("\n")}

module.exports = {
"pluginName": "${capabilities.pluginName}",
"capabilities": {
${[...(capabilities.capabilities?.entries() ?? [])]
.map(([capabilityName, provides]) => {
return `"${capabilityName}":[${provides.join(",")}],`;
})
.join("\n\t\t")}
},
"customPrimitives": [
${[capabilities.customPrimitives?.map((i) => `"${i}"`).join(",") ?? ""]}
]
}
`;

fs.writeFileSync(path.join(outputDirectory, "manifest.js"), tsManifestFile);
}
10 changes: 6 additions & 4 deletions converters/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export { TsConverter } from "./ts-to-xlr";
export type { TSConverterContext } from "./ts-to-xlr";
export { TSWriter } from "./xlr-to-ts";
export type { ConvertedType } from "./xlr-to-ts";
export { TsConverter } from "./ts/ts-to-xlr";
export type { TSConverterContext } from "./ts/ts-to-xlr";
export { TSWriter } from "./ts/xlr-to-ts";
export type { ConvertedType } from "./ts/xlr-to-ts";
export { converter } from "./helpers/converter";
export { writeManifest } from "./helpers/writeManifest";
export * from "./types";
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
isPrimitiveTypeNode,
isTypeScriptLibType,
} from "@xlr-lib/xlr-utils";
import { ConversionError } from "./types";
import { ConversionError } from "../types";

export type MappedType = "Pick" | "Omit" | "Required" | "Partial" | "Exclude";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
import type { TopLevelDeclaration } from "@xlr-lib/xlr-utils";
import { isGenericNamedType, isPrimitiveTypeNode } from "@xlr-lib/xlr-utils";
import ts from "typescript";
import { ConversionError } from "./types";
import { ConversionError } from "../types";

const templateTokenize = /(?=true\|false|\.\*|\[0-9]\*)/gm;
const tokenSplit = /(?<=true\|false|\.\*|\[0-9]\*)/gm;
Expand Down