-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.cjs
executable file
·417 lines (402 loc) · 15 KB
/
index.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env node
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
default: () => src_default
});
module.exports = __toCommonJS(src_exports);
var import_node_fs2 = __toESM(require("fs"));
// src/util/track.ts
var import_node_os = __toESM(require("os"));
var import_node_fs = __toESM(require("fs"));
var import_node_path = __toESM(require("path"));
var import_node_constants = __toESM(require("constants"));
var import_rimraf = __toESM(require("rimraf"));
var paths = [];
var flags = import_node_constants.default.O_CREAT | import_node_constants.default.O_TRUNC | import_node_constants.default.O_RDWR | import_node_constants.default.O_EXCL;
var mode = import_node_constants.default.S_IRUSR | import_node_constants.default.S_IWUSR;
var dir = import_node_path.default.resolve(import_node_os.default.tmpdir());
var tracking = false;
var attached = false;
var addListener = () => {
if (!attached) {
process.addListener("exit", function() {
try {
let path2 = paths.length > 0 ? paths.shift() : null;
while (path2) {
import_rimraf.default.sync(path2, { maxRetries: 6 });
path2 = paths.shift();
}
} catch (err) {
console.warn("Fail to clean temporary files on exit : ", err);
throw err;
}
});
attached = true;
}
};
var promisify = (cb) => {
let callback = cb;
const promise = new Promise((resolve, reject) => {
callback = (err, data) => {
cb?.(err, data);
process.nextTick(function() {
if (err) {
reject(err);
return;
}
resolve(data);
});
};
});
return {
promise,
callback
};
};
var pathify = (prefix) => {
const now = /* @__PURE__ */ new Date();
const name = [prefix, now.getFullYear(), now.getMonth(), now.getDate(), "-", process.pid, "-", (Math.random() * 4294967296 + 1).toString(36)].join("");
return import_node_path.default.join(dir, name);
};
var open = (cb) => {
const path2 = pathify("f-");
const p = promisify(cb);
import_node_fs.default.open(path2, flags, mode, (err, fd) => {
if (!err && tracking) {
paths.push(path2);
addListener();
}
p.callback(err, { path: path2, fd });
});
return p.promise;
};
var track_default = (track) => {
tracking = track !== false;
return { open };
};
// src/util/editor.ts
var import_node_child_process = require("child_process");
function editor_default(file, cb) {
const editor = /^win/.test(process.platform) ? "notepad" : "vim";
const args = editor.split(/\s+/);
const bin = args.shift();
const ps = (0, import_node_child_process.spawn)(
bin,
args.concat([file]),
{ stdio: "inherit" }
);
ps.on("exit", cb);
}
// src/util/logger.ts
var logger_default = console;
// src/lib/read-config.ts
var import_find_config = __toESM(require("find-config"));
var read_config_default = (config = ".cz-message.cjs") => {
const pkg = import_find_config.default.require("package.json", { home: false });
const czConfig = import_find_config.default.require(config, { home: false });
if (typeof pkg === "object" && typeof pkg.config === "object" && typeof pkg.config["cz-message-helper"] === "object" && typeof pkg.config["cz-message-helper"].config === "string") {
return import_find_config.default.require(pkg.config["cz-message-helper"].config, { home: false }) || czConfig;
}
if (czConfig) {
return czConfig;
}
logger_default.error("Unable to find a configuration file. Please look documentation: https://github.com/linpengteng/cz-message-helper#Usage");
};
// src/lib/build-commit.ts
var import_word_wrap = __toESM(require("word-wrap"));
var build_commit_default = (config, answers) => {
const wrap = (str, opt) => {
return (0, import_word_wrap.default)(str, {
width: 99999,
indent: "",
...opt
});
};
const format = (result) => {
return result?.replace(/\\n/g, "\n") || "";
};
return format(config.templater?.(answers, wrap));
};
// src/util/configs.ts
var configs_default = {
en: {
questions: [
{
type: "list",
name: "type",
message: "Please select the type of change that you're committing:",
choices: [
{ value: "fix", name: "fix: -------- A bug fix" },
{ value: "feat", name: "feat: ------- A new feature" },
{ value: "begin", name: "begin: ------ Begin new repository" },
{ value: "docs", name: "docs: ------- Documentation only changes" },
{ value: "style", name: "style: ------ Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)" },
{ value: "chore", name: "chore: ------ Changes to the build process or auxiliary tools and libraries such as documentation generation" },
{ value: "refactor", name: "refactor: --- A code change that neither fixes a bug nor adds a feature" },
{ value: "perf", name: "perf: ------- A code change that improves performance" },
{ value: "test", name: "test: ------- Add Test Unit" },
{ value: "revert", name: "revert: ----- Revert to a commit" },
{ value: "merge", name: "merge: ------ Merge from branches" },
{ value: "wip", name: "wip: -------- Work in progress" }
]
},
{
type: "list",
name: "scope",
message: "Please select the SCOPE of this change (optional):",
choices() {
return [
{ name: "empty", value: false },
{ name: "custom", value: "custom" }
];
}
},
{
type: "input",
name: "customScope",
message: "Please input the custom SCOPE of this change:",
when(answers) {
return answers.scope === "custom";
},
filter(value, answers) {
answers.scope = value || "";
return value || "";
}
},
{
type: "input",
name: "subject",
message: "Please write a SHORT tense description of the change(word number less than 72):",
validate(value) {
if (!value.trim()) {
return "Cannot be empty";
}
return value.length > 72 ? `Exceed limit: ${value.length} > 72` : true;
},
filter(value, answers) {
return value.trim();
}
},
{
type: "input",
name: "body",
message: 'Please provide a LONGER description of the change (optional). Use "\\n" to break new line:'
},
{
type: "input",
name: "breaking",
message: "Please list any BREAKING CHANGES (optional):",
when(answers) {
return /^(:[a-z0-9A-Z_-]+(:)(\s*))?(feat|fix)(\2\s*)?$/.test(answers.type.toLowerCase());
}
},
{
type: "input",
name: "footer",
message: "Please list any ISSUES CLOSED by this change (optional). Eg: #31, #34:"
}
],
templater: (answers, wrap) => {
let template = "";
template += answers.type ? `${answers.type}` : ``;
template += answers.scope ? `(${answers.scope})` : ``;
template += answers.subject ? `: ${answers.subject}` : ``;
template += answers.body ? `
${wrap(answers.body)}` : ``;
template += answers.breaking ? `
BREAKING CHANGE: ${wrap(answers.breaking)}` : ``;
template += answers.footer ? `
ISSUES CLOSED: ${wrap(answers.footer)}` : ``;
return template;
},
language: "en"
},
cn: {
questions: [
{
type: "list",
name: "type",
message: "\u8BF7\u9009\u62E9\u8981\u63D0\u4EA4\u7684\u66F4\u6539\u7C7B\u578B:",
choices: [
{ value: "fix", name: "fix: -------- \u4FEE\u590DBUG" },
{ value: "feat", name: "feat: ------- \u65B0\u529F\u80FD" },
{ value: "begin", name: "begin: ------ \u521B\u5EFA\u65B0\u5B58\u50A8\u5E93" },
{ value: "docs", name: "docs: ------- \u4EC5\u6587\u6863\u66F4\u6539" },
{ value: "style", name: "style: ------ \u4E0D\u5F71\u54CD\u4EE3\u7801\u8FD0\u884C\u7684\u66F4\u6539(\u8C03\u6574\u7A7A\u767D\u3001\u683C\u5F0F\u3001\u7F3A\u5C11\u5206\u53F7\u7B49)" },
{ value: "chore", name: "chore: ------ \u5BF9\u6784\u5EFA\u8FC7\u7A0B\u6216\u8F85\u52A9\u5DE5\u5177\u7684\u66F4\u6539\u4EE5\u53CA\u6587\u6863\u751F\u6210\u7B49\u5E93" },
{ value: "refactor", name: "refactor: --- \u91CD\u6784\u67B6\u6784\u6216\u4EE3\u7801" },
{ value: "perf", name: "perf: ------ \u6539\u8FDB\u6027\u80FD\u7684\u4EE3\u7801\u66F4\u6539" },
{ value: "test", name: "test: ------ \u6DFB\u52A0\u6D4B\u8BD5\u5355\u5143" },
{ value: "revert", name: "revert: ----- \u56DE\u9000\u81F3\u67D0\u4E00\u4E2A\u7248\u672C" },
{ value: "merge", name: "merge: ------ \u5408\u5E76\u4E00\u4E2A\u5206\u652F, \u89E3\u51B3\u51B2\u7A81\u5206\u652F" },
{ value: "wip", name: "wip: -------- \u6B63\u5728\u8FDB\u884C\u4E2D\u7684\u5DE5\u4F5C" }
]
},
{
type: "list",
name: "scope",
message: "\u8BF7\u9009\u62E9\u66F4\u6539\u7684\u8303\u56F4:",
choices() {
return [
{ name: "\u65E0", value: false },
{ name: "\u81EA\u5B9A\u4E49", value: "custom" }
];
},
filter(value, answers) {
return value || "";
}
},
{
type: "input",
name: "customScope",
message: "\u8BF7\u8F93\u5165\u81EA\u5B9A\u4E49\u7684\u53D8\u66F4\u7684\u8303\u56F4(\u53EF\u9009):",
when(answers) {
return answers.scope === "custom";
},
filter(value, answers) {
answers.scope = value || "";
return value || "";
}
},
{
type: "input",
name: "subject",
message: "\u8BF7\u7B80\u660E\u627C\u8981\u7684\u6458\u8981\u63CF\u8FF0(\u5EFA\u8BAE\u5B57\u6570\u572872\u5B57\u5185):",
validate(value) {
if (!value.trim()) {
return "\u63CF\u8FF0\u5185\u5BB9\u4E0D\u53EF\u4E3A\u7A7A";
}
return value.trim().length > 72 ? `\u5B57\u6570\u8D85\u51FA\u9650\u5236: ${value.trim().length} > 72` : true;
},
filter(value, answers) {
return value.trim();
}
},
{
type: "input",
name: "body",
message: "\u8BF7\u63D0\u4F9B\u66F4\u8BE6\u7EC6\u7684\u53D8\u66F4\u8BF4\u660E(\u53EF\u9009), \u4F7F\u7528\u201C\\n\u201D\u6362\u884C:"
},
{
type: "input",
name: "breaking",
message: "\u8BF7\u5217\u51FA\u4EFB\u4F55\u91CD\u5927\u53D8\u5316(\u53EF\u9009)\n",
when(answers) {
return /^(:[a-z0-9A-Z_-]+(:)(\s*))?(feat|fix)(\2\s*)?$/.test(answers.type.toLowerCase());
}
},
{
type: "input",
name: "footer",
message: "\u8BF7\u5217\u51FA\u6B64\u66F4\u6539\u5173\u95ED\u7684\u4EFB\u4F55\u95EE\u9898(\u53EF\u9009), \u4F8B\u5982: #31,#34:"
}
],
templater: (answers, wrap) => {
let template = "";
template += answers.type ? `${answers.type}` : ``;
template += answers.scope ? `(${answers.scope})` : ``;
template += answers.subject ? `: ${answers.subject}` : ``;
template += answers.body ? `
${wrap(answers.body)}` : ``;
template += answers.breaking ? `
BREAKING CHANGE: ${wrap(answers.breaking)}` : ``;
template += answers.footer ? `
ISSUES CLOSED: ${wrap(answers.footer)}` : ``;
return template;
},
language: "cn"
}
};
// src/lib/megre-config.ts
var megre_config_default = (cfg) => {
const def = cfg?.language === "cn" ? configs_default.cn : configs_default.en;
const lastCfg = {
type: "expand",
name: "confirmCommit",
default: 0,
choices: [
{ key: "y", name: cfg?.language !== "cn" ? "Yes" : "\u63D0\u4EA4", value: "yes" },
{ key: "n", name: cfg?.language !== "cn" ? "Abort commit" : "\u53D6\u6D88", value: "no" },
{ key: "e", name: cfg?.language !== "cn" ? "Edit message" : "\u4FEE\u6539", value: "edit" }
],
message(answers) {
const sep = "###--------------------------------------------------------###";
const msg = build_commit_default(finalConfig, answers);
logger_default.info(`
${sep}
${msg}
${sep}
`);
return cfg?.language !== "cn" ? "Are you sure you want to proceed with the commit above?" : "\u60A8\u786E\u5B9A\u8981\u7EE7\u7EED\u6267\u884C\u4E0A\u9762\u7684\u63D0\u4EA4\u5417\uFF1F";
}
};
const cfgQuestions = cfg && cfg.questions;
const cfgTemplater = cfg && cfg.templater;
const defTemplater = def.templater;
const defQuestions = def.questions;
const lastQuestion = (cfgQuestions || defQuestions)[0];
const pushQuestion = lastQuestion.name !== "confirmCommit" ? [lastCfg] : [];
const finalConfig = {
questions: [...cfgQuestions || defQuestions, ...pushQuestion],
templater: cfgTemplater || defTemplater
};
return finalConfig;
};
// src/index.ts
var src_default = {
prompter(cz, commit) {
const config = megre_config_default(read_config_default());
const questions = config.questions;
cz.prompt(questions).then((answers) => {
if (answers.confirmCommit === "no") {
logger_default.info("Commit has been canceled.");
return;
}
if (answers.confirmCommit === "yes") {
commit(build_commit_default(config, answers));
return;
}
track_default().open((err, info) => {
if (!err) {
import_node_fs2.default.writeSync(info.fd, build_commit_default(config, answers));
import_node_fs2.default.close(info.fd, () => {
editor_default(info.path, (code) => {
if (code !== 0) logger_default.info(`Editor returned non zero value. Commit message was:
${build_commit_default(config, answers)}`);
if (code === 0) commit(import_node_fs2.default.readFileSync(info.path, { encoding: "utf8" }));
});
});
}
});
});
}
};