From 3030be8aaa88fedbdb40cf21e32146bc92199e00 Mon Sep 17 00:00:00 2001 From: dmail Date: Wed, 14 Aug 2024 10:39:06 +0200 Subject: [PATCH] improve an error message --- .github/workflows/ci_eslint_and_test.yml | 2 +- .github/workflows/ci_test_workspace.yml | 2 +- package.json | 4 ++-- packages/independent/https-local/package.json | 2 +- .../https-local/src/certificate_request.js | 4 +++- .../https-local/src/https_local_cli.mjs | 17 ++++++++++++- scripts/dev/install_certificate_authority.mjs | 23 ------------------ scripts/test/certificate_install.mjs | 24 ------------------- .../manual/start_build_server.mjs | 3 +-- 9 files changed, 25 insertions(+), 56 deletions(-) delete mode 100644 scripts/dev/install_certificate_authority.mjs delete mode 100644 scripts/test/certificate_install.mjs diff --git a/.github/workflows/ci_eslint_and_test.yml b/.github/workflows/ci_eslint_and_test.yml index 4ebaa54c83..e78854e100 100644 --- a/.github/workflows/ci_eslint_and_test.yml +++ b/.github/workflows/ci_eslint_and_test.yml @@ -57,7 +57,7 @@ jobs: run: npx playwright install-deps - name: Install certificate # needed for @jsenv/service-worker tests if: runner.os == 'Linux' # https://docs.github.com/en/actions/learn-github-actions/contexts#runner-context - run: node ./scripts/test/certificate_install.mjs + run: npm run certificate:install - name: Fix lightningcss windows if: runner.os == 'Windows' run: npm install lightningcss-win32-x64-msvc diff --git a/.github/workflows/ci_test_workspace.yml b/.github/workflows/ci_test_workspace.yml index baa5bb0e58..4d0b55b879 100644 --- a/.github/workflows/ci_test_workspace.yml +++ b/.github/workflows/ci_test_workspace.yml @@ -55,7 +55,7 @@ jobs: run: npx playwright install-deps - name: Install certificate # needed for @jsenv/service-worker tests if: runner.os == 'Linux' # https://docs.github.com/en/actions/learn-github-actions/contexts#runner-context - run: node ./scripts/test/certificate_install.mjs + run: npm run certificate:install - name: Fix lightningcss windows if: runner.os == 'Windows' run: npm install lightningcss-win32-x64-msvc diff --git a/package.json b/package.json index 48d9795cf0..8250b159c7 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "workspace:test:ci": "CI=1 npm run workspace:test", "dev": "node --conditions=development ./scripts/dev/dev.mjs", "playwright:install": "npx playwright install-deps && npx playwright install", - "certificate:install": "node ./scripts/dev/install_certificate_authority.mjs", + "certificate:install": "npx @jsenv/https-local install", "prepublishOnly": "npm run build" }, "dependencies": { @@ -97,7 +97,7 @@ "@jsenv/core": "./", "@jsenv/eslint-config-relax": "workspace:*", "@jsenv/file-size-impact": "workspace:*", - "@jsenv/https-local": "3.0.7", + "@jsenv/https-local": "workspace:*", "@jsenv/monorepo": "workspace:*", "@jsenv/performance-impact": "workspace:*", "@jsenv/plugin-as-js-classic": "workspace:*", diff --git a/packages/independent/https-local/package.json b/packages/independent/https-local/package.json index 7ad0df1551..dafe532079 100644 --- a/packages/independent/https-local/package.json +++ b/packages/independent/https-local/package.json @@ -1,6 +1,6 @@ { "name": "@jsenv/https-local", - "version": "3.1.0", + "version": "3.1.1", "description": "A programmatic way to generate locally trusted certificates", "license": "MIT", "repository": { diff --git a/packages/independent/https-local/src/certificate_request.js b/packages/independent/https-local/src/certificate_request.js index 309542aab7..22657b5d09 100644 --- a/packages/independent/https-local/src/certificate_request.js +++ b/packages/independent/https-local/src/certificate_request.js @@ -46,7 +46,9 @@ export const requestCertificate = ({ } = getAuthorityFileInfos(); if (!rootCertificateFileInfo.exists) { throw new Error( - `Certificate authority not found, "installCertificateAuthority" must be called before "requestServerCertificate"`, + `Certificate authority not found, "installCertificateAuthority" must be called before "requestServerCertificate". +--- Suggested command to run --- +npx @jsenv/https-local install --trust`, ); } if (!rootCertificatePrivateKeyFileInfo.exists) { diff --git a/packages/independent/https-local/src/https_local_cli.mjs b/packages/independent/https-local/src/https_local_cli.mjs index 6063811049..12a6a68a5c 100755 --- a/packages/independent/https-local/src/https_local_cli.mjs +++ b/packages/independent/https-local/src/https_local_cli.mjs @@ -3,6 +3,7 @@ import { installCertificateAuthority, uninstallCertificateAuthority, + verifyHostsFile, } from "@jsenv/https-local"; import { parseArgs } from "node:util"; @@ -23,10 +24,16 @@ if (values.help || positionals.length === 0) { console.log(`https-local: Generate https certificates to use on your machine. Usage: + npx @jsenv/https-local install --trust + Install root certificate on the filesystem + - trust: Try to add root certificate to os and browser trusted stores. + npx @jsenv/https-local uninstall + Uninstall root certificate from the filesystem -trust: Try to add root certificate to os and browser trusted stores. +npx @jsenv/https-local localhost-mapping + Ensure localhost mapping to 127.0.0.1 is set on the filesystem https://github.com/jsenv/core/tree/main/packages/independent/https-local @@ -47,6 +54,14 @@ const commandHandlers = { tryToUntrust: true, }); }, + ["localhost-mapping"]: async () => { + await verifyHostsFile({ + ipMappings: { + "127.0.0.1": ["localhost"], + }, + tryToUpdatesHostsFile: true, + }); + }, }; const [command] = positionals; diff --git a/scripts/dev/install_certificate_authority.mjs b/scripts/dev/install_certificate_authority.mjs deleted file mode 100644 index 55fb08bbd0..0000000000 --- a/scripts/dev/install_certificate_authority.mjs +++ /dev/null @@ -1,23 +0,0 @@ -/* - * This file needs to be executed once. After that the root certificate is valid for 20 years. - * Re-executing this file will log the current root certificate validity and trust status. - * Re-executing this file 20 years later would reinstall a root certificate and re-trust it. - * - * Read more in https://github.com/jsenv/https-local#installCertificateAuthority - */ - -import { - installCertificateAuthority, - verifyHostsFile, -} from "@jsenv/https-local"; - -await installCertificateAuthority({ - tryToTrust: true, - NSSDynamicInstall: true, -}); -await verifyHostsFile({ - ipMappings: { - "127.0.0.1": ["localhost", "local"], - }, - tryToUpdatesHostsFile: true, -}); diff --git a/scripts/test/certificate_install.mjs b/scripts/test/certificate_install.mjs deleted file mode 100644 index 5f070e1cb7..0000000000 --- a/scripts/test/certificate_install.mjs +++ /dev/null @@ -1,24 +0,0 @@ -/* - * This file needs to be executed once. After that the root certificate is valid for 20 years. - * Re-executing this file will log the current root certificate validity and trust status. - * Re-executing this file 20 years later would reinstall a root certificate and re-trust it. - * - * Read more in https://github.com/jsenv/https-local#installCertificateAuthority - */ - -import { - installCertificateAuthority, - verifyHostsFile, -} from "@jsenv/https-local"; - -await installCertificateAuthority({ - logLevel: "debug", - tryToTrust: true, - NSSDynamicInstall: true, -}); -await verifyHostsFile({ - ipMappings: { - "127.0.0.1": ["localhost"], - }, - tryToUpdatesHostsFile: true, -}); diff --git a/tests/build_server/manual/start_build_server.mjs b/tests/build_server/manual/start_build_server.mjs index c37d07ea02..8ae1bf55aa 100644 --- a/tests/build_server/manual/start_build_server.mjs +++ b/tests/build_server/manual/start_build_server.mjs @@ -1,6 +1,5 @@ -import { requestCertificate } from "@jsenv/https-local"; - import { startBuildServer } from "@jsenv/core"; +import { requestCertificate } from "@jsenv/https-local"; const { certificate, privateKey } = requestCertificate({ altNames: ["local"] }); await startBuildServer({