Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/issue-1398: added support for custom configuration #1401

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

RohitLuthra19
Copy link

@RohitLuthra19 RohitLuthra19 commented Mar 25, 2024

Fixture for #1398

By proposed changes user will get the opportunity to control/handle the execution of custom task, As you can see in following example .lintstagedrc.js.

async function doFoo(files) {
  const { execa } = await import("execa");
  const result = await execa("eslint", [...files],
  {
    preferLocal: true,
    reject: false,
    shell: false,
    stdin: 'ignore'
  });
  if (result.failed || result.killed || result.signal != null) {
    throw result
  }
}

module.exports = {
  "**/*.{js,jsx}": (filenames) => {
    return {
      title: "Running a custom task",
      command: "Running a custom task",
      task: async () =>  {
        console.time()
        await doFoo(filenames)
        console.timeEnd()
      }
    }
  },
};

Following will be the output in case of no-console eslint error

image

Copy link

changeset-bot bot commented Mar 25, 2024

⚠️ No Changeset found

Latest commit: 20c3f86

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@RohitLuthra19 RohitLuthra19 changed the title WIP: feat/issue-1398: added support for custom configuration feat/issue-1398: added support for custom configuration Mar 25, 2024
'Function task should return a string or an array of strings',
resolved
if (isFn && command && typeof command === 'object') {
if (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we flip the condition and do an early return?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

})
} else {
throw new Error(
configurationError('[Function]', 'Function task should be in proper format', resolved)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we provide a meaningful error message on what specifically was the problem?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
cmdTasks.push({
title: command.title,
command: command.command,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need both the command and title?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we only need title, so removed the command field.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has started to become a bit complex... I wonder if we could split it somehow, maybe different handling for function and regular config.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@iiroj
Copy link
Member

iiroj commented Mar 28, 2024

We should add both unit and integration tests for this.

@RohitLuthra19 RohitLuthra19 requested a review from iiroj April 3, 2024 07:39
@iiroj
Copy link
Member

iiroj commented Apr 4, 2024

Hey, please don't add versioning commits directly to the PR! It's automated in the GitHub Actions! I'm still thinking about this feature, there's no rush to get this in.

@RohitLuthra19
Copy link
Author

iiroj I have incorporated all the review comments, you can proceed with the review/approval.

@iiroj
Copy link
Member

iiroj commented Apr 6, 2024

While I appreciate your contribution, I don't think this feature is yet ready to be shipped with lint-staged and I want to spend some time thinking about it. For now I'll leave this PR open and use it as a base when I'm satisfied with the solution.

I also want to think about deprecating the --shell mode because it has security issues and using this method is more powerful anyway.

@iiroj
Copy link
Member

iiroj commented Apr 28, 2024

After thinking about this, I like the feature but also realized that we can probably simplify the config by one level without it being a breaking change:

export default {
    "**/*.{js,jsx}": {
        title: "Running a custom task",
        task: async (filenames) =>  {
            console.time()
            await doFoo(filenames)
            console.timeEnd()
        }
    },
}

With this method the config looks like:

type Task = string

type CreateTask = (filenames: string[]) => Task | Task[] | Promise<Task | Task[]>

type FunctionTask = {
  title: string
  task: (filenames: string) => void | Promise<void>
}

type Configuration = Record<string, Task | Task[] | CreateTask | FunctionTask>

Unless I'm mistaken... what do you think? After this change we should just add some integration tests with a real config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants