From f9111d5c6f7bec462b8a62f9d7e6f648324e6d70 Mon Sep 17 00:00:00 2001 From: Rody van Sambeek Date: Thu, 21 Mar 2024 16:50:03 +0100 Subject: [PATCH] Updated way to check for main in ESM --- package-lock.json | 8 ++++++++ package.json | 3 +++ src/index.ts | 4 +++- tsconfig.json | 2 +- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0872fa5..ef10be9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,9 @@ "name": "@rodys/week", "version": "1.0.0", "license": "MIT", + "dependencies": { + "es-main": "^1.3.0" + }, "bin": { "week": "dist/index.js" }, @@ -1598,6 +1601,11 @@ "is-arrayish": "^0.2.1" } }, + "node_modules/es-main": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-main/-/es-main-1.3.0.tgz", + "integrity": "sha512-AzORKdz1Zt97TzbYQnIrI3ZiibWpRXUfpo/w0xOJ20GpNYd2bd3MU9m31zS/aJ1TJl6JfLTok83Y8HjNunYT0A==" + }, "node_modules/escalade": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.2.tgz", diff --git a/package.json b/package.json index 3577c23..3ab43ca 100644 --- a/package.json +++ b/package.json @@ -35,5 +35,8 @@ "jest": "^29.7.0", "ts-jest": "^29.1.2", "typescript": "^5.4.2" + }, + "dependencies": { + "es-main": "^1.3.0" } } diff --git a/src/index.ts b/src/index.ts index 7602049..23dd58c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,7 @@ #!/usr/bin/env node +import esMain from "es-main"; + const getCurrentISOWeek = (date: Date) => { const dayOfWeek = date.getDay(); const currentDay = date.getDate(); @@ -18,6 +20,6 @@ const getCurrentISOWeek = (date: Date) => { export default getCurrentISOWeek; -if (require.main === module) { +if (esMain(import.meta)) { console.log(getCurrentISOWeek(new Date())); } diff --git a/tsconfig.json b/tsconfig.json index 6fb473c..a9c2a75 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,7 +25,7 @@ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "CommonJS", /* Specify what module code is generated. */ + "module": "ESNext", /* Specify what module code is generated. */ "rootDir": "./src", /* Specify the root folder within your source files. */ "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */