zen-gitsync 1.0.3 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +36 -36
  3. package/package.json +1 -1
  4. package/src/gitCommit.js +166 -121
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 xz333221
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 xz333221
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,36 +1,36 @@
1
-
2
- # zen-gitsync
3
-
4
- `zen-gitsync` 是一个简单的命令行工具,用于自动化 Git 提交和推送操作。只需在控制台输入 `g`,即可自动执行 `git add`、`git commit` 和 `git push` 操作,极大提升 Git 工作流程的效率。
5
-
6
- ## 特性
7
-
8
- - 一键执行 `git add`、`git commit`、`git push`
9
- - 使用交互式命令行输入提交信息
10
- - 提交信息颜色随机显示
11
-
12
- ## 安装
13
-
14
- 通过 npm 安装 `zen-gitsync`,并进行全局安装:
15
-
16
- ```bash
17
- npm install -g zen-gitsync
18
- ```
19
-
20
- ## 使用方法
21
-
22
- 1. 在终端中,输入 `g` 并按回车。
23
- 2. 输入提交信息,按回车确认提交。
24
- 3. 工具将自动执行以下操作:
25
- - `git add .`
26
- - `git commit -m "你的提交信息"`
27
- - `git push`
28
-
29
- ### 示例:
30
-
31
- ```bash
32
- $ g
33
- 请输入你的提交信息: 修复了登录页样式问题
34
- ```
35
-
36
- 完成提交后,你会看到一个随机颜色的提交信息确认
1
+
2
+ # zen-gitsync
3
+
4
+ `zen-gitsync` 是一个简单的命令行工具,用于自动化 Git 提交和推送操作。只需在控制台输入 `g`,即可自动执行 `git add`、`git commit` 和 `git push` 操作,极大提升 Git 工作流程的效率。
5
+
6
+ ## 特性
7
+
8
+ - 一键执行 `git add`、`git commit`、`git push`
9
+ - 使用交互式命令行输入提交信息
10
+ - 提交信息颜色随机显示
11
+
12
+ ## 安装
13
+
14
+ 通过 npm 安装 `zen-gitsync`,并进行全局安装:
15
+
16
+ ```bash
17
+ npm install -g zen-gitsync
18
+ ```
19
+
20
+ ## 使用方法
21
+
22
+ 1. 在终端中,输入 `g` 并按回车。
23
+ 2. 输入提交信息,按回车确认提交。
24
+ 3. 工具将自动执行以下操作:
25
+ - `git add .`
26
+ - `git commit -m "你的提交信息"`
27
+ - `git push`
28
+
29
+ ### 示例:
30
+
31
+ ```bash
32
+ $ g
33
+ 请输入你的提交信息: 修复了登录页样式问题
34
+ ```
35
+
36
+ 完成提交后,你会看到一个随机颜色的提交信息确认
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-gitsync",
3
- "version": "1.0.3",
3
+ "version": "1.2.0",
4
4
  "description": "控制台输入g,回车,输入提交内容,自动执行git add+commit+push",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/gitCommit.js CHANGED
