Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .github/workflows/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const inputs = defineInputs({
required: false,
default: "",
},
"working-directory": {
description: "Directory to run dprint check in, relative to the workspace (ex. packages/app)",
required: false,
default: "",
},
cache: {
description: "Cache dprint's plugins and incremental state in the GitHub Actions cache (ex. true)",
required: false,
Expand Down Expand Up @@ -217,9 +222,15 @@ const install = step({
// the repo when dprint discovers the config itself (a remote config url can't
// be hashed, so that falls back to the repo's config files too)
const configPath = inputs["config-path"];
const workingDirectory = inputs["working-directory"];
// the config path is relative to the working directory, but hashFiles is
// relative to the workspace
const configPathInWorkspace = workingDirectory.notEquals("")
.then(concat(workingDirectory, "/", configPath))
.else(configPath);
const configHash = configPath.notEquals("")
.and(configPath.startsWith("http").not())
.then(hashFiles(configPath))
.then(hashFiles(configPathInWorkspace))
.else(hashFiles("**/dprint.json", "**/dprint.jsonc", "**/.dprint.json", "**/.dprint.jsonc"));

const prepareCache = step({
Expand Down Expand Up @@ -303,11 +314,13 @@ const check = step({
name: "Check formatting",
id: "check",
env: {
WORKING_DIRECTORY: workingDirectory,
CONFIG_PATH: configPath,
ANNOTATIONS: inputs.annotations,
ANNOTATE_SCRIPT: concat(expr("github.action_path"), "/scripts/annotate.mjs"),
},
run: [
`cd "\${WORKING_DIRECTORY:-.}"`,
`args=(\${CONFIG_PATH:+--config "$CONFIG_PATH"} ${inputs.args})`,
`if command -v node > /dev/null; then`,
` output="$RUNNER_TEMP/dprint-check.jsonl"`,
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/ci.generated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ jobs:
uses: ./
with:
args: "--excludes poorly-formatted.json"
- name: Check formatting in a working directory
id: working-directory
uses: ./
with:
working-directory: tests/working-dir
config-path: dprint.json
cache: true
- name: Verify the working directory was checked
env:
UNFORMATTED_COUNT: "${{ steps.working-directory.outputs.unformatted-count }}"
run: test "$UNFORMATTED_COUNT" = "0"
- name: Later steps can run dprint with the same cache
run: |-
test -n "$DPRINT_CACHE_DIR"
Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ const unattestedVersionCheck = step({
outputs: ["dprint-version"] as const,
});

const workingDirectoryCheck = step({
name: "Check formatting in a working directory",
id: "working-directory",
uses: "./",
with: { "working-directory": "tests/working-dir", "config-path": "dprint.json", cache: true },
outputs: ["unformatted-count"] as const,
});

const styleJob = job("style", {
runsOn: matrix.os,
strategy: { matrix },
Expand Down Expand Up @@ -110,6 +118,14 @@ const styleJob = job("style", {
uses: "./",
with: { args: "--excludes poorly-formatted.json" },
},
// the poorly-formatted file at the root would fail this if the check
// didn't run in the directory, and the config path is relative to it
workingDirectoryCheck,
{
name: "Verify the working directory was checked",
env: { UNFORMATTED_COUNT: workingDirectoryCheck.outputs["unformatted-count"] },
run: `test "$UNFORMATTED_COUNT" = "0"`,
},
{
name: "Later steps can run dprint with the same cache",
run: [
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ To use a specific config, specify that with the `config-path` input:
config-path: dprint-ci.json
```

### Working Directory

To run `dprint check` in a subdirectory, such as a package in a monorepo, specify that with the `working-directory` input:

```yml
- uses: dprint/check@v2
with:
working-directory: packages/app
```

The `config-path` and `args` inputs are relative to the working directory, while the annotations and the `unformatted-files` output are relative to the workspace.

### Args

To pass additional arguments to `dprint check`, pass them to the `args` input.
Expand Down
8 changes: 7 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ inputs:
description: Additional arguments to pass to dprint check
required: false
default: ""
working-directory:
description: "Directory to run dprint check in, relative to the workspace (ex. packages/app)"
required: false
default: ""
cache:
description: Cache dprint's plugins and incremental state in the GitHub Actions cache (ex. true)
required: false
Expand Down Expand Up @@ -189,7 +193,7 @@ runs:
if: inputs.cache == 'true'
shell: bash
env:
CONFIG_HASH: '${{ inputs.config-path != '''' && !startsWith(inputs.config-path, ''http'') && hashFiles(inputs.config-path) || hashFiles(''**/dprint.json'', ''**/dprint.jsonc'', ''**/.dprint.json'', ''**/.dprint.jsonc'') }}'
CONFIG_HASH: '${{ inputs.config-path != '''' && !startsWith(inputs.config-path, ''http'') && (hashFiles(inputs.working-directory != '''' && format(''{0}/{1}'', inputs.working-directory, inputs.config-path) || inputs.config-path)) || hashFiles(''**/dprint.json'', ''**/dprint.jsonc'', ''**/.dprint.json'', ''**/.dprint.jsonc'') }}'
run: |-
# respect a directory the user already set so later steps share the same cache
if [ -z "${DPRINT_CACHE_DIR:-}" ]; then
Expand Down Expand Up @@ -237,10 +241,12 @@ runs:
id: check
shell: bash
env:
WORKING_DIRECTORY: "${{ inputs.working-directory }}"
CONFIG_PATH: "${{ inputs.config-path }}"
ANNOTATIONS: "${{ inputs.annotations }}"
ANNOTATE_SCRIPT: "${{ github.action_path }}/scripts/annotate.mjs"
run: |-
cd "${WORKING_DIRECTORY:-.}"
args=(${CONFIG_PATH:+--config "$CONFIG_PATH"} ${{ inputs.args }})
if command -v node > /dev/null; then
output="$RUNNER_TEMP/dprint-check.jsonl"
Expand Down
5 changes: 5 additions & 0 deletions tests/working-dir/dprint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
"npm:@dprint/json@0.23.0"
]
}
3 changes: 3 additions & 0 deletions tests/working-dir/formatted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"formatted": true
}
Loading