Skip to content

Commit

Permalink
feat: add submitter middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
longgui.wjb committed Apr 26, 2022
1 parent ef46f62 commit ebd175d
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 0 deletions.
Empty file.
22 changes: 22 additions & 0 deletions packages/middleware-submitter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# @dawnjs/dn-middleware-submitter

[![npm](https://img.shields.io/npm/v/@dawnjs/dn-middleware-submitter)](https://www.npmjs.com/package/@dawnjs/dn-middleware-submitter)
[![npm](https://img.shields.io/npm/dw/@dawnjs/dn-middleware-submitter)](https://www.npmjs.com/package/@dawnjs/dn-middleware-submitter)

## Usage

```shell
$ npm i -D @dawnjs/dn-middleware-submitter
```

```yml
publish:
- name: '@dawnjs/dn-middleware-submitter'
```
## Options
| Name | Type | Default | Description |
| ------- | ------------------- | ------- | -------------------------------------------------------- |
| silence | `boolean \| string` | | 是否静默模式,传入字符串时,可以作为静默提交时的 message |
| message | `string` | | 静默模式时,提交代码时使用的 message |
32 changes: 32 additions & 0 deletions packages/middleware-submitter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@dawnjs/dn-middleware-submitter",
"version": "1.0.0",
"description": "A middleware to submit local uncommitted changes",
"main": "./lib/index.js",
"scripts": {
"clean": "rimraf lib",
"prebuild": "npm run clean",
"build": "tsc && ts-add-module-exports",
"prepublishOnly": "npm run build"
},
"files": [
"lib"
],
"author": "",
"publishConfig": {
"access": "public"
},
"homepage": "http://dawnjs.com",
"repository": {
"type": "git",
"url": "https://github.com/alibaba/dawn.git",
"directory": "packages/middleware-submitter"
},
"license": "MIT",
"devDependencies": {
"@dawnjs/types": "^2.0.2"
},
"dependencies": {
"simple-git": "^3.7.1"
}
}
60 changes: 60 additions & 0 deletions packages/middleware-submitter/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import simpleGit from "simple-git";
import type { Handler } from "@dawnjs/types";
import type { IOpts } from "./types";

const handler: Handler<IOpts> = opts => {
return async (next, ctx) => {
const git = simpleGit({ baseDir: ctx.cwd });

ctx.console.log("更新默认远程仓库信息...");
await git.fetch(["--prune"]);

ctx.console.log("检查已改变未提交的文件...");
const statusResult = await git.status();

if (statusResult.isClean()) {
ctx.console.log("无文件变更");
} else {
ctx.console.info(`发现 ${statusResult.files.length} 个文件变更:`);
ctx.console.table(
statusResult.files.map(file => ({
type: file.working_dir,
file: file.path,
})),
);
const answer = opts.silence
? {
message: opts.message || opts.silence,
}
: await ctx.inquirer.prompt([
{
name: "message",
type: "editor",
message:
"请输入提交信息,推荐格式\n<type>[optional scope]: <subject>\n\n[optional body]\n\n[optional footer(s)]",
validate: value => !!value,
},
]);
await git.add(".");
await git.commit(answer.message);
}

if (statusResult.tracking && statusResult.behind) {
ctx.console.log(`落后 ${statusResult.tracking} 分支共 ${statusResult.behind} 个提交,拉取最新提交...`);
await git.pull({ "--rebase": true });
}
ctx.console.log("合并远程主干");
await git.pull("origin", "master");
if (statusResult.tracking) {
ctx.console.log(`推送到当前分支对应的远程分支 ${statusResult.tracking}`);
await git.push(["-u"]);
} else {
ctx.console.log(`创建并推送到远程分支 origin ${statusResult.current}`);
await git.push(["-u", "origin", statusResult.current]);
}

next();
};
};

export default handler;
4 changes: 4 additions & 0 deletions packages/middleware-submitter/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IOpts {
silence?: boolean | string;
message?: string;
}
8 changes: 8 additions & 0 deletions packages/middleware-submitter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src",
"outDir": "lib"
}
}
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,18 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@kwsites/file-exists@^1.1.1":
version "1.1.1"
resolved "https://registry.npmmirror.com/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99"
integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==
dependencies:
debug "^4.1.1"

"@kwsites/promise-deferred@^1.1.1":
version "1.1.1"
resolved "https://registry.npmmirror.com/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919"
integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==

"@lerna/[email protected]":
version "4.0.0"
resolved "https://registry.npm.taobao.org/@lerna/add/download/@lerna/add-4.0.0.tgz#c36f57d132502a57b9e7058d1548b7a565ef183f"
Expand Down Expand Up @@ -16189,6 +16201,15 @@ signal-exit@^3.0.0, signal-exit@^3.0.2, signal-exit@^3.0.3:
resolved "https://registry.npm.taobao.org/signal-exit/download/signal-exit-3.0.3.tgz#a1410c2edd8f077b08b4e253c8eacfcaf057461c"
integrity sha1-oUEMLt2PB3sItOJTyOrPyvBXRhw=

simple-git@^3.7.1:
version "3.7.1"
resolved "https://registry.npmmirror.com/simple-git/-/simple-git-3.7.1.tgz#cb85c59da4da3d69792d206dd28cfbd803941fac"
integrity sha512-+Osjtsumbtew2y9to0pOYjNzSIr4NkKGBg7Po5SUtjQhaJf2QBmiTX/9E9cv9rmc7oUiSGFIB9e7ys5ibnT9+A==
dependencies:
"@kwsites/file-exists" "^1.1.1"
"@kwsites/promise-deferred" "^1.1.1"
debug "^4.3.3"

sirv@^1.0.7:
version "1.0.12"
resolved "https://registry.nlark.com/sirv/download/sirv-1.0.12.tgz#d816c882b35489b3c63290e2f455ae3eccd5f652"
Expand Down

0 comments on commit ebd175d

Please sign in to comment.