From 8468e8a0d1591138b32b16c68c293868b4b4f361 Mon Sep 17 00:00:00 2001 From: Klaus Reimer Date: Tue, 3 Jun 2025 23:35:27 +0200 Subject: [PATCH 1/2] Implement support for a JSON configuration file --- README.md | 18 ++++++++++++++++++ src/cli.mjs | 21 +++++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d32e764..ae670de 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,24 @@ The CLI supports the following options: - `--entrypoint `: Confirm the specified file and its transitive dependencies can be resolved using the generated import map. Auto-enabled when importmap is written into an HTML file. Can be specified multiple times. - `--dev`: Include devDependencies from `package.json`. Also favor `"development"` in [package exports](https://nodejs.org/docs/latest-v16.x/api/packages.html#packages_conditions_definitions). - `--keep-unused`: Keep all mappings, even if they are not currently used by entry file or its transitive dependencies. +- `--config `: Read additional settings from the given JSON configuration file. + +#### Configuration file + +A configuration file in JSON format can be specified with the `--config` CLI option. The following example shows such a JSON file with all supported properties set to their default values: + +```json +{ + "dir": ".", + "dev": false, + "keepUnused": false, + "entryPoints": [], + "manualImportmap": {}, + "packagesManualOverrides": {} +} +``` + +All properties are optional and CLI options like `--dev`, `--entrypoint` and `--keep-unused` will override the values read from the config. The objects `manualImportmap` and `packagesManualOverrides` have the same format as the corresponding objects in the API. ### API diff --git a/src/cli.mjs b/src/cli.mjs index 5f2189b..187e3ce 100755 --- a/src/cli.mjs +++ b/src/cli.mjs @@ -1,5 +1,6 @@ #!/usr/bin/env node +import { readFile } from "node:fs/promises"; import { pathToFileURL } from "node:url"; import { parseArgs } from "node:util"; import { writeImportmaps } from "./main.js"; @@ -21,10 +22,12 @@ const options = { "keep-unused": { type: "boolean", }, + "config": { + type: "string", + }, }; const { values, positionals } = parseArgs({ options, allowPositionals: true }); const outfile = positionals[0]; -values.entrypoint ??= []; if (values.help || positionals.length === 0) { usage(); @@ -46,21 +49,26 @@ if ( process.exit(1); } +const config = values.config ? JSON.parse(await readFile(values.config)) : {}; +const dev = values.dev ?? config.dev; + const currentDirectoryUrl = pathToFileURL(`${process.cwd()}/`); await writeImportmaps({ - directoryUrl: new URL(values.dir || ".", currentDirectoryUrl), + directoryUrl: new URL(values.dir ?? config.dir ?? ".", currentDirectoryUrl), importmaps: { [outfile]: { nodeMappings: { - devDependencies: values.dev, - packageUserConditions: values.dev ? ["development"] : [], + devDependencies: dev, + packageUserConditions: dev ? ["development"] : [], }, importResolution: { - entryPoints: values.entryPoints, + entryPoints: values.entrypoint ?? config.entryPoints ?? [], }, - keepUnusedMappings: values["keep-unused"], + keepUnusedMappings: values["keep-unused"] ?? config.keepUnused, + manualImportmap: config.manualImportmap, }, }, + packagesManualOverrides: config.packagesManualOverrides, }); function usage() { @@ -76,6 +84,7 @@ Options: --entrypoint file.js Confirm the specified file and its transitive dependencies can be resolved using the generated import map. Can be specified multiple times. --dev Include devDependencies from package.json and pick "developement" in package conditions. --keep-unused Remove mappings not used by any entrypoint or their transitive dependencies. Requires --entrypoint. + --config config.json Read additional settings from the given JSON configuration file. For more advanced options, see the API.`); } From 29a7de11cd706a4ac405da4db97f74d89f2b585c Mon Sep 17 00:00:00 2001 From: Klaus Reimer Date: Tue, 3 Jun 2025 23:41:25 +0200 Subject: [PATCH 2/2] Add missing encoding parameter --- src/cli.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli.mjs b/src/cli.mjs index 187e3ce..1ace50b 100755 --- a/src/cli.mjs +++ b/src/cli.mjs @@ -49,7 +49,7 @@ if ( process.exit(1); } -const config = values.config ? JSON.parse(await readFile(values.config)) : {}; +const config = values.config ? JSON.parse(await readFile(values.config, "utf-8")) : {}; const dev = values.dev ?? config.dev; const currentDirectoryUrl = pathToFileURL(`${process.cwd()}/`);