diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 420c51c..557ea0e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: install, lint and test +name: ci on: push: @@ -35,7 +35,7 @@ jobs: - uses: actions/cache@v4 with: path: node_modules - key: ${{matrix.node-version}}@${{matrix.platform}}-build-${{hashFiles('package.json')}} + key: ${{matrix.node-version}}@${{matrix.platform}}-build-${{hashFiles('package-lock.json')}} restore-keys: | ${{matrix.node-version}}@${{matrix.platform}}-build- - uses: actions/setup-node@v4 @@ -44,4 +44,5 @@ jobs: - run: npm install - run: npm run build - run: npm run lint - - run: npm test + - run: node_modules/.bin/tsx --test-reporter ./tools/dot-with-summary.reporter.mjs --test src/*.test.mts src/*.test.ts + # - run: npm test diff --git a/dist/extract-root-module-name.js b/dist/extract-root-module-name.js index 63ab757..d4224d4 100644 --- a/dist/extract-root-module-name.js +++ b/dist/extract-root-module-name.js @@ -6,12 +6,13 @@ const PACKAGE_RE = "[^/]+"; const SCOPED_PACKAGE_RE = "@[^/]+(/[^/]+)"; const ROOT_MODULE_RE = new RegExp(`^(${SCOPED_PACKAGE_RE}|${PACKAGE_RE})`, "g"); function extractRootModuleName(pModuleName) { - if (pModuleName.match(LOCAL_MODULE_RE) || - pModuleName.match(ABSOLUTE_MODULE_RE)) { - return pModuleName; - } - else { - return (pModuleName.match(ROOT_MODULE_RE) || []).shift(); - } + if ( + pModuleName.match(LOCAL_MODULE_RE) || + pModuleName.match(ABSOLUTE_MODULE_RE) + ) { + return pModuleName; + } else { + return (pModuleName.match(ROOT_MODULE_RE) || []).shift(); + } } exports.default = extractRootModuleName; diff --git a/dist/try-import.mjs b/dist/try-import.mjs index 12f2731..98c3d20 100644 --- a/dist/try-import.mjs +++ b/dist/try-import.mjs @@ -1,5 +1,5 @@ -import path from "path/posix"; -import { createRequire } from "module"; +import path from "node:path/posix"; +import { createRequire } from "node:module"; import { coerce, satisfies } from "semver"; const LOCAL_MODULE_RE = /^[.]{1,2}($|\/.*)/g; const ABSOLUTE_MODULE_RE = /^\/.*/g; @@ -8,33 +8,37 @@ const SCOPED_PACKAGE_RE = "@[^/]+(/[^/]+)"; const ROOT_MODULE_RE = new RegExp(`^(${SCOPED_PACKAGE_RE}|${PACKAGE_RE})`, "g"); const require = createRequire(import.meta.url); function extractRootModuleName(pModuleName) { - if (pModuleName.match(LOCAL_MODULE_RE) || - pModuleName.match(ABSOLUTE_MODULE_RE)) { - return pModuleName; - } - else { - return (pModuleName.match(ROOT_MODULE_RE) || []).shift(); - } + if ( + pModuleName.match(LOCAL_MODULE_RE) || + pModuleName.match(ABSOLUTE_MODULE_RE) + ) { + return pModuleName; + } else { + return (pModuleName.match(ROOT_MODULE_RE) || []).shift(); + } } function getVersion(pModuleName) { - const lManifest = require(path.join(extractRootModuleName(pModuleName), "package.json")); - return lManifest.version; + const lManifest = require( + path.join(extractRootModuleName(pModuleName), "package.json"), + ); + return lManifest.version; } export default async function tryImport(pModuleName, pSemanticVersion) { - try { - if (pSemanticVersion) { - const lVersion = getVersion(pModuleName); - const lCoerced = coerce(lVersion); - if (lVersion && - lCoerced && - !satisfies(lCoerced.version, pSemanticVersion)) { - return false; - } - } - const lModule = await import(pModuleName); - return lModule.default ? lModule.default : lModule; - } - catch (pError) { - return false; - } + try { + if (pSemanticVersion) { + const lVersion = getVersion(pModuleName); + const lCoerced = coerce(lVersion); + if ( + lVersion && + lCoerced && + !satisfies(lCoerced.version, pSemanticVersion) + ) { + return false; + } + } + const lModule = await import(pModuleName); + return lModule.default ? lModule.default : lModule; + } catch (pError) { + return false; + } } diff --git a/dist/try-require.js b/dist/try-require.js index 90e2d76..e49d3c7 100644 --- a/dist/try-require.js +++ b/dist/try-require.js @@ -1,32 +1,41 @@ "use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; +var __importDefault = + (this && this.__importDefault) || + function (mod) { + return mod && mod.__esModule ? mod : { default: mod }; + }; Object.defineProperty(exports, "__esModule", { value: true }); -const path_1 = __importDefault(require("path")); +const node_path_1 = __importDefault(require("node:path")); const satisfies_1 = __importDefault(require("semver/functions/satisfies")); const coerce_1 = __importDefault(require("semver/functions/coerce")); -const extract_root_module_name_1 = __importDefault(require("./extract-root-module-name")); +const extract_root_module_name_1 = __importDefault( + require("./extract-root-module-name"), +); function getVersion(pModuleName) { - return require(path_1.default.join((0, extract_root_module_name_1.default)(pModuleName), "package.json")) - .version; + return require( + node_path_1.default.join( + (0, extract_root_module_name_1.default)(pModuleName), + "package.json", + ), + ).version; } function tryRequire(pModuleName, pSemanticVersion) { - try { - if (pSemanticVersion) { - const lVersion = getVersion(pModuleName); - const lCoerced = (0, coerce_1.default)(lVersion); - if (lVersion && - lCoerced && - !(0, satisfies_1.default)(lCoerced.version, pSemanticVersion)) { - return false; - } - } - return require(pModuleName); - } - catch (pError) { - return false; - } + try { + if (pSemanticVersion) { + const lVersion = getVersion(pModuleName); + const lCoerced = (0, coerce_1.default)(lVersion); + if ( + lVersion && + lCoerced && + !(0, satisfies_1.default)(lCoerced.version, pSemanticVersion) + ) { + return false; + } + } + return require(pModuleName); + } catch (pError) { + return false; + } } exports.default = tryRequire; module.exports = tryRequire; diff --git a/package-lock.json b/package-lock.json index 3d103fa..e1f93e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "@typescript-eslint/eslint-plugin": "7.8.0", "@typescript-eslint/parser": "7.8.0", "c8": "9.1.0", + "clean-pkg-json": "1.2.0", "eslint": "^8.57.0", "eslint-config-moving-meadow": "4.0.2", "eslint-config-prettier": "9.1.0", @@ -932,6 +933,18 @@ "node": ">=8" } }, + "node_modules/clean-pkg-json": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/clean-pkg-json/-/clean-pkg-json-1.2.0.tgz", + "integrity": "sha512-QHBWWOtpSCv5nfMFKwyxn4WMvkYE0msxj5xiqejYGqYepWVrK7O/om1Vn6nSl/WPtYn8ge9YmSCbTi1Hp8k+Hg==", + "dev": true, + "bin": { + "clean-pkg-json": "dist/index.js" + }, + "funding": { + "url": "https://github.com/privatenumber/clean-pkg-json?sponsor=1" + } + }, "node_modules/clean-regexp": { "version": "1.0.0", "dev": true, diff --git a/package.json b/package.json index 7cc5974..34e5316 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "@typescript-eslint/eslint-plugin": "7.8.0", "@typescript-eslint/parser": "7.8.0", "c8": "9.1.0", + "clean-pkg-json": "1.2.0", "eslint": "^8.57.0", "eslint-config-moving-meadow": "4.0.2", "eslint-config-prettier": "9.1.0", @@ -65,18 +66,19 @@ ] }, "scripts": { - "build": "npm-run-all build:clean build:compile", + "build": "npm-run-all build:clean build:compile build:format", "build:clean": "rm -rf dist", "build:compile": "tsc", + "build:format": "prettier --log-level warn --write --use-tabs dist/", "check": "npm-run-all --parallel lint test", - "check:full": "npm-run-all --parallel check check:outdated", - "check:outdated": "npm outdated", "lint": "npm-run-all lint:eslint format:check", "lint:eslint": "eslint src", "lint:fix": "npm-run-all lint:fix:eslint format", "lint:fix:eslint": "eslint --fix src", - "format:check": "prettier --loglevel warn --check src types *.{md,yml,json}", - "format": "prettier --loglevel warn --write src types *.{md,yml,json}", + "format:check": "prettier --log-level warn --check src types *.{md,yml,json}", + "format": "prettier --log-level warn --write src types *.{md,yml,json}", + "prepack": "clean-pkg-json --dry | jq '.scripts = {test: \"echo for test, build and static analysis scripts: see the github repository\"}' > smol-package.json && mv smol-package.json package.json && prettier --log-level warn --write --use-tabs package.json types", + "postpack": "git restore package.json types", "scm:push": "run-p --aggregate-output scm:push:*", "scm:push:github": "run-p --aggregate-output scm:push:github:*", "scm:push:github:commits": "git push", diff --git a/src/try-import.mts b/src/try-import.mts index 8ca9523..7e7ce2f 100644 --- a/src/try-import.mts +++ b/src/try-import.mts @@ -1,5 +1,5 @@ -import path from "path/posix"; -import { createRequire } from "module"; +import path from "node:path/posix"; +import { createRequire } from "node:module"; import { coerce, satisfies } from "semver"; const LOCAL_MODULE_RE = /^[.]{1,2}($|\/.*)/g; diff --git a/src/try-import.test.mts b/src/try-import.test.mts index fa466f1..8cdfdff 100644 --- a/src/try-import.test.mts +++ b/src/try-import.test.mts @@ -1,13 +1,10 @@ import { equal, deepEqual } from "node:assert/strict"; import { describe, it } from "node:test"; import semver from "semver"; -// @ts-expect-error TS7016 import * as noDefaultExportMock from "no-default-export"; import tryImport from "./try-import.mjs"; -// @ts-expect-error TS7016 import betaMock from "beta"; -// @ts-expect-error TS7016 import rcMock from "rc"; describe("tryImport", () => { diff --git a/src/try-require-as-built.test.ts b/src/try-require-as-built.test.ts index ff84e74..579d017 100644 --- a/src/try-require-as-built.test.ts +++ b/src/try-require-as-built.test.ts @@ -1,9 +1,7 @@ -import { equal, deepEqual } from "node:assert"; +import { equal, deepEqual } from "node:assert/strict"; import { describe, it } from "node:test"; import semver from "semver"; -// @ts-expect-error TS7016 import betaMock from "beta"; -// @ts-expect-error TS7016 import rcMock from "rc"; const tryRequire = require("../dist/try-require.js"); diff --git a/src/try-require.test.ts b/src/try-require.test.ts index 321bb52..bcf719b 100644 --- a/src/try-require.test.ts +++ b/src/try-require.test.ts @@ -1,10 +1,8 @@ -import { equal, deepEqual } from "node:assert"; +import { equal, deepEqual } from "node:assert/strict"; import { describe, it } from "node:test"; import semver from "semver"; import tryRequire from "./try-require"; -// @ts-expect-error TS7016 import betaMock from "beta"; -// @ts-expect-error TS7016 import rcMock from "rc"; describe("tryRequire", () => { diff --git a/src/try-require.ts b/src/try-require.ts index 6869494..10cf402 100644 --- a/src/try-require.ts +++ b/src/try-require.ts @@ -1,5 +1,5 @@ /* eslint-disable import/no-import-module-exports */ -import path from "path"; +import path from "node:path"; import satisfies from "semver/functions/satisfies"; import coerce from "semver/functions/coerce"; import extractRootModuleName from "./extract-root-module-name";