Skip to content

[Help/Question] How to add scope dynamically? #1996

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

Open
rafaell-lycan opened this issue Jul 9, 2020 · 3 comments
Open

[Help/Question] How to add scope dynamically? #1996

rafaell-lycan opened this issue Jul 9, 2020 · 3 comments
Labels

Comments

@rafaell-lycan
Copy link

Expected Behavior

When creating a commit be able to identify from where (packages) the files are coming from:

$ git commit -m "feat: adding a new DB adapter for X" # (on packages A)
$ git commit -m "fix: this will solve a problem on the Service Z" # (on packages B)
$ git commit -m "chore: update eslint-config-custom to version 0.4.2" # (on packages A, B, C)

And we format the message given the package/scope they changes are coming from:

ccc64c55  "feat(A): adding a new database adapter for X"
42bc938a  "fix(B): this will solve a problem on the Service Z"
9f4fe8d3  "chore: update eslint-config-custom to version 0.4.2"

Current Behavior

When writing a commit we're obligated to define the scope of the package, and to avoid forgetting it we add the rule 'scope-empty': [2, 'never'] to enforce this definition when generating our CHANGELOG file.

The only way now is by always adding the (scope) manually as we couldn't find an easy way to adding it dynamically by detecting from which package the affected files are coming from.

There's an approach we can follow by reading the commit message/title and the staged files?

Current config

commitlint.config.js
const { getPackages } = require('@commitlint/config-lerna-scopes').utils;

module.exports = {
  extends: [
    '@commitlint/config-conventional',
    '@commitlint/config-lerna-scopes',
  ],
  rules: {
    'scope-case': [2, 'always', ['lower-case', 'pascal-case', 'camel-case']],
    'scope-enum': async (ctx) => {
      const pkgs = await getPackages(ctx);
      return [2, 'always', [...pkgs, 'deps', 'release']]
    },
    'scope-empty': [2, 'never'],
    'header-max-length': [2, 'always', 100],
  },
};
@escapedcat
Copy link
Member

Just to clarify. When you say packages you mean a monorepo with lerna or something else?

@rafaell-lycan
Copy link
Author

Exactly @escapedcat, as you can see in the configuration. Furthermore, we're trying to write our own config to see if we can manage to archive it and open-source it later, but no success so far.

@jdbruijn
Copy link
Contributor

I thought you might benefit from my plugin commitlint-plugin-function-rules, but after looking at your issue further I don't think that would work as you want o change the commit message if I understand correctly. Changing the commit message from a rule is currently not possible as far as I know.

You can use it to get the committed files by calling something like git diff --staged --name-only from the function but then you'd need some way to inject it into the commit message, which I don't know. You can, however, use something like this with the function rule to verify that the added scope matches the committed files and create a rule for that.

Anyhow, hope this helps any!

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

No branches or pull requests

3 participants