@lzwme/git-repo-sync
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

@lzwme/git-repo-sync

@lzwme/git-repo-sync

NPM version node version npm download GitHub issues GitHub forks GitHub stars

git 多仓库代码同步辅助工具。

应用场景:

当某个 npm 包在内部 npm 私域和 npmjs 上均有发布,但从内部向外部同步部署时,可能需要过滤敏感信息、替换一些不同的内容。 这个工作是重复且枯燥的,此时 grs 小工具或许能帮助到你。

特点:

  • 仓库文件同步
  • 支持文件内容匹配替换
  • 支持文件过滤
  • 支持执行自定义命令(配置文件)
  • 支持 git commit 提交记录同步(最近一条)
  • more...

示例:

初始化一个同步目录:

执行 grs 同步(无更新):

执行 grs 同步(有更新):

安装

# use npm
npm i -D @lzwme/git-repo-sync
# use yarn
yarn add -D @lzwme/git-repo-sync
# use pnpm
yarn add -D @lzwme/git-repo-sync

使用

命令行方式

可通过命令行执行,临时的进行手动同步:

grs --help
# 复杂的内容替换规则建议使用配置文件方式。新增 .grs.confg.js 配置文件,然后执行:
grs --debug
# 示例: 指定配置文件
grs -c .grs.config.js

也可结合 husky@7 配置至 git hooks 中,如 post-commit

# 在项目中安装 husky
yarn add -D husky
yarn husky install

# 创建 post-commit hook
yarn add .husky/post-commit "yarn grs -c .grs.config.js"

API 调用

提供了如下 API 调用方式:

import { grs } from '@lzwme/git-repo-sync';

const total = grs.sync({
  silent: true,
  git: {
    commit: true,
    rebase: true,
    push: true,
  }
});
console.log(total);

配置文件参考

// .grs.config.js
const path = require('path');

module.exports = {
  src: process.cwd(),
  dest: path.resolve(process.cwd(), `../${path.basename(process.cwd())}-sync`),
  silent: false,
  exclude: [/(\/|\\)\.git(\/|\\)/, 'dist', '.nyc', 'node_modules', '.grs.config.js'],
  include: ['src', 'package.json', 'readme.md'],
  replaceRules: [
    {
      /** 文件匹配规则,如不存在则表示匹配所有文件 */
      match: /README\.md/i,
      list: [
        {
          /** 匹配关键词,若为空则忽略 */
          from: '@test',
          /** 替换为 */
          to: '@lzwme',
        },
        {
          /** 匹配关键词,若为空则忽略 */
          from: /^\[.+\n/gm,
          /** 替换为 */
          to: '',
        },
      ],
    },
    {
      /** 文件匹配规则,如不存在则表示匹配所有文件 */
      match: /package\.json/i,
      list: [
        {
          /** 匹配关键词,若为空则忽略 */
          from: '@test',
          /** 替换为 */
          to: '@lzwme',
        },
      ],
    },
    {
      /** 文件匹配规则,如不存在则表示匹配所有文件 */
      match: null,
      list: [
        {
          from: 'https://gitlab.com/lzwme/fed-lint-helper',
          to: 'https://github.com/lzwme/fed-lint-helper',
        },
      ],
    },
  ],
  /** git 同步选项 */
  git: {
    /** 是否执行 commit */
    commit: true,
    /** 是否执行 rebase。适合多人协作的场景 */
    rebase: false,
    /** 是否执行 push */
    push: false,
    /** 是否跳过 git hooks。默认 false */
    noVerify: false,
  },
  /** 在 dest 同步目录执行的命令列表 */
  cmds: [],
};

开发

本地二次开发:

git clone https://github.com/lzwme/git-repo-sync
yarn install
npm link
yarn dev

或者 fork 本项目进行代码贡献。

欢迎贡献想法与代码。

License

@lzwme/git-repo-sync is released under the MIT license.

该插件由志文工作室开发和维护。

Package Sidebar

Install

npm i @lzwme/git-repo-sync

Weekly Downloads

1

Version

1.0.4

License

MIT

Unpacked Size

25.2 kB

Total Files

9

Last publish

Collaborators

  • renxia001
  • renxia