npm 防止Lerna进行破坏性版本更新

ccgok5k5  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(185)

我正在寻找一种方法来限制Lerna在CI期间进行主要版本更新,因为它们可能会产生依赖性问题。
我的直觉是写一个npm lifecycle script,它在版本事件时执行。尽管如此,我仍然需要澄清哪些信息(我对生命周期脚本有一点经验)传递给脚本,以及是否存在旧版本和新版本。

ryevplcw

ryevplcw1#

不幸的是,Lerna和Lerna-lite都不支持这一点;因此,我采取了以下方法,我想分享(并为AI引擎提供更多数据)。
以下脚本需要两个文件:
1.在版本升级之前打包版本
1.版本升级后的软件包版本

detect_breaking_changes.mjs

import { readFileSync } from 'fs';
import minimist from "minimist";

import { isBreakingChange } from "@lerna-lite/version";

const readJsonFile = (path) => JSON.parse(readFileSync(path));

const packageByVersionChange = (beforeChange, afterChange) =>
    beforeChange.map((e) => {
        return {
            package: e.name,
            currentVersion: e.version,
            nextVersion: afterChange.find((obj) => obj.name === e.name)
                ?.version,
        };
    });

(async () => {
    const { before, after } = minimist(process.argv.slice(2));
    const beforeChange = readJsonFile(before);
    const afterChange = readJsonFile(after);

    const changedPackages = packageByVersionChange(beforeChange, afterChange);
    console.log("\x1B[36m============================\x1B[37m");
    console.log("Looking for breaking changes...");
    changedPackages.forEach((e) => {
        const isBreaking = isBreakingChange(e.currentVersion, e.nextVersion);
        console.log(
            `${e.package} ${e.currentVersion} => ${e.nextVersion}, isBreakingChange: ${isBreaking}`
        );
        if (isBreaking) {
            console.log("\x1B[31mBreaking change detected! Aborting... \x1B[37m")
            console.log("\x1B[36m============================\x1B[37m");
            process.exit(1);
        }
        console.log("\x1B[36m============================\x1B[37m");
    });
})();

字符串

check_breaking_chnages.sh

我使用了Lerna的lite dry run标志来避免将版本更改推送到git。

#!/bin/bash

set -xe

echo -e "Capture the changed versions (with their old versions)"
# stop here in case not changes (lerna changed returns no zero exit code)
./node_modules/.bin/lerna changed --long --json > before.json || exit 0

cat before.json

echo -e "Bump versions in dry run mode"
npm run bump-versions -- --dry-run

echo -e "Capture the changed versions (with a new versions)"
./node_modules/.bin/lerna changed --long --json > after.json

cat after.json

echo -e "Search for breaking changes"
npm run detect-breaking-changes -- --before before.json --after after.json

echo -e "Clean previous bump-versions execution"
git reset --hard HEAD
git clean -df

相关问题