Skip to content

Commit 40f7596

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

File tree

7 files changed

+3681
-5588
lines changed

7 files changed

+3681
-5588
lines changed

index.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
const {format} = require('url');
2-
const {find, merge} = require('lodash');
3-
const getStream = require('get-stream');
4-
const intoStream = require('into-stream');
5-
const parser = require('conventional-commits-parser').sync;
6-
const writer = require('conventional-changelog-writer');
7-
const filter = require('conventional-commits-filter');
8-
const readPkgUp = require('read-pkg-up');
9-
const debug = require('debug')('semantic-release:release-notes-generator');
10-
const loadChangelogConfig = require('./lib/load-changelog-config.js');
11-
const HOSTS_CONFIG = require('./lib/hosts-config.js');
1+
import { format } from 'url';
2+
import { find, merge } from 'lodash-es';
3+
import getStream from 'get-stream';
4+
import intoStream from 'into-stream';
5+
import { sync as parser } from 'conventional-commits-parser';
6+
import writer from 'conventional-changelog-writer';
7+
import filter from 'conventional-commits-filter';
8+
import {readPackageUp} from 'read-pkg-up';
9+
import debugFactory from 'debug';
10+
import loadChangelogConfig from './lib/load-changelog-config.js';
11+
import HOSTS_CONFIG from './lib/hosts-config.js';
12+
13+
const debug = debugFactory('semantic-release:release-notes-generator');
1214

1315
/**
1416
* Generate the changelog for all the commits in `options.commits`.
@@ -26,7 +28,7 @@ const HOSTS_CONFIG = require('./lib/hosts-config.js');
2628
*
2729
* @returns {String} The changelog for all the commits in `context.commits`.
2830
*/
29-
async function generateNotes(pluginConfig, context) {
31+
export async function generateNotes(pluginConfig, context) {
3032
const {commits, lastRelease, nextRelease, options, cwd} = context;
3133
const repositoryUrl = options.repositoryUrl.replace(/\.git$/i, '');
3234
const {parserOpts, writerOpts} = await loadChangelogConfig(pluginConfig, context);
@@ -70,7 +72,7 @@ async function generateNotes(pluginConfig, context) {
7072
linkCompare: currentTag && previousTag,
7173
issue,
7274
commit,
73-
packageData: ((await readPkgUp({normalize: false, cwd})) || {}).packageJson,
75+
packageData: ((await readPackageUp({normalize: false, cwd})) || {}).packageJson,
7476
},
7577
{host: hostConfig, linkCompare, linkReferences, commit: commitConfig, issue: issueConfig}
7678
);
@@ -86,7 +88,6 @@ async function generateNotes(pluginConfig, context) {
8688
debug('issue: %o', changelogContext.issue);
8789
debug('commit: %o', changelogContext.commit);
8890

91+
console.log({writer})
8992
return getStream(intoStream.object(parsedCommits).pipe(writer(changelogContext, writerOpts)));
9093
}
91-
92-
module.exports = {generateNotes};

lib/hosts-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
github: {
33
hostname: 'github.com',
44
issue: 'issues',

lib/load-changelog-config.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
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>`.
810
*
911
* @param {Object} pluginConfig The plugin configuration.
1012
* @param {Object} pluginConfig.preset conventional-changelog preset ('angular', 'atom', 'codemirror', 'ember', 'eslint', 'express', 'jquery', 'jscs', 'jshint')
11-
* @param {string} pluginConfig.config Requierable npm package with a custom conventional-changelog preset
12-
* @param {Object} pluginConfig.parserOpts Additionnal `conventional-changelog-parser` options that will overwrite ones loaded by `preset` or `config`.
13-
* @param {Object} pluginConfig.writerOpts Additionnal `conventional-changelog-writer` options that will overwrite ones loaded by `preset` or `config`.
13+
* @param {string} pluginConfig.config Requireable npm package with a custom conventional-changelog preset
14+
* @param {Object} pluginConfig.parserOpts Additional `conventional-changelog-parser` options that will overwrite ones loaded by `preset` or `config`.
15+
* @param {Object} pluginConfig.writerOpts Additional `conventional-changelog-writer` options that will overwrite ones loaded by `preset` or `config`.
1416
* @param {Object} context The semantic-release context.
1517
* @param {Array<Object>} context.commits The commits to analyze.
1618
* @param {String} context.cwd The current working directory.
1719
*
1820
* @return {Promise<Object>} a `Promise` that resolve to the `conventional-changelog-core` config.
1921
*/
20-
module.exports = async ({preset, config, parserOpts, writerOpts, presetConfig}, {cwd}) => {
22+
export default async ({ preset, config, parserOpts, writerOpts, presetConfig }, { cwd }) => {
2123
let loadedConfig;
24+
const __dirname = dirname(fileURLToPath(import.meta.url));
2225

2326
if (preset) {
2427
const presetPackage = `conventional-changelog-${preset.toLowerCase()}`;
@@ -39,4 +42,4 @@ module.exports = async ({preset, config, parserOpts, writerOpts, presetConfig},
3942
parserOpts: {...loadedConfig.parserOpts, ...parserOpts},
4043
writerOpts: {...loadedConfig.writerOpts, ...writerOpts},
4144
};
42-
};
45+
}

0 commit comments

Comments
 (0)