@@ -1,121 +1,166 @@
1
- #!/usr/bin/env node
2
-
3
- const { exec, execSync } = require('child_process')
4
-
5
- // 有时候有乱码呢123神奇
6
- const readline = require('readline')
7
- const rl = readline.createInterface({
8
- input: process.stdin,
9
- output: process.stdout
10
- })
11
-
12
- function rlPromisify (fn) {
13
- return async (...args) => {
14
- return new Promise((resolve,reject) => fn(...args, resolve, reject))
15
- }
16
- }
17
-
18
- const question = rlPromisify(rl.question.bind(rl))
19
-
20
- const colors = [
21
- '\x1b[31m', // 红色
22
- '\x1b[32m', // 绿色
23
- '\x1b[33m', // 黄色
24
- '\x1b[34m', // 蓝色
25
- '\x1b[35m', // 紫色
26
- '\x1b[36m', // 青色
27
- ];
28
-
29
- function getRandomColor() {
30
- const randomIndex = Math.floor(Math.random() * colors.length);
31
- return colors[randomIndex];
32
- }
33
-
34
- function resetColor() {
35
- return '\x1b[0m';
36
- }
37
-
38
- function coloredLog(...args) {
39
- const color = getRandomColor();
40
- console.log(color, ...args.map(arg => `\n${arg}`), resetColor());
41
- }
42
-
43
-
44
- class GitCommit {
45
- constructor () {
46
- this.statusOutput = null
47
- this.init()
48
- }
49
-
50
- async init () {
51
- try {
52
- // 设置终端字符编码为UTF-8
53
- execSync('chcp 65001')
54
-
55
- this.statusOutput = this.execSyncGitCommand('git status')
56
- if (this.statusOutput.includes('nothing to commit, working tree clean')) {
57
- return
58
- }
59
- this.execSyncGitCommand('git diff')
60
-
61
- // 等待用户输入提交信息
62
- const commitMessage = await question('请输入提交信息:')
63
-
64
- // 执行 git add .
65
- this.statusOutput.includes('(use "git add <file>') && this.execSyncGitCommand('git add .')
66
-
67
- // 执行 git commit
68
- if (this.statusOutput.includes('Untracked files:') || this.statusOutput.includes('Changes not staged for commit') || this.statusOutput.includes('Changes to be committed')) {
69
- this.execSyncGitCommand(`git commit -m "${commitMessage || '提交'}"`)
70
- }
71
-
72
-
73
- // 检查是否需要拉取更新
74
- this.statusOutput.includes('use "git pull') && this.execSyncGitCommand('git pull')
75
-
76
- // 执行 git push
77
- // this.execSyncGitCommand(`git push`);
78
- this.execGitCommand('git push', {}, (error, stdout, stderr) => {
79
- console.log('提交完成。')
80
- this.execSyncGitCommand(`git log -n 1 --pretty=format:"%H %d %ad%n%B" --date=iso`)
81
- process.exit();
82
- })
83
- } catch (e) {
84
- console.log(`e ==> `, e)
85
- }
86
- }
87
-
88
- execSyncGitCommand (command, options = {}) {
89
- try {
90
- let { encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, cwd = process.cwd() } = options
91
- cwd = process.argv[2] || cwd
92
- const output = execSync(command, { encoding, maxBuffer, cwd })
93
- let result = output.trim()
94
- coloredLog(command, result)
95
- return result
96
- } catch (e) {
97
- console.log(`执行命令出错 ==> `, command, e)
98
- throw new Error(e)
99
- }
100
- }
101
-
102
- execGitCommand (command, options = {}, callback) {
103
- let { encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, cwd = process.cwd() } = options
104
- cwd = process.argv[2] || cwd
105
- exec(command, { encoding, maxBuffer, cwd }, (error, stdout, stderr) => {
106
- if (error) {
107
- coloredLog(command, error)
108
- return
109
- }
110
- if (stdout) {
111
- coloredLog(command, stdout)
112
- }
113
- if (stderr) {
114
- coloredLog(command, stderr)
115
- }
116
- callback && callback(error, stdout, stderr)
117
- })
118
- }
119
- }
120
-
121
- new GitCommit()
1
+ #!/usr/bin/env node
2
+
3
+ const {exec, execSync} = require('child_process')
4
+ const os = require('os');
5
+
6
+ const judgePlatform = () => {
7
+ // 判断是否是 Windows 系统
8
+ if (os.platform() === 'win32') {
9
+ try {
10
+ // 设置终端字符编码为 UTF-8
11
+ execSync('chcp 65001');
12
+ } catch (e) {
13
+ console.error('设置字符编码失败:', e.message);
14
+ }
15
+ }
16
+ };
17
+
18
+ // 有时候有乱码呢123神奇
19
+ const readline = require('readline')
20
+ const rl = readline.createInterface({
21
+ input: process.stdin,
22
+ output: process.stdout
23
+ })
24
+
25
+ function rlPromisify(fn) {
26
+ return async (...args) => {
27
+ return new Promise((resolve, reject) => fn(...args, resolve, reject))
28
+ }
29
+ }
30
+
31
+ const question = rlPromisify(rl.question.bind(rl))
32
+
33
+ const colors = [
34
+ '\x1b[31m', // 红色
35
+ '\x1b[32m', // 绿色
36
+ '\x1b[33m', // 黄色
37
+ '\x1b[34m', // 蓝色
38
+ '\x1b[35m', // 紫色
39
+ '\x1b[36m', // 青色
40
+ ];
41
+
42
+ function getRandomColor() {
43
+ return `\x1b[0m`;
44
+ // const randomIndex = Math.floor(Math.random() * colors.length);
45
+ // return colors[randomIndex];
46
+ }
47
+
48
+ function resetColor() {
49
+ return '\x1b[0m';
50
+ }
51
+
52
+ function coloredLog(...args) {
53
+ const color = getRandomColor();
54
+ // 获取控制台的宽度
55
+ const terminalWidth = process.stdout.columns;
56
+
57
+ // 创建与控制台宽度相同的横线
58
+ const line = '-'.repeat(terminalWidth);
59
+ let _args = args.map(arg => arg.split('\n')).flat().filter(arg => arg.trim() !== '');
60
+ console.log(line);
61
+ _args.map((arg, i) => {
62
+ let _color = color;
63
+ let trim_arg = arg.trim();
64
+ if (_args[0] === 'git diff' && arg.startsWith('-')) {
65
+ _color = '\x1b[31m';
66
+ }
67
+ if (_args[0] === 'git status' && trim_arg.startsWith('new file:')) {
68
+ _color = '\x1b[31m';
69
+ }
70
+ if (_args[0] === 'git diff' && arg.startsWith('+')) {
71
+ _color = '\x1b[32m';
72
+ }
73
+ if (_args[0] === 'git status' && trim_arg.startsWith('modified:')) {
74
+ _color = '\x1b[32m';
75
+ }
76
+ if (_args[0] === 'git diff' && arg.startsWith('@@ ')) {
77
+ _color = '\x1b[36m';
78
+ }
79
+ if (i === 0) {
80
+ console.log(`|\x1b[1m\x1b[34m ${arg}⬇️\x1b[22m\x1b[39m`);
81
+ } else {
82
+ console.log(`|${_color} ${arg}`, resetColor());
83
+ }
84
+ });
85
+ console.log(line);
86
+ }
87
+
88
+
89
+ class GitCommit {
90
+ constructor() {
91
+ this.statusOutput = null
92
+ this.init()
93
+ }
94
+
95
+ async init() {
96
+ try {
97
+ judgePlatform()
98
+
99
+ this.statusOutput = this.execSyncGitCommand('git status')
100
+ if (this.statusOutput.includes('nothing to commit, working tree clean')) {
101
+ process.exit();
102
+ return
103
+ }
104
+ this.execSyncGitCommand('git diff')
105
+
106
+ // 等待用户输入提交信息
107
+ const commitMessage = await question('请输入提交信息:')
108
+
109
+ // 执行 git add .
110
+ this.statusOutput.includes('(use "git add <file>') && this.execSyncGitCommand('git add .')
111
+
112
+ // 执行 git commit
113
+ if (this.statusOutput.includes('Untracked files:') || this.statusOutput.includes('Changes not staged for commit') || this.statusOutput.includes('Changes to be committed')) {
114
+ this.execSyncGitCommand(`git commit -m "${commitMessage || '提交'}"`)
115
+ }
116
+
117
+
118
+ // 检查是否需要拉取更新
119
+ this.statusOutput.includes('use "git pull') && this.execSyncGitCommand('git pull')
120
+
121
+ // 执行 git push
122
+ // this.execSyncGitCommand(`git push`);
123
+ this.execGitCommand('git push', {}, (error, stdout, stderr) => {
124
+ console.log('提交完成。')
125
+ this.execSyncGitCommand(`git log -n 1 --pretty=format:"%H %d %ad%n%B" --date=iso`)
126
+ process.exit();
127
+ })
128
+ } catch (e) {
129
+ console.log(`e ==> `, e)
130
+ }
131
+ }
132
+
133
+ execSyncGitCommand(command, options = {}) {
134
+ try {
135
+ let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, cwd = process.cwd()} = options
136
+ cwd = process.argv[2] || cwd
137
+ const output = execSync(command, {encoding, maxBuffer, cwd})
138
+ let result = output.trim()
139
+ coloredLog(command, result)
140
+ return result
141
+ } catch (e) {
142
+ console.log(`执行命令出错 ==> `, command, e)
143
+ throw new Error(e)
144
+ }
145
+ }
146
+
147
+ execGitCommand(command, options = {}, callback) {
148
+ let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, cwd = process.cwd()} = options
149
+ cwd = process.argv[2] || cwd
150
+ exec(command, {encoding, maxBuffer, cwd}, (error, stdout, stderr) => {
151
+ if (error) {
152
+ coloredLog(command, error)
153
+ return
154
+ }
155
+ if (stdout) {
156
+ coloredLog(command, stdout)
157
+ }
158
+ if (stderr) {
159
+ coloredLog(command, stderr)
160
+ }
161
+ callback && callback(error, stdout, stderr)
162
+ })
163
+ }
164
+ }
165
+
166
+ new GitCommit()