🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

sd-release-plus

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sd-release-plus

Lightweight release script for Front-end project

latest
npmnpm
Version
1.0.4
Version published
Maintainers
1
Created
Source

sd-release-plus

使用

安装

pnpm add -D  sd-release-plus

根路径新增发布脚本 scripts/release.ts

import defaultMain from sd-release-plus;
defaultMain();

package.json 增加 scripts 钩子

For example:

{
  "scripts": {
    "release": "ts-node ./scripts/release.ts"
  },
  "devDependencies": {
    "sd-release-plus": "^1.0.0"
  }
}

执行脚本

pnpm run release

默认发布流程

  • 选择升级版本 ->
  • 修改 package.json 的版本号 ->
  • 填写 commit 信息(校验格式,不通过终止) ->
  • 推送到代码仓库(推送失败会帮你把 package.json 里的版本回退) ->
  • 生成 changeLog ->
  • build 打包(这里执行的是 pnpm run build 所以你的 scripts 里必须有 build 命令,要不执行失败) ->
  • 发布包(执行的 npm publish --access=public 命令) ->
  • 打 tag 并且推送到 git 仓库

自定义流程

上面几个流程是这个包自带流程顺序,你可以配自己的顺序,

import { getNextVersion, gitPush, setChangelog, build, publishNpm, addTag, compose } from '@mx-design/release';

// getNextVersion 获取下个版本号函数
// updateVersion 修改版本号 返回值是回退版本号的函数
// gitPush push代码函数
// setChangelog 设置changeLog函数
// build 执行npm run build函数
// publish 执行npm publish函数
// addTag 打tag函数
// compose函数组合器

// 使用方法,这些函数顺序可以自己换位置,或者增删减
const middle = [getNextVersion, updateVersion, gitPush, setChangelog, build, publishNpm, addTag];

function execRelease() {
  compose(middle);
}

中间件原理

// compose 函数
export function compose(middleware) {
  // 共享数据仓库,每个函数都可以往里面加数据,所有函数都可以访问到里面的数据
  const otherOptions = {};
  // 函数发射器,一个函数执行完毕调用next(),就执行下一个函数
  function dispatch(index, otherOptions) {
    // 每次从middleware中间件里面选取一个函数执行,直到执行了全部函数
    if (index == middleware.length) return;
    const currMiddleware = middleware[index];
    // 执行函数,传入了一下一个函数的dispatch,已经更新共享数据
    currMiddleware((addOptions) => {
      dispatch(++index, { ...otherOptions, ...addOptions });
    }, otherOptions).catch((error) => {
      console.log('💣 发布失败,失败原因:', error);
    });
  }
  dispatch(0, otherOptions);
}
// middleware 写法

// 获取版本号函数,固定参数第一个是next,表示调用middleware里下一个函数
const getNextVersion = async (next) => {
  // _selectNextVersion会查看你package.json里的version参数是啥版本
  const nextVersion = await _selectNextVersion();
  // 缓存老的package.json数据,为了后面可以回退版本有老版本数据做准备
  const originPackageJson = getOriginPackageJson();
  // next传入的数据都会放到共享数据仓库,下面的函数都可以拿到这些数据
  next({
    nextVersion,
    originVersion: originPackageJson.version,
    originPackageJson,
  });
};

// 更新版本号方法
const updateVersion = async (next, otherOptions) => {
  if (!otherOptions?.nextVersion) {
    throw new Error('请传入package.json新版本号');
  }
  // 返回一个回退版本号的方法,上面的函数共享的originPackageJson数据就是给他用的
  // basicCatchError如果命令执行失败,就打印失败原因,并且返回false
  const backVersionFn = await _updateVersion(otherOptions.nextVersion, otherOptions.originPackageJson).catch(
    basicCatchError,
  );
  // 把回退版本的函数共享出来,给下面需要用的地方自己调用
  next({ backVersionFn });
};
// 调用
compose([getNextVersion, updateVersion]);

Keywords

release script

FAQs

Package last updated on 05 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts