Skip to content

Commit 1055feb

Browse files
committed
feat: migrate to pure ESM
1 parent 095d3ed commit 1055feb

File tree

201 files changed

+2168
-2074
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

201 files changed

+2168
-2074
lines changed
File renamed without changes.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('@commitlint/config-angular');
1+
export {default} from '@commitlint/config-angular';

@alias/commitlint-config-angular/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "commitlint-config-angular",
3+
"type": "module",
34
"version": "18.4.4",
45
"description": "Shareable commitlint config enforcing the angular commit convention",
56
"files": [
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('@commitlint/config-lerna-scopes');
1+
export {default} from '@commitlint/config-lerna-scopes';

@alias/commitlint-config-lerna-scopes/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "commitlint-config-lerna-scopes",
3+
"type": "module",
34
"version": "18.4.4",
45
"description": "Shareable commitlint config enforcing lerna package names as scopes",
56
"files": [
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('@commitlint/config-nx-scopes');
1+
export {default} from '@commitlint/config-nx-scopes';

@alias/commitlint-config-nx-scopes/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "commitlint-config-nx-scopes",
3+
"type": "module",
34
"version": "18.4.4",
45
"description": "Shareable commitlint config enforcing nx project names as scopes",
56
"files": [
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('@commitlint/config-patternplate');
1+
export {default} from '@commitlint/config-patternplate';

@alias/commitlint-config-patternplate/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "commitlint-config-patternplate",
3+
"type": "module",
34
"version": "18.4.4",
45
"description": "Lint your commits, patternplate-style",
56
"files": [

@alias/commitlint/cli.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/usr/bin/env node
2-
const pkgDir = require('@commitlint/cli');
3-
require(pkgDir);
2+
import '@commitlint/cli/cli.js';

@alias/commitlint/cli.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
import {createRequire} from 'module';
2+
import path from 'path';
3+
import {fileURLToPath} from 'url';
4+
15
import execa from 'execa';
26
import {fix} from '@commitlint/test';
37

8+
const require = createRequire(import.meta.url);
9+
10+
const __dirname = path.resolve(fileURLToPath(import.meta.url), '..');
11+
412
const bin = require.resolve('./cli.js');
513

614
function cli(args, options, input) {

@alias/commitlint/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "commitlint",
3+
"type": "module",
34
"version": "18.4.4",
45
"description": "Lint your commit messages",
56
"files": [

@commitlint/cli/cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env node
2-
require('./lib/cli.js');
2+
import './lib/cli.js';

@commitlint/cli/fixtures/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}
File renamed without changes.

@commitlint/cli/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
22
"name": "@commitlint/cli",
3+
"type": "module",
34
"version": "18.4.4",
45
"description": "Lint your commit messages",
56
"files": [
6-
"index.js",
7+
"index.cjs",
78
"cli.js",
89
"lib"
910
],
11+
"main": "index.cjs",
1012
"bin": {
1113
"commitlint": "./cli.js"
1214
},
@@ -39,7 +41,6 @@
3941
"devDependencies": {
4042
"@commitlint/test": "^18.0.0",
4143
"@commitlint/utils": "^18.4.4",
42-
"@types/lodash.isfunction": "^3.0.8",
4344
"@types/lodash.merge": "^4.6.8",
4445
"@types/node": "^18.11.9",
4546
"@types/yargs": "^17.0.29",
@@ -53,7 +54,6 @@
5354
"@commitlint/read": "^18.4.4",
5455
"@commitlint/types": "^18.4.4",
5556
"execa": "^5.0.0",
56-
"lodash.isfunction": "^3.0.9",
5757
"resolve-from": "5.0.0",
5858
"resolve-global": "1.0.0",
5959
"yargs": "^17.0.0"

@commitlint/cli/src/cli.test.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
import {createRequire} from 'module';
2+
import path from 'path';
3+
import {fileURLToPath} from 'url';
4+
15
import {fix, git} from '@commitlint/test';
26
import execa from 'execa';
37
import fs from 'fs-extra';
48
import merge from 'lodash.merge';
5-
import path from 'path';
9+
10+
const require = createRequire(import.meta.url);
11+
12+
const __dirname = path.resolve(fileURLToPath(import.meta.url), '..');
613

714
const bin = require.resolve('../cli.js');
815

@commitlint/cli/src/cli.ts

+38-26
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,45 @@
1-
import execa, {ExecaError} from 'execa';
2-
import load from '@commitlint/load';
3-
import lint from '@commitlint/lint';
4-
import read from '@commitlint/read';
5-
import isFunction from 'lodash.isfunction';
6-
import resolveFrom from 'resolve-from';
7-
import resolveGlobal from 'resolve-global';
8-
import yargs, {Arguments} from 'yargs';
1+
import {createRequire} from 'module';
2+
import path from 'path';
3+
import {fileURLToPath, pathToFileURL} from 'url';
94
import util from 'util';
105

11-
import {CliFlags} from './types';
12-
import {
6+
import lint from '@commitlint/lint';
7+
import load from '@commitlint/load';
8+
import read from '@commitlint/read';
9+
import type {
10+
Formatter,
1311
LintOptions,
1412
LintOutcome,
15-
ParserOptions,
1613
ParserPreset,
1714
QualifiedConfig,
18-
Formatter,
1915
UserConfig,
2016
} from '@commitlint/types';
21-
import {CliError} from './cli-error';
17+
import type {Options} from 'conventional-commits-parser';
18+
import execa, {ExecaError} from 'execa';
19+
import resolveFrom from 'resolve-from';
20+
import resolveGlobal from 'resolve-global';
21+
import yargs, {type Arguments} from 'yargs';
22+
23+
import {CliFlags} from './types.js';
24+
25+
import {CliError} from './cli-error.js';
26+
27+
const require = createRequire(import.meta.url);
28+
29+
const __dirname = path.resolve(fileURLToPath(import.meta.url), '..');
2230

23-
const pkg = require('../package');
31+
const dynamicImport = async <T>(id: string): Promise<T> => {
32+
const imported = await import(
33+
path.isAbsolute(id) ? pathToFileURL(id).toString() : id
34+
);
35+
return ('default' in imported && imported.default) || imported;
36+
};
37+
38+
const pkg: typeof import('../package.json') = require('../package.json');
2439

2540
const gitDefaultCommentChar = '#';
2641

27-
const cli = yargs
42+
const cli = yargs(process.argv.slice(2))
2843
.options({
2944
color: {
3045
alias: 'c',
@@ -206,7 +221,7 @@ async function main(args: MainArgs): Promise<void> {
206221
'[input] is required: supply via stdin, or --env or --edit or --from and --to',
207222
pkg.name
208223
);
209-
yargs.showHelp('log');
224+
yargs().showHelp('log');
210225
console.log(err.message);
211226
throw err;
212227
}
@@ -216,7 +231,7 @@ async function main(args: MainArgs): Promise<void> {
216231
file: flags.config,
217232
});
218233
const parserOpts = selectParserOpts(loaded.parserPreset);
219-
const opts: LintOptions & {parserOpts: ParserOptions} = {
234+
const opts: LintOptions & {parserOpts: Options} = {
220235
parserOpts: {},
221236
plugins: {},
222237
ignores: [],
@@ -234,7 +249,7 @@ async function main(args: MainArgs): Promise<void> {
234249
if (loaded.defaultIgnores === false) {
235250
opts.defaultIgnores = false;
236251
}
237-
const format = loadFormatter(loaded, flags);
252+
const format = await loadFormatter(loaded, flags);
238253

239254
// If reading from `.git/COMMIT_EDIT_MSG`, strip comments using
240255
// core.commentChar from git configuration, falling back to '#'.
@@ -422,21 +437,18 @@ function selectParserOpts(parserPreset: ParserPreset | undefined) {
422437
return parserPreset.parserOpts;
423438
}
424439

425-
function loadFormatter(config: QualifiedConfig, flags: CliFlags): Formatter {
440+
function loadFormatter(
441+
config: QualifiedConfig,
442+
flags: CliFlags
443+
): Promise<Formatter> {
426444
const moduleName = flags.format || config.formatter || '@commitlint/format';
427445
const modulePath =
428446
resolveFrom.silent(__dirname, moduleName) ||
429447
resolveFrom.silent(flags.cwd, moduleName) ||
430448
resolveGlobal.silent(moduleName);
431449

432450
if (modulePath) {
433-
const moduleInstance = require(modulePath);
434-
435-
if (isFunction(moduleInstance.default)) {
436-
return moduleInstance.default;
437-
}
438-
439-
return moduleInstance;
451+
return dynamicImport<Formatter>(modulePath);
440452
}
441453

442454
throw new Error(`Using format ${moduleName}, but cannot find the module.`);

@commitlint/config-angular-type-enum/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ const types = [
1111
'test',
1212
];
1313

14-
module.exports.rules = {
15-
'type-enum': [2, 'always', types],
14+
export default {
15+
rules: {
16+
'type-enum': [2, 'always', types],
17+
},
18+
value: () => types,
1619
};
17-
18-
module.exports.value = () => types;

@commitlint/config-angular-type-enum/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "@commitlint/config-angular-type-enum",
3+
"type": "module",
34
"version": "18.4.4",
45
"description": "Shareable commitlint config enforcing the angular commit convention types",
56
"files": [

@commitlint/config-angular/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const typeEnum = require('@commitlint/config-angular-type-enum');
1+
import typeEnum from '@commitlint/config-angular-type-enum';
22

3-
module.exports = {
3+
export default {
44
parserPreset: {parserOpts: {headerPattern: /^(\w*)(?:\((.*)\))?!?: (.*)$/}},
55
rules: {
66
'subject-exclamation-mark': [2, 'never'],

@commitlint/config-angular/index.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import lint from '@commitlint/lint';
2-
import {rules, parserPreset} from '.';
2+
3+
import config from './index.js';
4+
5+
const {rules, parserPreset} = config;
36

47
const lintMessage = async (message) => {
58
const parserOpts = parserPreset.parserOpts;

@commitlint/config-angular/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "@commitlint/config-angular",
3+
"type": "module",
34
"version": "18.4.4",
45
"description": "Shareable commitlint config enforcing the angular commit convention",
56
"files": [

@commitlint/config-conventional/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
parserPreset: 'conventional-changelog-conventionalcommits',
33
rules: {
44
'body-leading-blank': [1, 'always'],

@commitlint/config-conventional/index.test.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
import path from 'path';
2+
import {pathToFileURL} from 'url';
3+
14
import lint from '@commitlint/lint';
2-
import {rules, parserPreset} from '.';
5+
6+
import config from './index.js';
7+
8+
const {rules, parserPreset} = config;
9+
10+
const dynamicImport = async (id) => {
11+
const imported = await import(
12+
path.isAbsolute(id) ? pathToFileURL(id).toString() : id
13+
);
14+
return ('default' in imported && imported.default) || imported;
15+
};
316

417
const commitLint = async (message) => {
5-
const preset = await require(parserPreset)();
18+
const preset = (await dynamicImport(parserPreset))();
619
return lint(message, rules, {...preset});
720
};
821

@commitlint/config-conventional/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "@commitlint/config-conventional",
3+
"type": "module",
34
"version": "18.4.4",
45
"description": "Shareable commitlint config enforcing conventional commits",
56
"files": [

@commitlint/config-lerna-scopes/index.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
const glob = require('glob');
2-
const Path = require('path');
3-
const importFrom = require('import-from');
4-
const semver = require('semver');
1+
import {createRequire} from 'module';
2+
import Path from 'path';
3+
import {pathToFileURL} from 'url';
54

6-
module.exports = {
5+
import glob from 'glob';
6+
import importFrom from 'import-from';
7+
import semver from 'semver';
8+
9+
const require = createRequire(import.meta.url);
10+
11+
export default {
712
utils: {getPackages},
813
rules: {
914
'scope-enum': (ctx) =>
@@ -62,6 +67,5 @@ function getLernaVersion(cwd) {
6267
'lerna'
6368
);
6469
const modulePackageJson = Path.join(moduleDir, 'package.json');
65-
6670
return require(modulePackageJson).version;
6771
}

@commitlint/config-lerna-scopes/index.test.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import Path from 'path';
1+
import path from 'path';
2+
import {fileURLToPath} from 'url';
3+
24
import {npm} from '@commitlint/test';
3-
import config from '.';
5+
6+
import config from './index.js';
7+
8+
const __dirname = path.resolve(fileURLToPath(import.meta.url), '..');
49

510
test('exports rules key', () => {
611
expect(config).toHaveProperty('rules');
@@ -78,7 +83,7 @@ test('returns expected value for scoped lerna repository', async () => {
7883

7984
test('returns expected value for yarn workspaces', async () => {
8085
const {'scope-enum': fn} = config.rules;
81-
const cwd = Path.join(__dirname, 'fixtures', 'yarn');
86+
const cwd = path.join(__dirname, 'fixtures', 'yarn');
8287
const [, , value] = await fn({cwd});
8388
expect(value.sort()).toEqual(['a', 'b']);
8489
});

@commitlint/config-lerna-scopes/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "@commitlint/config-lerna-scopes",
3+
"type": "module",
34
"version": "18.4.4",
45
"description": "Shareable commitlint config enforcing lerna package and workspace names as scopes",
56
"files": [
@@ -45,7 +46,8 @@
4546
},
4647
"devDependencies": {
4748
"@commitlint/test": "^18.0.0",
48-
"@commitlint/utils": "^18.4.4"
49+
"@commitlint/utils": "^18.4.4",
50+
"@types/glob": "^8.1.0"
4951
},
5052
"gitHead": "70f7f4688b51774e7ac5e40e896cdaa3f132b2bc"
5153
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const {Workspaces} = require('nx/src/config/workspaces');
1+
import {Workspaces} from 'nx/src/config/workspaces.js';
22

3-
module.exports = {
3+
export default {
44
utils: {getProjects},
55
rules: {
66
'scope-enum': (ctx) =>

0 commit comments

Comments
 (0)