Skip to content

Commit c8c5783

Browse files
committed
Add support for --config and --fix parameters, begin deprecating corresponding dedicated entry points.
1 parent 6b4507d commit c8c5783

12 files changed

+2064
-144
lines changed

README.md

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ As a [GitHub Action][github-action] via
6767
markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)
6868
https://github.com/DavidAnson/markdownlint-cli2
6969
70-
Syntax: markdownlint-cli2 glob0 [glob1] [...] [globN]
71-
markdownlint-cli2-fix glob0 [glob1] [...] [globN]
72-
markdownlint-cli2-config config-file glob0 [glob1] [...] [globN]
70+
Syntax: markdownlint-cli2 glob0 [glob1] [...] [globN] [--config file] [--fix]
7371
7472
Glob expressions (from the globby library):
7573
- * matches any number of characters, but not /
@@ -84,6 +82,10 @@ Dot-only glob:
8482
- Instead, it is mapped to "markdownlint-cli2 *.{md,markdown}" which lints all Markdown files in the current directory
8583
- To lint every file in the current directory tree, the command "markdownlint-cli2 **" can be used instead
8684
85+
Optional parameters:
86+
- --config specifies the path to a configuration file to define the base configuration
87+
- --fix updates files to resolve fixable issues (can be overridden in configuration)
88+
8789
Configuration via:
8890
- .markdownlint-cli2.jsonc
8991
- .markdownlint-cli2.yaml
@@ -116,28 +118,47 @@ markdownlint-cli2 "**/*.md" "#node_modules"
116118
```
117119

118120
Because sharing the same configuration between "normal" and "fix" modes is
119-
common, the following command defaults the `fix` property (see below) to `true`:
121+
common, the `--fix` argument can be used to default the `fix` property (see
122+
below) to `true` (though it can still be overridden by a configuration file):
120123

121124
```bash
122-
markdownlint-cli2-fix "**/*.md" "#node_modules"
125+
markdownlint-cli2 --fix "**/*.md" "#node_modules"
123126
```
124127

125-
Other than the default behavior of the `fix` property (which can be overridden),
126-
these two commands behave identically.
128+
> **Deprecated**
129+
>
130+
> The following command behaves similarly, defaulting the `fix` property to
131+
> `true`:
132+
>
133+
> ```bash
134+
> markdownlint-cli2-fix "**/*.md" "#node_modules"
135+
> ```
136+
>
137+
> Otherwise, `markdownlint-cli2-fix` behaves the same as `markdownlint-cli2`.
127138

128139
In cases where it is not convenient to store a configuration file in the root
129-
of a project, the `markdownlint-cli2-config` command can be used. This command
130-
accepts a path to any supported configuration file as its first argument:
140+
of a project, the `--config` argument can be used to provide a path to any
141+
supported configuration file:
131142

132143
```bash
133-
markdownlint-cli2-config "config/.markdownlint-cli2.jsonc" "**/*.md" "#node_modules"
144+
markdownlint-cli2 --config "config/.markdownlint-cli2.jsonc" "**/*.md" "#node_modules"
134145
```
135146

136-
The configuration file name must be (or end with) one of the supported types
147+
The configuration file name must be (or end with) one of the supported names
137148
above. For example, `.markdownlint.json` or `example.markdownlint-cli2.jsonc`.
138149
The specified configuration file will be loaded, parsed, and applied as a base
139150
configuration for the current directory - which will then be handled normally.
140-
Otherwise, this command behaves identically to `markdownlint-cli2`.
151+
152+
> **Deprecated**
153+
>
154+
> The following command behaves similarly, accepting a base configuration file
155+
> path as its first parameter:
156+
>
157+
> ```bash
158+
> markdownlint-cli2-config "config/.markdownlint-cli2.jsonc" "**/*.md" "#node_modules"
159+
> ```
160+
>
161+
> Otherwise, `markdownlint-cli2-config` behaves the same as `markdownlint-cli2`.
141162

142163
### Container Image
143164

@@ -165,12 +186,14 @@ Notes:
165186
docker run -w /myfolder -v $PWD:/myfolder davidanson/markdownlint-cli2:v0.7.1 "**/*.md" "#node_modules"
166187
```
167188

168-
To invoke the `markdownlint-cli2-config` or `markdownlint-cli2-fix` commands
169-
instead, use Docker's `--entrypoint` flag:
170-
171-
```bash
172-
docker run -v $PWD:/workdir --entrypoint="markdownlint-cli2-fix" davidanson/markdownlint-cli2:v0.7.1 "**/*.md" "#node_modules"
173-
```
189+
> **Deprecated**
190+
>
191+
> To invoke the `markdownlint-cli2-config` or `markdownlint-cli2-fix` commands
192+
> instead, use Docker's `--entrypoint` flag:
193+
>
194+
> ```bash
195+
> docker run -v $PWD:/workdir --entrypoint="markdownlint-cli2-fix" davidanson/markdownlint-cli2:v0.7.1 "**/*.md" "#node_modules"
196+
> ```
174197

