Skip to content

Commit e94f05a

Browse files
committed
feat(cz-git): reduce word-wrap and temp dev number
link #2
1 parent d709130 commit e94f05a

10 files changed

Lines changed: 260 additions & 45 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"@commitlint/config-conventional": "^16.2.1",
5555
"@types/jest": "^27.4.0",
5656
"@types/node": "^17.0.18",
57+
"@types/rimraf": "^3.0.2",
5758
"@types/temp": "^0.9.1",
5859
"@typescript-eslint/eslint-plugin": "^5.12.0",
5960
"@typescript-eslint/parser": "^5.12.0",

‎packages/cz-git/package.json‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@
3939
},
4040
"dependencies": {
4141
"@commitlint/load": "^16.2.1",
42-
"commitizen": "^4.2.4",
43-
"inquirer": "^8.0.0",
44-
"inquirer-autocomplete-prompt": "^1.4.0",
45-
"temp": "^0.9.4",
46-
"word-wrap": "^1.2.3"
42+
"commitizen": "4.2.4",
43+
"inquirer": "8.2.0",
44+
"inquirer-autocomplete-prompt": "1.4.0",
45+
"rimraf": "3.0.2"
4746
},
4847
"peerDependencies": {
4948
"commitizen": ">=2"

‎packages/cz-git/src/share/commitlint.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @description: fork by "@commitlint/types"
2+
* @description: fork by "@commitlint/types" v16.2.1
33
*/
44

55
/** ========== rules ========== */

‎packages/cz-git/src/until/editor.ts‎

Lines changed: 166 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,179 @@
11
import fs from "fs";
22
import path from "path";
3+
import os from "os";
4+
import cnst from "constants";
5+
import rimraf from "rimraf";
36
import { spawn } from "child_process";
4-
import { open as tempOpen } from "temp";
57
import { Answers, CommitizenGitOptions } from "../share";
68
import { buildCommit, log } from "./until";
79

810
/**
9-
* @description: fork by "temp/open"
11+
* @description: fork by "temp/open" v0.9.4
1012
*/
13+
interface OpenFile {
14+
path: string;
15+
fd: number;
16+
}
17+
18+
interface AffixOptions {
19+
prefix?: string | null | undefined;
20+
suffix?: string | null | undefined;
21+
dir?: string | undefined;
22+
}
23+
24+
const dir = path.resolve(os.tmpdir());
25+
const RDWR_EXCL = cnst.O_CREAT | cnst.O_TRUNC | cnst.O_RDWR | cnst.O_EXCL;
26+
const dirsToDelete: string[] = [];
27+
const rimrafSync = rimraf.sync;
28+
29+
const promisify = function (callback: any, ...arges: any[]) {
30+
if (typeof callback === "function") {
31+
return [undefined, callback];
32+
}
33+
34+
let promiseCallback;
35+
const promise = new Promise(function (resolve, reject) {
36+
promiseCallback = function () {
37+
const args = Array.from(arges);
38+
const err = args.shift();
39+
40+
process.nextTick(function () {
41+
if (err) {
42+
reject(err);
43+
} else if (args.length === 1) {
44+
resolve(args[0]);
45+
} else {
46+
resolve(args);
47+
}
48+
});
49+
};
50+
});
51+
52+
return [promise, promiseCallback];
53+
};
54+
55+
const parseAffixes = function (
56+
rawAffixes: string | AffixOptions | undefined,
57+
defaultPrefix: string
58+
) {
59+
let affixes: AffixOptions = { prefix: null, suffix: null };
60+
if (rawAffixes) {
61+
switch (typeof rawAffixes) {
62+
case "string":
63+
affixes.prefix = rawAffixes;
64+
break;
65+
case "object":
66+
affixes = rawAffixes;
67+
break;
68+
default:
69+
throw new Error("Unknown affix declaration: " + affixes);
70+
}
71+
} else {
72+
affixes.prefix = defaultPrefix;
73+
}
74+
return affixes;
75+
};
76+
77+
const generateName = function (
78+
rawAffixes: string | AffixOptions | undefined,
79+
defaultPrefix: string
80+
) {
81+
const affixes: AffixOptions = parseAffixes(rawAffixes, defaultPrefix);
82+
const now = new Date();
83+
const name = [
84+
affixes.prefix,
85+
now.getFullYear(),
86+
now.getMonth(),
87+
now.getDate(),
88+
"-",
89+
process.pid,
90+
"-",
91+
(Math.random() * 0x100000000 + 1).toString(36),
92+
affixes.suffix
93+
].join("");
94+
return path.join(affixes.dir || dir, name);
95+
};
96+
97+
function cleanupFilesSync() {
98+
if (!tracking) {
99+
return false;
100+
}
101+
let count = 0;
102+
let toDelete;
103+
while ((toDelete = filesToDelete.shift()) !== undefined) {
104+
rimrafSync(toDelete, { maxBusyTries: 6 });
105+
count++;
106+
}
107+
return count;
108+
}
109+
110+
function cleanupDirsSync() {
111+
if (!tracking) {
112+
return false;
113+
}
114+
let count = 0;
115+
let toDelete;
116+
while ((toDelete = dirsToDelete.shift()) !== undefined) {
117+
rimrafSync(toDelete, { maxBusyTries: 6 });
118+
count++;
119+
}
120+
return count;
121+
}
122+
123+
function cleanupSync() {
124+
if (!tracking) {
125+
return false;
126+
}
127+
const fileCount = cleanupFilesSync();
128+
const dirCount = cleanupDirsSync();
129+
return { files: fileCount, dirs: dirCount };
130+
}
131+
132+
const tracking = false;
133+
let exitListenerAttached = false;
134+
135+
function attachExitListener() {
136+
if (!tracking) return false;
137+
if (!exitListenerAttached) {
138+
process.addListener("exit", function () {
139+
try {
140+
cleanupSync();
141+
} catch (err) {
142+
console.warn("Fail to clean temporary files on exit : ", err);
143+
throw err;
144+
}
145+
});
146+
exitListenerAttached = true;
147+
}
148+
}
149+
const filesToDelete: string[] = [];
150+
151+
function deleteFileOnExit(filePath: string) {
152+
if (!tracking) return false;
153+
attachExitListener();
154+
filesToDelete.push(filePath);
155+
}
156+
157+
const tempOpen = (
158+
affixes: string | AffixOptions | undefined,
159+
callback: (err: any, result: OpenFile) => void
160+
) => {
161+
const p = promisify(callback);
162+
const promise = p[0];
163+
callback = p[1];
164+
165+
const path = generateName(affixes, "f-");
166+
fs.open(path, RDWR_EXCL, 0o600, (err, fd) => {
167+
if (!err) {
168+
deleteFileOnExit(path);
169+
}
170+
callback(err, { path, fd });
171+
});
172+
return promise;
173+
};
11174

12175
/**
13-
* @description: fork by "editor"
176+
* @description: fork by "editor" v1.0.0
14177
*/
15178
const editor = (file?: string, opts?: any | object, cb?: any) => {
16179
if (typeof opts === "function") {

‎packages/cz-git/src/until/rules.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @description: fork by "@commitlint/cz-commitlint"
2+
* @description: fork by "@commitlint/cz-commitlint/src/utils/" v16.2.1
33
*/
44

55
import { RuleConfigCondition, RuleConfigSeverity } from "../share";

‎packages/cz-git/src/until/until.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @license: MIT
55
*/
66

7-
import wrap from "word-wrap";
7+
import { wrap } from "./wrap";
88
// @ts-ignore
99

1010
import { Answers, CommitizenGitOptions, Option, ScopesType } from "../share";

‎packages/cz-git/src/until/wrap.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @description: fork by "word-wrap" v1.2.3"
3+
*/
4+
5+
function identity(str: string) {
6+
return str;
7+
}
8+
9+
interface Options {
10+
width?: number;
11+
indent?: string;
12+
newline?: string;
13+
escape?: (str: string) => string;
14+
trim?: boolean;
15+
cut?: boolean;
16+
}
17+
18+
export const wrap = (str: string, options: Options) => {
19+
options = options || {};
20+
if (str == null) {
21+
return str;
22+
}
23+
24+
const width = options.width || 50;
25+
const indent = typeof options.indent === "string" ? options.indent : "";
26+
27+
const newline = options.newline || "\n" + indent;
28+
const escape = typeof options.escape === "function" ? options.escape : identity;
29+
30+
let regexString = ".{1," + width + "}";
31+
if (options.cut !== true) {
32+
regexString += "([\\s\u200B]+|$)|[^\\s\u200B]+?([\\s\u200B]+|$)";
33+
}
34+
35+
const re = new RegExp(regexString, "g");
36+
const lines = str.match(re) || [];
37+
let result =
38+
indent +
39+
lines
40+
.map(function (line) {
41+
if (line.slice(-1) === "\n") {
42+
line = line.slice(0, line.length - 1);
43+
}
44+
return escape(line);
45+
})
46+
.join(newline);
47+
48+
if (options.trim === true) {
49+
result = result.replace(/[ \t]*$/gm, "");
50+
}
51+
return result;
52+
};

‎packages/cz-git/tsconfig.json‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@
55
"rootDir": "src",
66
"outDir": "lib"
77
},
8-
"references": [
9-
{ "path": "../@cz-git/editor/tsconfig.json" }
10-
]
8+
"references": []
119
}

‎tsconfig.build.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"references": [
44
{ "path": "./packages/cz-git/tsconfig.json" },
55
{ "path": "./packages/@cz-git/loader/tsconfig.json" },
6-
{ "path": "./packages/@cz-git/editor/tsconfig.json" },
76
],
87
"files": []
98
}

0 commit comments

Comments
 (0)