Skip to content

Commit

Permalink
Add "command" input to allow invoking -fix and -config commands (fixes
Browse files Browse the repository at this point in the history
…#7, fixes #8).
  • Loading branch information
DavidAnson committed Jul 22, 2022
1 parent 4309653 commit 0820d56
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 12 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,38 @@ jobs:
globs: |
*.md
test/*
command-config:
name: Command = config (test/errors.md, errors)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
with:
command: config
globs: |
config/.markdownlint.jsonc
test/*
command-config-missing:
name: Command = config (missing configuration file)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
with:
command: config
command-fix:
name: Command = fix (test/errors.md, no errors)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
with:
command: fix
command-unsupported:
name: Command = unsupported (fails)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
with:
command: unsupported
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ information.

## Inputs

### command (optional)

Command to run (unset, `fix`, or `config`)

If unspecified or `""`, the `markdownlint-cli2` command is run.

If set to `fix`, the `markdownlint-cli2-fix` command is run and supported
issues will be fixed automatically.

If set to `config`, the `markdownlint-cli2-config` command is run and the
first element of `globs` should specify a supported configuration file.

For more detail: [command line documentation for `markdownlint-cli2`][command-line].

### globs (optional)

Glob expression(s) of files to lint (newline-delimited)

The default `*.{md,markdown}` lints all Markdown files in the base directory
of a project.

For more detail, read about [glob syntax in `markdownlint-cli2`][glob-syntax].
For more detail: [glob syntax in `markdownlint-cli2`][glob-syntax].

## Outputs

Expand Down Expand Up @@ -63,6 +77,7 @@ To prevent linting issues from failing the workflow run:
See [`example.yml`][example-yml] for a simple GitHub workflow that uses
`markdownlint-cli2-action`.

[command-line]: https://github.com/DavidAnson/markdownlint-cli2#command-line
[commonmark]: https://commonmark.org/
[example-yml]: .github/workflows/example.yml
[glob-syntax]: https://github.com/DavidAnson/markdownlint-cli2#use
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ branding:
icon: 'check-square'
color: 'orange'
inputs:
command:
description: 'Command to run (unset, "fix", or "config")'
default: ''
required: false
globs:
description: 'Glob expression(s) of files to lint (newline-delimited)'
default: '*.{md,markdown}'
Expand Down
3 changes: 3 additions & 0 deletions config/.markdownlint.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"single-trailing-newline": false
}
32 changes: 27 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32158,14 +32158,36 @@ const argv =
split("\n").
filter(String);

markdownlintCli2({
const parameters = {
argv,
logMessage,
logError
}).then(
(code) => code && core.setFailed(`Failed with exit code: ${code}`),
(error) => core.setFailed(`Failed due to error: ${error}`)
);
};
let invoke = true;
const command = core.getInput("command");
switch (command) {
case "":
// Default behavior
break;
case "config":
parameters.name = "markdownlint-cli2-config";
break;
case "fix":
parameters.name = "markdownlint-cli2-fix";
parameters.fixDefault = true;
break;
default:
core.setFailed(`Unsupported command: ${command}`);
invoke = false;
break;
}

if (invoke) {
markdownlintCli2(parameters).then(
(code) => code && core.setFailed(`Failed with exit code: ${code}`),
(error) => core.setFailed(`Failed due to error: ${error}`)
);
}

})();

Expand Down
32 changes: 27 additions & 5 deletions markdownlint-cli2-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,33 @@ const argv =
split("\n").
filter(String);

markdownlintCli2({
const parameters = {
argv,
logMessage,
logError
}).then(
(code) => code && core.setFailed(`Failed with exit code: ${code}`),
(error) => core.setFailed(`Failed due to error: ${error}`)
);
};
let invoke = true;
const command = core.getInput("command");
switch (command) {
case "":
// Default behavior
break;
case "config":
parameters.name = "markdownlint-cli2-config";
break;
case "fix":
parameters.name = "markdownlint-cli2-fix";
parameters.fixDefault = true;
break;
default:
core.setFailed(`Unsupported command: ${command}`);
invoke = false;
break;
}

if (invoke) {
markdownlintCli2(parameters).then(
(code) => code && core.setFailed(`Failed with exit code: ${code}`),
(error) => core.setFailed(`Failed due to error: ${error}`)
);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"build": "ncc build markdownlint-cli2-action.js",
"docker-npm-install": "docker run --rm --tty --name npm-install --volume $PWD:/home/workdir --workdir /home/workdir --user node node:16 npm install",
"docker-npm-run-upgrade": "docker run --rm --tty --name npm-run-upgrade --volume $PWD:/home/workdir --workdir /home/workdir --user node node:16 npm run upgrade",
"lint": "eslint *.js",
"lint": "eslint *.js && markdownlint-cli2 *.md",
"test": "npm run lint && npm run build && git diff --exit-code",
"upgrade": "npx --yes npm-check-updates --upgrade"
},
Expand Down

0 comments on commit 0820d56

Please sign in to comment.