Skip to content

Commit b4f9865

Browse files
committed
refactor(esm): converted the package to esm
BREAKING CHANGE: `@semantic-release/commit-analyzer` is now a native ES Module. It has named exports for each plugin hook (`analyzeCommits`) fixes #296
1 parent d662357 commit b4f9865

16 files changed

+353
-1514
lines changed

index.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
const {isUndefined} = require('lodash');
2-
const parser = require('conventional-commits-parser').sync;
3-
const filter = require('conventional-commits-filter');
4-
const debug = require('debug')('semantic-release:commit-analyzer');
5-
const loadParserConfig = require('./lib/load-parser-config');
6-
const loadReleaseRules = require('./lib/load-release-rules');
7-
const analyzeCommit = require('./lib/analyze-commit');
8-
const compareReleaseTypes = require('./lib/compare-release-types');
9-
const RELEASE_TYPES = require('./lib/default-release-types');
10-
const DEFAULT_RELEASE_RULES = require('./lib/default-release-rules');
1+
import { isUndefined } from 'lodash-es';
2+
import { sync as parser } from 'conventional-commits-parser';
3+
import filter from 'conventional-commits-filter';
4+
import debugFactory from 'debug';
5+
import loadParserConfig from './lib/load-parser-config.js';
6+
import loadReleaseRules from './lib/load-release-rules.js';
7+
import analyzeCommit from './lib/analyze-commit.js';
8+
import compareReleaseTypes from './lib/compare-release-types.js';
9+
import RELEASE_TYPES from './lib/default-release-types.js';
10+
import DEFAULT_RELEASE_RULES from './lib/default-release-rules.js';
11+
12+
const debug = debugFactory('semantic-release:commit-analyzer');
1113

