Skip to content

Commit 79ae82f

Browse files
authored
👷 Changes to build inference types (#464)
Follow up to #449 Review with whitespaces off
1 parent 9bbf175 commit 79ae82f

File tree

11 files changed

+347
-190
lines changed

11 files changed

+347
-190
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,7 @@ dist
103103
# TernJS port file
104104
.tern-port
105105

106-
.DS_Store
106+
.DS_Store
107+
108+
# Generated by doc-internal
109+
docs

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"prettier": "^3.1.0",
1919
"prettier-plugin-svelte": "^3.1.2",
2020
"semver": "^7.5.0",
21-
"ts-node": "^10.9.1",
21+
"tsx": "^4.7.0",
2222
"tsup": "^6.7.0",
2323
"typescript": "^5.0.4",
2424
"vite": "^5.0.2",

packages/doc-internal/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"doc-hub": "typedoc --tsconfig ../hub/tsconfig.json --githubPages false --plugin typedoc-plugin-markdown --out ../../docs/hub --hideBreadcrumbs --hideInPageTOC --sourceLinkTemplate https://github.com/huggingface/huggingface.js/blob/main/{path}#L{line} ../hub/index.ts",
1414
"doc-inference": "typedoc --tsconfig ../inference/tsconfig.json --githubPages false --plugin typedoc-plugin-markdown --out ../../docs/inference --hideBreadcrumbs --hideInPageTOC --sourceLinkTemplate https://github.com/huggingface/huggingface.js/blob/main/{path}#L{line} ../inference/src/index.ts",
1515
"doc-agents": "typedoc --tsconfig ../agents/tsconfig.json --githubPages false --plugin typedoc-plugin-markdown --out ../../docs/agents --hideBreadcrumbs --hideInPageTOC --sourceLinkTemplate https://github.com/huggingface/huggingface.js/blob/main/{path}#L{line} ../agents/src/index.ts",
16-
"update-toc": "node --experimental-specifier-resolution=node --loader ts-node/esm update-toc.ts",
17-
"fix-cdn-versions": "node --experimental-specifier-resolution=node --loader ts-node/esm fix-cdn-versions.ts",
18-
"fix-md-links": "node --experimental-specifier-resolution=node --loader ts-node/esm fix-md-links.ts",
19-
"fix-md-headinghashlinks": "node --experimental-specifier-resolution=node --loader ts-node/esm fix-md-headinghashlinks.ts"
16+
"update-toc": "tsx update-toc.ts",
17+
"fix-cdn-versions": "tsx fix-cdn-versions.ts",
18+
"fix-md-links": "tsx fix-md-links.ts",
19+
"fix-md-headinghashlinks": "tsx fix-md-headinghashlinks.ts"
2020
},
2121
"type": "module",
2222
"license": "MIT",

packages/inference/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@
3030
],
3131
"source": "src/index.ts",
3232
"types": "./dist/index.d.ts",
33-
"main": "./dist/index.js",
34-
"module": "./dist/index.mjs",
33+
"main": "./dist/index.cjs",
34+
"module": "./dist/index.js",
3535
"exports": {
3636
"types": "./dist/index.d.ts",
37-
"require": "./dist/index.js",
38-
"import": "./dist/index.mjs"
37+
"require": "./dist/index.cjs",
38+
"import": "./dist/index.js"
3939
},
40+
"type": "module",
4041
"scripts": {
4142
"build": "tsup src/index.ts --format cjs,esm --clean && pnpm run dts",
42-
"dts": "ts-node scripts/generate-dts.ts",
43+
"dts": "tsx scripts/generate-dts.ts",
4344
"lint": "eslint --quiet --fix --ext .cjs,.ts .",
4445
"lint:check": "eslint --ext .cjs,.ts .",
4546
"format": "prettier --write .",

packages/inference/scripts/generate-dts.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ function* extractAsyncFunctions(fileContent: string): Iterable<string> {
174174
}
175175
}
176176

177-
writeFileSync("./dist/index.js", '/// <reference path="./index.d.ts" />\n' + readFileSync("./dist/index.js", "utf-8"));
178-
writeFileSync(
179-
"./dist/index.mjs",
180-
'/// <reference path="./index.d.ts" />\n' + readFileSync("./dist/index.mjs", "utf-8")
181-
);
177+
for (const distPath of ["./dist/index.js", "./dist/index.cjs"]) {
178+
writeFileSync(distPath, '/// <reference path="./index.d.ts" />\n' + readFileSync(distPath, "utf-8"));
179+
}

