From a1624e03602869d106d03fbd6af0efdd896d5cc3 Mon Sep 17 00:00:00 2001 From: Matt Travi Date: Fri, 3 Sep 2021 14:41:13 -0500 Subject: [PATCH] Revert "feat: esm (#247)" This reverts commit a773c8bd4bca054ef3542e2836525cd864e51e6e. --- README.md | 8 ++--- index.js | 27 ++++++++--------- lib/analyze-commit.js | 17 +++++------ lib/compare-release-types.js | 4 +-- lib/default-release-rules.js | 2 +- lib/default-release-types.js | 2 +- lib/esm-import.js | 11 ------- lib/load-parser-config.js | 16 +++++----- lib/load-release-rules.js | 15 +++++---- package.json | 9 ++---- test/analyze-commit.test.js | 4 +-- test/compare-release-types.test.js | 4 +-- test/fixtures/release-rules-invalid.js | 2 +- test/fixtures/release-rules.js | 2 +- test/integration.test.js | 12 ++++---- test/load-parser-config.test.js | 7 ++--- test/load-release-rules.test.js | 42 +++++++++++++------------- 17 files changed, 78 insertions(+), 106 deletions(-) delete mode 100644 lib/esm-import.js diff --git a/README.md b/README.md index b7d72626..0b872811 100644 --- a/README.md +++ b/README.md @@ -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": [ @@ -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'}, diff --git a/index.js b/index.js index 06764144..65d4f298 100644 --- a/index.js +++ b/index.js @@ -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. @@ -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; @@ -82,4 +79,4 @@ async function analyzeCommits(pluginConfig, context) { return releaseType; } -export {analyzeCommits}; +module.exports = {analyzeCommits}; diff --git a/lib/analyze-commit.js b/lib/analyze-commit.js index 968ea926..757a45dc 100644 --- a/lib/analyze-commit.js +++ b/lib/analyze-commit.js @@ -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. @@ -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 @@ -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 => { diff --git a/lib/compare-release-types.js b/lib/compare-release-types.js index 5fdaee5f..51c97b35 100644 --- a/lib/compare-release-types.js +++ b/lib/compare-release-types.js @@ -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. @@ -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); diff --git a/lib/default-release-rules.js b/lib/default-release-rules.js index 4f1b1ed7..338e4ef1 100644 --- a/lib/default-release-rules.js +++ b/lib/default-release-rules.js @@ -3,7 +3,7 @@ * * @type {Array} */ -export default [ +module.exports = [ {breaking: true, release: 'major'}, {revert: true, release: 'patch'}, // Angular diff --git a/lib/default-release-types.js b/lib/default-release-types.js index d292fb96..937b01a0 100644 --- a/lib/default-release-types.js +++ b/lib/default-release-types.js @@ -3,4 +3,4 @@ * * @type {Array} */ -export default ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease']; +module.exports = ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease']; diff --git a/lib/esm-import.js b/lib/esm-import.js deleted file mode 100644 index 3a2dcbf2..00000000 --- a/lib/esm-import.js +++ /dev/null @@ -1,11 +0,0 @@ -export const esmImport = async name => { - try { - return (await import(name)).default; - } catch (error) { - if (error.code === 'ERR_MODULE_NOT_FOUND') { - error.code = 'MODULE_NOT_FOUND'; - } - - throw error; - } -}; diff --git a/lib/load-parser-config.js b/lib/load-parser-config.js index 6c185e0f..af093eba 100644 --- a/lib/load-parser-config.js +++ b/lib/load-parser-config.js @@ -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` or a `Promise`. @@ -14,16 +14,16 @@ import {esmImport} from './esm-import.js'; * @param {String} context.cwd The current working directory. * @return {Promise} 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' diff --git a/lib/load-release-rules.js b/lib/load-release-rules.js index fd189d39..508b0349 100644 --- a/lib/load-release-rules.js +++ b/lib/load-release-rules.js @@ -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. @@ -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'); diff --git a/package.json b/package.json index fe093dd3..28c57343 100644 --- a/package.json +++ b/package.json @@ -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_)", @@ -57,7 +56,7 @@ "semantic-release" ], "license": "MIT", - "exports": "./index.js", + "main": "index.js", "nyc": { "include": [ "lib/**/*.js", @@ -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": { diff --git a/test/analyze-commit.test.js b/test/analyze-commit.test.js index 2f8a7329..45f07715 100644 --- a/test/analyze-commit.test.js +++ b/test/analyze-commit.test.js @@ -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 = { diff --git a/test/compare-release-types.test.js b/test/compare-release-types.test.js index 1d61b52d..82fc3bce 100644 --- a/test/compare-release-types.test.js +++ b/test/compare-release-types.test.js @@ -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')); diff --git a/test/fixtures/release-rules-invalid.js b/test/fixtures/release-rules-invalid.js index 7a4e8a72..888cae37 100644 --- a/test/fixtures/release-rules-invalid.js +++ b/test/fixtures/release-rules-invalid.js @@ -1 +1 @@ -export default 42; +module.exports = 42; diff --git a/test/fixtures/release-rules.js b/test/fixtures/release-rules.js index 8cb14f87..87297d18 100644 --- a/test/fixtures/release-rules.js +++ b/test/fixtures/release-rules.js @@ -1,4 +1,4 @@ -export default [ +module.exports = [ {breaking: true, release: 'major'}, {type: 'feat', release: 'minor'}, {type: 'fix', release: 'patch'}, diff --git a/test/integration.test.js b/test/integration.test.js index 3c52034c..e7ac70ae 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -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}; }); @@ -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} ); @@ -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/, }); }); diff --git a/test/load-parser-config.test.js b/test/load-parser-config.test.js index 88e15cb7..233cb284 100644 --- a/test/load-parser-config.test.js +++ b/test/load-parser-config.test.js @@ -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(); @@ -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 => { diff --git a/test/load-release-rules.test.js b/test/load-release-rules.test.js index ae928e56..a6818dae 100644 --- a/test/load-release-rules.test.js +++ b/test/load-release-rules.test.js @@ -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}, @@ -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/, }); });