175198
For convenience, the container image
176199
[`davidanson/markdownlint-cli2-rules`][docker-hub-markdownlint-cli2-rules]

markdownlint-cli2-config.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
const { run } = require("./markdownlint-cli2");
88

9-
run({
10-
"name": "markdownlint-cli2-config"
11-
});
9+
run(
10+
{
11+
"name": "markdownlint-cli2-config"
12+
},
13+
[ "--config" ]
14+
);

markdownlint-cli2-fix.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
const { run } = require("./markdownlint-cli2");
88

9-
run({
10-
"fixDefault": true,
11-
"name": "markdownlint-cli2-fix"
12-
});
9+
run(
10+
{
11+
"name": "markdownlint-cli2-fix"
12+
},
13+
[ "--fix" ]
14+
);

markdownlint-cli2.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ const removeIgnoredFiles = (dir, files, ignores) => {
181181

182182
// Process/normalize command-line arguments and return glob patterns
183183
const processArgv = (argv) => {
184-
const globPatterns = (argv || []).map(
184+
const globPatterns = argv.map(
185185
(glob) => {
186186
if (glob.startsWith(":")) {
187187
return glob;
@@ -208,9 +208,7 @@ const showHelp = (logMessage) => {
208208
/* eslint-disable max-len */
209209
logMessage(`https://github.com/DavidAnson/markdownlint-cli2
210210
211-
Syntax: markdownlint-cli2 glob0 [glob1] [...] [globN]
212-
markdownlint-cli2-fix glob0 [glob1] [...] [globN]
213-
markdownlint-cli2-config config-file glob0 [glob1] [...] [globN]
211+
Syntax: markdownlint-cli2 glob0 [glob1] [...] [globN] [--config file] [--fix]
214212
215213
Glob expressions (from the globby library):
216214
- * matches any number of characters, but not /
@@ -225,6 +223,10 @@ Dot-only glob:
225223
- Instead, it is mapped to "markdownlint-cli2 ${dotOnlySubstitute}" which lints all Markdown files in the current directory
226224
- To lint every file in the current directory tree, the command "markdownlint-cli2 **" can be used instead
227225
226+
Optional parameters:
227+
- --config specifies the path to a configuration file to define the base configuration
228+
- --fix updates files to resolve fixable issues (can be overridden in configuration)
229+
228230
Configuration via:
229231
- .markdownlint-cli2.jsonc
230232
- .markdownlint-cli2.yaml
@@ -818,7 +820,6 @@ const main = async (params) => {
818820
argv,
819821
optionsDefault,
820822
optionsOverride,
821-
fixDefault,
822823
fileContents,
823824
nonFileContents,
824825
noErrors,
@@ -838,17 +839,33 @@ const main = async (params) => {
838839
// eslint-disable-next-line max-len
839840
`${name || packageName} v${packageVersion} (${libraryName} v${libraryVersion})`
840841
);
842+
// Merge and process args/argv
843+
let fixDefault = false;
844+
// eslint-disable-next-line unicorn/no-useless-undefined
845+
let configPath = undefined;
846+
const argvFiltered = (argv || []).filter((arg) => {
847+
if (configPath === null) {
848+
configPath = arg;
849+
return false;
850+
} else if (arg === "--config") {
851+
configPath = null;
852+
return false;
853+
} else if (arg === "--fix") {
854+
fixDefault = true;
855+
return false;
856+
}
857+
return true;
858+
});
841859
// Read argv configuration file (if relevant and present)
842860
let optionsArgv = null;
843861
let relativeDir = null;
844-
const [ configPath ] = (argv || []);
845-
if ((name === "markdownlint-cli2-config") && configPath) {
862+
if (configPath) {
846863
optionsArgv =
847864
await readOptionsOrConfig(configPath, fs, noRequire);
848865
relativeDir = path.dirname(configPath);
849866
}
850867
// Process arguments and get base options
851-
const globPatterns = processArgv(optionsArgv ? argv.slice(1) : argv);
868+
const globPatterns = processArgv(argvFiltered);
852869
const { baseMarkdownlintOptions, dirToDirInfo } =
853870
await getBaseOptions(
854871
fs,
@@ -921,11 +938,13 @@ const main = async (params) => {
921938
};
922939

923940
// Run function
924-
const run = (overrides) => {
941+
const run = (overrides, args) => {
925942
(async () => {
943+
const argsAndArgv = args || [];
944+
appendToArray(argsAndArgv, process.argv.slice(2));
926945
try {
927946
const defaultParams = {
928-
"argv": process.argv.slice(2),
947+
"argv": argsAndArgv,
929948
"logMessage": console.log,
930949
"logError": console.error
931950
};

0 commit comments

Comments
 (0)