From 6bc8d9b6bbd7ca56d0e575c6a53d6f91a30272f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=BF=8A?= Date: Wed, 25 Dec 2019 21:08:04 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=A2=9E=E5=8A=A0angular=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E7=9A=84=E6=8F=90=E4=BA=A4=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 36 ++++----- package-lock.json | 156 +++++++++++++++++-------------------- package.json | 71 +++++++++-------- src/commander/index.ts | 7 +- src/config/promps.ts | 56 +++++++------ src/git/commit.ts | 21 ++++- src/git/rebase.ts | 0 src/template/commitType.ts | 45 ++++++++++- src/template/rc.ts | 15 ++-- src/utils/gitpro.ts | 43 +++++----- src/utils/index.ts | 20 ++++- 11 files changed, 280 insertions(+), 190 deletions(-) create mode 100644 src/git/rebase.ts diff --git a/README.md b/README.md index 9d795cf..b742550 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ npm install -g git-pro #### git commit 增强 - 带 emoji 选择的 commit 提交 + 带类型选择的 commit 提交,会向上查找配置文件 ```shell git-pro commit @@ -49,31 +49,31 @@ git-pro status # 就相当于 git status 默认的提交选项很多,有时候我们可能不需要这么多,甚至可能需要特殊的提交类型,这时候可以使用自定义功能进行配置 ```shell -git-pro custom +git-pro custom [-g] ``` -执行后会在项目根目录下生成`.gitprorc.js`文件 +执行后会在项目根目录下生成`.gitprorc.js`文件,如果存在`-g`参数,会在用户根目录生成 -| 参数说明 | 类型 | 说明 | -| :-----------: | :----: | :----------- | -| commitList | Object | 提交类型列表 | -| commitMessage | Object | 提交信息 | +| 参数说明 | 类型 | 说明 | +| :------: | :----: | :----------------------------------- | +| type | String | 'emoji' / 'text' | +| types | Array | 提交类型自定义列表 | +| scopes | Array | scope 列表,如果存在,提交时会提供选项 | -##### commitList 配置 +##### types 配置 -| 参数说明 | 类型 | 说明 | -| :------: | :----: | :--------------------- | -| message | String | 提交类型选择时提示文字 | -| choices | Array | 提交类型类别 | +| 参数说明 | 类型 | 说明 | +| :------: | :----: | :--------------- | +| name | String | 提交类型提示文字 | +| value | Array | 提交类型值 | -##### commitMessage 配置 +##### messages 配置 -| 参数说明 | 类型 | 说明 | -| :------: | :----: | :--------------------- | -| message | String | 提交信息输入时提示文字 | +| 参数说明 | 类型 | 说明 | +| :------: | :----: | :--------------- | +| xxx | String | 不同阶段提示文字 | ## TODO -1. ~~自定义commit类型列表~~ 2. 按使用热度排序 -3. 历史记录调用 \ No newline at end of file +3. 历史记录调用 diff --git a/package-lock.json b/package-lock.json index 8620a42..16a12c9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,15 +5,15 @@ "requires": true, "dependencies": { "@types/node": { - "version": "10.12.18", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.18.tgz", - "integrity": "sha512-fh+pAqt4xRzPfqA6eh3Z2y6fyZavRIumvjhaCL753+TVkGKGhpPeyrJG2JftD0T9q4GF00KjefsQ+PQNDdWQaQ==", + "version": "10.17.12", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.12.tgz", + "integrity": "sha512-SSB4O9/0NVv5mbQ5/MabnAyFfcpVFRVIJj1TZkG21HHgwXQGjosiQB3SBWC9pMCMUTNpWL9gUe//9mFFPQAdKw==", "dev": true }, "ansi-escapes": { - "version": "3.1.0", - "resolved": "http://registry.npmjs.org/ansi-escapes/-/ansi-escapes-3.1.0.tgz", - "integrity": "sha512-UgAb8H9D41AQnu/PbWlCofQVcnV4Gs2bBJi9eZPxfU/hgglFh3SMDMENRIqdr7H6XFnXdoknctFByVsCOotTVw==" + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-3.2.0.tgz", + "integrity": "sha512-cBhpre4ma+U0T1oM5fXg7Dy1Jw7zzwv7lt/GoCpr+hDQJoYnKVPLL4dCvSEFMmQurOQvSrwT7SL/DAlhBI97RQ==" }, "ansi-regex": { "version": "3.0.0", @@ -43,9 +43,9 @@ } }, "chalk": { - "version": "2.4.1", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz", - "integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==", + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", "requires": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", @@ -66,9 +66,9 @@ } }, "cli-spinners": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-1.3.1.tgz", - "integrity": "sha512-1QL4544moEsDVH9T/l6Cemov/37iv1RtoKf7NJ04A60+4MREXNfx/QvavbH6QoGdsD4N4Mwy49cmaINR/o2mdg==" + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-2.2.0.tgz", + "integrity": "sha512-tgU3fKwzYjiLEQgPMD9Jt+JjHVL9kW93FiIMX/l7rivvOD4/LL0Mf7gda3+4U2KJBloybwgj5KEoQgGRioMiKQ==" }, "cli-width": { "version": "2.2.0", @@ -94,9 +94,9 @@ "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, "commander": { - "version": "2.19.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.19.0.tgz", - "integrity": "sha512-6tvAOO+D6OENvRAh524Dh9jcfKTYDQAqvqezbCW82xj5X0pSrcpxtvRKHLG0yBY6SD7PSDrJaj+0AiOcKVd1Xg==" + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==" }, "concat-map": { "version": "0.0.1", @@ -117,9 +117,9 @@ "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" }, "external-editor": { - "version": "3.0.3", - "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-3.0.3.tgz", - "integrity": "sha512-bn71H9+qWoOQKyZDo25mOMVpSmXROAsTJVVVYzrrtol3d4y+AsKjf4Iwl2Q+IuT0kFSQ1qo166UuIwqYq7mGnA==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-3.1.0.tgz", + "integrity": "sha512-hMQ4CX1p1izmuLYyZqLMO/qGNw10wSv9QDCPfzXfyFrOaCSSoRfqE1Kf1s5an66J5JZC62NewG+mK49jOCtQew==", "requires": { "chardet": "^0.7.0", "iconv-lite": "^0.4.24", @@ -150,9 +150,9 @@ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, "glob": { - "version": "7.1.3", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz", - "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", + "version": "7.1.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", + "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -163,9 +163,9 @@ } }, "graceful-fs": { - "version": "4.1.15", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz", - "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==" + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.3.tgz", + "integrity": "sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==" }, "has-flag": { "version": "3.0.0", @@ -190,34 +190,34 @@ } }, "inherits": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", - "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "inquirer": { - "version": "6.2.1", - "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-6.2.1.tgz", - "integrity": "sha512-088kl3DRT2dLU5riVMKKr1DlImd6X7smDhpXUCkJDCKvTEJeRiXh0G132HG9u5a+6Ylw9plFRY7RuTnwohYSpg==", + "version": "6.5.2", + "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-6.5.2.tgz", + "integrity": "sha512-cntlB5ghuB0iuO65Ovoi8ogLHiWGs/5yNrtUcKjFhSSiVeAIVpD7koaSU9RM8mpXw5YDi9RdYXGQMaOURB7ycQ==", "requires": { - "ansi-escapes": "^3.0.0", - "chalk": "^2.0.0", + "ansi-escapes": "^3.2.0", + "chalk": "^2.4.2", "cli-cursor": "^2.1.0", "cli-width": "^2.0.0", - "external-editor": "^3.0.0", + "external-editor": "^3.0.3", "figures": "^2.0.0", - "lodash": "^4.17.10", + "lodash": "^4.17.12", "mute-stream": "0.0.7", "run-async": "^2.2.0", - "rxjs": "^6.1.0", + "rxjs": "^6.4.0", "string-width": "^2.1.0", - "strip-ansi": "^5.0.0", + "strip-ansi": "^5.1.0", "through": "^2.3.6" } }, "interpret": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.1.0.tgz", - "integrity": "sha1-ftGxQQxqDg94z5XTuEQMY/eLhhQ=" + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.2.0.tgz", + "integrity": "sha512-mT34yGKMNceBQUoVn7iCDKDntA7SC6gycMAWzGx1z/CMCTV7b2AAtXlo3nRyHZ1FelRkQbQjprHSYGwzLtkVbw==" }, "is-fullwidth-code-point": { "version": "2.0.0", @@ -238,9 +238,9 @@ } }, "lodash": { - "version": "4.17.11", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", - "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" }, "log-symbols": { "version": "2.2.0", @@ -285,36 +285,26 @@ } }, "ora": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/ora/-/ora-3.0.0.tgz", - "integrity": "sha512-LBS97LFe2RV6GJmXBi6OKcETKyklHNMV0xw7BtsVn2MlsgsydyZetSCbCANr+PFLmDyv4KV88nn0eCKza665Mg==", + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/ora/-/ora-3.4.0.tgz", + "integrity": "sha512-eNwHudNbO1folBP3JsZ19v9azXWtQZjICdr3Q0TDPIaeBQ3mXLrh54wM+er0+hSp+dWKf+Z8KM58CYzEyIYxYg==", "requires": { - "chalk": "^2.3.1", + "chalk": "^2.4.2", "cli-cursor": "^2.1.0", - "cli-spinners": "^1.1.0", + "cli-spinners": "^2.0.0", "log-symbols": "^2.2.0", - "strip-ansi": "^4.0.0", + "strip-ansi": "^5.2.0", "wcwidth": "^1.0.1" - }, - "dependencies": { - "strip-ansi": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", - "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", - "requires": { - "ansi-regex": "^3.0.0" - } - } } }, "os-tmpdir": { "version": "1.0.2", - "resolved": "http://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=" }, "path-is-absolute": { "version": "1.0.1", - "resolved": "http://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" }, "path-parse": { @@ -323,9 +313,9 @@ "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==" }, "prettier": { - "version": "1.15.3", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.15.3.tgz", - "integrity": "sha512-gAU9AGAPMaKb3NNSUUuhhFAS7SCO4ALTN4nRIn6PJ075Qd28Yn2Ig2ahEJWdJwJmlEBTUfC7mMUSFy8MwsOCfg==" + "version": "1.19.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.19.1.tgz", + "integrity": "sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==" }, "rechoir": { "version": "0.6.2", @@ -336,9 +326,9 @@ } }, "resolve": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.9.0.tgz", - "integrity": "sha512-TZNye00tI67lwYvzxCxHGjwTNlUV70io54/Ed4j6PscB8xVfuBJpRenI/o6dVk0cY0PYTY27AgCoGGxRnYuItQ==", + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.14.1.tgz", + "integrity": "sha512-fn5Wobh4cxbLzuHaE+nphztHy43/b++4M6SsGFC2gB8uYwf0C8LcarfCz1un7UTW8OFQg9iNjZ4xpcFVGebDPg==", "requires": { "path-parse": "^1.0.6" } @@ -361,9 +351,9 @@ } }, "rxjs": { - "version": "6.3.3", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.3.3.tgz", - "integrity": "sha512-JTWmoY9tWCs7zvIk/CvRjhjGaOd+OVBM987mxFo+OW66cGpdKjZcpmc74ES1sB//7Kl/PAe8+wEakuhG4pcgOw==", + "version": "6.5.3", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.5.3.tgz", + "integrity": "sha512-wuYsAYYFdWTAnAaPoKGNhfpWwKZbJW+HgAJ+mImp+Epl7BG8oNWBCTyRM8gba9k4lk8BgWdoYm21Mo/RYhhbgA==", "requires": { "tslib": "^1.9.0" } @@ -408,17 +398,17 @@ } }, "strip-ansi": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.0.0.tgz", - "integrity": "sha512-Uu7gQyZI7J7gn5qLn1Np3G9vcYGTVqB+lFTytnDJv83dd8T22aGH451P3jueT2/QemInJDfxHB5Tde5OzgG1Ow==", + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", "requires": { - "ansi-regex": "^4.0.0" + "ansi-regex": "^4.1.0" }, "dependencies": { "ansi-regex": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.0.0.tgz", - "integrity": "sha512-iB5Dda8t/UqpPI/IjsejXu5jOGDrzn41wJyljwPH65VCIbk6+1BzFIMJGFwTNrYXT1CrD+B4l19U7awiQ8rk7w==" + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==" } } }, @@ -432,7 +422,7 @@ }, "through": { "version": "2.3.8", - "resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" }, "tmp": { @@ -444,14 +434,14 @@ } }, "tslib": { - "version": "1.9.3", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz", - "integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==" + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.10.0.tgz", + "integrity": "sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==" }, "typescript": { - "version": "3.2.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.2.2.tgz", - "integrity": "sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==", + "version": "3.7.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.4.tgz", + "integrity": "sha512-A25xv5XCtarLwXpcDNZzCGvW2D1S3/bACratYBx2sax8PefsFhlYmkQicKHvpYflFS8if4zne5zT5kpJ7pzuvw==", "dev": true }, "universalify": { diff --git a/package.json b/package.json index 11d3e3f..575ab90 100644 --- a/package.json +++ b/package.json @@ -1,36 +1,39 @@ { - "name": "git-pro", - "version": "0.2.6", - "description": "git commit 增强工具", - "main": "lib/index.js", - "scripts": { - "start": "ts-node src/index.ts", - "build": "tsc -d -P tsconfig.json" - }, - "bin": { - "git-pro": "lib/index.js", - "gitp": "lib/index.js" - }, - "repository": { - "type": "git", - "url": "https://github.com/GodD6366/git-pro.git" - }, - "author": "", - "license": "ISC", - "devDependencies": { - "@types/node": "^10.12.18", - "typescript": "^3.2.2" - }, - "dependencies": { - "chalk": "^2.4.1", - "commander": "^2.19.0", - "fs-extra": "^7.0.1", - "inquirer": "^6.2.0", - "ora": "^3.0.0", - "prettier": "^1.15.3", - "shelljs": "^0.8.2" - }, - "engines": { - "node": ">=8.4.0" - } + "name": "git-pro", + "version": "0.2.6", + "description": "git commit 增强工具", + "main": "lib/index.js", + "scripts": { + "start": "ts-node src/index.ts", + "dev": "tsc -d -w -P tsconfig.json", + "build": "tsc -d -P tsconfig.json" + }, + "bin": { + "git-pro": "lib/index.js", + "gitp": "lib/index.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/GodD6366/git-pro.git" + }, + "author": "", + "license": "ISC", + "devDependencies": { + "@types/node": "^10.12.18", + "typescript": "^3.2.2" + }, + "dependencies": { + "chalk": "^2.4.1", + "commander": "^2.19.0", + "debug": "^4.1.1", + "fs-extra": "^7.0.1", + "inquirer": "^6.2.0", + "ora": "^3.0.0", + "prettier": "^1.15.3", + "shelljs": "^0.8.2", + "user-home": "^2.0.0" + }, + "engines": { + "node": ">=8.4.0" + } } diff --git a/src/commander/index.ts b/src/commander/index.ts index 7e43676..842f6df 100755 --- a/src/commander/index.ts +++ b/src/commander/index.ts @@ -39,10 +39,11 @@ program }); program - .command('custom') + .command('custom [options]') .description('进行一些自定义配置') - .action(() => { - init(); + .option('-g, --glob', '全局') + .action((name: String, cmd: any) => { + init(cmd || {}); }); /** diff --git a/src/config/promps.ts b/src/config/promps.ts index b477177..0a55dda 100644 --- a/src/config/promps.ts +++ b/src/config/promps.ts @@ -1,33 +1,43 @@ // emoji 参考 https://gitmoji.carloscuesta.me/ import { getRcInfo } from '../utils/gitpro'; -import { choices } from '../template/commitType'; +import { emojiTypes, textTypes } from '../template/commitType'; +const debug = require('debug')('git-pro#src/config/promps.ts'); let customConfig = getRcInfo(); +debug('customConfig', customConfig); export default { ciType: [ - Object.assign( - { - type: 'list', - name: 'type', - message: '请选择本次提交的类型:', - choices, - }, - customConfig.commitList - ), - ], - ciMsg: Object.assign( { - type: 'input', - name: 'msg', - message: '请输入提交文本:', - validate: function(value: String) { - if (value) { - return true; - } - return '文本必须输入!'; - }, + type: 'list', + name: 'type', + message: customConfig.messages.type || '选择一种你的提交类型:', + choices: customConfig.type === 'emoji' ? emojiTypes : textTypes, + }, + ], + ciScope: + customConfig.scopes.length > 0 + ? { + type: 'list', + name: 'scope', + message: customConfig.messages.scopeList || '选择一个scope (可选):', + choices: customConfig.scopes, + } + : { + type: 'input', + name: 'scope', + message: customConfig.messages.scope || '请输入scope (可选):', + }, + + ciMsg: { + type: 'input', + name: 'msg', + message: customConfig.messages.subject || '请输入提交信息:', + validate: function(value: String) { + if (value) { + return true; + } + return '提交信息必须输入!'; }, - customConfig.commitMessage - ), + }, }; diff --git a/src/git/commit.ts b/src/git/commit.ts index 13c66ee..aeb8c12 100644 --- a/src/git/commit.ts +++ b/src/git/commit.ts @@ -2,19 +2,36 @@ const shell = require('shelljs'); const inquirer = require('inquirer'); const chalk = require('chalk'); const ora = require('ora'); +const debug = require('debug')('git-pro#src/git/commit.ts'); + import prompsConfig from '../config/promps'; import checkAdded from './checkAdded'; +import { getRcInfo } from '../utils/gitpro'; + +let customConfig = getRcInfo(); async function commit() { if (!checkAdded()) { return; } + const isTextType = customConfig.type === 'text'; let { type } = await inquirer.prompt(prompsConfig.ciType); - let { msg } = await inquirer.prompt(prompsConfig.ciMsg); + + const prompList = []; + if (isTextType) { + prompList.push(prompsConfig.ciScope); + } + prompList.push(prompsConfig.ciMsg); + + let { scope, msg } = await inquirer.prompt(prompList); const commitSpinner = ora('git commit').start(); - let commitMsg = `git commit -m "${type} ${msg}"`; + const pref = isTextType ? `${type}${scope ? `(${scope})` : ''}:` : `${type}`; + + let commitMsg = `git commit -m "${pref} ${msg}"`; + + debug(commitMsg); const { code, stdout, stderr } = shell.exec(commitMsg); if (code !== 0) { commitSpinner.fail('commit failed!'); diff --git a/src/git/rebase.ts b/src/git/rebase.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/template/commitType.ts b/src/template/commitType.ts index 94d052e..8d76ea7 100644 --- a/src/template/commitType.ts +++ b/src/template/commitType.ts @@ -1,4 +1,4 @@ -export const choices = [ +export const emojiTypes = [ { name: '🚧 工作进行中', value: ':construction:', @@ -96,3 +96,46 @@ export const choices = [ value: ':heavy_minus_sign:', }, ]; + +export const textTypes = [ + { + name: 'feat: 添加新功能', + value: 'feat', + }, + { + name: 'fix: 修复 bug', + value: 'fix', + }, + { + name: 'WIP: 工作进行中', + value: 'WIP', + }, + { + name: 'docs: 撰写文档', + value: 'docs', + }, + { + name: 'style: 更新用户界面和样式文件', + value: 'style', + }, + { + name: 'refactor: 改进代码的结构/格式', + value: 'refactor', + }, + { + name: 'perf: 提升性能', + value: 'perf', + }, + { + name: 'chore: 更改配置文件', + value: 'chore', + }, + { + name: 'revert: 回滚一次提交', + value: 'revert', + }, + { + name: 'test: 增加测试', + value: 'test', + }, +]; diff --git a/src/template/rc.ts b/src/template/rc.ts index 04fb268..396ab4b 100644 --- a/src/template/rc.ts +++ b/src/template/rc.ts @@ -1,13 +1,14 @@ -import { choices } from './commitType'; +import { textTypes } from './commitType'; const RC_TEMPLATE = ` module.exports = { - commitList: { - // message: '选择本次提交的类型:', - choices: ${JSON.stringify(choices)} - }, - commitMessage: { - // message: '请输入提交文本:', + // 目前只有两种类型 text / emoji + type: 'emoji', + messages: { + type: '选择一种你的提交类型:', + scopeList: '选择一个scope (可选):', + scope: '请输入scope (可选):', + subject: '请输入提交信息:', } }; `; diff --git a/src/utils/gitpro.ts b/src/utils/gitpro.ts index 7339739..ecdd45a 100644 --- a/src/utils/gitpro.ts +++ b/src/utils/gitpro.ts @@ -1,12 +1,22 @@ -import { getGitProRcPath } from './index'; +import { getGitProRcPath, CONFIG_FILE_NAME } from './index'; import RC_TEMPLATE from '../template/rc'; const shell = require('shelljs'); const fs = require('fs-extra'); +const path = require('path'); +const userHome = require('user-home'); +const debug = require('debug')('git-pro#src/utils/gitpro.ts'); + +export function init({ glob = false }) { + let config = ''; + debug('glob', glob); + if (glob) { + config = path.join(userHome, CONFIG_FILE_NAME); + } else { + config = path.join(process.cwd(), CONFIG_FILE_NAME); + } -export function init() { - let config = getGitProRcPath(); if (fs.existsSync(config)) { - console.log('文件已经存在'); + console.log('文件已经存在', config); } else { fs.outputFileSync(config, RC_TEMPLATE); shell.exec(`prettier --write ${config}`); @@ -15,20 +25,17 @@ export function init() { export function getRcInfo() { let config: string = getGitProRcPath(); + let rcConfig = {}; if (fs.existsSync(config)) { - // fs.readFileSync(config, 'utf-8'); - let rcConfig = require(config); - // return { - // commitList: { - // message: rcConfig.commitList.message, - // choices: rcConfig.commitList.choices - // }, - // commitMessage: { message: rcConfig.commitMessage.message } - // }; - return { - commitList: rcConfig.commitList || {}, - commitMessage: rcConfig.commitMessage || {}, - }; + debug('配置文件存在'); + rcConfig = require(config); } - return {}; + return Object.assign( + { + type: 'emoji', + messages: {} as any, + scopes: [], + }, + rcConfig + ); } diff --git a/src/utils/index.ts b/src/utils/index.ts index 80a9652..16ca5ae 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,13 +1,31 @@ const shell = require('shelljs'); const path = require('path'); +const fs = require('fs'); +const userHome = require('user-home'); +const debug = require('debug')('git-pro#src/utils/index.ts'); + shell.config.silent = true; +export const CONFIG_FILE_NAME = '.gitprorc.js'; + export function getPwd(): string { let { stdout } = shell.exec('pwd'); return stdout.trim(); } export function getGitProRcPath(): string { - let rcConfig = path.resolve(getPwd(), '.gitprorc.js'); + let parentDir = getPwd(); + let rcConfig = ''; + + debug('parentDir', parentDir); + debug('userHome', userHome); + + while (!rcConfig || (!fs.existsSync(rcConfig) && parentDir !== userHome)) { + parentDir = path.dirname(path.dirname(rcConfig)); + debug('尝试在父级目录读取 ".gitprorc.js"', rcConfig); + rcConfig = path.resolve(parentDir, '.gitprorc.js'); + } + + debug('rcConfig', rcConfig); return rcConfig; }