Skip to content

Commit bfb969c

Browse files
committed
feat(cz-git): done commit prompt overall process
link #1
1 parent 47973de commit bfb969c

7 files changed

Lines changed: 139 additions & 41 deletions

File tree

‎.commitlintrc.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = {
3232
{ value: "revert", name: "revert: Reverts a previous commit", emoji: ":rewind:" }
3333
],
3434
useEmoji: false,
35-
scopes: [{ name: 'READEME' }, { name: 'theme' }, { name: 'module' }, { name: 'plugin' }],
35+
scopes: [{ name: 'cz-git' }, { name: 'guide' }, { name: 'module' }, { name: 'plugin' }],
3636
allowCustomScopes: true,
3737
allowBreakingChanges: ['feat', 'fix'],
3838
skipQuestions: ['body'],

‎package.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@
3939
"dependencies": {
4040
"@commitlint/load": "^16.2.1",
4141
"commitizen": "^4.2.4",
42+
"editor": "^1.0.0",
4243
"inquirer-autocomplete-prompt": "^1.4.0",
43-
"lodash": "^4.17.21",
44+
"temp": "^0.9.4",
4445
"word-wrap": "^1.2.3"
4546
},
4647
"devDependencies": {
4748
"@commitlint/cli": "^16.2.1",
4849
"@commitlint/config-conventional": "^16.2.1",
4950
"@commitlint/types": "^16.2.1",
50-
"@types/lodash": "^4.14.178",
5151
"@types/node": "^17.0.18",
52+
"@types/temp": "^0.9.1",
5253
"@typescript-eslint/eslint-plugin": "^5.12.0",
5354
"@typescript-eslint/parser": "^5.12.0",
5455
"eslint": "^8.9.0",

‎src/index.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,32 @@
77

88
// @ts-ignore
99
import autocompletePrompt from "inquirer-autocomplete-prompt";
10-
import { generateOptions, generateQuestions } from "./loader";
1110
import commitlintLoad from "@commitlint/load";
12-
import type { QuestionsType } from "./loader";
13-
import type { Answers } from "./share";
11+
import { generateOptions, generateQuestions } from "./loader";
12+
import { buildCommit, editCommit, log } from "./until";
13+
import type { CommitizenType } from "./share";
1414

1515
export * from "./share";
1616

17-
interface CommitizenType {
18-
registerPrompt: (type: string, plugin: unknown) => void;
19-
prompt: (qs: QuestionsType) => Promise<Answers>;
20-
}
21-
22-
// @ts-ignore
23-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2417
export const prompter = (cz: CommitizenType, commit: (message: string) => void) => {
2518
commitlintLoad().then((clConfig) => {
2619
const options = generateOptions(clConfig);
2720
const questions = generateQuestions(options, cz);
2821
cz.registerPrompt("autocomplete", autocompletePrompt);
2922
cz.prompt(questions).then((answers) => {
30-
console.log(answers);
31-
return false;
23+
switch (answers.confirmCommit) {
24+
case "edit":
25+
editCommit(answers, options, commit);
26+
break;
27+
28+
case "yes":
29+
commit(buildCommit(answers, options));
30+
break;
31+
32+
default:
33+
log("info", "Commit has been canceled.");
34+
break;
35+
}
3236
});
3337
});
3438
};

‎src/loader.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
187187
},
188188
{
189189
type: "input",
190-
name: "footerPre",
190+
name: "footerPrefixsSelect",
191191
message: options.messages?.customFooterPrefixs,
192192
when(answers: Answers) {
193193
return answers.footerPrefixsSelect === "custom";
@@ -212,7 +212,8 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
212212
],
213213
default: 0,
214214
message(answers: Answers) {
215-
const SEP = "###--------------------------------------------------------###";
215+
// TODO: COLOR
216+
const SEP = "\u001B[1;90m###--------------------------------------------------------###\u001B[0m";
216217
console.info(`\n${SEP}\n${buildCommit(answers, options, true)}\n${SEP}\n`);
217218
return options.messages?.confirmCommit;
218219
}

‎src/share.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @license: MIT
55
* @copyright: Copyright (c) 2022-present Qiubin Zheng
66
*/
7-
7+
import type { QuestionsType } from "./loader";
88
export type Config = Omit<Partial<typeof defaultConfig>, "scopes"> & {
99
scopes: TypesOption[];
1010
disableScopeLowerCase?: boolean;
@@ -31,6 +31,11 @@ export type Answers = {
3131
confirmCommit?: string;
3232
};
3333

34+
export interface CommitizenType {
35+
registerPrompt: (type: string, plugin: unknown) => void;
36+
prompt: (qs: QuestionsType) => Promise<Answers>;
37+
}
38+
3439
interface Option {
3540
/**
3641
* @description: show prompt name

‎src/until.ts‎

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import { RuleConfigCondition, RuleConfigSeverity } from "@commitlint/types";
55
import fs from "fs";
66
import path from "path";
7-
// import wrap from "word-wrap";
7+
import wrap from "word-wrap";
8+
// @ts-ignore
9+
import editor from "editor";
10+
import { open as tempOpen } from "temp";
811
import { Answers, CommitizenGitOptions } from "./share";
912

1013
export type Rule =
@@ -66,7 +69,7 @@ export function log(type: "info" | "warm" | "err", msg: string) {
6669
err: "\u001B[31m",
6770
reset: "\u001B[0m"
6871
};
69-
console.log(`${colorMapping[type]}[${type}]>>>: ${msg}`);
72+
console.log(`${colorMapping[type]}[${type}]>>>: ${msg}${colorMapping.reset}`);
7073
}
7174

7275
export const getPreparedCommit = (context: string) => {
@@ -100,11 +103,12 @@ export const getMaxSubjectLength = (
100103
scope: Answers["scope"],
101104
options: CommitizenGitOptions
102105
) => {
103-
if (!options.maxHeaderWidth || !type?.length) return 100;
104106
return (
105-
options.maxHeaderWidth -
106-
type.length -
107+
(options?.maxHeaderWidth ? options?.maxHeaderWidth : 100) -
108+
(type?.length ? type.length : 0) -
109+
// `: `
107110
2 -
111+
// `()`
108112
(scope ? scope.length + 2 : 0) -
109113
(options.useEmoji ? 2 : 0)
110114
);
@@ -133,16 +137,70 @@ const addSubject = (subject?: string, color?: boolean) => {
133137
return subject.trim();
134138
};
135139

136-
export const buildCommit = (answers: Answers, options: CommitizenGitOptions, color: boolean) => {
137-
// const wrapOption = {
138-
// trim: true,
139-
// newLine: "\n",
140-
// indent: "",
141-
// width: 100
142-
// };
140+
const addBreaklinesIfNeeded = (value: string, breaklineChar = "|") =>
141+
value.split(breaklineChar).join("\n").valueOf();
142+
143+
const addFooter = (footer: string, footerPrefixsSelect = "", color?: boolean) => {
144+
if (footerPrefixsSelect === "") {
145+
return color ? `\n\n\u001B[32m${footer}\u001B[0m` : `\n\n${footer}`;
146+
}
147+
return color
148+
? `\n\n\u001B[32m${footerPrefixsSelect} ${footer}\u001B[0m`
149+
: `\n\n${footerPrefixsSelect} ${footer}`;
150+
};
151+
152+
export const buildCommit = (answers: Answers, options: CommitizenGitOptions, color = false) => {
153+
const wrapOptions = {
154+
trim: true,
155+
newLine: "\n",
156+
indent: "",
157+
width: 100
158+
};
143159
const head =
144160
addType(answers.type ?? "", options, color) +
145161
addScope(answers.scope, color) +
146162
addSubject(answers.subject, color);
147-
return head;
163+
const body = wrap(answers.body ?? "", wrapOptions);
164+
const breaking = wrap(answers.breaking ?? "", wrapOptions);
165+
const footer = wrap(answers.footer ?? "", wrapOptions);
166+
167+
let result = head;
168+
if (body) {
169+
// TODO: options.breaklineChar => prams
170+
result += `\n\n${addBreaklinesIfNeeded(body)}`;
171+
}
172+
if (breaking) {
173+
result += `\n\nBREAKING CHANGE :\n${addBreaklinesIfNeeded(breaking)}`;
174+
}
175+
if (footer) {
176+
result += addFooter(footer, answers.footerPrefixsSelect, color);
177+
}
178+
return result;
179+
};
180+
181+
export const editCommit = (
182+
answers: Answers,
183+
options: CommitizenGitOptions,
184+
cb: (message: string) => void
185+
) => {
186+
tempOpen(undefined, (err, info) => {
187+
if (!err) {
188+
fs.writeSync(info.fd, buildCommit(answers, options));
189+
fs.close(info.fd, () => {
190+
editor(info.path, (code: number) => {
191+
if (code === 0) {
192+
const commitStr = fs.readFileSync(info.path, {
193+
encoding: "utf8"
194+
});
195+
cb(commitStr);
196+
} else {
197+
log(
198+
"warm",
199+
`Editor exit non zero. Commit message was:\n${buildCommit(answers, options)}`
200+
);
201+
}
202+
});
203+
});
204+
}
205+
});
148206
};

‎yarn.lock‎

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,6 @@
270270
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz#97edc9037ea0c38585320b28964dde3b39e4660d"
271271
integrity sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==
272272

273-
"@types/lodash@^4.14.178":
274-
version "4.14.178"
275-
resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.178.tgz#341f6d2247db528d4a13ddbb374bcdc80406f4f8"
276-
integrity sha512-0d5Wd09ItQWH1qFbEyQ7oTQ3GZrMfth5JkbN3EvTKLXcHLRDSXeLnlvlOn0wvxVIwK5o2M8JzP/OWz7T3NRsbw==
277-
278273
"@types/minimatch@*":
279274
version "3.0.5"
280275
resolved "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
@@ -300,6 +295,13 @@
300295
resolved "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
301296
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==
302297

298+
"@types/temp@^0.9.1":
299+
version "0.9.1"
300+
resolved "https://registry.npmjs.org/@types/temp/-/temp-0.9.1.tgz#448560fbe6235b521778367c26208e784f15adb1"
301+
integrity sha512-yDQ8Y+oQi9V7VkexwE6NBSVyNuyNFeGI275yWXASc2DjmxNicMi9O50KxDpNlST1kBbV9jKYBHGXhgNYFMPqtA==
302+
dependencies:
303+
"@types/node" "*"
304+
303305
"@typescript-eslint/eslint-plugin@^5.12.0":
304306
version "5.12.0"
305307
resolved "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.12.0.tgz#bb46dd7ce7015c0928b98af1e602118e97df6c70"
@@ -851,6 +853,11 @@ eastasianwidth@^0.2.0:
851853
resolved "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb"
852854
integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==
853855

856+
editor@^1.0.0:
857+
version "1.0.0"
858+
resolved "https://registry.npmjs.org/editor/-/editor-1.0.0.tgz#60c7f87bd62bcc6a894fa8ccd6afb7823a24f742"
859+
integrity sha1-YMf4e9YrzGqJT6jM1q+3gjok90I=
860+
854861
emoji-regex@^8.0.0:
855862
version "8.0.0"
856863
resolved "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
@@ -1666,7 +1673,7 @@ lodash.merge@^4.6.2:
16661673
resolved "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
16671674
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
16681675

1669-
lodash@^4.17.12, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21:
1676+
lodash@^4.17.12, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20:
16701677
version "4.17.21"
16711678
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
16721679
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
@@ -1764,9 +1771,9 @@ min-indent@^1.0.0:
17641771
integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==
17651772

17661773
minimatch@^3.0.4:
1767-
version "3.1.1"
1768-
resolved "https://registry.npmjs.org/minimatch/-/minimatch-3.1.1.tgz#879ad447200773912898b46cd516a7abbb5e50b0"
1769-
integrity sha512-reLxBcKUPNBnc/sVtAbxgRVFSegoGeLaSjmphNhcwcolhYLRgtJscn5mRl6YRZNQv40Y7P6JM2YhSIsbL9OB5A==
1774+
version "3.1.2"
1775+
resolved "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b"
1776+
integrity sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==
17701777
dependencies:
17711778
brace-expansion "^1.1.7"
17721779

@@ -1779,11 +1786,18 @@ minimist-options@4.1.0:
17791786
is-plain-obj "^1.1.0"
17801787
kind-of "^6.0.3"
17811788

1782-
minimist@1.2.5:
1789+
minimist@1.2.5, minimist@^1.2.5:
17831790
version "1.2.5"
17841791
resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
17851792
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==
17861793

1794+
mkdirp@^0.5.1:
1795+
version "0.5.5"
1796+
resolved "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz#d91cefd62d1436ca0f41620e251288d420099def"
1797+
integrity sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==
1798+
dependencies:
1799+
minimist "^1.2.5"
1800+
17871801
ms@2.1.2:
17881802
version "2.1.2"
17891803
resolved "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
@@ -2116,6 +2130,13 @@ rimraf@^3.0.2:
21162130
dependencies:
21172131
glob "^7.1.3"
21182132

2133+
rimraf@~2.6.2:
2134+
version "2.6.3"
2135+
resolved "https://registry.npmjs.org/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"
2136+
integrity sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==
2137+
dependencies:
2138+
glob "^7.1.3"
2139+
21192140
run-async@^2.2.0, run-async@^2.4.0:
21202141
version "2.4.1"
21212142
resolved "https://registry.npmjs.org/run-async/-/run-async-2.4.1.tgz#8440eccf99ea3e70bd409d49aab88e10c189a455"
@@ -2379,6 +2400,14 @@ supports-preserve-symlinks-flag@^1.0.0:
23792400
resolved "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
23802401
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==
23812402

2403+
temp@^0.9.4:
2404+
version "0.9.4"
2405+
resolved "https://registry.npmjs.org/temp/-/temp-0.9.4.tgz#cd20a8580cb63635d0e4e9d4bd989d44286e7620"
2406+
integrity sha512-yYrrsWnrXMcdsnu/7YMYAofM1ktpL5By7vZhf15CrXijWWrEYZks5AXBudalfSWJLlnen/QUJUB5aoB0kqZUGA==
2407+
dependencies:
2408+
mkdirp "^0.5.1"
2409+
rimraf "~2.6.2"
2410+
23822411
text-extensions@^1.0.0:
23832412
version "1.9.0"
23842413
resolved "https://registry.npmjs.org/text-extensions/-/text-extensions-1.9.0.tgz#1853e45fee39c945ce6f6c36b2d659b5aabc2a26"

0 commit comments

Comments
 (0)