Skip to content

Commit

Permalink
feat: migrate to semantic-release 24
Browse files Browse the repository at this point in the history
  • Loading branch information
Stamo-Gochev committed Jan 24, 2025
1 parent a57ef3b commit 3bd491b
Show file tree
Hide file tree
Showing 19 changed files with 6,746 additions and 2,396 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = {
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": "latest"
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
}
Expand Down
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
# . "$(dirname "$0")/_/husky.sh"

npx --no -- commitlint --edit "$1"
3 changes: 3 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# require meeting the Node version restriction strictly
# defined in package.json -> engines section
engine-strict=true
85 changes: 43 additions & 42 deletions analyzeCommits.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const analyzeCommits = require('@semantic-release/commit-analyzer')
const SemanticReleaseError = require('@semantic-release/error')
const execSync = require('child_process').execSync;
const lastTag = require('./lastTag');
const utils = require('./utils');
import SemanticReleaseError from '@semantic-release/error';
import { execSync } from 'child_process';
import { lastTag } from './lastTag.js';
import { ghActionsBranch } from './utils.js';

const until = f => array => {
const first = array[0];
Expand All @@ -11,7 +10,7 @@ const until = f => array => {
return [];
}

return [ first, ...until(f)(array.slice(1)) ];
return [first, ...until(f)(array.slice(1))];
};

