Skip to content

Commit

Permalink
Merge branch 'working-dir'
Browse files Browse the repository at this point in the history
This is a squashed version of #29
  • Loading branch information
cdepillabout committed Oct 25, 2024
2 parents 0cf4b4e + 66d835c commit 37dd913
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
31 changes: 31 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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

Expand Down Expand Up @@ -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");
}
}
Expand Down
31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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

Expand Down Expand Up @@ -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");
}
}
Expand Down

0 comments on commit 37dd913

Please sign in to comment.