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 src/scripts/**.ts --format cjs,esm --clean --dts",
28
  "prepare": "pnpm run build",
29
  "check": "tsc",
30
- "inference-codegen": "pnpm run build && node dist/scripts/inference-codegen.js"
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 * as ts from "typescript";
7
 
8
  const TYPESCRIPT_HEADER_FILE = `
9
  /**
@@ -15,16 +15,17 @@ const TYPESCRIPT_HEADER_FILE = `
15
  `;
16
 
17
  const rootDirFinder = function (): string {
18
- const parts = __dirname.split("/");
19
- let level = parts.length - 1;
20
- while (level > 0) {
21
- const currentPath = parts.slice(0, level).join("/");
22
- if (pathExists(`${currentPath}/package.json`)) {
23
- return path.normalize(currentPath);
24
  }
25
- level--;
 
26
  }
27
- return "";
 
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
- async function main() {
143
- const rootDir = rootDirFinder();
144
- const tasksDir = path.join(rootDir, "src", "tasks");
145
- const allTasks = await Promise.all(
146
- (await fs.readdir(tasksDir, { withFileTypes: true }))
147
- .filter((entry) => entry.isDirectory())
148
- .filter((entry) => entry.name !== "placeholder")
149
- .map(async (entry) => ({ task: entry.name, dirPath: path.join(entry.path, entry.name) }))
150
- );
151
- const allSpecFiles = [
152
- path.join(tasksDir, "common-definitions.json"),
153
- ...allTasks
154
- .flatMap(({ dirPath }) => [path.join(dirPath, "spec", "input.json"), path.join(dirPath, "spec", "output.json")])
155
- .filter((filepath) => pathExists(filepath)),
156
- ];
157
-
158
- for (const { task, dirPath } of allTasks) {
159
- const taskSpecDir = path.join(dirPath, "spec");
160
- if (!(pathExists(path.join(taskSpecDir, "input.json")) && pathExists(path.join(taskSpecDir, "output.json")))) {
161
- console.debug(`No spec found for task ${task} - skipping`);
162
- continue;
163
- }
164
- console.debug(`✨ Generating types for task`, task);
165
-
166
- console.debug(" πŸ“¦ Building input data");
167
- const inputData = await buildInputData(task, taskSpecDir, allSpecFiles);
168
-
169
- console.debug(" 🏭 Generating typescript code");
170
- {
171
- const { lines } = await generateTypescript(inputData);
172
- await fs.writeFile(`${dirPath}/inference.ts`, [TYPESCRIPT_HEADER_FILE, ...lines].join(`\n`), {
173
- flag: "w+",
174
- encoding: "utf-8",
175
- });
176
- }
177
 
178
- const outputSpec = JSON.parse(await fs.readFile(`${taskSpecDir}/output.json`, { encoding: "utf-8" }));
179
 
180
- console.log(" 🩹 Post-processing the generated code");
181
- await postProcessOutput(`${dirPath}/inference.ts`, outputSpec);
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": "CommonJS",
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
  }