1214
/**
1315
* Determine the type of release to create based on a list of commits.
1416
*
1517
* @param {Object} pluginConfig The plugin configuration.
1618
* @param {String} pluginConfig.preset conventional-changelog preset ('angular', 'atom', 'codemirror', 'ember', 'eslint', 'express', 'jquery', 'jscs', 'jshint')
17-
* @param {String} pluginConfig.config Requirable npm package with a custom conventional-changelog preset
19+
* @param {String} pluginConfig.config Requireable npm package with a custom conventional-changelog preset
1820
* @param {String|Array} pluginConfig.releaseRules A `String` to load an external module or an `Array` of rules.
1921
* @param {Object} pluginConfig.parserOpts Additional `conventional-changelog-parser` options that will overwrite ones loaded by `preset` or `config`.
2022
* @param {Object} context The semantic-release context.
@@ -23,7 +25,7 @@ const DEFAULT_RELEASE_RULES = require('./lib/default-release-rules');
2325
*
2426
* @returns {String|null} the type of release to create based on the list of commits or `null` if no release has to be done.
2527
*/
26-
async function analyzeCommits(pluginConfig, context) {
28+
export async function analyzeCommits(pluginConfig, context) {
2729
const {commits, logger} = context;
2830
const releaseRules = loadReleaseRules(pluginConfig, context);
2931
const config = await loadParserConfig(pluginConfig, context);
@@ -78,5 +80,3 @@ async function analyzeCommits(pluginConfig, context) {
7880

7981
return releaseType;
8082
}
81-
82-
module.exports = {analyzeCommits};

lib/analyze-commit.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
const {isMatchWith, isString} = require('lodash');
2-
const micromatch = require('micromatch');
3-
const debug = require('debug')('semantic-release:commit-analyzer');
4-
const RELEASE_TYPES = require('./default-release-types');
5-
const compareReleaseTypes = require('./compare-release-types');
1+
import { isMatchWith, isString } from 'lodash-es';
2+
import micromatch from 'micromatch';
3+
import debugFactory from 'debug';
4+
import RELEASE_TYPES from './default-release-types.js';
5+
import compareReleaseTypes from './compare-release-types.js';
66

7+
const debug = debugFactory('semantic-release:commit-analyzer');
78
/**
89
* Find all the rules matching and return the highest release type of the matching rules.
910
*
1011
* @param {Array} releaseRules the rules to match the commit against.
1112
* @param {Commit} commit a parsed commit.
1213
* @return {string} the highest release type of the matching rules or `undefined` if no rule match the commit.
1314
*/
14-
module.exports = (releaseRules, commit) => {
15+
export default (releaseRules, commit) => {
1516
let releaseType;
1617

1718
releaseRules
@@ -47,4 +48,4 @@ module.exports = (releaseRules, commit) => {
4748
});
4849

4950
return releaseType;
50-
};
51+
}

lib/compare-release-types.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const RELEASE_TYPES = require('./default-release-types');
1+
import RELEASE_TYPES from './default-release-types.js';
22

33
/**
44
* Test if a realease type is of higher level than a given one.
@@ -7,5 +7,5 @@ const RELEASE_TYPES = require('./default-release-types');
77
* @param {string} releaseType the release type to compare with.
88
* @return {Boolean} true if `releaseType` is higher than `currentReleaseType`.
99
*/
10-
module.exports = (currentReleaseType, releaseType) =>
11-
!currentReleaseType || RELEASE_TYPES.indexOf(releaseType) < RELEASE_TYPES.indexOf(currentReleaseType);
10+
export default (currentReleaseType, releaseType) =>
11+
!currentReleaseType || RELEASE_TYPES.indexOf(releaseType) < RELEASE_TYPES.indexOf(currentReleaseType)

lib/default-release-rules.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33
*
44
* @type {Array}
55
*/
6-
module.exports = [
7-
{breaking: true, release: 'major'},
8-
{revert: true, release: 'patch'},
6+
export default [
7+
{ breaking: true, release: 'major' },
8+
{ revert: true, release: 'patch' },
99
// Angular
10-
{type: 'feat', release: 'minor'},
11-
{type: 'fix', release: 'patch'},
12-
{type: 'perf', release: 'patch'},
10+
{ type: 'feat', release: 'minor' },
11+
{ type: 'fix', release: 'patch' },
12+
{ type: 'perf', release: 'patch' },
1313
// Atom
14-
{emoji: ':racehorse:', release: 'patch'},
15-
{emoji: ':bug:', release: 'patch'},
16-
{emoji: ':penguin:', release: 'patch'},
17-
{emoji: ':apple:', release: 'patch'},
18-
{emoji: ':checkered_flag:', release: 'patch'},
14+
{ emoji: ':racehorse:', release: 'patch' },
15+
{ emoji: ':bug:', release: 'patch' },
16+
{ emoji: ':penguin:', release: 'patch' },
17+
{ emoji: ':apple:', release: 'patch' },
18+
{ emoji: ':checkered_flag:', release: 'patch' },
1919
// Ember
20-
{tag: 'BUGFIX', release: 'patch'},
21-
{tag: 'FEATURE', release: 'minor'},
22-
{tag: 'SECURITY', release: 'patch'},
20+
{ tag: 'BUGFIX', release: 'patch' },
21+
{ tag: 'FEATURE', release: 'minor' },
22+
{ tag: 'SECURITY', release: 'patch' },
2323
// ESLint
24-
{tag: 'Breaking', release: 'major'},
25-
{tag: 'Fix', release: 'patch'},
26-
{tag: 'Update', release: 'minor'},
27-
{tag: 'New', release: 'minor'},
24+
{ tag: 'Breaking', release: 'major' },
25+
{ tag: 'Fix', release: 'patch' },
26+
{ tag: 'Update', release: 'minor' },
27+
{ tag: 'New', release: 'minor' },
2828
// Express
29-
{component: 'perf', release: 'patch'},
30-
{component: 'deps', release: 'patch'},
29+
{ component: 'perf', release: 'patch' },
30+
{ component: 'deps', release: 'patch' },
3131
// JSHint
32-
{type: 'FEAT', release: 'minor'},
33-
{type: 'FIX', release: 'patch'},
32+
{ type: 'FEAT', release: 'minor' },
33+
{ type: 'FIX', release: 'patch' },
3434
];

lib/default-release-types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
*
44
* @type {Array}
55
*/
6-
module.exports = ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'];
6+
export default ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'];

lib/load-parser-config.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
const {promisify} = require('util');
2-
const {isPlainObject} = require('lodash');
3-
const importFrom = require('import-from');
4-
const conventionalChangelogAngular = require('conventional-changelog-angular');
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { promisify } from 'node:util';
4+
import { isPlainObject } from 'lodash-es';
5+
import importFrom from 'import-from';
6+
import conventionalChangelogAngular from 'conventional-changelog-angular';
57

68
/**
79
* Load `conventional-changelog-parser` options. Handle presets that return either a `Promise<Array>` or a `Promise<Function>`.
@@ -14,8 +16,9 @@ const conventionalChangelogAngular = require('conventional-changelog-angular');
1416
* @param {String} context.cwd The current working directory.
1517
* @return {Promise<Object>} a `Promise` that resolve to the `conventional-changelog-parser` options.
1618
*/
17-
module.exports = async ({preset, config, parserOpts, presetConfig}, {cwd}) => {
19+
export default async ({ preset, config, parserOpts, presetConfig }, { cwd }) => {
1820
let loadedConfig;
21+
const __dirname = dirname(fileURLToPath(import.meta.url));
1922

2023
if (preset) {
2124
const presetPackage = `conventional-changelog-${preset.toLowerCase()}`;
@@ -33,4 +36,4 @@ module.exports = async ({preset, config, parserOpts, presetConfig}, {cwd}) => {
3336
: loadedConfig);
3437

3538
return {...loadedConfig.parserOpts, ...parserOpts};
36-
};
39+
}

lib/load-release-rules.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
const {isUndefined} = require('lodash');
2-
const importFrom = require('import-from');
3-
const RELEASE_TYPES = require('./default-release-types');
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { isUndefined } from 'lodash-es';
4+
import importFrom from 'import-from';
5+
import RELEASE_TYPES from './default-release-types.js';
46

57
/**
68
* Load and validate the `releaseRules` rules.
@@ -15,8 +17,9 @@ const RELEASE_TYPES = require('./default-release-types');
1517
*
1618
* @return {Array} the loaded and validated `releaseRules`.
1719
*/
18-
module.exports = ({releaseRules}, {cwd}) => {
20+
export default ({ releaseRules }, { cwd }) => {
1921
let loadedReleaseRules;
22+
const __dirname = dirname(fileURLToPath(import.meta.url));
2023

2124
if (releaseRules) {
2225
loadedReleaseRules =
@@ -42,4 +45,4 @@ module.exports = ({releaseRules}, {cwd}) => {
4245
}
4346

4447
return loadedReleaseRules;
45-
};
48+
}

0 commit comments

Comments
 (0)