packages/inference/tsconfig.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
"skipLibCheck": true,
1212
"noImplicitOverride": true
1313
},
14-
"ts-node": {
15-
"compilerOptions": {
16-
"module": "commonjs"
17-
}
18-
},
1914
"include": ["src", "test", "index.ts", "../shared/src"],
2015
"exclude": ["dist"]
2116
}

packages/tasks/.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

packages/tasks/package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
"publishConfig": {
88
"access": "public"
99
},
10-
"main": "./dist/index.js",
11-
"module": "./dist/index.mjs",
10+
"main": "./dist/index.cjs",
11+
"module": "./dist/index.js",
1212
"types": "./dist/index.d.ts",
1313
"exports": {
1414
".": {
1515
"types": "./dist/index.d.ts",
16-
"require": "./dist/index.js",
17-
"import": "./dist/index.mjs"
16+
"require": "./dist/index.cjs",
17+
"import": "./dist/index.js"
1818
}
1919
},
2020
"source": "src/index.ts",
@@ -24,11 +24,12 @@
2424
"format": "prettier --write .",
2525
"format:check": "prettier --check .",
2626
"prepublishOnly": "pnpm run build",
27-
"build": "tsup src/index.ts src/scripts/**.ts --format cjs,esm --clean --dts",
27+
"build": "tsup src/index.ts --format cjs,esm --clean --dts && pnpm run inference-codegen",
2828
"prepare": "pnpm run build",
2929
"check": "tsc",
30-
"inference-codegen": "pnpm run build && node dist/scripts/inference-codegen.js"
30+
"inference-codegen": "tsx scripts/inference-codegen.ts && prettier --write src/tasks/*/inference.ts"
3131
},
32+
"type": "module",
3233
"files": [
3334
"dist",
3435
"src",

packages/tasks/src/scripts/inference-codegen.ts renamed to packages/tasks/scripts/inference-codegen.ts

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { SerializedRenderResult } from "quicktype-core";
22
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";
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";
77

88
const TYPESCRIPT_HEADER_FILE = `
99
/**
@@ -15,16 +15,17 @@ const TYPESCRIPT_HEADER_FILE = `
1515
`;
1616

1717
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);
18+
let currentPath = path.normalize(import.meta.url);
19+
20+
while (currentPath !== "/") {
21+
if (pathExists(path.join(currentPath, "package.json"))) {
22+
return currentPath;
2423
}
25-
level--;
24+
25+
currentPath = path.normalize(path.join(currentPath, ".."));
2626
}
27-
return "";
27+
28+
return "/";
2829
};
2930

3031
/**
@@ -53,6 +54,7 @@ async function generateTypescript(inputData: InputData): Promise<SerializedRende
5354
inputData,
5455
lang: "typescript",
5556
alphabetizeProperties: true,
57+
indentation: "\t",
5658
rendererOptions: {
5759
"just-types": true,
5860
"nice-property-names": true,
@@ -139,54 +141,44 @@ async function postProcessOutput(path2generated: string, outputSpec: Record<stri
139141
return;
140142
}
141143

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-
}
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+
}
177178

178-
const outputSpec = JSON.parse(await fs.readFile(`${taskSpecDir}/output.json`, { encoding: "utf-8" }));
179+
const outputSpec = JSON.parse(await fs.readFile(`${taskSpecDir}/output.json`, { encoding: "utf-8" }));
179180

180-
console.log(" 🩹 Post-processing the generated code");
181-
await postProcessOutput(`${dirPath}/inference.ts`, outputSpec);
182-
}
183-
console.debug("✅ All done!");
181+
console.log(" 🩹 Post-processing the generated code");
182+
await postProcessOutput(`${dirPath}/inference.ts`, outputSpec);
184183
}
185-
186-
let exit = 0;
187-
main()
188-
.catch((err) => {
189-
console.error("Failure", err);
190-
exit = 1;
191-
})
192-
.finally(() => process.exit(exit));
184+
console.debug("✅ All done!");

packages/tasks/tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"compilerOptions": {
33
"allowSyntheticDefaultImports": true,
44
"lib": ["ES2022", "DOM"],
5-
"module": "CommonJS",
5+
"module": "ESNext",
6+
"target": "ESNext",
67
"moduleResolution": "node",
7-
"target": "ES2022",
88
"forceConsistentCasingInFileNames": true,
99
"strict": true,
1010
"noImplicitAny": true,
@@ -13,6 +13,6 @@
1313
"noImplicitOverride": true,
1414
"outDir": "./dist"
1515
},
16-
"include": ["src"],
16+
"include": ["src", "scripts"],
1717
"exclude": ["dist"]
1818
}

0 commit comments

Comments
 (0)