Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ES Modules #526

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const {defaultTo, castArray} = require('lodash');
const verifyGit = require('./lib/verify.js');
const prepareGit = require('./lib/prepare.js');
import { defaultTo, castArray } from 'lodash-es';
import verifyGit from './lib/verify.js';
import prepareGit from './lib/prepare.js';

let verified;

function verifyConditions(pluginConfig, context) {
export function verifyConditions(pluginConfig, context) {
const {options} = context;
// If the Git prepare plugin is used and has `assets` or `message` configured, validate them now in order to prevent any release if the configuration is wrong
if (options.prepare) {
Expand All @@ -19,13 +19,11 @@ function verifyConditions(pluginConfig, context) {
verified = true;
}

async function prepare(pluginConfig, context) {
export async function prepare(pluginConfig, context) {
if (!verified) {
verifyGit(pluginConfig);
verified = true;
}

await prepareGit(pluginConfig, context);
}

module.exports = {verifyConditions, prepare};
}
21 changes: 13 additions & 8 deletions lib/definitions/errors.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
const pkg = require('../../package.json');
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const { homepage } = require('../../package.json');

const [homepage] = pkg.homepage.split('#');
const linkify = (file) => `${homepage}/blob/master/${file}`;

module.exports = {
EINVALIDASSETS: ({assets}) => ({
export function EINVALIDASSETS({ assets }) {
return ({
message: 'Invalid `assets` option.',
details: `The [assets option](${linkify(
'README.md#assets'
)}) option must be an \`Array\` of \`Strings\` or \`Objects\` with a \`path\` property.

Your configuration for the \`assets\` option is \`${assets}\`.`,
}),
EINVALIDMESSAGE: ({message}) => ({
});
}

export function EINVALIDMESSAGE({ message }) {
return ({
message: 'Invalid `message` option.',
details: `The [message option](${linkify('README.md#message')}) option, if defined, must be a non empty \`String\`.

Your configuration for the \`successComment\` option is \`${message}\`.`,
}),
};
});
}
6 changes: 3 additions & 3 deletions lib/get-error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const SemanticReleaseError = require('@semantic-release/error');
const ERROR_DEFINITIONS = require('./definitions/errors.js');
import SemanticReleaseError from '@semantic-release/error';
import ERROR_DEFINITIONS from './definitions/errors.js';

module.exports = (code, ctx) => {
export default function getError(code, ctx) {
const {message, details} = ERROR_DEFINITIONS[code](ctx);
return new SemanticReleaseError(message, code, details);
};
18 changes: 9 additions & 9 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const execa = require('execa');
const debug = require('debug')('semantic-release:git');
import execa from 'execa';
import debugFactory from 'debug';

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

/**
* Retrieve the list of files modified on the local repository.
Expand All @@ -8,7 +10,7 @@ const debug = require('debug')('semantic-release:git');
*
* @return {Array<String>} Array of modified files path.
*/
async function getModifiedFiles(execaOptions) {
export async function getModifiedFiles(execaOptions) {
return (await execa('git', ['ls-files', '-m', '-o'], execaOptions)).stdout
.split('\n')
.map((file) => file.trim())
Expand All @@ -21,7 +23,7 @@ async function getModifiedFiles(execaOptions) {
* @param {Array<String>} files Array of files path to add to the index.
* @param {Object} [execaOpts] Options to pass to `execa`.
*/
async function add(files, execaOptions) {
export async function add(files, execaOptions) {
const shell = await execa('git', ['add', '--force', '--ignore-errors', ...files], {...execaOptions, reject: false});
debug('add file to git index', shell);
}
Expand All @@ -34,7 +36,7 @@ async function add(files, execaOptions) {
*
* @throws {Error} if the commit failed.
*/
async function commit(message, execaOptions) {
export async function commit(message, execaOptions) {
await execa('git', ['commit', '-m', message], execaOptions);
}

Expand All @@ -47,7 +49,7 @@ async function commit(message, execaOptions) {
*
* @throws {Error} if the push failed.
*/
async function push(origin, branch, execaOptions) {
export async function push(origin, branch, execaOptions) {
await execa('git', ['push', '--tags', origin, `HEAD:${branch}`], execaOptions);
}

Expand All @@ -58,8 +60,6 @@ async function push(origin, branch, execaOptions) {
*
* @return {String} The sha of the head commit on the local repository
*/
async function gitHead(execaOptions) {
export async function gitHead(execaOptions) {
return (await execa('git', ['rev-parse', 'HEAD'], execaOptions)).stdout;
}

module.exports = {getModifiedFiles, add, gitHead, commit, push};
18 changes: 10 additions & 8 deletions lib/prepare.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const {isPlainObject, isArray, template, castArray, uniq} = require('lodash');
const micromatch = require('micromatch');
const dirGlob = require('dir-glob');
const pReduce = require('p-reduce');
const debug = require('debug')('semantic-release:git');
const resolveConfig = require('./resolve-config.js');
const {getModifiedFiles, add, commit, push} = require('./git.js');
import { isPlainObject, isArray, template, castArray, uniq } from 'lodash-es';
import micromatch from 'micromatch';
import dirGlob from 'dir-glob';
import pReduce from 'p-reduce';
import debugFactory from 'debug';
import resolveConfig from './resolve-config.js';
import { getModifiedFiles, add, commit, push } from './git.js';

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

/**
* Prepare a release commit including configurable files.
Expand All @@ -18,7 +20,7 @@ const {getModifiedFiles, add, commit, push} = require('./git.js');
* @param {Object} context.nextRelease The next release.
* @param {Object} logger Global logger.
*/
module.exports = async (pluginConfig, context) => {
export default async function prepare (pluginConfig, context) {
const {
env,
cwd,
Expand Down
20 changes: 11 additions & 9 deletions lib/resolve-config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const {isNil, castArray} = require('lodash');
import { isNil, castArray } from 'lodash-es';

module.exports = ({assets, message}) => ({
assets: isNil(assets)
? ['CHANGELOG.md', 'package.json', 'package-lock.json', 'npm-shrinkwrap.json']
: assets
? castArray(assets)
: assets,
message,
});
export default function resolveConfig ({assets, message}) {
return {
assets: isNil(assets)
? ['CHANGELOG.md', 'package.json', 'package-lock.json', 'npm-shrinkwrap.json']
: assets
? castArray(assets)
: assets,
message,
}
};
10 changes: 5 additions & 5 deletions lib/verify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {isString, isNil, isArray, isPlainObject} = require('lodash');
const AggregateError = require('aggregate-error');
const getError = require('./get-error.js');
const resolveConfig = require('./resolve-config.js');
import { isString, isNil, isArray, isPlainObject } from 'lodash-es';
import AggregateError from 'aggregate-error';
import getError from './get-error.js';
import resolveConfig from './resolve-config.js';

const isNonEmptyString = (value) => isString(value) && value.trim();
const isStringOrStringArray = (value) => {
Expand All @@ -27,7 +27,7 @@ const VALIDATORS = {
* @param {String|Array<String|Object>} [pluginConfig.assets] Files to include in the release commit. Can be files path or globs.
* @param {String} [pluginConfig.message] The commit message for the release.
*/
module.exports = (pluginConfig) => {
export default function verify(pluginConfig) {
const options = resolveConfig(pluginConfig);

const errors = Object.entries(options).reduce(
Expand Down
Loading
Loading