diff --git a/.github/workflows/action.ts b/.github/workflows/action.ts index ae99305..8ae62c0 100755 --- a/.github/workflows/action.ts +++ b/.github/workflows/action.ts @@ -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, @@ -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({ @@ -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"`, diff --git a/.github/workflows/ci.generated.yml b/.github/workflows/ci.generated.yml index 30fc0c8..bb49ffb 100644 --- a/.github/workflows/ci.generated.yml +++ b/.github/workflows/ci.generated.yml @@ -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" diff --git a/.github/workflows/ci.ts b/.github/workflows/ci.ts index 1f9ebca..b35f54e 100755 --- a/.github/workflows/ci.ts +++ b/.github/workflows/ci.ts @@ -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 }, @@ -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: [ diff --git a/README.md b/README.md index a61a17f..2d27954 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/action.yml b/action.yml index d0d93f0..da809ef 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -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 @@ -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" diff --git a/tests/working-dir/dprint.json b/tests/working-dir/dprint.json new file mode 100644 index 0000000..aa68796 --- /dev/null +++ b/tests/working-dir/dprint.json @@ -0,0 +1,5 @@ +{ + "plugins": [ + "npm:@dprint/json@0.23.0" + ] +} diff --git a/tests/working-dir/formatted.json b/tests/working-dir/formatted.json new file mode 100644 index 0000000..9ca89ff --- /dev/null +++ b/tests/working-dir/formatted.json @@ -0,0 +1,3 @@ +{ + "formatted": true +}