Skip to content

Commit b4f9865

Browse files
committedDec 16, 2021
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+
}

‎package-lock.json

+262-1,431
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "@semantic-release/commit-analyzer",
33
"description": "semantic-release plugin to analyze commits with conventional-changelog",
44
"version": "0.0.0-development",
5+
"type": "module",
56
"author": "Pierre Vanduynslager (https://twitter.com/@pvdlg_)",
67
"ava": {
78
"files": [
@@ -21,18 +22,18 @@
2122
"conventional-commits-parser": "^3.2.3",
2223
"debug": "^4.0.0",
2324
"import-from": "^4.0.0",
24-
"lodash": "^4.17.4",
25+
"lodash-es": "^4.17.21",
2526
"micromatch": "^4.0.2"
2627
},
2728
"devDependencies": {
2829
"ava": "3.15.0",
30+
"c8": "7.10.0",
2931
"conventional-changelog-atom": "2.0.8",
3032
"conventional-changelog-conventionalcommits": "4.6.1",
3133
"conventional-changelog-ember": "2.0.9",
3234
"conventional-changelog-eslint": "3.0.9",
3335
"conventional-changelog-express": "2.0.6",
3436
"conventional-changelog-jshint": "2.0.9",
35-
"nyc": "15.1.0",
3637
"semantic-release": "18.0.1",
3738
"sinon": "12.0.1",
3839
"xo": "0.28.3"
@@ -57,7 +58,7 @@
5758
],
5859
"license": "MIT",
5960
"main": "index.js",
60-
"nyc": {
61+
"c8": {
6162
"include": [
6263
"lib/**/*.js",
6364
"index.js"
@@ -87,8 +88,8 @@
8788
"lint": "xo",
8889
"pretest": "npm run lint",
8990
"semantic-release": "semantic-release",
90-
"test": "nyc ava -v",
91-
"test:ci": "nyc ava -v"
91+
"test": "c8 ava -v",
92+
"test:ci": "c8 ava -v"
9293
},
9394
"xo": {
9495
"prettier": true,

‎test/analyze-commit.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const test = require('ava');
2-
const analyzeCommit = require('../lib/analyze-commit');
1+
import test from 'ava';
2+
import analyzeCommit from '../lib/analyze-commit.js';
33

44
test('Match breaking change', t => {
55
const commit = {

‎test/compare-release-types.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const test = require('ava');
2-
const compareReleaseTypes = require('../lib/compare-release-types');
1+
import test from 'ava';
2+
import compareReleaseTypes from '../lib/compare-release-types.js';
33

44
test('Compares release types', t => {
55
t.true(compareReleaseTypes('patch', 'minor'));
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = 42;
1+
export default 42;

‎test/fixtures/release-rules.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
module.exports = [
2-
{breaking: true, release: 'major'},
3-
{type: 'feat', release: 'minor'},
4-
{type: 'fix', release: 'patch'},
5-
{type: 'perf', release: 'patch'},
1+
export default [
2+
{ breaking: true, release: 'major' },
3+
{ type: 'feat', release: 'minor' },
4+
{ type: 'fix', release: 'patch' },
5+
{ type: 'perf', release: 'patch' },
66
];

‎test/integration.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const test = require('ava');
2-
const {stub} = require('sinon');
3-
const {analyzeCommits} = require('..');
1+
import test from 'ava';
2+
import { stub } from 'sinon';
3+
import { analyzeCommits } from '../index.js';
44

55
const cwd = process.cwd();
66

‎test/load-parser-config.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const test = require('ava');
2-
const loadParserConfig = require('../lib/load-parser-config');
1+
import test from 'ava';
2+
import loadParserConfig from '../lib/load-parser-config.js';
33

44
const cwd = process.cwd();
55

@@ -34,7 +34,7 @@ async function loadConfig(t, config, pluginOptions) {
3434
loadConfig.title = (providedTitle, config) => `${providedTitle} Load "${config}" config`.trim();
3535

3636
test('Load "conventional-changelog-angular" by default', async t => {
37-
t.deepEqual(await loadParserConfig({}, {cwd}), (await require('conventional-changelog-angular')).parserOpts);
37+
t.deepEqual(await loadParserConfig({}, {cwd}), (await (await import('conventional-changelog-angular'))).parserOpts);
3838
});
3939

4040
test('Accept a "parserOpts" object as option', async t => {

‎test/load-release-rules.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const test = require('ava');
2-
const loadReleaseRules = require('../lib/load-release-rules');
3-
const testReleaseRules = require('./fixtures/release-rules');
1+
import test from 'ava';
2+
import loadReleaseRules from '../lib/load-release-rules.js';
3+
import testReleaseRules from './fixtures/release-rules.js';
44

55
const cwd = process.cwd();
66

@@ -10,7 +10,7 @@ test('Accept a "releaseRules" option', t => {
1010
t.deepEqual(releaseRules, testReleaseRules);
1111
});
1212

13-
test('Accept a "releaseRules" option that reference a requirable module', t => {
13+
test('Accept a "releaseRules" option that reference a requireable module', t => {
1414
const releaseRules = loadReleaseRules({releaseRules: './test/fixtures/release-rules'}, {cwd});
1515

1616
t.deepEqual(releaseRules, testReleaseRules);

0 commit comments

Comments
 (0)
Please sign in to comment.