Skip to content

Commit 2a03a8b

Browse files
committed
feat: update exports and replace update-index script with update-exports
1 parent 92bcf05 commit 2a03a8b

3 files changed

Lines changed: 84 additions & 33 deletions

File tree

package.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@
2626
],
2727
"exports": {
2828
".": "./dist/index.js",
29+
"./v10": "./dist/v10.js",
30+
"./v11": "./dist/v11.js",
31+
"./v12": "./dist/v12.js",
32+
"./v12.1": "./dist/v12.1.js",
33+
"./v13": "./dist/v13.js",
34+
"./v14": "./dist/v14.js",
35+
"./v15": "./dist/v15.js",
36+
"./v15.1": "./dist/v15.1.js",
37+
"./v16": "./dist/v16.js",
38+
"./v4.1": "./dist/v4.1.js",
39+
"./v5": "./dist/v5.js",
40+
"./v5.1": "./dist/v5.1.js",
41+
"./v5.2": "./dist/v5.2.js",
42+
"./v6": "./dist/v6.js",
43+
"./v6.1": "./dist/v6.1.js",
44+
"./v6.2": "./dist/v6.2.js",
45+
"./v6.3": "./dist/v6.3.js",
46+
"./v7": "./dist/v7.js",
47+
"./v8": "./dist/v8.js",
48+
"./v9": "./dist/v9.js",
2949
"./package.json": "./package.json"
3050
},
3151
"main": "./dist/index.js",
@@ -45,7 +65,7 @@
4565
"typecheck": "tsc --noEmit",
4666
"gen:data-files": "ucd generate --output-dir=./data-files all",
4767
"gen:fields": "ucd codegen fields ./data-files --bundle \"./src/v{version}.ts\"",
48-
"update-index": "tsx ./scripts/update-index.ts"
68+
"update-exports": "tsx ./scripts/update-exports.ts"
4969
},
5070
"dependencies": {
5171
"farver": "^0.4.2",

scripts/update-exports.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { exec } from "node:child_process";
2+
import { readdir, readFile, writeFile } from "node:fs/promises";
3+
import process from "node:process";
4+
import { promisify } from "node:util";
5+
6+
const execAsync = promisify(exec);
7+
8+
async function run() {
9+
console.log("updating index.ts...");
10+
let files = await readdir("./src");
11+
files = files.filter((file) => file.endsWith(".ts") && file !== "index.ts");
12+
13+
const content = `// This file is auto-generated by scripts/update-index.ts
14+
// Do not edit this file directly.
15+
16+
${files.map((file) => `export * as ${file.replace(".ts", "").replace(".", "_")} from './${file.replace(".ts", "")}';`).join("\n")}
17+
`;
18+
19+
// update index.ts
20+
await writeFile("./src/index.ts", content, {
21+
encoding: "utf-8",
22+
});
23+
24+
console.log("index.ts updated successfully!");
25+
26+
console.log("updating exports...");
27+
// read pkg.json
28+
const pkg = JSON.parse(await readFile("./package.json", "utf-8"));
29+
30+
const exports = pkg.exports;
31+
const newExports = {};
32+
33+
const exportEntries = Object.entries(exports);
34+
35+
for (const file of files) {
36+
const name = file.replace(".ts", "");
37+
exportEntries.push([`./${name}`, `./dist/${name}.js`]);
38+
}
39+
40+
exportEntries.sort(([keyA], [keyB]) => {
41+
if (keyA === ".") return -1; // "." always first
42+
if (keyB === ".") return 1;
43+
if (keyA === "./package.json") return 1; // "./package.json" always last
44+
if (keyB === "./package.json") return -1;
45+
return keyA.localeCompare(keyB); // alphabetical sort for the rest
46+
});
47+
48+
for (const [key, value] of exportEntries) {
49+
newExports[key] = value;
50+
}
51+
52+
await writeFile("./package.json", JSON.stringify({ ...pkg, exports: newExports }, null, 2), {
53+
encoding: "utf-8",
54+
});
55+
console.log("exports updated successfully!");
56+
57+
await execAsync("npx eslint ./src/index.ts ./package.json --fix");
58+
}
59+
60+
run().catch((error) => {
61+
console.error(error);
62+
process.exit(1);
63+
});

scripts/update-index.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)