From b2e0f87e519ec7ccad2f736c4b60a2cbb1031d7f Mon Sep 17 00:00:00 2001 From: Miorel-Lucian Palii Date: Sat, 14 Sep 2024 22:30:38 -0700 Subject: [PATCH] Migrate `post-leetcode-potd-to-discord` workspace to "module" format Per https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c Note that trying to get `ts-node` to work was quite painful, but `tsx` came to the rescue. So I think I'm going to favor `tsx` going forward. --- workspaces/download-leetcode-submissions/package.json | 5 +++-- workspaces/fetch-leetcode-problem-list/README.md | 4 ++-- workspaces/fetch-leetcode-problem-list/package.json | 10 ++++++---- .../fetch-leetcode-problem-list/webpack.config.ts | 10 +++++++--- .../README.md | 4 ++-- .../package.json | 10 ++++++---- .../webpack.config.ts | 10 +++++++--- workspaces/post-leetcode-potd-to-discord/README.md | 4 ++-- workspaces/post-leetcode-potd-to-discord/package.json | 10 ++++++---- .../src/getPotdMessage.ts | 5 +++-- workspaces/post-leetcode-potd-to-discord/src/main.ts | 10 +++++----- .../src/sendDiscordMessage.ts | 2 +- .../src/writeScriptData.ts | 2 +- .../post-leetcode-potd-to-discord/webpack.config.ts | 10 +++++++--- yarn.lock | 11 +++++++---- 15 files changed, 65 insertions(+), 42 deletions(-) diff --git a/workspaces/download-leetcode-submissions/package.json b/workspaces/download-leetcode-submissions/package.json index 5196e443..6afa8f83 100644 --- a/workspaces/download-leetcode-submissions/package.json +++ b/workspaces/download-leetcode-submissions/package.json @@ -12,17 +12,17 @@ "name": "Miorel-Lucian Palii", "url": "https://github.com/miorel" }, + "exports": "./src/main.ts", "scripts": { "format": "prettier --color --write .", "lint": "eslint --color --max-warnings=0 .", - "start": "ts-node src/main.ts", + "start": "tsx src/main.ts", "typecheck": "tsc --pretty --project ." }, "dependencies": { "@code-chronicles/leetcode-api": "workspace:*", "@code-chronicles/util": "workspace:*", "nullthrows": "1.1.1", - "ts-node": "10.9.2", "zod": "3.23.8" }, "devDependencies": { @@ -30,6 +30,7 @@ "@types/node": "22.5.5", "eslint": "9.10.0", "prettier": "3.3.3", + "tsx": "4.19.1", "typescript": "5.6.2" } } diff --git a/workspaces/fetch-leetcode-problem-list/README.md b/workspaces/fetch-leetcode-problem-list/README.md index 0742d415..f44e0549 100644 --- a/workspaces/fetch-leetcode-problem-list/README.md +++ b/workspaces/fetch-leetcode-problem-list/README.md @@ -36,10 +36,10 @@ yarn yarn build # Run it with Node! -node dist/fetch-leetcode-problem-list.js +node dist/fetch-leetcode-problem-list.cjs # Or if your system can handle executable files, try running it directly: -./dist/fetch-leetcode-problem-list.js +./dist/fetch-leetcode-problem-list.cjs # Examine the output: cat problems.jsonl diff --git a/workspaces/fetch-leetcode-problem-list/package.json b/workspaces/fetch-leetcode-problem-list/package.json index 94f9ed18..13190b84 100644 --- a/workspaces/fetch-leetcode-problem-list/package.json +++ b/workspaces/fetch-leetcode-problem-list/package.json @@ -12,12 +12,13 @@ "name": "Miorel-Lucian Palii", "url": "https://github.com/miorel" }, - "main": "src/main.ts", + "type": "module", + "exports": "./src/main.ts", "scripts": { - "build": "webpack && chmod +x dist/fetch-leetcode-problem-list.js", + "build": "cross-env NODE_OPTIONS=\"--import tsx\" webpack && chmod +x dist/fetch-leetcode-problem-list.cjs", "format": "prettier --color --write .", "lint": "eslint --color --max-warnings=0 .", - "start": "ts-node src/main.ts", + "start": "tsx src/main.ts", "typecheck": "tsc --pretty --project ." }, "dependencies": { @@ -27,10 +28,11 @@ "devDependencies": { "@code-chronicles/eslint-config": "workspace:*", "@types/node": "22.5.5", + "cross-env": "7.0.3", "eslint": "9.10.0", "prettier": "3.3.3", "ts-loader": "9.5.1", - "ts-node": "10.9.2", + "tsx": "4.19.1", "typescript": "5.6.2", "webpack": "5.94.0", "webpack-cli": "5.1.4" diff --git a/workspaces/fetch-leetcode-problem-list/webpack.config.ts b/workspaces/fetch-leetcode-problem-list/webpack.config.ts index 4e317ef3..33601c43 100644 --- a/workspaces/fetch-leetcode-problem-list/webpack.config.ts +++ b/workspaces/fetch-leetcode-problem-list/webpack.config.ts @@ -9,13 +9,14 @@ import webpack, { import { stripPrefix } from "@code-chronicles/util/stripPrefix"; import { stripPrefixOrThrow } from "@code-chronicles/util/stripPrefixOrThrow"; -import packageJson from "./package.json"; +import packageJson from "./package.json" with { type: "module" }; const config: Configuration = { target: "node", - entry: path.resolve(__dirname, packageJson.main), + entry: path.resolve(__dirname, packageJson.exports), output: { - filename: stripPrefixOrThrow(packageJson.name, "@code-chronicles/") + ".js", + filename: + stripPrefixOrThrow(packageJson.name, "@code-chronicles/") + ".cjs", path: path.resolve(__dirname, "dist"), }, @@ -40,6 +41,9 @@ const config: Configuration = { resolve: { extensions: [".tsx", ".ts", "..."], + extensionAlias: { + ".js": [".ts", ".tsx", ".js"], + }, }, externalsType: "commonjs", diff --git a/workspaces/fetch-recent-accepted-leetcode-submissions/README.md b/workspaces/fetch-recent-accepted-leetcode-submissions/README.md index 797d47e7..4f2939f3 100644 --- a/workspaces/fetch-recent-accepted-leetcode-submissions/README.md +++ b/workspaces/fetch-recent-accepted-leetcode-submissions/README.md @@ -31,10 +31,10 @@ yarn yarn build # Run it with Node! -node dist/fetch-recent-accepted-leetcode-submissions.js elimanzo VehicleOfPuzzle +node dist/fetch-recent-accepted-leetcode-submissions.cjs elimanzo VehicleOfPuzzle # Or if your system can handle executable files, try running it directly: -./dist/fetch-recent-accepted-leetcode-submissions.js elimanzo VehicleOfPuzzle +./dist/fetch-recent-accepted-leetcode-submissions.cjs elimanzo VehicleOfPuzzle ``` The output is JSON and will look something like: diff --git a/workspaces/fetch-recent-accepted-leetcode-submissions/package.json b/workspaces/fetch-recent-accepted-leetcode-submissions/package.json index 659b2c3a..5b3a6ca2 100644 --- a/workspaces/fetch-recent-accepted-leetcode-submissions/package.json +++ b/workspaces/fetch-recent-accepted-leetcode-submissions/package.json @@ -12,12 +12,13 @@ "name": "Miorel-Lucian Palii", "url": "https://github.com/miorel" }, - "main": "src/main.ts", + "type": "module", + "exports": "./src/main.ts", "scripts": { - "build": "webpack && chmod +x dist/fetch-recent-accepted-leetcode-submissions.js", + "build": "cross-env NODE_OPTIONS=\"--import tsx\" webpack && chmod +x dist/fetch-recent-accepted-leetcode-submissions.cjs", "format": "prettier --color --write .", "lint": "eslint --color --max-warnings=0 .", - "start": "ts-node src/main.ts", + "start": "tsx src/main.ts", "typecheck": "tsc --pretty --project ." }, "dependencies": { @@ -27,10 +28,11 @@ "devDependencies": { "@code-chronicles/eslint-config": "workspace:*", "@types/node": "22.5.5", + "cross-env": "7.0.3", "eslint": "9.10.0", "prettier": "3.3.3", "ts-loader": "9.5.1", - "ts-node": "10.9.2", + "tsx": "4.19.1", "typescript": "5.6.2", "webpack": "5.94.0", "webpack-cli": "5.1.4" diff --git a/workspaces/fetch-recent-accepted-leetcode-submissions/webpack.config.ts b/workspaces/fetch-recent-accepted-leetcode-submissions/webpack.config.ts index 4e317ef3..33601c43 100644 --- a/workspaces/fetch-recent-accepted-leetcode-submissions/webpack.config.ts +++ b/workspaces/fetch-recent-accepted-leetcode-submissions/webpack.config.ts @@ -9,13 +9,14 @@ import webpack, { import { stripPrefix } from "@code-chronicles/util/stripPrefix"; import { stripPrefixOrThrow } from "@code-chronicles/util/stripPrefixOrThrow"; -import packageJson from "./package.json"; +import packageJson from "./package.json" with { type: "module" }; const config: Configuration = { target: "node", - entry: path.resolve(__dirname, packageJson.main), + entry: path.resolve(__dirname, packageJson.exports), output: { - filename: stripPrefixOrThrow(packageJson.name, "@code-chronicles/") + ".js", + filename: + stripPrefixOrThrow(packageJson.name, "@code-chronicles/") + ".cjs", path: path.resolve(__dirname, "dist"), }, @@ -40,6 +41,9 @@ const config: Configuration = { resolve: { extensions: [".tsx", ".ts", "..."], + extensionAlias: { + ".js": [".ts", ".tsx", ".js"], + }, }, externalsType: "commonjs", diff --git a/workspaces/post-leetcode-potd-to-discord/README.md b/workspaces/post-leetcode-potd-to-discord/README.md index 56dbc570..f6f69fc9 100644 --- a/workspaces/post-leetcode-potd-to-discord/README.md +++ b/workspaces/post-leetcode-potd-to-discord/README.md @@ -48,10 +48,10 @@ To use: yarn build # Run it with Node! - node dist/post-leetcode-potd-to-discord.js + node dist/post-leetcode-potd-to-discord.cjs # Or if your system can handle executable files, try running it directly: - ./dist/post-leetcode-potd-to-discord.js + ./dist/post-leetcode-potd-to-discord.cjs ``` Here's an example message: diff --git a/workspaces/post-leetcode-potd-to-discord/package.json b/workspaces/post-leetcode-potd-to-discord/package.json index dc728549..4aff6f40 100644 --- a/workspaces/post-leetcode-potd-to-discord/package.json +++ b/workspaces/post-leetcode-potd-to-discord/package.json @@ -12,12 +12,13 @@ "name": "Miorel-Lucian Palii", "url": "https://github.com/miorel" }, - "main": "src/main.ts", + "type": "module", + "exports": "./src/main.ts", "scripts": { - "build": "webpack && chmod +x dist/post-leetcode-potd-to-discord.js", + "build": "cross-env NODE_OPTIONS=\"--import tsx\" webpack && chmod +x dist/post-leetcode-potd-to-discord.cjs", "format": "prettier --color --write .", "lint": "eslint --color --max-warnings=0 .", - "start": "ts-node src/main.ts", + "start": "tsx src/main.ts", "typecheck": "tsc --pretty --project ." }, "dependencies": { @@ -34,9 +35,10 @@ "@code-chronicles/eslint-config": "workspace:*", "@types/invariant": "2.2.37", "@types/node": "22.5.5", + "cross-env": "7.0.3", "eslint": "9.10.0", "prettier": "3.3.3", - "ts-node": "10.9.2", + "tsx": "4.19.1", "typescript": "5.6.2", "webpack": "5.94.0", "webpack-cli": "5.1.4" diff --git a/workspaces/post-leetcode-potd-to-discord/src/getPotdMessage.ts b/workspaces/post-leetcode-potd-to-discord/src/getPotdMessage.ts index 8c7b0d6d..b09a5fc8 100644 --- a/workspaces/post-leetcode-potd-to-discord/src/getPotdMessage.ts +++ b/workspaces/post-leetcode-potd-to-discord/src/getPotdMessage.ts @@ -1,7 +1,8 @@ import type { ActiveDailyCodingChallengeQuestion } from "@code-chronicles/leetcode-api"; -import { formatTimestampForDiscord } from "./formatTimestampForDiscord"; -import { yearMonthDayToTimestampInSeconds } from "@code-chronicles/util/yearMonthDayToTimestampInSeconds"; import { SEC_IN_DAY } from "@code-chronicles/util/timeConstants"; +import { yearMonthDayToTimestampInSeconds } from "@code-chronicles/util/yearMonthDayToTimestampInSeconds"; + +import { formatTimestampForDiscord } from "./formatTimestampForDiscord.js"; export function getPotdMessage({ date, diff --git a/workspaces/post-leetcode-potd-to-discord/src/main.ts b/workspaces/post-leetcode-potd-to-discord/src/main.ts index 73a7f1da..d170e843 100644 --- a/workspaces/post-leetcode-potd-to-discord/src/main.ts +++ b/workspaces/post-leetcode-potd-to-discord/src/main.ts @@ -11,11 +11,11 @@ import { import { whileReturnsTrueAsync } from "@code-chronicles/util/whileReturnsTrueAsync"; import { yearMonthDayToTimestampInSeconds } from "@code-chronicles/util/yearMonthDayToTimestampInSeconds"; -import { getPotdMessage } from "./getPotdMessage"; -import { readScriptData } from "./readScriptData"; -import { readSecrets } from "./readSecrets"; -import { sendDiscordMessage } from "./sendDiscordMessage"; -import { writeScriptData } from "./writeScriptData"; +import { getPotdMessage } from "./getPotdMessage.js"; +import { readScriptData } from "./readScriptData.js"; +import { readSecrets } from "./readSecrets.js"; +import { sendDiscordMessage } from "./sendDiscordMessage.js"; +import { writeScriptData } from "./writeScriptData.js"; async function main(): Promise { // TODO: maybe create the file from a template if it doesn't exist diff --git a/workspaces/post-leetcode-potd-to-discord/src/sendDiscordMessage.ts b/workspaces/post-leetcode-potd-to-discord/src/sendDiscordMessage.ts index 12f7a06e..6e10fc0f 100644 --- a/workspaces/post-leetcode-potd-to-discord/src/sendDiscordMessage.ts +++ b/workspaces/post-leetcode-potd-to-discord/src/sendDiscordMessage.ts @@ -1,7 +1,7 @@ import { ChannelType, Client, GatewayIntentBits } from "discord.js"; import invariant from "invariant"; -import type { Secrets } from "./readSecrets"; +import type { Secrets } from "./readSecrets.js"; export async function sendDiscordMessage( { discordChannelID, discordToken }: Secrets, diff --git a/workspaces/post-leetcode-potd-to-discord/src/writeScriptData.ts b/workspaces/post-leetcode-potd-to-discord/src/writeScriptData.ts index 5e5158d9..978e81d3 100644 --- a/workspaces/post-leetcode-potd-to-discord/src/writeScriptData.ts +++ b/workspaces/post-leetcode-potd-to-discord/src/writeScriptData.ts @@ -1,6 +1,6 @@ import { writeFile } from "node:fs/promises"; -import { DATA_FILE, type Data } from "./readScriptData"; +import { DATA_FILE, type Data } from "./readScriptData.js"; export async function writeScriptData(data: Data): Promise { await writeFile(DATA_FILE, JSON.stringify(data), { diff --git a/workspaces/post-leetcode-potd-to-discord/webpack.config.ts b/workspaces/post-leetcode-potd-to-discord/webpack.config.ts index 3881f86f..95568beb 100644 --- a/workspaces/post-leetcode-potd-to-discord/webpack.config.ts +++ b/workspaces/post-leetcode-potd-to-discord/webpack.config.ts @@ -9,13 +9,14 @@ import webpack, { import { stripPrefix } from "@code-chronicles/util/stripPrefix"; import { stripPrefixOrThrow } from "@code-chronicles/util/stripPrefixOrThrow"; -import packageJson from "./package.json"; +import packageJson from "./package.json" with { type: "module" }; const config: Configuration = { target: "node", - entry: path.resolve(__dirname, packageJson.main), + entry: path.resolve(__dirname, packageJson.exports), output: { - filename: stripPrefixOrThrow(packageJson.name, "@code-chronicles/") + ".js", + filename: + stripPrefixOrThrow(packageJson.name, "@code-chronicles/") + ".cjs", path: path.resolve(__dirname, "dist"), }, @@ -40,6 +41,9 @@ const config: Configuration = { resolve: { extensions: [".tsx", ".ts", "..."], + extensionAlias: { + ".js": [".ts", ".tsx", ".js"], + }, }, externalsType: "commonjs", diff --git a/yarn.lock b/yarn.lock index 161e3eea..e6ea4960 100644 --- a/yarn.lock +++ b/yarn.lock @@ -517,7 +517,7 @@ __metadata: eslint: "npm:9.10.0" nullthrows: "npm:1.1.1" prettier: "npm:3.3.3" - ts-node: "npm:10.9.2" + tsx: "npm:4.19.1" typescript: "npm:5.6.2" zod: "npm:3.23.8" languageName: unknown @@ -550,10 +550,11 @@ __metadata: "@code-chronicles/leetcode-api": "workspace:*" "@code-chronicles/util": "workspace:*" "@types/node": "npm:22.5.5" + cross-env: "npm:7.0.3" eslint: "npm:9.10.0" prettier: "npm:3.3.3" ts-loader: "npm:9.5.1" - ts-node: "npm:10.9.2" + tsx: "npm:4.19.1" typescript: "npm:5.6.2" webpack: "npm:5.94.0" webpack-cli: "npm:5.1.4" @@ -568,10 +569,11 @@ __metadata: "@code-chronicles/leetcode-api": "workspace:*" "@code-chronicles/util": "workspace:*" "@types/node": "npm:22.5.5" + cross-env: "npm:7.0.3" eslint: "npm:9.10.0" prettier: "npm:3.3.3" ts-loader: "npm:9.5.1" - ts-node: "npm:10.9.2" + tsx: "npm:4.19.1" typescript: "npm:5.6.2" webpack: "npm:5.94.0" webpack-cli: "npm:5.1.4" @@ -639,11 +641,12 @@ __metadata: "@types/invariant": "npm:2.2.37" "@types/node": "npm:22.5.5" bufferutil: "npm:4.0.8" + cross-env: "npm:7.0.3" discord.js: "npm:14.15.3" eslint: "npm:9.10.0" invariant: "npm:2.2.4" prettier: "npm:3.3.3" - ts-node: "npm:10.9.2" + tsx: "npm:4.19.1" typescript: "npm:5.6.2" utf-8-validate: "npm:6.0.4" webpack: "npm:5.94.0"