Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate .mts scripts to .mjs #1280

Merged
merged 4 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@
"integration/*"
],
"scripts": {
"ts-node": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm",
"postinstall": "husky install",
"prepare": "lerna run tsc",
"setup": "yarn ts-node ./tools/setup-packages.mts",
"setup": "node ./tools/setup-packages.mjs",
"pretest": "eslint . && lerna run tsc",
"test": "vitest --run packages && node --test integration"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/checkbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/confirm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/expand/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/input/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/password/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/prompts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"engines": {
"node": ">=14.18.0"
Expand Down
3 changes: 1 addition & 2 deletions packages/rawlist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/select/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"exports": {
".": {
Expand Down
3 changes: 1 addition & 2 deletions packages/type/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
"tsc:esm": "tsc -p ./tsconfig.json",
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"engines": {
"node": ">=14.18.0"
Expand Down
12 changes: 6 additions & 6 deletions tools/fix-ext.mts → tools/fix-ext.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as path from 'node:path';
import * as fs from 'node:fs/promises';
import path from 'node:path';
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 and their imports to .js
const mjsFiles: string[] = await globby(['dist/cjs/**/*.mjs', '!**/node_modules']);
mjsFiles.forEach(async (pathname: string) => {
const mjsFiles = await globby(['dist/cjs/**/*.mjs', '!**/node_modules']);
mjsFiles.forEach(async (pathname) => {
// 1. Rename imports
const fileContent = await fs.readFile(pathname, 'utf-8');
await fs.writeFile(
Expand All @@ -26,8 +26,8 @@ mjsFiles.forEach(async (pathname: string) => {

// Similarly, we rename the .d.mts files to .d.ts. 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) => {
const dmtsFiles = await globby(['dist/cjs/**/*.d.mts', '!**/node_modules']);
dmtsFiles.forEach(async (pathname) => {
// 1. Rename imports from `.mjs` to `.js`
const fileContent = await fs.readFile(pathname, 'utf-8');
await fs.writeFile(
Expand Down
26 changes: 12 additions & 14 deletions tools/setup-packages.mts → tools/setup-packages.mjs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import * as path from 'node:path';
import * as fs from 'node:fs';
import * as url from 'node:url';
import path from 'node:path';
import fs from 'node:fs/promises';
import url from 'node:url';
import { globby } from 'globby';
import prettier from 'prettier';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

function readFile(filepath: string): Promise<string> {
return fs.promises.readFile(filepath, 'utf-8');
function readFile(filepath) {
return fs.readFile(filepath, 'utf-8');
}

function readJSONFile(filepath: string): Promise<any> {
function readJSONFile(filepath) {
return readFile(filepath).then(JSON.parse);
}

function fileExists(filepath: string): Promise<boolean> {
return fs.promises.access(filepath).then(
function fileExists(filepath) {
return fs.access(filepath).then(
() => true,
() => false,
);
}

async function writeFile(filepath: string, content: string): Promise<any> {
async function writeFile(filepath, content) {
if ((await fileExists(filepath)) && (await readFile(filepath)) !== content) {
await fs.promises.writeFile(filepath, content);
await fs.writeFile(filepath, content);
}
}

Expand All @@ -34,7 +34,7 @@ paths.forEach(async (pkgPath) => {
const dir = path.dirname(pkgPath);

const prettierJsonOption = await prettier.resolveConfig('tsconfig.json');
const formatJSON = (content: any) =>
const formatJSON = (content) =>
prettier.format(JSON.stringify(content), { ...prettierJsonOption, parser: 'json' });

// Set multi-module system builds exports
Expand Down Expand Up @@ -80,9 +80,7 @@ paths.forEach(async (pkgPath) => {
tsc: 'yarn run clean && yarn run tsc:esm && yarn run tsc:cjs',
clean: 'rm -rf dist',
'tsc:esm': 'tsc -p ./tsconfig.json',
'tsc:cjs': 'tsc -p ./tsconfig.cjs.json && yarn run fix-ext',
'fix-ext':
'node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts',
'tsc:cjs': 'tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs',
};

// Set ESM tsconfig
Expand Down
Loading