Skip to content
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
15 changes: 13 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ on:
- patch
- minor
- major
target_version:
description: 'Optional exact version override (for example, 1.2.1). If set, version_type is ignored.'
required: false
type: string

jobs:
release:
Expand All @@ -36,8 +40,15 @@ jobs:

- name: Bump version
id: bump
env:
VERSION_TYPE: ${{ github.event.inputs.version_type }}
TARGET_VERSION: ${{ github.event.inputs.target_version }}
run: |
node scripts/bump-version.js ${{ github.event.inputs.version_type }} > bump_output.txt
if [ -n "$TARGET_VERSION" ]; then
node scripts/bump-version.js "$TARGET_VERSION" > bump_output.txt
else
node scripts/bump-version.js "$VERSION_TYPE" > bump_output.txt
fi
cat bump_output.txt
NEW_VERSION=$(grep "NEW_VERSION=" bump_output.txt | cut -d'=' -f2)
OLD_VERSION=$(grep "OLD_VERSION=" bump_output.txt | cut -d'=' -f2)
Expand Down Expand Up @@ -67,7 +78,7 @@ jobs:
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add manifest.template.json package.json
git add manifest.template.json package.json package-lock.json
git commit -m "chore: release version ${{ steps.bump.outputs.new_version }}"
git tag -a "v${{ steps.bump.outputs.new_version }}" -m "Release version ${{ steps.bump.outputs.new_version }}"
git push origin main
Expand Down
56 changes: 49 additions & 7 deletions scripts/bump-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rootDir = join(__dirname, '..');

// Get version type from command line args (patch, minor, major)
const versionType = process.argv[2] || 'patch';
// Get version type or exact target version from command line args.
// Supported values: patch, minor, major, or an exact Chrome extension version
// such as 1.2.1.
const versionInput = process.argv[2] || 'patch';

if (!['patch', 'minor', 'major'].includes(versionType)) {
console.error('Usage: node bump-version.js [patch|minor|major]');
const VERSION_PATTERN = /^\d+\.\d+\.\d+$/;

if (!['patch', 'minor', 'major'].includes(versionInput) && !VERSION_PATTERN.test(versionInput)) {
console.error('Usage: node bump-version.js [patch|minor|major|x.y.z]');
process.exit(1);
}

Expand All @@ -37,19 +41,57 @@ function bumpVersion(version, type) {
return parts.join('.');
}

function compareVersions(a, b) {
const aParts = a.split('.').map(Number);
const bParts = b.split('.').map(Number);

for (let i = 0; i < 3; i++) {
if (aParts[i] !== bParts[i]) {
return aParts[i] - bParts[i];
}
}

return 0;
}

function writeJson(path, data) {
writeFileSync(path, JSON.stringify(data, null, 2) + '\n');
}

// Update manifest.template.json
const manifestPath = join(rootDir, 'manifest.template.json');
const manifest = JSON.parse(readFileSync(manifestPath, 'utf-8'));
const oldVersion = manifest.version;
const newVersion = bumpVersion(oldVersion, versionType);
const newVersion = VERSION_PATTERN.test(versionInput) ? versionInput : bumpVersion(oldVersion, versionInput);

if (compareVersions(newVersion, oldVersion) <= 0) {
console.error(`Error: new version (${newVersion}) must be greater than current version (${oldVersion})`);
process.exit(1);
}

manifest.version = newVersion;
writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + '\n');
writeJson(manifestPath, manifest);

// Update package.json
const packagePath = join(rootDir, 'package.json');
const packageJson = JSON.parse(readFileSync(packagePath, 'utf-8'));
packageJson.version = newVersion;
writeFileSync(packagePath, JSON.stringify(packageJson, null, 2) + '\n');
writeJson(packagePath, packageJson);

// Update package-lock.json if present so release commits keep npm metadata in sync.
const packageLockPath = join(rootDir, 'package-lock.json');
try {
const packageLockJson = JSON.parse(readFileSync(packageLockPath, 'utf-8'));
packageLockJson.version = newVersion;
if (packageLockJson.packages?.['']) {
packageLockJson.packages[''].version = newVersion;
}
writeJson(packageLockPath, packageLockJson);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

console.log(`Version bumped from ${oldVersion} to ${newVersion}`);
console.log(`NEW_VERSION=${newVersion}`);
Expand Down
Loading