commitlint icon indicating copy to clipboard operation
commitlint copied to clipboard

Add projectType filtering to @commitlint/config-nx-scopes

Open jaytavares opened this issue 3 years ago • 2 comments

It would be helpful if we could filter the scopes provided by @commitlint/config-nx-scopes by project type (e.g. only load projects of type 'application')

Expected Behavior

Here's an example of how this could work (implementation below):

.commitlintrc.js:

const {
  utils: { getProjects }
} = require('@commitlint/config-nx-scopes')

module.exports = {
  rules: {
    'scope-enum': async ctx => [
      2,
      'always',
      [...(await getProjects(ctx, 'application'))]  // ⬅ addition of projectType parameter
    ]
  }
}

Affected packages

  • config-nx-scopes

Possible Solution

Implementation of the above example:

@commitlint/config-nx-scopes/index.js:

const {Workspaces} = require('@nrwl/tao/src/shared/workspace');

module.exports = {
	utils: {getProjects},
	rules: {
		'scope-enum': (ctx) =>
			getProjects(ctx).then((packages) => [2, 'always', packages]),
	},
};

function getProjects(context, projectType) {
	return Promise.resolve()
		.then(() => {
			const ctx = context || {};
			const cwd = ctx.cwd || process.cwd();
			const ws = new Workspaces(cwd);
			const workspace = ws.readWorkspaceConfiguration();
			return Object.entries(workspace.projects || {}).map(
				([name, project]) => ({
					name,
					...project,
				})
			);
		})
		.then((projects) => {
			return projects
				.filter((project) => projectType ? project.projectType === projectType : true)
				.filter((project) => project.targets)
				.map((project) => project.name)
				.map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name));
		});
}

Context

We have a lot of libraries in our Nx workspace. I don't see us gaining much in return for the burden of having so many options for scope at commit time.

jaytavares avatar Apr 28 '22 21:04 jaytavares

@jaytavares happy for a PR if you have time and motivation

escapedcat avatar Apr 29 '22 01:04 escapedcat

@escapedcat Done! ✅

jaytavares avatar Apr 29 '22 17:04 jaytavares