-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcommitlint.config.ts
More file actions
50 lines (43 loc) · 1.95 KB
/
Copy pathcommitlint.config.ts
File metadata and controls
50 lines (43 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/// <reference types="node" />
import fs from 'node:fs';
import path from 'node:path';
import yaml from 'js-yaml';
const WORKSPACE_NAME_PREFIX = '@opendatacapture/';
const projectRoot = import.meta.dirname;
function readWorkspaceGlobs(): string[] {
const workspace: unknown = yaml.load(fs.readFileSync(path.join(projectRoot, 'pnpm-workspace.yaml'), 'utf8'));
if (typeof workspace !== 'object' || workspace === null || !('packages' in workspace)) {
throw new Error("pnpm-workspace.yaml has no 'packages' field");
}
const { packages } = workspace;
if (!Array.isArray(packages) || !packages.every((glob) => typeof glob === 'string')) {
throw new Error("pnpm-workspace.yaml 'packages' must be a list of globs");
}
return packages;
}
// The vendor wrappers are workspaces too, but their names (react__19.x and the like) are not
// meaningful areas of the codebase, so they are excluded from the scope list on purpose.
function readWorkspaceScopes(): string[] {
const globs = readWorkspaceGlobs().filter((glob) => !glob.startsWith('vendor/'));
const manifests = fs.globSync(
globs.map((glob) => path.posix.join(glob, 'package.json')),
{ cwd: projectRoot }
);
const names = manifests.map((manifestPath) => {
const manifest: unknown = JSON.parse(fs.readFileSync(path.join(projectRoot, manifestPath), 'utf8'));
const name = typeof manifest === 'object' && manifest !== null && 'name' in manifest ? manifest.name : null;
return typeof name === 'string' ? name : '';
});
return names
.filter((name) => name.startsWith(WORKSPACE_NAME_PREFIX))
.map((name) => name.slice(WORKSPACE_NAME_PREFIX.length))
.sort();
}
export default {
extends: ['@commitlint/config-conventional'],
rules: {
// A warning while contributors adopt the workspace names as scopes. Raising this to an error
// is tracked in https://github.com/DouglasNeuroInformatics/OpenDataCapture/issues/1529
'scope-enum': [1, 'always', readWorkspaceScopes()]
}
};