|
1 | 1 | import fs from "fs"; |
2 | 2 | import path from "path"; |
| 3 | +import os from "os"; |
| 4 | +import cnst from "constants"; |
| 5 | +import rimraf from "rimraf"; |
3 | 6 | import { spawn } from "child_process"; |
4 | | -import { open as tempOpen } from "temp"; |
5 | 7 | import { Answers, CommitizenGitOptions } from "../share"; |
6 | 8 | import { buildCommit, log } from "./until"; |
7 | 9 |
|
8 | 10 | /** |
9 | | - * @description: fork by "temp/open" |
| 11 | + * @description: fork by "temp/open" v0.9.4 |
10 | 12 | */ |
| 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 | +}; |
11 | 174 |
|
12 | 175 | /** |
13 | | - * @description: fork by "editor" |
| 176 | + * @description: fork by "editor" v1.0.0 |
14 | 177 | */ |
15 | 178 | const editor = (file?: string, opts?: any | object, cb?: any) => { |
16 | 179 | if (typeof opts === "function") { |
|
0 commit comments