Skip to content

Commit

Permalink
chore(npm): removes unused run scripts from published manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij committed May 4, 2024
1 parent c5f5434 commit cb76b0a
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 76 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: install, lint and test
name: ci

on:
push:
Expand Down Expand Up @@ -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
Expand All @@ -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
15 changes: 8 additions & 7 deletions dist/extract-root-module-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
58 changes: 31 additions & 27 deletions dist/try-import.mjs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
}
53 changes: 31 additions & 22 deletions dist/try-require.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/try-import.mts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 0 additions & 3 deletions src/try-import.test.mts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
4 changes: 1 addition & 3 deletions src/try-require-as-built.test.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
4 changes: 1 addition & 3 deletions src/try-require.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/try-require.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit cb76b0a

Please sign in to comment.