Skip to content

Commit

Permalink
feat: Use ESM modules
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Requires semantic-release 20 or above.

Thanks to @bryanjtc for doing all the work to get this done. Much appreciated!

Co-Authored-By: Bryan Thomas <[email protected]>
  • Loading branch information
pmowrer and bryanjtc committed Jan 17, 2024
1 parent 5576099 commit c8a20c0
Show file tree
Hide file tree
Showing 13 changed files with 634 additions and 3,102 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"scripts": {
"format": "prettier --write --single-quote --trailing-comma es5",
"format:all": "yarn format \"./**/*.js\"",
"test": "jest"
"test": "vitest run --no-threads"
},
"license": "MIT",
"peerDependencies": {
"semantic-release": ">=15.11.x < 20"
"semantic-release": ">=20"
},
"dependencies": {
"debug": "^3.1.0",
Expand All @@ -27,15 +27,15 @@
"pkg-up": "^2.0.0",
"ramda": "^0.25.0",
"read-pkg": "^5.0.0",
"semantic-release-plugin-decorators": "^3.0.0",
"semantic-release-plugin-decorators": "^4.0.0",
"tempy": "1.0.1"
},
"devDependencies": {
"husky": "^4.2.1",
"jest": "^25.1.0",
"lint-staged": "^10.0.7",
"p-each-series": "^2.1.0",
"prettier": "^1.19.1"
"prettier": "^1.19.1",
"vitest": "0.34.6"
},
"husky": {
"hooks": {
Expand Down
34 changes: 17 additions & 17 deletions src/git-utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const execa = require('execa');
const { pipeP, split } = require('ramda');
const fse = require('fs-extra');
const path = require('path');
const tempy = require('tempy');
const fileUrl = require('file-url');
const gitLogParser = require('git-log-parser');
const pEachSeries = require('p-each-series');
const getStream = require('get-stream');
import execa from 'execa';
import { pipeP, split } from 'ramda';
import fse from 'fs-extra';
import path from 'path';
import tempy from 'tempy';
import fileUrl from 'file-url';
import gitLogParser from 'git-log-parser';
import pEachSeries from 'p-each-series';
import getStream from 'get-stream';

const git = async (args, options = {}) => {
const { stdout } = await execa('git', args, options);
Expand Down Expand Up @@ -43,11 +43,11 @@ const getRoot = () => git(['rev-parse', '--show-toplevel']);
const gitCommitsWithFiles = async commits => {
for (const commit of commits) {
for (const file of commit.files) {
let filePath = path.join(process.cwd(), file.name);
await fse.outputFile(
filePath,
(file.body = !'undefined' ? file.body : commit.message)
);
const filePath = path.join(process.cwd(), file.name);
if (file.body === undefined) {
file.body = commit.message;
}
await fse.outputFile(filePath, file.body);
await execa('git', ['add', filePath]);
}
await execa('git', [
Expand Down Expand Up @@ -117,7 +117,7 @@ const gitCommits = async (messages, execaOptions) => {
*
* @return {Array<Commit>} The list of parsed commits.
*/
gitGetCommits = async from => {
const gitGetCommits = async from => {
Object.assign(gitLogParser.fields, {
hash: 'H',
message: 'B',
Expand All @@ -127,7 +127,7 @@ gitGetCommits = async from => {
return (
await getStream.array(
gitLogParser.parse(
{ _: `${from ? from + '..' : ''}HEAD` },
{ _: `${from ? `${from}..` : ''}HEAD` },
{ env: { ...process.env } }
)
)
Expand Down Expand Up @@ -228,7 +228,7 @@ const gitCheckout = async (branch, create, execaOptions) => {
);
};

module.exports = {
export {
getCommitFiles,
getRoot,
gitCommitsWithFiles,
Expand Down
28 changes: 12 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const readPkg = require('read-pkg');
const { compose } = require('ramda');
const { withOnlyPackageCommits } = require('./only-package-commits');
const versionToGitTag = require('./version-to-git-tag');
const logPluginVersion = require('./log-plugin-version');
const { wrapStep } = require('semantic-release-plugin-decorators');

const {
import readPkg from 'read-pkg';
import { compose } from 'ramda';
import { wrapStep } from 'semantic-release-plugin-decorators';
import { withOnlyPackageCommits } from './only-package-commits.js';
import versionToGitTag from './version-to-git-tag.js';
import logPluginVersion from './log-plugin-version.js';
import {
mapNextReleaseVersion,
withOptionsTransforms,
} = require('./options-transforms');
} from './options-transforms.js';

const analyzeCommits = wrapStep(
'analyzeCommits',
Expand Down Expand Up @@ -54,10 +53,7 @@ const fail = wrapStep(
}
);

module.exports = {
analyzeCommits,
generateNotes,
success,
fail,
tagFormat: readPkg.sync().name + '-v${version}',
};
const tagFormat = `${readPkg.sync().name}-v\${version}`;

//TODO Change to esm export when https://github.com/semantic-release/semantic-release/pull/3037 is merged
module.exports = { analyzeCommits, generateNotes, success, fail, tagFormat };
6 changes: 2 additions & 4 deletions src/lens-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { curry, set, view } = require('ramda');
import { curry, set, view } from 'ramda';

/**
* Async version of Ramda's `over` lens utility.
Expand All @@ -8,6 +8,4 @@ const overA = curry(async (lens, f, x) => {
return set(lens, value, x);
});

module.exports = {
overA,
};
export { overA };
10 changes: 6 additions & 4 deletions src/log-plugin-version.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { resolve } = require('path');
const readPkg = require('read-pkg');
const debug = require('debug')('semantic-release:monorepo');
import { resolve } from 'path';
import readPkg from 'read-pkg';
import createDebug from 'debug';

const debug = createDebug('semantic-release:monorepo');

const logPluginVersion = type => plugin => async (pluginConfig, config) => {
if (config.options.debug) {
Expand All @@ -11,4 +13,4 @@ const logPluginVersion = type => plugin => async (pluginConfig, config) => {
return plugin(pluginConfig, config);
};

module.exports = logPluginVersion;
export default logPluginVersion;
23 changes: 10 additions & 13 deletions src/only-package-commits.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { identity, memoizeWith, pipeP } = require('ramda');
const pkgUp = require('pkg-up');
const readPkg = require('read-pkg');
const path = require('path');
const pLimit = require('p-limit');
const debug = require('debug')('semantic-release:monorepo');
const { getCommitFiles, getRoot } = require('./git-utils');
const { mapCommits } = require('./options-transforms');
import { identity, memoizeWith, pipeP } from 'ramda';
import pkgUp from 'pkg-up';
import readPkg from 'read-pkg';
import path from 'path';
import pLimit from 'p-limit';
import createDebug from 'debug';
import { getCommitFiles, getRoot } from './git-utils.js';
import { mapCommits } from './options-transforms.js';

const debug = createDebug('semantic-release:monorepo');
const memoizedGetCommitFiles = memoizeWith(identity, getCommitFiles);

/**
Expand Down Expand Up @@ -89,8 +90,4 @@ const withOnlyPackageCommits = plugin => async (pluginConfig, config) => {
);
};

module.exports = {
withOnlyPackageCommits,
onlyPackageCommits,
withFiles,
};
export { withOnlyPackageCommits, onlyPackageCommits, withFiles };
28 changes: 14 additions & 14 deletions src/only-package-commits.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { gitCommitsWithFiles, initGitRepo } = require('./git-utils');
const { onlyPackageCommits, withFiles } = require('./only-package-commits');
const path = require('path');

import { gitCommitsWithFiles, initGitRepo } from './git-utils.js';
import { onlyPackageCommits, withFiles } from './only-package-commits.js';
import path from 'path';
import { describe, it, expect } from 'vitest';
async function getCommitWithFileFromMessage(commits, message) {
commitsWithFiles = await withFiles(
const commitsWithFiles = await withFiles(
Array.of(commits.find(obj => obj.subject === message))
);
if (commitsWithFiles.length !== 0) {
Expand All @@ -16,7 +16,7 @@ async function getCommitWithFileFromMessage(commits, message) {
describe('filter commits', () => {
it('should filter 0 commits (no root folder support) ', async () => {
const gitRepo = await initGitRepo(false);
let commitsToCreate = [
const commitsToCreate = [
{ message: 'init1', files: [{ name: 'package.json' }] },
{ message: 'message1', files: [{ name: 'readme.md' }] },
{ message: 'message2', files: [{ name: 'module1/readme.md' }] },
Expand All @@ -26,14 +26,14 @@ describe('filter commits', () => {
},
];
process.chdir(gitRepo.cwd);
commits = await gitCommitsWithFiles(commitsToCreate);
result = await onlyPackageCommits(commits);
const commits = await gitCommitsWithFiles(commitsToCreate);
const result = await onlyPackageCommits(commits);
expect(result).toHaveLength(0);
});

it('should filter 3 commits (folder module1) ', async () => {
const gitRepo = await initGitRepo(false);
let commitsToCreate = [
const commitsToCreate = [
{
message: 'init1',
files: [{ name: 'package.json' }, { name: 'module1/package.json' }],
Expand All @@ -46,9 +46,9 @@ describe('filter commits', () => {
},
];
process.chdir(gitRepo.cwd);
commits = await gitCommitsWithFiles(commitsToCreate);
const commits = await gitCommitsWithFiles(commitsToCreate);
process.chdir(path.join(gitRepo.cwd, 'module1'));
result = await onlyPackageCommits(commits);
const result = await onlyPackageCommits(commits);

expect(result).toHaveLength(3);
expect(result).toContainEqual(
Expand All @@ -67,7 +67,7 @@ describe('filter commits', () => {

it('should filter 2 commits (folder module2) ', async () => {
const gitRepo = await initGitRepo(false);
let commitsToCreate = [
const commitsToCreate = [
{
message: 'init1',
files: [{ name: 'package.json' }, { name: 'module1/package.json' }],
Expand All @@ -87,9 +87,9 @@ describe('filter commits', () => {
},
];
process.chdir(gitRepo.cwd);
commits = await gitCommitsWithFiles(commitsToCreate);
const commits = await gitCommitsWithFiles(commitsToCreate);
process.chdir(path.join(gitRepo.cwd, 'module2'));
result = await onlyPackageCommits(commits);
const result = await onlyPackageCommits(commits);

expect(result).toHaveLength(2);
expect(result).not.toContainEqual(
Expand Down
10 changes: 3 additions & 7 deletions src/options-transforms.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { compose, composeP, lensProp } = require('ramda');
const { overA } = require('./lens-utils');
import { compose, composeP, lensProp } from 'ramda';
import { overA } from './lens-utils.js';

const commits = lensProp('commits');
const nextRelease = lensProp('nextRelease');
Expand All @@ -16,8 +16,4 @@ const withOptionsTransforms = transforms => plugin => async (
return plugin(pluginConfig, await composeP(...transforms)(config));
};

module.exports = {
mapCommits,
mapNextReleaseVersion,
withOptionsTransforms,
};
export { mapCommits, mapNextReleaseVersion, withOptionsTransforms };
8 changes: 2 additions & 6 deletions src/options-transforms.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const {
mapNextReleaseVersion,
mapLastReleaseVersionToLastReleaseGitTag,
mapNextReleaseVersionToNextReleaseGitTag,
mapCommits,
} = require('./options-transforms');
import { mapNextReleaseVersion, mapCommits } from './options-transforms.js';
import { describe, it, expect } from 'vitest';

const OPTIONS = {
commits: [1, 2, 3, 4],
Expand Down
6 changes: 3 additions & 3 deletions src/version-to-git-tag.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const readPkg = require('read-pkg');
import readPkg from 'read-pkg';

module.exports = async version => {
export default async function(version) {
if (!version) {
return null;
}

const { name } = await readPkg();
return `${name}-v${version}`;
};
}
6 changes: 3 additions & 3 deletions src/version-to-git-tag.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const versionToGitTag = require('./version-to-git-tag');
import versionToGitTag from './version-to-git-tag.js';
import { describe, it, expect } from 'vitest';

describe('#versionToGitTag', () => {
describe('if passed a falsy version', () => {
it('returns null rather than creating a bad git-tag', async done => {
it('returns null rather than creating a bad git-tag', () => async () => {
expect(await versionToGitTag('')).toBe(null);
expect(await versionToGitTag(undefined)).toBe(null);
expect(await versionToGitTag(null)).toBe(null);
done();
});
});
});
7 changes: 7 additions & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
include: ["./src/*.spec.js"],
},
});
Loading

0 comments on commit c8a20c0

Please sign in to comment.