Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
machineuser
commited on
Commit
Β·
1e51cd1
1
Parent(s):
afa4e5a
Sync widgets demo
Browse files
packages/tasks/package.json
CHANGED
|
@@ -24,11 +24,12 @@
|
|
| 24 |
"format": "prettier --write .",
|
| 25 |
"format:check": "prettier --check .",
|
| 26 |
"prepublishOnly": "pnpm run build",
|
| 27 |
-
"build": "tsup src/index.ts
|
| 28 |
"prepare": "pnpm run build",
|
| 29 |
"check": "tsc",
|
| 30 |
-
"inference-codegen": "
|
| 31 |
},
|
|
|
|
| 32 |
"files": [
|
| 33 |
"dist",
|
| 34 |
"src",
|
|
|
|
| 24 |
"format": "prettier --write .",
|
| 25 |
"format:check": "prettier --check .",
|
| 26 |
"prepublishOnly": "pnpm run build",
|
| 27 |
+
"build": "tsup src/index.ts --format cjs,esm --clean --dts && pnpm run inference-codegen",
|
| 28 |
"prepare": "pnpm run build",
|
| 29 |
"check": "tsc",
|
| 30 |
+
"inference-codegen": "node --experimental-specifier-resolution=node --loader ts-node/esm scripts/inference-codegen.ts && prettier --write src/tasks/*/inference.ts"
|
| 31 |
},
|
| 32 |
+
"type": "module",
|
| 33 |
"files": [
|
| 34 |
"dist",
|
| 35 |
"src",
|
packages/tasks/{src/scripts β scripts}/inference-codegen.ts
RENAMED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import type { SerializedRenderResult } from "quicktype-core";
|
| 2 |
import { quicktype, InputData, JSONSchemaInput, FetchingJSONSchemaStore } from "quicktype-core";
|
| 3 |
-
import * as fs from "fs/promises";
|
| 4 |
-
import { existsSync as pathExists } from "fs";
|
| 5 |
-
import * as path from "path";
|
| 6 |
-
import
|
| 7 |
|
| 8 |
const TYPESCRIPT_HEADER_FILE = `
|
| 9 |
/**
|
|
@@ -15,16 +15,17 @@ const TYPESCRIPT_HEADER_FILE = `
|
|
| 15 |
`;
|
| 16 |
|
| 17 |
const rootDirFinder = function (): string {
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
while (
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return path.normalize(currentPath);
|
| 24 |
}
|
| 25 |
-
|
|
|
|
| 26 |
}
|
| 27 |
-
|
|
|
|
| 28 |
};
|
| 29 |
|
| 30 |
/**
|
|
@@ -53,6 +54,7 @@ async function generateTypescript(inputData: InputData): Promise<SerializedRende
|
|
| 53 |
inputData,
|
| 54 |
lang: "typescript",
|
| 55 |
alphabetizeProperties: true,
|
|
|
|
| 56 |
rendererOptions: {
|
| 57 |
"just-types": true,
|
| 58 |
"nice-property-names": true,
|
|
@@ -139,54 +141,44 @@ async function postProcessOutput(path2generated: string, outputSpec: Record<stri
|
|
| 139 |
return;
|
| 140 |
}
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
(
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
{
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
}
|
| 177 |
|
| 178 |
-
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
}
|
| 183 |
-
console.debug("β
All done!");
|
| 184 |
}
|
| 185 |
-
|
| 186 |
-
let exit = 0;
|
| 187 |
-
main()
|
| 188 |
-
.catch((err) => {
|
| 189 |
-
console.error("Failure", err);
|
| 190 |
-
exit = 1;
|
| 191 |
-
})
|
| 192 |
-
.finally(() => process.exit(exit));
|
|
|
|
| 1 |
import type { SerializedRenderResult } from "quicktype-core";
|
| 2 |
import { quicktype, InputData, JSONSchemaInput, FetchingJSONSchemaStore } from "quicktype-core";
|
| 3 |
+
import * as fs from "node:fs/promises";
|
| 4 |
+
import { existsSync as pathExists } from "node:fs";
|
| 5 |
+
import * as path from "node:path/posix";
|
| 6 |
+
import ts from "typescript";
|
| 7 |
|
| 8 |
const TYPESCRIPT_HEADER_FILE = `
|
| 9 |
/**
|
|
|
|
| 15 |
`;
|
| 16 |
|
| 17 |
const rootDirFinder = function (): string {
|
| 18 |
+
let currentPath = path.normalize(import.meta.url);
|
| 19 |
+
|
| 20 |
+
while (currentPath !== "/") {
|
| 21 |
+
if (pathExists(path.join(currentPath, "package.json"))) {
|
| 22 |
+
return currentPath;
|
|
|
|
| 23 |
}
|
| 24 |
+
|
| 25 |
+
currentPath = path.normalize(path.join(currentPath, ".."));
|
| 26 |
}
|
| 27 |
+
|
| 28 |
+
return "/";
|
| 29 |
};
|
| 30 |
|
| 31 |
/**
|
|
|
|
| 54 |
inputData,
|
| 55 |
lang: "typescript",
|
| 56 |
alphabetizeProperties: true,
|
| 57 |
+
indentation: "\t",
|
| 58 |
rendererOptions: {
|
| 59 |
"just-types": true,
|
| 60 |
"nice-property-names": true,
|
|
|
|
| 141 |
return;
|
| 142 |
}
|
| 143 |
|
| 144 |
+
const rootDir = rootDirFinder();
|
| 145 |
+
const tasksDir = path.join(rootDir, "src", "tasks");
|
| 146 |
+
const allTasks = await Promise.all(
|
| 147 |
+
(await fs.readdir(tasksDir, { withFileTypes: true }))
|
| 148 |
+
.filter((entry) => entry.isDirectory())
|
| 149 |
+
.filter((entry) => entry.name !== "placeholder")
|
| 150 |
+
.map(async (entry) => ({ task: entry.name, dirPath: path.join(entry.path, entry.name) }))
|
| 151 |
+
);
|
| 152 |
+
const allSpecFiles = [
|
| 153 |
+
path.join(tasksDir, "common-definitions.json"),
|
| 154 |
+
...allTasks
|
| 155 |
+
.flatMap(({ dirPath }) => [path.join(dirPath, "spec", "input.json"), path.join(dirPath, "spec", "output.json")])
|
| 156 |
+
.filter((filepath) => pathExists(filepath)),
|
| 157 |
+
];
|
| 158 |
+
|
| 159 |
+
for (const { task, dirPath } of allTasks) {
|
| 160 |
+
const taskSpecDir = path.join(dirPath, "spec");
|
| 161 |
+
if (!(pathExists(path.join(taskSpecDir, "input.json")) && pathExists(path.join(taskSpecDir, "output.json")))) {
|
| 162 |
+
console.debug(`No spec found for task ${task} - skipping`);
|
| 163 |
+
continue;
|
| 164 |
+
}
|
| 165 |
+
console.debug(`β¨ Generating types for task`, task);
|
| 166 |
+
|
| 167 |
+
console.debug(" π¦ Building input data");
|
| 168 |
+
const inputData = await buildInputData(task, taskSpecDir, allSpecFiles);
|
| 169 |
+
|
| 170 |
+
console.debug(" π Generating typescript code");
|
| 171 |
+
{
|
| 172 |
+
const { lines } = await generateTypescript(inputData);
|
| 173 |
+
await fs.writeFile(`${dirPath}/inference.ts`, [TYPESCRIPT_HEADER_FILE, ...lines].join(`\n`), {
|
| 174 |
+
flag: "w+",
|
| 175 |
+
encoding: "utf-8",
|
| 176 |
+
});
|
| 177 |
+
}
|
|
|
|
| 178 |
|
| 179 |
+
const outputSpec = JSON.parse(await fs.readFile(`${taskSpecDir}/output.json`, { encoding: "utf-8" }));
|
| 180 |
|
| 181 |
+
console.log(" π©Ή Post-processing the generated code");
|
| 182 |
+
await postProcessOutput(`${dirPath}/inference.ts`, outputSpec);
|
|
|
|
|
|
|
| 183 |
}
|
| 184 |
+
console.debug("β
All done!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
packages/tasks/tsconfig.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
"compilerOptions": {
|
| 3 |
"allowSyntheticDefaultImports": true,
|
| 4 |
"lib": ["ES2022", "DOM"],
|
| 5 |
-
"module": "
|
| 6 |
"moduleResolution": "node",
|
| 7 |
"target": "ES2022",
|
| 8 |
"forceConsistentCasingInFileNames": true,
|
|
@@ -13,6 +13,6 @@
|
|
| 13 |
"noImplicitOverride": true,
|
| 14 |
"outDir": "./dist"
|
| 15 |
},
|
| 16 |
-
"include": ["src"],
|
| 17 |
"exclude": ["dist"]
|
| 18 |
}
|
|
|
|
| 2 |
"compilerOptions": {
|
| 3 |
"allowSyntheticDefaultImports": true,
|
| 4 |
"lib": ["ES2022", "DOM"],
|
| 5 |
+
"module": "ESNext",
|
| 6 |
"moduleResolution": "node",
|
| 7 |
"target": "ES2022",
|
| 8 |
"forceConsistentCasingInFileNames": true,
|
|
|
|
| 13 |
"noImplicitOverride": true,
|
| 14 |
"outDir": "./dist"
|
| 15 |
},
|
| 16 |
+
"include": ["src", "scripts"],
|
| 17 |
"exclude": ["dist"]
|
| 18 |
}
|