const lastTaggedRelease = () => {
Expand All @@ -21,47 +20,49 @@ const lastTaggedRelease = () => {
return execSync(`git rev-list ${args}`, { encoding: 'utf8' }).trim();
};

module.exports = function (pluginConfig, config, cb) {
export default async function (pluginConfig, config) {
let commitAnalyzer = (await import('@semantic-release/commit-analyzer'));
// run standard commit analysis
return analyzeCommits(pluginConfig, config, function(error, type) {
const branch = config.env.TRAVIS_BRANCH || config.env.GIT_LOCAL_BRANCH || utils.ghActionsBranch(config.env);
const branchTags = config.options.branchTags;
const distTag = branchTags && branchTags[branch];
const analyzeCommitsResult = await commitAnalyzer.analyzeCommits(pluginConfig, config);
const type = analyzeCommitsResult;

// use default behavior if not publishing a custom dist-tag
if (!distTag) {
return cb(error, type);
}
const branch = config.env.TRAVIS_BRANCH || config.env.GIT_LOCAL_BRANCH || ghActionsBranch(config.env);
const branchTags = config.options.branchTags;
const distTag = branchTags && branchTags[branch];

let releaseType = type;
if (type) {
// map all types of releases to prereleases
releaseType = {
'major': 'premajor',
'minor': 'preminor',
'patch': 'prepatch'
}[type] || type;
// use default behavior if not publishing a custom dist-tag
if (!distTag) {
return type;
}

console.log("Publishing a " + releaseType + " release.");
}
let releaseType = type;
if (type) {
// map all types of releases to prereleases
releaseType = {
'major': 'premajor',
'minor': 'preminor',
'patch': 'prepatch'
}[type] || type;

// suppress NPM releases of non-feature commits (chore/docs/etc)
const lastReleaseHash = lastTaggedRelease();
const untilLastRelease = until(commit => commit.hash === lastReleaseHash);
const commits = untilLastRelease(config.commits);
const commitSubset = Object.assign({}, config, { commits });
console.log("Publishing a " + releaseType + " release.");
}

analyzeCommits(pluginConfig, commitSubset, function(_, type) {
if (!type) {
// commits since last dist-tag release are empty, suppress release
return cb(new SemanticReleaseError(
'There are no relevant changes, so no new version is released.',
'ENOCHANGE'
));
}
// suppress NPM releases of non-feature commits (chore/docs/etc)
const lastReleaseHash = lastTaggedRelease();
const untilLastRelease = until(commit => commit.hash === lastReleaseHash);
const commits = untilLastRelease(config.commits);
const commitSubset = Object.assign({}, config, { commits });

cb(error, releaseType);
});
});
};
const result = await commitAnalyzer.analyzeCommits(pluginConfig, commitSubset)

if (!type) {
// commits since last dist-tag release are empty, suppress release
return new SemanticReleaseError(
'There are no relevant changes, so no new version is released.',
'ENOCHANGE'
);
}

return result;
}

4 changes: 2 additions & 2 deletions bin/find-dev-deps.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

const fs = require('fs');
const json = fs.readFileSync('package.json', { encoding: 'utf-8' });
import { readFileSync } from 'fs';
const json = readFileSync('package.json', { encoding: 'utf-8' });
const meta = JSON.parse(json);
const deps = Object.assign({}, meta.dependencies, meta.peerDependencies);

Expand Down
61 changes: 32 additions & 29 deletions bin/semantic-prerelease.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
#!/usr/bin/env node

const utils = require('../utils');
const path = require('path');
const validateConfig = require('../validateConfig');
const config = require(path.resolve('package.json'));
const branch = process.env.TRAVIS_BRANCH || process.env.GIT_LOCAL_BRANCH || utils.ghActionsBranch(process.env);
import { ghActionsBranch } from '../utils.js';
import validateConfig from '../validateConfig.js';
import config from 'package.json';
import { exec } from 'child_process';
const branch = process.env.TRAVIS_BRANCH || process.env.GIT_LOCAL_BRANCH || ghActionsBranch(process.env);
const branchTags = config.release && config.release.branchTags;
const tag = branchTags && branchTags[branch];
const dryRun = process.argv.find(arg => /^(--dry-run|-n)$/.test(arg));
const publicPackage = process.argv.find(arg => /^(--public)$/.test(arg));
const validate = process.argv.find(arg => /^(--validate|-v)$/.test(arg));
const command = [ 'npm', 'publish' ];
const command = ['npm', 'publish'];

if (validate) {
function semanticRelease() {
if (validate) {
validateConfig(config);
return;
}

if (tag) {
command.push('--tag', tag);
}
}
// } else {
if (tag) {
command.push('--tag', tag);
}

if (publicPackage) {
command.push('--access=public');
}
if (publicPackage) {
command.push('--access=public');
}

if (!branchTags) {
console.warn('[WARN] No branch tag configuration');
}
if (!branchTags) {
console.warn('[WARN] No branch tag configuration');
}

if (dryRun) {
console.log(command.join(' '));
} else {
const exec = require('child_process').exec;
exec(command.join(' '), function(error, stdout, stderr) {
console.log(stdout);
if (dryRun) {
console.log(command.join(' '));
} else {
exec(command.join(' '), function (error, stdout, stderr) {
console.log(stdout);

if (error) {
console.error(`[ERROR] npm publish: ${stderr}`);
process.exit(1);
}
});
if (error) {
console.error(`[ERROR] npm publish: ${stderr}`);
process.exit(1);
}
});
}
}

semanticRelease();
3 changes: 3 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
extends: ["@commitlint/config-conventional"],
};
4 changes: 2 additions & 2 deletions condition-github-actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var SRError = require('@semantic-release/error')
import SRError from '@semantic-release/error'

module.exports = function (pluginConfig, config, cb) {
export function verifyConditions(pluginConfig, config, cb) {
var env = config.env

if (!Object.hasOwnProperty.call(env, 'GITHUB_ACTION')) {
Expand Down
14 changes: 7 additions & 7 deletions generateNotes.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const changelog = require('conventional-changelog')
const parseUrl = require('github-url-from-git')
const lastTag = require('./lastTag');
import conventionalChangelog from 'conventional-changelog';
import parseUrl from 'github-url-from-git';
import { lastTag } from './lastTag.js';

module.exports = function (pluginConfig, {pkg}, cb) {
export default function (pluginConfig, {pkg}, cb) {
console.log("============================= generate notes");
const repository = pkg.repository ? parseUrl(pkg.repository.url) : null
const from = lastTag();

changelog({
conventionalChangelog({
version: pkg.version,
repository: repository,
from: from,
file: false
}, cb)
};

}
14 changes: 7 additions & 7 deletions getLastRelease.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const defaultLastRelease = require('@semantic-release/last-release-npm');
const lastTag = require('./lastTag');
const utils = require('./utils');
module.exports = function (pluginConfig, config, cb) {
import defaultLastRelease from '@semantic-release/last-release-npm';
import lastTag from './lastTag';
import { ghActionsBranch } from './utils';

export default function (pluginConfig, config, cb) {
let branch;
let oldTag;

Expand All @@ -10,7 +11,7 @@ module.exports = function (pluginConfig, config, cb) {
} else if (config.env.GIT_LOCAL_BRANCH) {
branch = config.env.GIT_LOCAL_BRANCH;
} else if (config.env.GITHUB_REF) {
branch = utils.ghActionsBranch(config.env);
branch = ghActionsBranch(config.env);
} else {
throw new Error('Unable to determine Git branch. Tried TRAVIS_BRANCH, GIT_LOCAL_BRANCH and GITHUB_REF');
}
Expand All @@ -34,5 +35,4 @@ module.exports = function (pluginConfig, config, cb) {
}
cb(err, res);
});
};

}
8 changes: 3 additions & 5 deletions lastTag.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const execSync = require('child_process').execSync;
import { execSync } from 'child_process';

const lastTag = ({ branch = 'origin/master', dev = true } = {}) => {
export const lastTag = ({ branch = 'origin/master', dev = true } = {}) => {
const exclude = dev ? ' --exclude="*dev*"' : '';
return execSync(`git describe --tags --match "v[0-9]*" ${exclude} --abbrev=0 ${branch} || true`,
{ encoding: 'utf8' }
).trim();
};

module.exports = lastTag;
}
Loading

0 comments on commit 3bd491b

Please sign in to comment.