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

Refactor to ESM #46

Merged
merged 2 commits into from
Jul 29, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock=false
save-exact=true
13 changes: 13 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 4,
"overrides": [
{
"files": ["*.json", "*.yml"],
"options": {
"tabWidth": 2
}
}
]
}
22 changes: 22 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import prettierConfig from 'eslint-config-prettier';
import prettierPlugin from 'eslint-plugin-prettier/recommended';
import globals from 'globals';
import js from '@eslint/js';

export default [
js.configs.recommended,
prettierConfig,
prettierPlugin,
{
languageOptions: {
globals: {
...globals.node,
...globals.browser,
global: true,
},
},
},
{
ignores: ['tap-snapshots/*', 'node_modules/*'],
},
];
108 changes: 54 additions & 54 deletions lib/analyze-commits.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
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_UPDATE = 'Version: ✅ Updated Eik version from v%s to v%s.';
const I_VERSION_NOT_NEEDED =
"Version: ✅ Not needed as the current version of this package already contains these files.";
'Version: ✅ Not needed as the current version of this package already contains these files.';
const I_PUBLISH_NOT_NEEDED =
"Publish: ✅ Not needed as the current version of this package is already published.";
'Publish: ✅ Not needed as the current version of this package is already published.';
const I_PUBLISH_NEEDED =
"Version: ✅ Not needed as the current Eik version not yet published.";
'Version: ✅ Not needed as the current Eik version not yet published.';
const E_SERVER_CONNECTION_ERROR =
"Version: ❌ Unable to connect with Eik server.";
'Version: ❌ Unable to connect with Eik server.';
const E_VERSIONING_ERROR =
"Version: ❌ Unable to perform versioning operation.";
'Version: ❌ Unable to perform versioning operation.';

