Skip to content

Commit

Permalink
feat: migrate to pure ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jan 15, 2024
1 parent 095d3ed commit f7b0772
Show file tree
Hide file tree
Showing 201 changed files with 2,176 additions and 2,077 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion @alias/commitlint-config-angular/index.js
@@ -1 +1 @@
module.exports = require('@commitlint/config-angular');
export {default} from '@commitlint/config-angular';
1 change: 1 addition & 0 deletions @alias/commitlint-config-angular/package.json
@@ -1,5 +1,6 @@
{
"name": "commitlint-config-angular",
"type": "module",
"version": "18.4.4",
"description": "Shareable commitlint config enforcing the angular commit convention",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion @alias/commitlint-config-lerna-scopes/index.js
@@ -1 +1 @@
module.exports = require('@commitlint/config-lerna-scopes');
export {default} from '@commitlint/config-lerna-scopes';
1 change: 1 addition & 0 deletions @alias/commitlint-config-lerna-scopes/package.json
@@ -1,5 +1,6 @@
{
"name": "commitlint-config-lerna-scopes",
"type": "module",
"version": "18.4.4",
"description": "Shareable commitlint config enforcing lerna package names as scopes",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion @alias/commitlint-config-nx-scopes/index.js
@@ -1 +1 @@
module.exports = require('@commitlint/config-nx-scopes');
export {default} from '@commitlint/config-nx-scopes';
1 change: 1 addition & 0 deletions @alias/commitlint-config-nx-scopes/package.json
@@ -1,5 +1,6 @@
{
"name": "commitlint-config-nx-scopes",
"type": "module",
"version": "18.4.4",
"description": "Shareable commitlint config enforcing nx project names as scopes",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion @alias/commitlint-config-patternplate/index.js
@@ -1 +1 @@
module.exports = require('@commitlint/config-patternplate');
export {default} from '@commitlint/config-patternplate';
1 change: 1 addition & 0 deletions @alias/commitlint-config-patternplate/package.json
@@ -1,5 +1,6 @@
{
"name": "commitlint-config-patternplate",
"type": "module",
"version": "18.4.4",
"description": "Lint your commits, patternplate-style",
"files": [
Expand Down
3 changes: 1 addition & 2 deletions @alias/commitlint/cli.js
@@ -1,3 +1,2 @@
#!/usr/bin/env node
const pkgDir = require('@commitlint/cli');
require(pkgDir);
import '@commitlint/cli/cli.js';
8 changes: 8 additions & 0 deletions @alias/commitlint/cli.test.js
@@ -1,6 +1,14 @@
import {createRequire} from 'module';
import path from 'path';
import {fileURLToPath} from 'url';

import execa from 'execa';
import {fix} from '@commitlint/test';

const require = createRequire(import.meta.url);

const __dirname = path.resolve(fileURLToPath(import.meta.url), '..');

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

function cli(args, options, input) {
Expand Down
1 change: 1 addition & 0 deletions @alias/commitlint/package.json
@@ -1,5 +1,6 @@
{
"name": "commitlint",
"type": "module",
"version": "18.4.4",
"description": "Lint your commit messages",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/cli/cli.js
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('./lib/cli.js');
import './lib/cli.js';
3 changes: 3 additions & 0 deletions @commitlint/cli/fixtures/package.json
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
File renamed without changes.
6 changes: 3 additions & 3 deletions @commitlint/cli/package.json
@@ -1,12 +1,14 @@
{
"name": "@commitlint/cli",
"type": "module",
"version": "18.4.4",
"description": "Lint your commit messages",
"files": [
"index.js",
"index.cjs",
"cli.js",
"lib"
],
"main": "index.cjs",
"bin": {
"commitlint": "./cli.js"
},
Expand Down Expand Up @@ -39,7 +41,6 @@
"devDependencies": {
"@commitlint/test": "^18.0.0",
"@commitlint/utils": "^18.4.4",
"@types/lodash.isfunction": "^3.0.8",
"@types/lodash.merge": "^4.6.8",
"@types/node": "^18.11.9",
"@types/yargs": "^17.0.29",
Expand All @@ -53,7 +54,6 @@
"@commitlint/read": "^18.4.4",
"@commitlint/types": "^18.4.4",
"execa": "^5.0.0",
"lodash.isfunction": "^3.0.9",
"resolve-from": "5.0.0",
"resolve-global": "1.0.0",
"yargs": "^17.0.0"
Expand Down
9 changes: 8 additions & 1 deletion @commitlint/cli/src/cli.test.ts
@@ -1,8 +1,15 @@
import {createRequire} from 'module';
import path from 'path';
import {fileURLToPath} from 'url';

import {fix, git} from '@commitlint/test';
import execa from 'execa';
import fs from 'fs-extra';
import merge from 'lodash.merge';
import path from 'path';

const require = createRequire(import.meta.url);

const __dirname = path.resolve(fileURLToPath(import.meta.url), '..');

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

Expand Down
64 changes: 38 additions & 26 deletions @commitlint/cli/src/cli.ts
@@ -1,30 +1,45 @@
import execa, {ExecaError} from 'execa';
import load from '@commitlint/load';
import lint from '@commitlint/lint';
import read from '@commitlint/read';
import isFunction from 'lodash.isfunction';
import resolveFrom from 'resolve-from';
import resolveGlobal from 'resolve-global';
import yargs, {Arguments} from 'yargs';
import {createRequire} from 'module';
import path from 'path';
import {fileURLToPath, pathToFileURL} from 'url';
import util from 'util';

import {CliFlags} from './types';
import {
import lint from '@commitlint/lint';
import load from '@commitlint/load';
import read from '@commitlint/read';
import type {
Formatter,
LintOptions,
LintOutcome,
ParserOptions,
ParserPreset,
QualifiedConfig,
Formatter,
UserConfig,
} from '@commitlint/types';
import {CliError} from './cli-error';
import type {Options} from 'conventional-commits-parser';
import execa, {ExecaError} from 'execa';
import resolveFrom from 'resolve-from';
import resolveGlobal from 'resolve-global';
import yargs, {type Arguments} from 'yargs';

import {CliFlags} from './types.js';

import {CliError} from './cli-error.js';

const require = createRequire(import.meta.url);

const __dirname = path.resolve(fileURLToPath(import.meta.url), '..');

const pkg = require('../package');
const dynamicImport = async <T>(id: string): Promise<T> => {
const imported = await import(
path.isAbsolute(id) ? pathToFileURL(id).toString() : id
);
return ('default' in imported && imported.default) || imported;
};

const pkg: typeof import('../package.json') = require('../package.json');

const gitDefaultCommentChar = '#';

const cli = yargs
const cli = yargs(process.argv.slice(2))
.options({
color: {
alias: 'c',
Expand Down Expand Up @@ -206,7 +221,7 @@ async function main(args: MainArgs): Promise<void> {
'[input] is required: supply via stdin, or --env or --edit or --from and --to',
pkg.name
);
yargs.showHelp('log');
yargs().showHelp('log');
console.log(err.message);
throw err;
}
Expand All @@ -216,7 +231,7 @@ async function main(args: MainArgs): Promise<void> {
file: flags.config,
});
const parserOpts = selectParserOpts(loaded.parserPreset);
const opts: LintOptions & {parserOpts: ParserOptions} = {
const opts: LintOptions & {parserOpts: Options} = {
parserOpts: {},
plugins: {},
ignores: [],
Expand All @@ -234,7 +249,7 @@ async function main(args: MainArgs): Promise<void> {
if (loaded.defaultIgnores === false) {
opts.defaultIgnores = false;
}
const format = loadFormatter(loaded, flags);
const format = await loadFormatter(loaded, flags);

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

function loadFormatter(config: QualifiedConfig, flags: CliFlags): Formatter {
function loadFormatter(
config: QualifiedConfig,
flags: CliFlags
): Promise<Formatter> {
const moduleName = flags.format || config.formatter || '@commitlint/format';
const modulePath =
resolveFrom.silent(__dirname, moduleName) ||
resolveFrom.silent(flags.cwd, moduleName) ||
resolveGlobal.silent(moduleName);

if (modulePath) {
const moduleInstance = require(modulePath);

if (isFunction(moduleInstance.default)) {
return moduleInstance.default;
}

return moduleInstance;
return dynamicImport<Formatter>(modulePath);
}

throw new Error(`Using format ${moduleName}, but cannot find the module.`);
Expand Down
9 changes: 5 additions & 4 deletions @commitlint/config-angular-type-enum/index.js
Expand Up @@ -11,8 +11,9 @@ const types = [
'test',
];

module.exports.rules = {
'type-enum': [2, 'always', types],
export default {
rules: {
'type-enum': [2, 'always', types],
},
value: () => types,
};

module.exports.value = () => types;
1 change: 1 addition & 0 deletions @commitlint/config-angular-type-enum/package.json
@@ -1,5 +1,6 @@
{
"name": "@commitlint/config-angular-type-enum",
"type": "module",
"version": "18.4.4",
"description": "Shareable commitlint config enforcing the angular commit convention types",
"files": [
Expand Down
4 changes: 2 additions & 2 deletions @commitlint/config-angular/index.js
@@ -1,6 +1,6 @@
const typeEnum = require('@commitlint/config-angular-type-enum');
import typeEnum from '@commitlint/config-angular-type-enum';

module.exports = {
export default {
parserPreset: {parserOpts: {headerPattern: /^(\w*)(?:\((.*)\))?!?: (.*)$/}},
rules: {
'subject-exclamation-mark': [2, 'never'],
Expand Down
5 changes: 4 additions & 1 deletion @commitlint/config-angular/index.test.js
@@ -1,5 +1,8 @@
import lint from '@commitlint/lint';
import {rules, parserPreset} from '.';

import config from './index.js';

const {rules, parserPreset} = config;

const lintMessage = async (message) => {
const parserOpts = parserPreset.parserOpts;
Expand Down
1 change: 1 addition & 0 deletions @commitlint/config-angular/package.json
@@ -1,5 +1,6 @@
{
"name": "@commitlint/config-angular",
"type": "module",
"version": "18.4.4",
"description": "Shareable commitlint config enforcing the angular commit convention",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/config-conventional/index.js
@@ -1,4 +1,4 @@
module.exports = {
export default {
parserPreset: 'conventional-changelog-conventionalcommits',
rules: {
'body-leading-blank': [1, 'always'],
Expand Down
17 changes: 15 additions & 2 deletions @commitlint/config-conventional/index.test.js
@@ -1,8 +1,21 @@
import path from 'path';
import {pathToFileURL} from 'url';

import lint from '@commitlint/lint';
import {rules, parserPreset} from '.';

import config from './index.js';

const {rules, parserPreset} = config;

const dynamicImport = async (id) => {
const imported = await import(
path.isAbsolute(id) ? pathToFileURL(id).toString() : id
);
return ('default' in imported && imported.default) || imported;
};

const commitLint = async (message) => {
const preset = await require(parserPreset)();
const preset = (await dynamicImport(parserPreset))();
return lint(message, rules, {...preset});
};

Expand Down
1 change: 1 addition & 0 deletions @commitlint/config-conventional/package.json
@@ -1,5 +1,6 @@
{
"name": "@commitlint/config-conventional",
"type": "module",
"version": "18.4.4",
"description": "Shareable commitlint config enforcing conventional commits",
"files": [
Expand Down
16 changes: 10 additions & 6 deletions @commitlint/config-lerna-scopes/index.js
@@ -1,9 +1,14 @@
const glob = require('glob');
const Path = require('path');
const importFrom = require('import-from');
const semver = require('semver');
import {createRequire} from 'module';
import Path from 'path';
import {pathToFileURL} from 'url';

module.exports = {
import glob from 'glob';
import importFrom from 'import-from';
import semver from 'semver';

const require = createRequire(import.meta.url);

export default {
utils: {getPackages},
rules: {
'scope-enum': (ctx) =>
Expand Down Expand Up @@ -62,6 +67,5 @@ function getLernaVersion(cwd) {
'lerna'
);
const modulePackageJson = Path.join(moduleDir, 'package.json');

return require(modulePackageJson).version;
}
11 changes: 8 additions & 3 deletions @commitlint/config-lerna-scopes/index.test.js
@@ -1,6 +1,11 @@
import Path from 'path';
import path from 'path';
import {fileURLToPath} from 'url';

import {npm} from '@commitlint/test';
import config from '.';

import config from './index.js';

const __dirname = path.resolve(fileURLToPath(import.meta.url), '..');

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

test('returns expected value for yarn workspaces', async () => {
const {'scope-enum': fn} = config.rules;
const cwd = Path.join(__dirname, 'fixtures', 'yarn');
const cwd = path.join(__dirname, 'fixtures', 'yarn');
const [, , value] = await fn({cwd});
expect(value.sort()).toEqual(['a', 'b']);
});
Expand Down
4 changes: 3 additions & 1 deletion @commitlint/config-lerna-scopes/package.json
@@ -1,5 +1,6 @@
{
"name": "@commitlint/config-lerna-scopes",
"type": "module",
"version": "18.4.4",
"description": "Shareable commitlint config enforcing lerna package and workspace names as scopes",
"files": [
Expand Down Expand Up @@ -45,7 +46,8 @@
},
"devDependencies": {
"@commitlint/test": "^18.0.0",
"@commitlint/utils": "^18.4.4"
"@commitlint/utils": "^18.4.4",
"@types/glob": "^8.1.0"
},
"gitHead": "70f7f4688b51774e7ac5e40e896cdaa3f132b2bc"
}
4 changes: 2 additions & 2 deletions @commitlint/config-nx-scopes/index.js
@@ -1,6 +1,6 @@
const {Workspaces} = require('nx/src/config/workspaces');
import {Workspaces} from 'nx/src/config/workspaces.js';

module.exports = {
export default {
utils: {getProjects},
rules: {
'scope-enum': (ctx) =>
Expand Down

0 comments on commit f7b0772

Please sign in to comment.