Skip to content

Commit ac106ac

Browse files
authored
Merge pull request #82 from jsenv/config_file
Config file
2 parents c949baa + 70a9d74 commit ac106ac

18 files changed

Lines changed: 138 additions & 8 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ The CLI supports the following options:
5555
- `--entrypoint <file>`: 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.
5656
- `--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)</a><sup>↗</sup>.
5757
- `--keep-unused`: Keep all mappings, even if they are not currently used by entry file or its transitive dependencies.
58+
- `--config <file>`: Read additional settings from the given JSON configuration file.
59+
60+
#### Configuration file
61+
62+
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:
63+
64+
```json
65+
{
66+
"dir": ".",
67+
"dev": false,
68+
"keepUnused": false,
69+
"entryPoints": [],
70+
"manualImportmap": {},
71+
"packagesManualOverrides": {}
72+
}
73+
```
74+
75+
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.
5876

5977
### API
6078

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jsenv/importmap-node-module",
3-
"version": "7.2.2",
3+
"version": "7.3.0",
44
"description": "Generate importmap for node_modules",
55
"license": "MIT",
66
"repository": {

src/cli.mjs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
22

3+
import { readFile } from "node:fs/promises";
34
import { pathToFileURL } from "node:url";
45
import { parseArgs } from "node:util";
56
import { writeImportmaps } from "./main.js";
@@ -21,10 +22,12 @@ const options = {
2122
"keep-unused": {
2223
type: "boolean",
2324
},
25+
"config": {
26+
type: "string",
27+
},
2428
};
2529
const { values, positionals } = parseArgs({ options, allowPositionals: true });
2630
const outfile = positionals[0];
27-
values.entrypoint ??= [];
2831

2932
if (values.help || positionals.length === 0) {
3033
usage();
@@ -46,21 +49,26 @@ if (
4649
process.exit(1);
4750
}
4851

52+
const config = values.config ? JSON.parse(await readFile(values.config, "utf-8")) : {};
53+
const dev = values.dev ?? config.dev;
54+
4955
const currentDirectoryUrl = pathToFileURL(`${process.cwd()}/`);
5056
await writeImportmaps({
51-
directoryUrl: new URL(values.dir || ".", currentDirectoryUrl),
57+
directoryUrl: new URL(values.dir ?? config.dir ?? ".", currentDirectoryUrl),
5258
importmaps: {
5359
[outfile]: {
5460
nodeMappings: {
55-
devDependencies: values.dev,
56-
packageUserConditions: values.dev ? ["development"] : [],
61+
devDependencies: dev,
62+
packageUserConditions: dev ? ["development"] : [],
5763
},
5864
importResolution: {
59-
entryPoints: values.entryPoints,
65+
entryPoints: values.entrypoint ?? config.entryPoints ?? [],
6066
},
61-
keepUnusedMappings: values["keep-unused"],
67+
keepUnusedMappings: values["keep-unused"] ?? config.keepUnused,
68+
manualImportmap: config.manualImportmap,
6269
},
6370
},
71+
packagesManualOverrides: config.packagesManualOverrides,
6472
});
6573

6674
function usage() {
@@ -76,6 +84,7 @@ Options:
7684
--entrypoint file.js Confirm the specified file and its transitive dependencies can be resolved using the generated import map. Can be specified multiple times.
7785
--dev Include devDependencies from package.json and pick "developement" in package conditions.
7886
--keep-unused Remove mappings not used by any entrypoint or their transitive dependencies. Requires --entrypoint.
87+
--config config.json Read additional settings from the given JSON configuration file.
7988
8089
For more advanced options, see the API.`);
8190
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ replaceFileStructureSync({
99
from: import.meta.resolve("./fixtures/"),
1010
to: import.meta.resolve("./git_ignored/"),
1111
});
12-
execSync("node ../../../src/cli.mjs ./index.html", {
12+
execSync("node ../../../../src/cli.mjs ./index.html", {
1313
cwd: new URL(import.meta.resolve("./git_ignored/")),
1414
});
1515
copyFileSync({
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)