From 66d835c78943f6d549927fa32669cfe932dcb37a Mon Sep 17 00:00:00 2001 From: Jake Keuhlen Date: Wed, 23 Oct 2024 08:18:48 -0600 Subject: [PATCH] Add support for working directory --- action.yml | 4 ++++ dist/index.js | 31 +++++++++++++++++++++++++++++++ index.js | 31 +++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/action.yml b/action.yml index 4b5b010..c565cad 100644 --- a/action.yml +++ b/action.yml @@ -38,6 +38,10 @@ inputs: necessarily backwards-compatible. You are recommended to specify the version of fourmolu you wish to use. default: latest + working-directory: + description: > + Directory to run Fourmolu. + required: false runs: using: 'node20' main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index c335b1d..a9b86ab 100644 --- a/dist/index.js +++ b/dist/index.js @@ -33000,6 +33000,7 @@ const input_version = core.getInput('version'); const input_pattern = core.getInput('pattern'); const input_follow_symbolic_links = core.getInput('follow-symbolic-links').toUpperCase() !== 'FALSE'; const input_extra_args = core.getInput('extra-args'); +const input_working_directory = core.getInput('working-directory'); async function run() { @@ -33011,7 +33012,26 @@ async function run() { // run on some systems. const fourmolu_linux_url = `https://github.com/fourmolu/fourmolu/releases/download/v${fourmolu_version}/fourmolu-${fourmolu_version}-linux-x86_64`; + // Declare originalCwd outside the try block so it's accessible in catch below for error handling + let originalCwd = undefined; + try { + // Set working directory if specified + if (input_working_directory) { + originalCwd = process.cwd(); + + const absoluteWorkingDir = path__WEBPACK_IMPORTED_MODULE_0__.resolve(input_working_directory); + core.info(`Attempting to change to directory: ${absoluteWorkingDir}`); + + if (!fs__WEBPACK_IMPORTED_MODULE_1__.existsSync(absoluteWorkingDir)) { + core.setFailed(`Working directory '${absoluteWorkingDir}' does not exist`); + return; + } + + process.chdir(absoluteWorkingDir); + const newCwd = process.cwd(); + core.info(`Changed working directory to: ${newCwd}`); + } // Download fourmolu binary @@ -33084,7 +33104,18 @@ async function run() { core.warning("The glob patterns did not match any source files"); } + // Restore original working directory if it was changed + if (originalCwd) { + process.chdir(originalCwd); + core.info(`Restored working directory to: ${originalCwd}`); + } + } catch (error) { + // Restore original working directory even if there was an error + if (originalCwd) { + process.chdir(originalCwd); + core.info(`Restored working directory to: ${originalCwd}`); + } core.setFailed("fourmolu detected unformatted files"); } } diff --git a/index.js b/index.js index 2009a93..5316692 100644 --- a/index.js +++ b/index.js @@ -47,6 +47,7 @@ const input_version = core.getInput('version'); const input_pattern = core.getInput('pattern'); const input_follow_symbolic_links = core.getInput('follow-symbolic-links').toUpperCase() !== 'FALSE'; const input_extra_args = core.getInput('extra-args'); +const input_working_directory = core.getInput('working-directory'); async function run() { @@ -58,7 +59,26 @@ async function run() { // run on some systems. const fourmolu_linux_url = `https://github.com/fourmolu/fourmolu/releases/download/v${fourmolu_version}/fourmolu-${fourmolu_version}-linux-x86_64`; + // Declare originalCwd outside the try block so it's accessible in catch below for error handling + let originalCwd = undefined; + try { + // Set working directory if specified + if (input_working_directory) { + originalCwd = process.cwd(); + + const absoluteWorkingDir = path.resolve(input_working_directory); + core.info(`Attempting to change to directory: ${absoluteWorkingDir}`); + + if (!fs.existsSync(absoluteWorkingDir)) { + core.setFailed(`Working directory '${absoluteWorkingDir}' does not exist`); + return; + } + + process.chdir(absoluteWorkingDir); + const newCwd = process.cwd(); + core.info(`Changed working directory to: ${newCwd}`); + } // Download fourmolu binary @@ -131,7 +151,18 @@ async function run() { core.warning("The glob patterns did not match any source files"); } + // Restore original working directory if it was changed + if (originalCwd) { + process.chdir(originalCwd); + core.info(`Restored working directory to: ${originalCwd}`); + } + } catch (error) { + // Restore original working directory even if there was an error + if (originalCwd) { + process.chdir(originalCwd); + core.info(`Restored working directory to: ${originalCwd}`); + } core.setFailed("fourmolu detected unformatted files"); } }