diff --git a/eslint.config.js b/eslint.config.js index e9ef5784..cbb37785 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -31,6 +31,7 @@ export default [ { files: [ "scripts/**", + "tools/**", "packages/migrate-config/src/migrate-config-cli.js", ], rules: { diff --git a/packages/compat/package.json b/packages/compat/package.json index 1ed44d78..3a38791b 100644 --- a/packages/compat/package.json +++ b/packages/compat/package.json @@ -25,7 +25,7 @@ "test": "tests" }, "scripts": { - "build:cts": "node -e \"fs.copyFileSync('dist/esm/index.d.ts', 'dist/cjs/index.d.cts')\"", + "build:cts": "node ../../tools/build-cts.js dist/esm/index.d.ts dist/cjs/index.d.cts", "build": "rollup -c && tsc -p tsconfig.esm.json && npm run build:cts", "test:jsr": "npx jsr@latest publish --dry-run", "test": "mocha tests/*.js", diff --git a/packages/config-array/package.json b/packages/config-array/package.json index 943a8646..5ddeb9bd 100644 --- a/packages/config-array/package.json +++ b/packages/config-array/package.json @@ -32,7 +32,7 @@ "homepage": "https://github.com/eslint/rewrite#readme", "scripts": { "build:dedupe-types": "node ../../tools/dedupe-types.js dist/cjs/index.cjs dist/esm/index.js", - "build:cts": "node -e \"fs.copyFileSync('dist/esm/index.d.ts', 'dist/cjs/index.d.cts')\"", + "build:cts": "node ../../tools/build-cts.js dist/esm/index.d.ts dist/cjs/index.d.cts", "build:std__path": "rollup -c rollup.std__path-config.js && node fix-std__path-imports", "build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts && npm run build:std__path", "test:jsr": "npx jsr@latest publish --dry-run", diff --git a/packages/object-schema/package.json b/packages/object-schema/package.json index 5c16feef..e8d58b3b 100644 --- a/packages/object-schema/package.json +++ b/packages/object-schema/package.json @@ -25,7 +25,7 @@ "test": "tests" }, "scripts": { - "build:cts": "node -e \"fs.copyFileSync('dist/esm/index.d.ts', 'dist/cjs/index.d.cts')\"", + "build:cts": "node ../../tools/build-cts.js dist/esm/index.d.ts dist/cjs/index.d.cts", "build": "rollup -c && tsc -p tsconfig.esm.json && npm run build:cts", "test:jsr": "npx jsr@latest publish --dry-run", "test": "mocha tests/", diff --git a/packages/plugin-kit/build-cts.js b/packages/plugin-kit/build-cts.js deleted file mode 100644 index ade7ae13..00000000 --- a/packages/plugin-kit/build-cts.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * @fileoverview Rewrites import expressions for CommonJS compatibility. - * This script creates "dist/cjs/index.d.cts" from "dist/esm/index.d.ts" by modifying imports - * from `"./types.ts"` to `"./types.cts"`. - * - * @author Francesco Trotta - */ - -import { readFile, writeFile } from "node:fs/promises"; - -const oldSourceText = await readFile("dist/esm/index.d.ts", "utf-8"); -const newSourceText = oldSourceText.replaceAll( - 'import("./types.ts")', - 'import("./types.cts")', -); -await writeFile("dist/cjs/index.d.cts", newSourceText); diff --git a/packages/plugin-kit/package.json b/packages/plugin-kit/package.json index d641ab5f..4c71e5e1 100644 --- a/packages/plugin-kit/package.json +++ b/packages/plugin-kit/package.json @@ -32,7 +32,7 @@ "homepage": "https://github.com/eslint/rewrite#readme", "scripts": { "build:dedupe-types": "node ../../tools/dedupe-types.js dist/cjs/index.cjs dist/esm/index.js", - "build:cts": "node ./build-cts.js", + "build:cts": "node ../../tools/build-cts.js dist/esm/index.d.ts dist/cjs/index.d.cts", "build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts", "pretest": "npm run build", "test": "mocha tests/", diff --git a/tools/build-cts.js b/tools/build-cts.js new file mode 100644 index 00000000..ee635094 --- /dev/null +++ b/tools/build-cts.js @@ -0,0 +1,32 @@ +/** + * @fileoverview Rewrites import expressions for CommonJS compatibility. + * This script creates "dist/cjs/index.d.cts" from "dist/esm/index.d.ts" by modifying imports + * from `"./types.ts"` to `"./types.cts"`. + * + * node tools/build-cts.js /path/to/esm/index.d.ts path/to/cjs/index.d.cts + * + * @author Francesco Trotta + */ + +import { readFile, writeFile } from "node:fs/promises"; + +const filename = process.argv[2]; +const newFilename = process.argv[3]; + +if (!filename) { + console.error("No filename provided."); + process.exit(1); +} + +if (!newFilename) { + console.error("No new filename provided."); + process.exit(1); +} + +const oldSourceText = await readFile(filename, "utf-8"); +const newSourceText = oldSourceText.replaceAll( + 'import("./types.ts")', + 'import("./types.cts")', +); + +await writeFile(newFilename, newSourceText);