Skip to content

Commit

Permalink
refactor: rewrite to ESM
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this module is now esm
  • Loading branch information
wkillerud committed Jul 29, 2024
1 parent 4af25ea commit fc473a7
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 45 deletions.
8 changes: 4 additions & 4 deletions lib/analyze-commits.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const eik = require('@eik/cli');
import eik from '@eik/cli';

const I_VERSION_UPDATE = 'Version: ✅ Updated Eik version from v%s to v%s.';
const I_VERSION_NOT_NEEDED =
Expand All @@ -15,13 +15,13 @@ const E_VERSIONING_ERROR =
/**
* Semantic Release "Analyze Commits" hook
* Responsible for determining the type of the next release (major, minor or patch). If multiple plugins with a analyzeCommits step are defined, the release type will be the highest one among plugins output.
* See https://github.com/semantic-release/semantic-release/blob/master/docs/usage/plugins.md#plugins
* See https://semantic-release.gitbook.io/semantic-release/usage/plugins
*
* @param {object} options
* @param {object} context
* @param {object} state
*/
module.exports = async function analyzeCommits(options, context, state) {
export default async function analyzeCommits(options, context, state) {
const { version: currentVersion, server, type, name } = state.eikJSON;

// attempt to version Eik assets
Expand Down Expand Up @@ -73,4 +73,4 @@ module.exports = async function analyzeCommits(options, context, state) {

throw err;
}
};
}
10 changes: 5 additions & 5 deletions lib/generate-notes.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const { join } = require('path');
const { typeSlug } = require('@eik/common-utils');
import { join } from 'path';
import { typeSlug } from '@eik/common-utils';