/**
* 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) {
const { version: currentVersion, server, type, name } = state.eikJSON;
export default async function analyzeCommits(options, context, state) {
const { version: currentVersion, server, type, name } = state.eikJSON;

// attempt to version Eik assets
try {
state.versionToPublish = await eik.version({
name,
version: currentVersion,
type,
files: state.eikJSON.files,
server,
cwd: context.cwd,
level: options.level,
map: state.eikJSON.map,
out: state.eikJSON.out,
});
// attempt to version Eik assets
try {
state.versionToPublish = await eik.version({
name,
version: currentVersion,
type,
files: state.eikJSON.files,
server,
cwd: context.cwd,
level: options.level,
map: state.eikJSON.map,
out: state.eikJSON.out,
});

// successful versioning, publish is now needed
context.logger.log(
I_VERSION_UPDATE,
currentVersion,
state.versionToPublish
);
state.publishNeeded = true;
return options.level || "patch";
} catch (err) {
// unsuccessful versioning, handle reason individually
// successful versioning, publish is now needed
context.logger.log(
I_VERSION_UPDATE,
currentVersion,
state.versionToPublish,
);
state.publishNeeded = true;
return options.level || 'patch';
} catch (err) {
// unsuccessful versioning, handle reason individually

if (err.message.includes("Unable to fetch package metadata")) {
context.logger.log(E_SERVER_CONNECTION_ERROR);
}
if (err.message.includes('Unable to fetch package metadata')) {
context.logger.log(E_SERVER_CONNECTION_ERROR);
}

if (err.message.includes("Unable to hash local files for comparison")) {
context.logger.log(E_VERSIONING_ERROR);
}
if (err.message.includes('Unable to hash local files for comparison')) {
context.logger.log(E_VERSIONING_ERROR);
}

if (err.message.includes("package has not yet been published")) {
state.publishNeeded = true;
state.versionToPublish = currentVersion;
context.logger.log(I_PUBLISH_NEEDED);
return options.level || "patch";
}
if (err.message.includes('package has not yet been published')) {
state.publishNeeded = true;
state.versionToPublish = currentVersion;
context.logger.log(I_PUBLISH_NEEDED);
return options.level || 'patch';
}

if (err.message.includes("package already contains these files")) {
context.logger.log(I_VERSION_NOT_NEEDED);
state.publishNeeded = false;
context.logger.log(I_PUBLISH_NOT_NEEDED);
return null;
}
if (err.message.includes('package already contains these files')) {
context.logger.log(I_VERSION_NOT_NEEDED);
state.publishNeeded = false;
context.logger.log(I_PUBLISH_NOT_NEEDED);
return null;
}

throw err;
}
};
throw err;
}
}
28 changes: 14 additions & 14 deletions lib/generate-notes.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
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) {
if (!state.publishNeeded) return;
export default async function generateNotes(options, context, state) {
if (!state.publishNeeded) return;

const { name, server, type } = state.eikJSON;
const version = state.versionToPublish;
const date = new Date();
const dateString = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
const slug = typeSlug(type);
const versionURL = new URL(join(slug, name, version), server).href;
const nameURL = new URL(join(slug, name), server).href;
const { name, server, type } = state.eikJSON;
const version = state.versionToPublish;
const date = new Date();
const dateString = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
const slug = typeSlug(type);
const versionURL = new URL(join(slug, name, version), server).href;
const nameURL = new URL(join(slug, name), server).href;

return `(${dateString}) Version [${version}](${versionURL}) of Eik package [${name}](${nameURL}) published to [${server}](${server})`;
};
return `(${dateString}) Version [${version}](${versionURL}) of Eik package [${name}](${nameURL}) published to [${server}](${server})`;
}
38 changes: 19 additions & 19 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
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 = "";
eikJSON = {};
versionToPublish = null;
publishNeeded = null;
eikToken = '';
eikJSON = {};
versionToPublish = null;
publishNeeded = null;
}

const state = new State();

module.exports = {
verifyConditions: (options, ctx) => verifyConditions(options, ctx, state),
analyzeCommits: (options, ctx) => analyzeCommits(options, ctx, state),
verifyRelease: (options, ctx) => verifyRelease(options, ctx, state),
generateNotes: (options, ctx) => generateNotes(options, ctx, state),
prepare: (options, ctx) => prepare(options, ctx, state),
publish: (options, ctx) => publish(options, ctx, state),
success: (options, ctx) => success(options, ctx, state),
export default {
verifyConditions: (options, ctx) => verifyConditions(options, ctx, state),
analyzeCommits: (options, ctx) => analyzeCommits(options, ctx, state),
verifyRelease: (options, ctx) => verifyRelease(options, ctx, state),
generateNotes: (options, ctx) => generateNotes(options, ctx, state),
prepare: (options, ctx) => prepare(options, ctx, state),
publish: (options, ctx) => publish(options, ctx, state),
success: (options, ctx) => success(options, ctx, state),
};
26 changes: 13 additions & 13 deletions lib/prepare.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
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.";
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) {
if (!state.publishNeeded) return;
export default async function prepare(options, context, state) {
if (!state.publishNeeded) return;

if (state.eikJSON.version !== state.versionToPublish) {
context.logger.log(VERSION_WRITTEN, state.versionToPublish);
await json.writeEik(
{ version: state.versionToPublish },
{ cwd: context.cwd }
);
}
};
if (state.eikJSON.version !== state.versionToPublish) {
context.logger.log(VERSION_WRITTEN, state.versionToPublish);
await json.writeEik(
{ version: state.versionToPublish },
{ cwd: context.cwd },
);
}
}
58 changes: 32 additions & 26 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
const eik = require("@eik/cli");
import eik from '@eik/cli';

const PUBLISHED = "Publish: ✅ Published %s file(s) to Eik server %s";
const PUBLISHED = 'Publish: ✅ Published %s file(s) to Eik server %s';
const E_PUBLISH_FAILED =
"Publish: ❌ Failed, this may leave your build in a broken state. If retrying the build fails, try pushing a new commit.";
'Publish: ❌ Failed, this may leave your build in a broken state. If retrying the build fails, try pushing a new commit.';

/**
* 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) {
if (!state.publishNeeded) return;
export default async function publish(options, context, state) {
if (!state.publishNeeded) return;

try {
const result = await eik.publish({
name: state.eikJSON.name,
type: state.eikJSON.type,
server: state.eikJSON.server,
files: state.eikJSON.files,
cwd: context.cwd,
token: state.eikToken,
dryRun: false,
version: state.versionToPublish,
map: state.eikJSON.map,
out: state.eikJSON.out,
});
const filesPublished = result.files.filter((file) => file.type === "pkg");
context.logger.log(PUBLISHED, filesPublished.length, state.eikJSON.server);
} catch (err) {
context.logger.log(E_PUBLISH_FAILED);
throw err;
}
};
try {
const result = await eik.publish({
name: state.eikJSON.name,
type: state.eikJSON.type,
server: state.eikJSON.server,
files: state.eikJSON.files,
cwd: context.cwd,
token: state.eikToken,
dryRun: false,
version: state.versionToPublish,
map: state.eikJSON.map,
out: state.eikJSON.out,
});
const filesPublished = result.files.filter(
(file) => file.type === 'pkg',
);
context.logger.log(
PUBLISHED,
filesPublished.length,
state.eikJSON.server,
);
} catch (err) {
context.logger.log(E_PUBLISH_FAILED);
throw err;
}
}
Loading