Skip to content

Commit

Permalink
refactor(esm): converted the package to esm
Browse files Browse the repository at this point in the history
fixes #296

BREAKING CHANGE: `@semantic-release/release-notes-generator` is now a native ES Module. It has
named exports for each plugin hook (`generateNotes`)
  • Loading branch information
travi committed Jan 27, 2023
1 parent 1dd2bfe commit 40f7596
Show file tree
Hide file tree
Showing 7 changed files with 3,681 additions and 5,588 deletions.
31 changes: 16 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const {format} = require('url');
const {find, merge} = require('lodash');
const getStream = require('get-stream');
const intoStream = require('into-stream');
const parser = require('conventional-commits-parser').sync;
const writer = require('conventional-changelog-writer');
const filter = require('conventional-commits-filter');
const readPkgUp = require('read-pkg-up');
const debug = require('debug')('semantic-release:release-notes-generator');
const loadChangelogConfig = require('./lib/load-changelog-config.js');
const HOSTS_CONFIG = require('./lib/hosts-config.js');
import { format } from 'url';
import { find, merge } from 'lodash-es';
import getStream from 'get-stream';
import intoStream from 'into-stream';
import { sync as parser } from 'conventional-commits-parser';
import writer from 'conventional-changelog-writer';
import filter from 'conventional-commits-filter';
import {readPackageUp} from 'read-pkg-up';
import debugFactory from 'debug';
import loadChangelogConfig from './lib/load-changelog-config.js';
import HOSTS_CONFIG from './lib/hosts-config.js';

const debug = debugFactory('semantic-release:release-notes-generator');

/**
* Generate the changelog for all the commits in `options.commits`.
Expand All @@ -26,7 +28,7 @@ const HOSTS_CONFIG = require('./lib/hosts-config.js');
*
* @returns {String} The changelog for all the commits in `context.commits`.
*/
async function generateNotes(pluginConfig, context) {
export async function generateNotes(pluginConfig, context) {
const {commits, lastRelease, nextRelease, options, cwd} = context;
const repositoryUrl = options.repositoryUrl.replace(/\.git$/i, '');
const {parserOpts, writerOpts} = await loadChangelogConfig(pluginConfig, context);
Expand Down Expand Up @@ -70,7 +72,7 @@ async function generateNotes(pluginConfig, context) {
linkCompare: currentTag && previousTag,
issue,
commit,
packageData: ((await readPkgUp({normalize: false, cwd})) || {}).packageJson,
packageData: ((await readPackageUp({normalize: false, cwd})) || {}).packageJson,
},
{host: hostConfig, linkCompare, linkReferences, commit: commitConfig, issue: issueConfig}
);
Expand All @@ -86,7 +88,6 @@ async function generateNotes(pluginConfig, context) {
debug('issue: %o', changelogContext.issue);
debug('commit: %o', changelogContext.commit);

console.log({writer})
return getStream(intoStream.object(parsedCommits).pipe(writer(changelogContext, writerOpts)));
}

module.exports = {generateNotes};
2 changes: 1 addition & 1 deletion lib/hosts-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
github: {
hostname: 'github.com',
issue: 'issues',
Expand Down
21 changes: 12 additions & 9 deletions lib/load-changelog-config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
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>`.
*
* @param {Object} pluginConfig The plugin configuration.
* @param {Object} pluginConfig.preset conventional-changelog preset ('angular', 'atom', 'codemirror', 'ember', 'eslint', 'express', 'jquery', 'jscs', 'jshint')
* @param {string} pluginConfig.config Requierable npm package with a custom conventional-changelog preset
* @param {Object} pluginConfig.parserOpts Additionnal `conventional-changelog-parser` options that will overwrite ones loaded by `preset` or `config`.
* @param {Object} pluginConfig.writerOpts Additionnal `conventional-changelog-writer` options that will overwrite ones loaded by `preset` or `config`.
* @param {string} pluginConfig.config Requireable npm package with a custom conventional-changelog preset
* @param {Object} pluginConfig.parserOpts Additional `conventional-changelog-parser` options that will overwrite ones loaded by `preset` or `config`.
* @param {Object} pluginConfig.writerOpts Additional `conventional-changelog-writer` options that will overwrite ones loaded by `preset` or `config`.
* @param {Object} context The semantic-release context.
* @param {Array<Object>} context.commits The commits to analyze.
* @param {String} context.cwd The current working directory.
*
* @return {Promise<Object>} a `Promise` that resolve to the `conventional-changelog-core` config.
*/
module.exports = async ({preset, config, parserOpts, writerOpts, presetConfig}, {cwd}) => {
export default async ({ preset, config, parserOpts, writerOpts, presetConfig }, { cwd }) => {
let loadedConfig;
const __dirname = dirname(fileURLToPath(import.meta.url));

if (preset) {
const presetPackage = `conventional-changelog-${preset.toLowerCase()}`;
Expand All @@ -39,4 +42,4 @@ module.exports = async ({preset, config, parserOpts, writerOpts, presetConfig},
parserOpts: {...loadedConfig.parserOpts, ...parserOpts},
writerOpts: {...loadedConfig.writerOpts, ...writerOpts},
};
};
}
Loading

0 comments on commit 40f7596

Please sign in to comment.