From 5079af2e77174632d5ca723c7c6cf9f61add8f3a Mon Sep 17 00:00:00 2001 From: Simon Boudrias Date: Sat, 20 May 2023 12:07:20 -0400 Subject: [PATCH] fix #1234 Properly compiled .d.mts files in packages so it work with TS node16 target. --- .prettierignore | 1 + packages/checkbox/package.json | 4 ++-- packages/confirm/package.json | 4 ++-- packages/core/package.json | 4 ++-- packages/editor/package.json | 4 ++-- packages/expand/package.json | 4 ++-- packages/input/package.json | 4 ++-- packages/password/package.json | 4 ++-- packages/prompts/package.json | 4 ++-- packages/rawlist/package.json | 4 ++-- packages/select/package.json | 4 ++-- packages/testing/package.json | 4 ++-- packages/type/package.json | 4 ++-- tools/rename-ext.mts | 36 +++++++++++++++++++++++++++++----- tools/setup-packages.mts | 2 +- 15 files changed, 57 insertions(+), 30 deletions(-) diff --git a/.prettierignore b/.prettierignore index 7a03adc46..3d3745c82 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,3 @@ yarn.lock .prettierignore +coverage diff --git a/packages/checkbox/package.json b/packages/checkbox/package.json index d0b105ecd..7c1a616e3 100644 --- a/packages/checkbox/package.json +++ b/packages/checkbox/package.json @@ -6,7 +6,7 @@ }, "description": "Inquirer checkbox prompt", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -80,7 +80,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/packages/confirm/package.json b/packages/confirm/package.json index f4461ab3a..fedfec302 100644 --- a/packages/confirm/package.json +++ b/packages/confirm/package.json @@ -3,7 +3,7 @@ "version": "1.0.9", "description": "Inquirer confirm prompt", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -78,7 +78,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/packages/core/package.json b/packages/core/package.json index 1d658fa5c..daa2becdb 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -6,7 +6,7 @@ }, "description": "Core Inquirer prompt API", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -89,7 +89,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/packages/editor/package.json b/packages/editor/package.json index b53f336ff..19efd2e74 100644 --- a/packages/editor/package.json +++ b/packages/editor/package.json @@ -3,7 +3,7 @@ "version": "1.0.9", "description": "Inquirer multiline editor prompt", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -79,7 +79,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/packages/expand/package.json b/packages/expand/package.json index 3bc9b565b..252747ea7 100644 --- a/packages/expand/package.json +++ b/packages/expand/package.json @@ -3,7 +3,7 @@ "version": "1.0.9", "description": "Inquirer checkbox prompt", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -79,7 +79,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/packages/input/package.json b/packages/input/package.json index e88bbe2b2..da9716c5f 100644 --- a/packages/input/package.json +++ b/packages/input/package.json @@ -3,7 +3,7 @@ "version": "1.1.0", "description": "Inquirer input text prompt", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -81,7 +81,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/packages/password/package.json b/packages/password/package.json index 6d57ff190..a2efccb5d 100644 --- a/packages/password/package.json +++ b/packages/password/package.json @@ -6,7 +6,7 @@ }, "description": "Inquirer password prompt", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -78,7 +78,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/packages/prompts/package.json b/packages/prompts/package.json index 5ace6438b..7b5e75209 100644 --- a/packages/prompts/package.json +++ b/packages/prompts/package.json @@ -3,7 +3,7 @@ "version": "1.2.1", "description": "Inquirer prompts, combined in a single package", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -71,7 +71,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/packages/rawlist/package.json b/packages/rawlist/package.json index 19ec5e0fe..58acecec7 100644 --- a/packages/rawlist/package.json +++ b/packages/rawlist/package.json @@ -3,7 +3,7 @@ "version": "1.1.1", "description": "Inquirer rawlist prompt", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -78,7 +78,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/packages/select/package.json b/packages/select/package.json index 7a1beb764..277f4af6b 100644 --- a/packages/select/package.json +++ b/packages/select/package.json @@ -3,7 +3,7 @@ "version": "1.1.5", "description": "Inquirer select/list prompt", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -80,7 +80,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/packages/testing/package.json b/packages/testing/package.json index f33b96b04..4b7be18fa 100644 --- a/packages/testing/package.json +++ b/packages/testing/package.json @@ -6,7 +6,7 @@ }, "description": "Inquirer testing utilities", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -78,7 +78,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/packages/type/package.json b/packages/type/package.json index f5333cb0e..8640f038a 100644 --- a/packages/type/package.json +++ b/packages/type/package.json @@ -3,7 +3,7 @@ "version": "1.0.3", "description": "Inquirer core TS types", "main": "./dist/cjs/index.js", - "typings": "./dist/cjs/types/index.d.mts", + "typings": "./dist/cjs/types/index.d.cts", "files": [ "dist/**/*" ], @@ -71,7 +71,7 @@ "default": "./dist/esm/index.mjs" }, "require": { - "types": "./dist/cjs/types/index.d.mts", + "types": "./dist/cjs/types/index.d.cts", "default": "./dist/cjs/index.js" } } diff --git a/tools/rename-ext.mts b/tools/rename-ext.mts index 5f731162a..41221ebb6 100644 --- a/tools/rename-ext.mts +++ b/tools/rename-ext.mts @@ -3,13 +3,17 @@ import fs from 'node:fs/promises'; import { globby } from 'globby'; // Because we're using .mts files, TS compiles to .mjs files disregarding the target. So here we -// manually rename the common.js files to .js -const paths: string[] = await globby(['dist/cjs/**/*.mjs', '!**/node_modules']); - -paths.forEach(async (pathname: string) => { +// manually rename the common.js files and their imports to .js +const mjsFiles: string[] = await globby(['dist/cjs/**/*.mjs', '!**/node_modules']); +mjsFiles.forEach(async (pathname: string) => { + // 1. Rename imports const fileContent = await fs.readFile(pathname, 'utf-8'); - await fs.writeFile(pathname, fileContent.replace(/\.mjs/g, '.js')); + await fs.writeFile( + pathname, + fileContent.replace(/require\(['"]([^'"]*)\.mjs['"]\)/g, "require('$1.js')") + ); + // 2. Rename files const newPath = path.format({ ...path.parse(pathname), base: '', @@ -19,3 +23,25 @@ paths.forEach(async (pathname: string) => { console.log(`Renaming ${pathname} to ${newPath}...`); await fs.rename(pathname, newPath); }); + +// Similarly, we rename the .d.mts files to .d.cts. This is because Typescript `node16` target will +// masquerade as ESM otherwise. +const dmtsFiles: string[] = await globby(['dist/cjs/**/*.d.mts', '!**/node_modules']); +dmtsFiles.forEach(async (pathname: string) => { + // 1. Rename imports + const fileContent = await fs.readFile(pathname, 'utf-8'); + await fs.writeFile( + pathname, + fileContent.replace(/from '([^']*)\.mjs'/g, "from '$1.d.cts'") + ); + + // 2. Rename files + const newPath = path.format({ + ...path.parse(pathname), + base: '', + ext: '.cts', + }); + + console.log(`Renaming ${pathname} to ${newPath}...`); + await fs.rename(pathname, newPath); +}); diff --git a/tools/setup-packages.mts b/tools/setup-packages.mts index 165dbd760..e4dc7b485 100644 --- a/tools/setup-packages.mts +++ b/tools/setup-packages.mts @@ -51,7 +51,7 @@ paths.forEach(async (pkgPath) => { default: './dist/esm/index.mjs', }, require: { - types: './dist/cjs/types/index.d.mts', + types: './dist/cjs/types/index.d.cts', default: './dist/cjs/index.js', }, },