Skip to content

Commit

Permalink
fix: revert the esm migration for now (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
travi authored Sep 3, 2021
1 parent a773c8b commit af37269
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 106 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ With this configuration:

##### External package / file

`releaseRules` can also reference a module, either by it's `npm` name or path. Note that the path must include the `.js` extension.

`releaseRules` can also reference a module, either by it's `npm` name or path:
```json
{
"plugins": [
Expand All @@ -175,12 +174,9 @@ With this configuration:
]
}
```

The file must be an ES Module exporting an array as default

```js
// File: config/release-rules.js
export default [
module.exports = [
{type: 'docs', scope: 'README', release: 'patch'},
{type: 'refactor', scope: 'core-*', release: 'minor'},
{type: 'refactor', release: 'patch'},
Expand Down
27 changes: 12 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import lodash from 'lodash';
const {isUndefined} = lodash;
import {sync as parser} from 'conventional-commits-parser';
import filter from 'conventional-commits-filter';
import debug 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';

debug('semantic-release:commit-analyzer');
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');

/**
* Determine the type of release to create based on a list of commits.
Expand All @@ -28,7 +25,7 @@ debug('semantic-release:commit-analyzer');
*/
async function analyzeCommits(pluginConfig, context) {
const {commits, logger} = context;
const releaseRules = await loadReleaseRules(pluginConfig, context);
const releaseRules = loadReleaseRules(pluginConfig, context);
const config = await loadParserConfig(pluginConfig, context);
let releaseType = null;

Expand Down Expand Up @@ -82,4 +79,4 @@ async function analyzeCommits(pluginConfig, context) {
return releaseType;
}

export {analyzeCommits};
module.exports = {analyzeCommits};
17 changes: 7 additions & 10 deletions lib/analyze-commit.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import lodash from 'lodash';
import micromatch from 'micromatch';
import debug from 'debug';
import RELEASE_TYPES from './default-release-types.js';
import compareReleaseTypes from './compare-release-types.js';
const {isMatchWith, isString} = lodash;
const {isMatch} = micromatch;
debug('semantic-release:commit-analyzer');
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');

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

releaseRules
Expand All @@ -26,7 +23,7 @@ export default (releaseRules, commit) => {
(!revert || commit.revert) &&
// Otherwise match the regular rules
isMatchWith(commit, rule, (object, src) =>
isString(src) && isString(object) ? isMatch(object, src) : undefined
isString(src) && isString(object) ? micromatch.isMatch(object, src) : undefined
)
)
.every(match => {
Expand Down
4 changes: 2 additions & 2 deletions lib/compare-release-types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import RELEASE_TYPES from './default-release-types.js';
const RELEASE_TYPES = require('./default-release-types');

/**
* Test if a realease type is of higher level than a given one.
Expand All @@ -7,5 +7,5 @@ import RELEASE_TYPES from './default-release-types.js';
* @param {string} releaseType the release type to compare with.
* @return {Boolean} true if `releaseType` is higher than `currentReleaseType`.
*/
export default (currentReleaseType, releaseType) =>
module.exports = (currentReleaseType, releaseType) =>
!currentReleaseType || RELEASE_TYPES.indexOf(releaseType) < RELEASE_TYPES.indexOf(currentReleaseType);
2 changes: 1 addition & 1 deletion lib/default-release-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @type {Array}
*/
export default [
module.exports = [
{breaking: true, release: 'major'},
{revert: true, release: 'patch'},
// Angular
Expand Down
2 changes: 1 addition & 1 deletion lib/default-release-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
*
* @type {Array}
*/
export default ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'];
module.exports = ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'];
11 changes: 0 additions & 11 deletions lib/esm-import.js

This file was deleted.

16 changes: 8 additions & 8 deletions lib/load-parser-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {promisify} from 'util';
import lodash from 'lodash';
const {isPlainObject} = lodash;
import {esmImport} from './esm-import.js';
const {promisify} = require('util');
const {isPlainObject} = require('lodash');
const importFrom = require('import-from');
const conventionalChangelogAngular = require('conventional-changelog-angular');

/**
* Load `conventional-changelog-parser` options. Handle presets that return either a `Promise<Array>` or a `Promise<Function>`.
Expand All @@ -14,16 +14,16 @@ import {esmImport} from './esm-import.js';
* @param {String} context.cwd The current working directory.
* @return {Promise<Object>} a `Promise` that resolve to the `conventional-changelog-parser` options.
*/
export default async ({preset, config, parserOpts, presetConfig}, {_}) => {
module.exports = async ({preset, config, parserOpts, presetConfig}, {cwd}) => {
let loadedConfig;

if (preset) {
const presetPackage = `conventional-changelog-${preset.toLowerCase()}`;
loadedConfig = await esmImport(presetPackage);
loadedConfig = importFrom.silent(__dirname, presetPackage) || importFrom(cwd, presetPackage);
} else if (config) {
loadedConfig = await esmImport(config);
loadedConfig = importFrom.silent(__dirname, config) || importFrom(cwd, config);
} else {
loadedConfig = await esmImport('conventional-changelog-angular');
loadedConfig = conventionalChangelogAngular;
}

loadedConfig = await (typeof loadedConfig === 'function'
Expand Down
15 changes: 7 additions & 8 deletions lib/load-release-rules.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import lodash from 'lodash';
const {isUndefined} = lodash;
import {esmImport} from './esm-import.js';
import RELEASE_TYPES from './default-release-types.js';
import {resolve} from 'path';
import {pathToFileURL} from 'url';
const {isUndefined} = require('lodash');
const importFrom = require('import-from');
const RELEASE_TYPES = require('./default-release-types');

/**
* Load and validate the `releaseRules` rules.
Expand All @@ -18,12 +15,14 @@ import {pathToFileURL} from 'url';
*
* @return {Array} the loaded and validated `releaseRules`.
*/
export default async ({releaseRules}, {cwd}) => {
module.exports = ({releaseRules}, {cwd}) => {
let loadedReleaseRules;

if (releaseRules) {
loadedReleaseRules =
typeof releaseRules === 'string' ? await esmImport(pathToFileURL(resolve(cwd, releaseRules)).href) : releaseRules;
typeof releaseRules === 'string'
? importFrom.silent(__dirname, releaseRules) || importFrom(cwd, releaseRules)
: releaseRules;

if (!Array.isArray(loadedReleaseRules)) {
throw new TypeError('Error in commit-analyzer configuration: "releaseRules" must be an array of rules');
Expand Down
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "@semantic-release/commit-analyzer",
"type": "module",
"description": "semantic-release plugin to analyze commits with conventional-changelog",
"version": "0.0.0-development",
"author": "Pierre Vanduynslager (https://twitter.com/@pvdlg_)",
Expand Down Expand Up @@ -57,7 +56,7 @@
"semantic-release"
],
"license": "MIT",
"exports": "./index.js",
"main": "index.js",
"nyc": {
"include": [
"lib/**/*.js",
Expand Down Expand Up @@ -95,11 +94,7 @@
"prettier": true,
"space": true,
"rules": {
"unicorn/string-content": "off",
"unicorn/import-index": "off",
"import/extensions": "off",
"import/no-useless-path-segments": "off",
"node/no-unsupported-features/es-syntax": "off"
"unicorn/string-content": "off"
}
},
"renovate": {
Expand Down
4 changes: 2 additions & 2 deletions test/analyze-commit.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import analyzeCommit from '../lib/analyze-commit.js';
const test = require('ava');
const analyzeCommit = require('../lib/analyze-commit');

test('Match breaking change', t => {
const commit = {
Expand Down
4 changes: 2 additions & 2 deletions test/compare-release-types.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import compareReleaseTypes from '../lib/compare-release-types.js';
const test = require('ava');
const compareReleaseTypes = require('../lib/compare-release-types');

test('Compares release types', t => {
t.true(compareReleaseTypes('patch', 'minor'));
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/release-rules-invalid.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default 42;
module.exports = 42;
2 changes: 1 addition & 1 deletion test/fixtures/release-rules.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default [
module.exports = [
{breaking: true, release: 'major'},
{type: 'feat', release: 'minor'},
{type: 'fix', release: 'patch'},
Expand Down
12 changes: 6 additions & 6 deletions test/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import test from 'ava';
import sinon from 'sinon';
import {analyzeCommits} from '../index.js';
const test = require('ava');
const {stub} = require('sinon');
const {analyzeCommits} = require('..');

const cwd = process.cwd();

test.beforeEach(t => {
const log = sinon.stub();
const log = stub();
t.context.log = log;
t.context.logger = {log};
});
Expand Down Expand Up @@ -117,7 +117,7 @@ test('Accept a "releaseRules" option that reference a requierable module', async
{hash: '456', message: 'feat(scope2): Second feature'},
];
const releaseType = await analyzeCommits(
{releaseRules: './test/fixtures/release-rules.js'},
{releaseRules: './test/fixtures/release-rules'},
{cwd, commits, logger: t.context.logger}
);

Expand Down Expand Up @@ -357,7 +357,7 @@ test('Throw error if "releaseRules" is not an Array or a String', async t => {
});

test('Throw error if "releaseRules" option reference a requierable module that is not an Array or a String', async t => {
await t.throwsAsync(analyzeCommits({releaseRules: './test/fixtures/release-rules-invalid.js'}, {cwd}), {
await t.throwsAsync(analyzeCommits({releaseRules: './test/fixtures/release-rules-invalid'}, {cwd}), {
message: /Error in commit-analyzer configuration: "releaseRules" must be an array of rules/,
});
});
Expand Down
7 changes: 3 additions & 4 deletions test/load-parser-config.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import test from 'ava';
import loadParserConfig from '../lib/load-parser-config.js';
import conventionalChangelogAngular from 'conventional-changelog-angular';
const test = require('ava');
const loadParserConfig = require('../lib/load-parser-config');

const cwd = process.cwd();

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

test('Load "conventional-changelog-angular" by default', async t => {
t.deepEqual(await loadParserConfig({}, {cwd}), (await conventionalChangelogAngular).parserOpts);
t.deepEqual(await loadParserConfig({}, {cwd}), (await require('conventional-changelog-angular')).parserOpts);
});

test('Accept a "parserOpts" object as option', async t => {
Expand Down
42 changes: 21 additions & 21 deletions test/load-release-rules.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import test from 'ava';
import loadReleaseRules from '../lib/load-release-rules.js';
import testReleaseRules from './fixtures/release-rules.js';
const test = require('ava');
const loadReleaseRules = require('../lib/load-release-rules');
const testReleaseRules = require('./fixtures/release-rules');

const cwd = process.cwd();

test('Accept a "releaseRules" option', async t => {
const releaseRules = await loadReleaseRules({releaseRules: testReleaseRules}, {cwd});
test('Accept a "releaseRules" option', t => {
const releaseRules = loadReleaseRules({releaseRules: testReleaseRules}, {cwd});

t.deepEqual(releaseRules, testReleaseRules);
});

test('Accept a "releaseRules" option that reference a requierable module', async t => {
const releaseRules = await loadReleaseRules({releaseRules: './test/fixtures/release-rules.js'}, {cwd});
test('Accept a "releaseRules" option that reference a requierable module', t => {
const releaseRules = loadReleaseRules({releaseRules: './test/fixtures/release-rules'}, {cwd});

t.deepEqual(releaseRules, testReleaseRules);
});

test('Return undefined if "releaseRules" not set', async t => {
const releaseRules = await loadReleaseRules({}, {cwd});
test('Return undefined if "releaseRules" not set', t => {
const releaseRules = loadReleaseRules({}, {cwd});

t.is(releaseRules, undefined);
});

test('Preserve release rules set to "false" or "null"', async t => {
const releaseRules = await loadReleaseRules(
test('Preserve release rules set to "false" or "null"', t => {
const releaseRules = loadReleaseRules(
{
releaseRules: [
{type: 'feat', release: false},
Expand All @@ -39,32 +39,32 @@ test('Preserve release rules set to "false" or "null"', async t => {
]);
});

test('Throw error if "releaseRules" reference invalid commit type', async t => {
await t.throwsAsync(() => loadReleaseRules({releaseRules: [{tag: 'Update', release: 'invalid'}]}, {cwd}), {
test('Throw error if "releaseRules" reference invalid commit type', t => {
t.throws(() => loadReleaseRules({releaseRules: [{tag: 'Update', release: 'invalid'}]}, {cwd}), {
message: /Error in commit-analyzer configuration: "invalid" is not a valid release type\. Valid values are:\[?.*]/,
});
});

test('Throw error if a rule in "releaseRules" does not have a release type', async t => {
await t.throwsAsync(() => loadReleaseRules({releaseRules: [{tag: 'Update'}]}, {cwd}), {
test('Throw error if a rule in "releaseRules" does not have a release type', t => {
t.throws(() => loadReleaseRules({releaseRules: [{tag: 'Update'}]}, {cwd}), {
message: /Error in commit-analyzer configuration: rules must be an object with a "release" property/,
});
});

test('Throw error if "releaseRules" is not an Array or a String', async t => {
await t.throwsAsync(() => loadReleaseRules({releaseRules: {}}, {cwd}), {
test('Throw error if "releaseRules" is not an Array or a String', t => {
t.throws(() => loadReleaseRules({releaseRules: {}}, {cwd}), {
message: /Error in commit-analyzer configuration: "releaseRules" must be an array of rules/,
});
});

test('Throw error if "releaseRules" option reference a requierable module that is not an Array or a String', async t => {
await t.throwsAsync(() => loadReleaseRules({releaseRules: './test/fixtures/release-rules-invalid.js'}, {cwd}), {
test('Throw error if "releaseRules" option reference a requierable module that is not an Array or a String', t => {
t.throws(() => loadReleaseRules({releaseRules: './test/fixtures/release-rules-invalid'}, {cwd}), {
message: /Error in commit-analyzer configuration: "releaseRules" must be an array of rules/,
});
});

test('Throw error if "releaseRules" contains an undefined rule', async t => {
await t.throwsAsync(() => loadReleaseRules({releaseRules: [{type: 'feat', release: 'minor'}, undefined]}, {cwd}), {
test('Throw error if "releaseRules" contains an undefined rule', t => {
t.throws(() => loadReleaseRules({releaseRules: [{type: 'feat', release: 'minor'}, undefined]}, {cwd}), {
message: /Error in commit-analyzer configuration: rules must be an object with a "release" property/,
});
});

0 comments on commit af37269

Please sign in to comment.