Skip to content

Commit

Permalink
feat(cz-git): reduce word-wrap and temp dev number
Browse files Browse the repository at this point in the history
link #2
  • Loading branch information
Zhengqbbb committed Mar 2, 2022
1 parent d709130 commit e94f05a
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 45 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@commitlint/config-conventional": "^16.2.1",
"@types/jest": "^27.4.0",
"@types/node": "^17.0.18",
"@types/rimraf": "^3.0.2",
"@types/temp": "^0.9.1",
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
Expand Down
9 changes: 4 additions & 5 deletions packages/cz-git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@
},
"dependencies": {
"@commitlint/load": "^16.2.1",
"commitizen": "^4.2.4",
"inquirer": "^8.0.0",
"inquirer-autocomplete-prompt": "^1.4.0",
"temp": "^0.9.4",
"word-wrap": "^1.2.3"
"commitizen": "4.2.4",
"inquirer": "8.2.0",
"inquirer-autocomplete-prompt": "1.4.0",
"rimraf": "3.0.2"
},
"peerDependencies": {
"commitizen": ">=2"
Expand Down
2 changes: 1 addition & 1 deletion packages/cz-git/src/share/commitlint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @description: fork by "@commitlint/types"
* @description: fork by "@commitlint/types" v16.2.1
*/

/** ========== rules ========== */
Expand Down
169 changes: 166 additions & 3 deletions packages/cz-git/src/until/editor.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,179 @@
import fs from "fs";
import path from "path";
import os from "os";
import cnst from "constants";
import rimraf from "rimraf";
import { spawn } from "child_process";
import { open as tempOpen } from "temp";
import { Answers, CommitizenGitOptions } from "../share";
import { buildCommit, log } from "./until";

/**
* @description: fork by "temp/open"
* @description: fork by "temp/open" v0.9.4
*/
interface OpenFile {
path: string;
fd: number;
}

interface AffixOptions {
prefix?: string | null | undefined;
suffix?: string | null | undefined;
dir?: string | undefined;
}

const dir = path.resolve(os.tmpdir());
const RDWR_EXCL = cnst.O_CREAT | cnst.O_TRUNC | cnst.O_RDWR | cnst.O_EXCL;
const dirsToDelete: string[] = [];
const rimrafSync = rimraf.sync;

const promisify = function (callback: any, ...arges: any[]) {
if (typeof callback === "function") {
return [undefined, callback];
}

let promiseCallback;
const promise = new Promise(function (resolve, reject) {
promiseCallback = function () {
const args = Array.from(arges);
const err = args.shift();

process.nextTick(function () {
if (err) {
reject(err);
} else if (args.length === 1) {
resolve(args[0]);
} else {
resolve(args);
}
});
};
});

return [promise, promiseCallback];
};

const parseAffixes = function (
rawAffixes: string | AffixOptions | undefined,
defaultPrefix: string
) {
let affixes: AffixOptions = { prefix: null, suffix: null };
if (rawAffixes) {
switch (typeof rawAffixes) {
case "string":
affixes.prefix = rawAffixes;
break;
case "object":
affixes = rawAffixes;
break;
default:
throw new Error("Unknown affix declaration: " + affixes);
}
} else {
affixes.prefix = defaultPrefix;
}
return affixes;
};

const generateName = function (
rawAffixes: string | AffixOptions | undefined,
defaultPrefix: string
) {
const affixes: AffixOptions = parseAffixes(rawAffixes, defaultPrefix);
const now = new Date();
const name = [
affixes.prefix,
now.getFullYear(),
now.getMonth(),
now.getDate(),
"-",
process.pid,
"-",
(Math.random() * 0x100000000 + 1).toString(36),
affixes.suffix
].join("");
return path.join(affixes.dir || dir, name);
};

function cleanupFilesSync() {
if (!tracking) {
return false;
}
let count = 0;
let toDelete;
while ((toDelete = filesToDelete.shift()) !== undefined) {
rimrafSync(toDelete, { maxBusyTries: 6 });
count++;
}
return count;
}

function cleanupDirsSync() {
if (!tracking) {
return false;
}
let count = 0;
let toDelete;
while ((toDelete = dirsToDelete.shift()) !== undefined) {
rimrafSync(toDelete, { maxBusyTries: 6 });
count++;
}
return count;
}

function cleanupSync() {
if (!tracking) {
return false;
}
const fileCount = cleanupFilesSync();
const dirCount = cleanupDirsSync();
return { files: fileCount, dirs: dirCount };
}

const tracking = false;
let exitListenerAttached = false;

function attachExitListener() {
if (!tracking) return false;
if (!exitListenerAttached) {
process.addListener("exit", function () {
try {
cleanupSync();
} catch (err) {
console.warn("Fail to clean temporary files on exit : ", err);
throw err;
}
});
exitListenerAttached = true;
}
}
const filesToDelete: string[] = [];

function deleteFileOnExit(filePath: string) {
if (!tracking) return false;
attachExitListener();
filesToDelete.push(filePath);
}

const tempOpen = (
affixes: string | AffixOptions | undefined,
callback: (err: any, result: OpenFile) => void
) => {
const p = promisify(callback);
const promise = p[0];
callback = p[1];

const path = generateName(affixes, "f-");
fs.open(path, RDWR_EXCL, 0o600, (err, fd) => {
if (!err) {
deleteFileOnExit(path);
}
callback(err, { path, fd });
});
return promise;
};

/**
* @description: fork by "editor"
* @description: fork by "editor" v1.0.0
*/
const editor = (file?: string, opts?: any | object, cb?: any) => {
if (typeof opts === "function") {
Expand Down
2 changes: 1 addition & 1 deletion packages/cz-git/src/until/rules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @description: fork by "@commitlint/cz-commitlint"
* @description: fork by "@commitlint/cz-commitlint/src/utils/" v16.2.1
*/

import { RuleConfigCondition, RuleConfigSeverity } from "../share";
Expand Down
2 changes: 1 addition & 1 deletion packages/cz-git/src/until/until.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @license: MIT
*/

import wrap from "word-wrap";
import { wrap } from "./wrap";
// @ts-ignore

import { Answers, CommitizenGitOptions, Option, ScopesType } from "../share";
Expand Down
52 changes: 52 additions & 0 deletions packages/cz-git/src/until/wrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @description: fork by "word-wrap" v1.2.3"
*/

function identity(str: string) {
return str;
}

interface Options {
width?: number;
indent?: string;
newline?: string;
escape?: (str: string) => string;
trim?: boolean;
cut?: boolean;
}

export const wrap = (str: string, options: Options) => {
options = options || {};
if (str == null) {
return str;
}

const width = options.width || 50;
const indent = typeof options.indent === "string" ? options.indent : "";

const newline = options.newline || "\n" + indent;
const escape = typeof options.escape === "function" ? options.escape : identity;

let regexString = ".{1," + width + "}";
if (options.cut !== true) {
regexString += "([\\s\u200B]+|$)|[^\\s\u200B]+?([\\s\u200B]+|$)";
}

const re = new RegExp(regexString, "g");
const lines = str.match(re) || [];
let result =
indent +
lines
.map(function (line) {
if (line.slice(-1) === "\n") {
line = line.slice(0, line.length - 1);
}
return escape(line);
})
.join(newline);

if (options.trim === true) {
result = result.replace(/[ \t]*$/gm, "");
}
return result;
};
4 changes: 1 addition & 3 deletions packages/cz-git/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@
"rootDir": "src",
"outDir": "lib"
},
"references": [
{ "path": "../@cz-git/editor/tsconfig.json" }
]
"references": []
}
1 change: 0 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"references": [
{ "path": "./packages/cz-git/tsconfig.json" },
{ "path": "./packages/@cz-git/loader/tsconfig.json" },
{ "path": "./packages/@cz-git/editor/tsconfig.json" },
],
"files": []
}
Loading

0 comments on commit e94f05a

Please sign in to comment.