/**
* Semantic Release "generate notes" hook
* "Responsible for generating the content of the release note. If multiple plugins with a generateNotes step are defined, the release notes will be the result of the concatenation of each plugin output."
* See https://github.com/semantic-release/semantic-release/blob/master/docs/usage/plugins.md#plugins
* See https://semantic-release.gitbook.io/semantic-release/usage/plugins
*
* @param {object} options
* @param {object} context
* @param {object} state
*/
module.exports = async function generateNotes(options, context, state) {
export default async function generateNotes(options, context, state) {
if (!state.publishNeeded) return;

const { name, server, type } = state.eikJSON;
Expand All @@ -22,4 +22,4 @@ module.exports = async function generateNotes(options, context, state) {
const nameURL = new URL(join(slug, name), server).href;

return `(${dateString}) Version [${version}](${versionURL}) of Eik package [${name}](${nameURL}) published to [${server}](${server})`;
};
}
16 changes: 8 additions & 8 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const verifyConditions = require('./verify-conditions');
const analyzeCommits = require('./analyze-commits');
const verifyRelease = require('./verify-release');
const generateNotes = require('./generate-notes');
const prepare = require('./prepare');
const publish = require('./publish');
const success = require('./success');
import verifyConditions from './verify-conditions.js';
import analyzeCommits from './analyze-commits.js';
import verifyRelease from './verify-release.js';
import generateNotes from './generate-notes.js';
import prepare from './prepare.js';
import publish from './publish.js';
import success from './success.js';

class State {
eikToken = '';
Expand All @@ -15,7 +15,7 @@ class State {

const state = new State();

module.exports = {
export default {
verifyConditions: (options, ctx) => verifyConditions(options, ctx, state),
analyzeCommits: (options, ctx) => analyzeCommits(options, ctx, state),
verifyRelease: (options, ctx) => verifyRelease(options, ctx, state),
Expand Down
8 changes: 4 additions & 4 deletions lib/prepare.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const json = require('@eik/cli/utils/json');
import json from '@eik/cli/utils/json/index.js';

const VERSION_WRITTEN = 'Version: ✅ v%s written back to eik.json file.';

/**
* Semantic Release "prepare" hook
* "Responsible for preparing the release, for example creating or updating files such as package.json, CHANGELOG.md, documentation or compiled assets and pushing a commit."
* See https://github.com/semantic-release/semantic-release/blob/master/docs/usage/plugins.md#plugins
* See https://semantic-release.gitbook.io/semantic-release/usage/plugins
*
* @param {object} options
* @param {object} context
* @param {object} state
*/
module.exports = async function prepare(options, context, state) {
export default async function prepare(options, context, state) {
if (!state.publishNeeded) return;

if (state.eikJSON.version !== state.versionToPublish) {
Expand All @@ -21,4 +21,4 @@ module.exports = async function prepare(options, context, state) {
{ cwd: context.cwd },
);
}
};
}
8 changes: 4 additions & 4 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const eik = require('@eik/cli');
import eik from '@eik/cli';

const PUBLISHED = 'Publish: ✅ Published %s file(s) to Eik server %s';
const E_PUBLISH_FAILED =
Expand All @@ -7,13 +7,13 @@ const E_PUBLISH_FAILED =
/**
* Semantic Release "publish" hook
* "Responsible for publishing the release."
* See https://github.com/semantic-release/semantic-release/blob/master/docs/usage/plugins.md#plugins
* See https://semantic-release.gitbook.io/semantic-release/usage/plugins
*
* @param {object} options
* @param {object} context
* @param {object} state
*/
module.exports = async function publish(options, context, state) {
export default async function publish(options, context, state) {
if (!state.publishNeeded) return;

try {
Expand Down Expand Up @@ -41,4 +41,4 @@ module.exports = async function publish(options, context, state) {
context.logger.log(E_PUBLISH_FAILED);
throw err;
}
};
}
11 changes: 5 additions & 6 deletions lib/success.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { join } = require('path');
const { default: fetch } = require('node-fetch');
const { typeSlug } = require('@eik/common-utils');
import { join } from 'node:path';
import { typeSlug } from '@eik/common-utils';

const PUBLISH_SUCCESS =
'Publish: ✅ Successfully published package %s (v%s) to %s.';
Expand All @@ -10,13 +9,13 @@ const PUBLISH_FAILURE =
/**
* Semantic Release "success" hook
* "Responsible for notifying of a new release."
* See https://github.com/semantic-release/semantic-release/blob/master/docs/usage/plugins.md#plugins
* See https://semantic-release.gitbook.io/semantic-release/usage/plugins
*
* @param {object} options
* @param {object} context
* @param {object} state
*/
module.exports = async function success(options, context, state) {
export default async function success(options, context, state) {
if (!state.publishNeeded) return;
const { logger } = context;
const { server, name, type } = state.eikJSON;
Expand All @@ -35,4 +34,4 @@ module.exports = async function success(options, context, state) {
result.status,
);
}
};
}
12 changes: 6 additions & 6 deletions lib/verify-conditions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const AggregateError = require('aggregate-error');
const eik = require('@eik/cli');
const { configStore } = require('@eik/common-config-loader');
import eik from '@eik/cli';
import { configStore } from '@eik/common-config-loader';
import AggregateError from 'aggregate-error';

const E_TOKEN_MISSING = 'Verify: ❌ EIK_TOKEN environment variable not found';
const E_LEVEL_INVALID =
Expand All @@ -11,13 +11,13 @@ const LEVELS = ['major', 'minor', 'patch'];
/**
* Semantic Release "verify conditions" hook
* "Verify all the conditions to proceed with the release."
* See https://github.com/semantic-release/semantic-release/blob/master/docs/usage/plugins.md#plugins
* See https://semantic-release.gitbook.io/semantic-release/usage/plugins
*
* @param {object} options
* @param {object} context
* @param {object} state
*/
module.exports = async function verifyConditions(options, context, state) {
export default async function verifyConditions(options, context, state) {
const errors = [];

// verify EIK_TOKEN present in the environment
Expand Down Expand Up @@ -46,4 +46,4 @@ module.exports = async function verifyConditions(options, context, state) {
if (errors.length > 0) {
throw new AggregateError(errors);
}
};
}
8 changes: 4 additions & 4 deletions lib/verify-release.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const eik = require('@eik/cli');
import eik from '@eik/cli';

const E_NO_FILES = `Verify: ❌ No files detected for upload. Did you run bundling first (if needed)? Is the "files" field of eik.json correct?`;
const E_VERIFY_FAIL = 'Verify: ❌ Verification checks failed to complete';
Expand All @@ -7,13 +7,13 @@ const FOUND_FILES = 'Verify: ✅ Found %d file(s) to publish';
/**
* Semantic Release "verify release" hook
* "Responsible for verifying the parameters (version, type, dist-tag etc...) of the release that is about to be published."
* See https://github.com/semantic-release/semantic-release/blob/master/docs/usage/plugins.md#plugins
* See https://semantic-release.gitbook.io/semantic-release/usage/plugins
*
* @param {object} options
* @param {object} context
* @param {object} state
*/
module.exports = async function verifyRelease(options, context, state) {
export default async function verifyRelease(options, context, state) {
if (!state.publishNeeded) return;

try {
Expand All @@ -40,4 +40,4 @@ module.exports = async function verifyRelease(options, context, state) {
context.logger.log(E_VERIFY_FAIL);
throw err;
}
};
}
6 changes: 2 additions & 4 deletions test/main.js → test/main.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const plugin = require('../lib/main.js');
const tap = require('tap');
import tap from 'tap';
import plugin from '../lib/main.js';

tap.test('Simple test', (t) => {
t.type(plugin.verifyConditions, 'function');
Expand Down

0 comments on commit fc473a7

Please sign in to comment.