Files
backstage/scripts/prepare-release.js
T
Patrik Oldsberg e6bef72496 scripts: rename pre-release to prepare-release + cleanup
Co-authored-by: Johan Haals <johan.haals@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
2022-01-12 11:40:16 +01:00

111 lines
3.2 KiB
JavaScript
Executable File

#!/usr/bin/env node
/* eslint-disable import/no-extraneous-dependencies */
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs-extra');
const semver = require('semver');
const { getPackages } = require('@manypkg/get-packages');
const path = require('path');
const DEPENDENCY_TYPES = [
'dependencies',
'devDependencies',
'optionalDependencies',
'peerDependencies',
];
async function main() {
const patchedJsonPath = path.resolve('.changeset', 'patched.json');
const { currentReleaseVersion } = await fs.readJSON(patchedJsonPath);
if (Object.keys(currentReleaseVersion).length === 0) {
console.log('No currentReleaseVersion overrides found, skipping.');
return;
}
const { packages } = await getPackages(path.resolve('.'));
const pendingVersionBumps = new Map();
for (const [name, version] of Object.entries(currentReleaseVersion)) {
const pkg = packages.find(p => p.packageJson.name === name);
if (!pkg) {
throw new Error(`Package ${name} not found`);
}
if (!semver.valid(version)) {
throw new Error(`Invalid base version ${version} for package ${name}`);
}
const currentVersion = semver.parse(pkg.packageJson.version);
const targetVersion = semver.parse(version).inc('patch');
targetVersion.prerelease = currentVersion.prerelease;
const targetVersionString = targetVersion.format();
pendingVersionBumps.set(name, {
targetVersion: targetVersionString,
targetRange: `^${targetVersionString}`,
});
}
for (const { dir, packageJson } of packages) {
let hasChanges = false;
if (pendingVersionBumps.has(packageJson.name)) {
packageJson.version = pendingVersionBumps.get(
packageJson.name,
).targetVersion;
hasChanges = true;
}
for (const depType of DEPENDENCY_TYPES) {
const deps = packageJson[depType];
for (const depName of Object.keys(deps ?? {})) {
const currentRange = deps[depName];
if (currentRange === '*') {
continue;
}
if (pendingVersionBumps.has(depName)) {
const pendingBump = pendingVersionBumps.get(depName);
console.log(
`Replacing ${depName} ${currentRange} with ${pendingBump.targetRange} in ${depType} of ${packageJson.name}`,
);
deps[depName] = pendingBump.targetRange;
hasChanges = true;
}
}
}
if (hasChanges) {
await fs.writeJSON(path.resolve(dir, 'package.json'), packageJson, {
spaces: 2,
});
}
}
await fs.writeJSON(
patchedJsonPath,
{ currentReleaseVersion: {} },
{ spaces: 2 },
);
}
main().catch(error => {
console.error(error.stack);
process.exit(1);
});