Skip to content

Commit

Permalink
refactor(esm): converted the package to esm
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `@semantic-release/commit-analyzer` is now a native ES Module. It has named exports
for each plugin hook (`analyzeCommits`)

fixes #296
  • Loading branch information
travi committed Dec 16, 2021
1 parent d662357 commit b4f9865
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 1,514 deletions.
28 changes: 14 additions & 14 deletions index.js
@@ -1,20 +1,22 @@
const {isUndefined} = require('lodash');
const parser = require('conventional-commits-parser').sync;
const filter = require('conventional-commits-filter');
const debug = require('debug')('semantic-release:commit-analyzer');
const loadParserConfig = require('./lib/load-parser-config');
const loadReleaseRules = require('./lib/load-release-rules');
const analyzeCommit = require('./lib/analyze-commit');
const compareReleaseTypes = require('./lib/compare-release-types');
const RELEASE_TYPES = require('./lib/default-release-types');
const DEFAULT_RELEASE_RULES = require('./lib/default-release-rules');
import { isUndefined } from 'lodash-es';
import { sync as parser } from 'conventional-commits-parser';
import filter from 'conventional-commits-filter';
import debugFactory from 'debug';
import loadParserConfig from './lib/load-parser-config.js';
import loadReleaseRules from './lib/load-release-rules.js';
import analyzeCommit from './lib/analyze-commit.js';
import compareReleaseTypes from './lib/compare-release-types.js';
import RELEASE_TYPES from './lib/default-release-types.js';
import DEFAULT_RELEASE_RULES from './lib/default-release-rules.js';

const debug = debugFactory('semantic-release:commit-analyzer');

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

return releaseType;
}

module.exports = {analyzeCommits};
15 changes: 8 additions & 7 deletions lib/analyze-commit.js
@@ -1,17 +1,18 @@
const {isMatchWith, isString} = require('lodash');
const micromatch = require('micromatch');
const debug = require('debug')('semantic-release:commit-analyzer');
const RELEASE_TYPES = require('./default-release-types');
const compareReleaseTypes = require('./compare-release-types');
import { isMatchWith, isString } from 'lodash-es';
import micromatch from 'micromatch';
import debugFactory from 'debug';
import RELEASE_TYPES from './default-release-types.js';
import compareReleaseTypes from './compare-release-types.js';

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

releaseRules
Expand Down Expand Up @@ -47,4 +48,4 @@ module.exports = (releaseRules, commit) => {
});

return releaseType;
};
}
6 changes: 3 additions & 3 deletions lib/compare-release-types.js
@@ -1,4 +1,4 @@
const RELEASE_TYPES = require('./default-release-types');
import RELEASE_TYPES from './default-release-types.js';

/**
* Test if a realease type is of higher level than a given one.
Expand All @@ -7,5 +7,5 @@ const RELEASE_TYPES = require('./default-release-types');
* @param {string} releaseType the release type to compare with.
* @return {Boolean} true if `releaseType` is higher than `currentReleaseType`.
*/
module.exports = (currentReleaseType, releaseType) =>
!currentReleaseType || RELEASE_TYPES.indexOf(releaseType) < RELEASE_TYPES.indexOf(currentReleaseType);
export default (currentReleaseType, releaseType) =>
!currentReleaseType || RELEASE_TYPES.indexOf(releaseType) < RELEASE_TYPES.indexOf(currentReleaseType)
44 changes: 22 additions & 22 deletions lib/default-release-rules.js
Expand Up @@ -3,32 +3,32 @@
*
* @type {Array}
*/
module.exports = [
{breaking: true, release: 'major'},
{revert: true, release: 'patch'},
export default [
{ breaking: true, release: 'major' },
{ revert: true, release: 'patch' },
// Angular
{type: 'feat', release: 'minor'},
{type: 'fix', release: 'patch'},
{type: 'perf', release: 'patch'},
{ type: 'feat', release: 'minor' },
{ type: 'fix', release: 'patch' },
{ type: 'perf', release: 'patch' },
// Atom
{emoji: ':racehorse:', release: 'patch'},
{emoji: ':bug:', release: 'patch'},
{emoji: ':penguin:', release: 'patch'},
{emoji: ':apple:', release: 'patch'},
{emoji: ':checkered_flag:', release: 'patch'},
{ emoji: ':racehorse:', release: 'patch' },
{ emoji: ':bug:', release: 'patch' },
{ emoji: ':penguin:', release: 'patch' },
{ emoji: ':apple:', release: 'patch' },
{ emoji: ':checkered_flag:', release: 'patch' },
// Ember
{tag: 'BUGFIX', release: 'patch'},
{tag: 'FEATURE', release: 'minor'},
{tag: 'SECURITY', release: 'patch'},
{ tag: 'BUGFIX', release: 'patch' },
{ tag: 'FEATURE', release: 'minor' },
{ tag: 'SECURITY', release: 'patch' },
// ESLint
{tag: 'Breaking', release: 'major'},
{tag: 'Fix', release: 'patch'},
{tag: 'Update', release: 'minor'},
{tag: 'New', release: 'minor'},
{ tag: 'Breaking', release: 'major' },
{ tag: 'Fix', release: 'patch' },
{ tag: 'Update', release: 'minor' },
{ tag: 'New', release: 'minor' },
// Express
{component: 'perf', release: 'patch'},
{component: 'deps', release: 'patch'},
{ component: 'perf', release: 'patch' },
{ component: 'deps', release: 'patch' },
// JSHint
{type: 'FEAT', release: 'minor'},
{type: 'FIX', release: 'patch'},
{ type: 'FEAT', release: 'minor' },
{ type: 'FIX', release: 'patch' },
];
2 changes: 1 addition & 1 deletion lib/default-release-types.js
Expand Up @@ -3,4 +3,4 @@
*
* @type {Array}
*/
module.exports = ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'];
export default ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'];
15 changes: 9 additions & 6 deletions lib/load-parser-config.js
@@ -1,7 +1,9 @@
const {promisify} = require('util');
const {isPlainObject} = require('lodash');
const importFrom = require('import-from');
const conventionalChangelogAngular = require('conventional-changelog-angular');
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { promisify } from 'node:util';
import { isPlainObject } from 'lodash-es';
import importFrom from 'import-from';
import conventionalChangelogAngular from 'conventional-changelog-angular';

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

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

return {...loadedConfig.parserOpts, ...parserOpts};
};
}
13 changes: 8 additions & 5 deletions lib/load-release-rules.js
@@ -1,6 +1,8 @@
const {isUndefined} = require('lodash');
const importFrom = require('import-from');
const RELEASE_TYPES = require('./default-release-types');
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { isUndefined } from 'lodash-es';
import importFrom from 'import-from';
import RELEASE_TYPES from './default-release-types.js';

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

if (releaseRules) {
loadedReleaseRules =
Expand All @@ -42,4 +45,4 @@ module.exports = ({releaseRules}, {cwd}) => {
}

return loadedReleaseRules;
};
}

0 comments on commit b4f9865

Please sign in to comment.