UNPKG

@eljs/utils

Version:
685 lines (665 loc) 31.3 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } import { run } from "../cp"; import { isPathExists, isPathExistsSync, readFile, readFileSync } from "../file"; import { isObject } from "../type"; import execa from 'execa'; import ini from 'ini'; import os, { EOL } from 'node:os'; import path from 'node:path'; import { URL } from 'node:url'; /** * git 远程仓库信息 */ /** * git 仓库信息 */ /** * 获取 git 地址 * @param cwd 当前工作目录 * @param exact 是否在当前目录下提取 */ export function getGitUrl(_x, _x2) { return _getGitUrl.apply(this, arguments); } /** * 获取 git 地址 * @param cwd 当前工作目录 * @param exact 是否在当前目录下提取 */ function _getGitUrl() { _getGitUrl = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(cwd, exact) { var gitDir, parsed; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (!exact) { _context.next = 4; break; } _context.t0 = path.join(cwd, '.git'); _context.next = 10; break; case 4: _context.next = 6; return getProjectGitDir(cwd); case 6: _context.t1 = _context.sent; if (_context.t1) { _context.next = 9; break; } _context.t1 = ''; case 9: _context.t0 = _context.t1; case 10: gitDir = _context.t0; _context.next = 13; return isPathExists(gitDir); case 13: if (_context.sent) { _context.next = 15; break; } return _context.abrupt("return", ''); case 15: _context.prev = 15; _context.t2 = ini; _context.next = 19; return readFile(path.join(gitDir, 'config')); case 19: _context.t3 = _context.sent; parsed = _context.t2.parse.call(_context.t2, _context.t3); if (!parsed['remote "origin"']) { _context.next = 23; break; } return _context.abrupt("return", parsed['remote "origin"'].url); case 23: _context.next = 27; break; case 25: _context.prev = 25; _context.t4 = _context["catch"](15); case 27: return _context.abrupt("return", ''); case 28: case "end": return _context.stop(); } }, _callee, null, [[15, 25]]); })); return _getGitUrl.apply(this, arguments); } export function getGitUrlSync(cwd, exact) { var gitDir = exact ? path.join(cwd, '.git') : getProjectGitDirSync(cwd) || ''; if (!isPathExistsSync(gitDir)) { return ''; } try { var parsed = ini.parse(readFileSync(path.join(gitDir, 'config'))); if (parsed['remote "origin"']) { return parsed['remote "origin"'].url; } } catch (err) { // ... } return ''; } /** * 获取 git 分支 * @param options 选项 */ export function getGitBranch(_x3) { return _getGitBranch.apply(this, arguments); } /** * 获取 git 远程分支 * @param options 选项 */ function _getGitBranch() { _getGitBranch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(options) { return _regeneratorRuntime().wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: return _context2.abrupt("return", run('git', ['rev-parse', '--abbrev-ref', 'HEAD'], options).then(function (data) { return data.stdout.trim(); })); case 1: case "end": return _context2.stop(); } }, _callee2); })); return _getGitBranch.apply(this, arguments); } export function getGitUpstreamBranch(_x4) { return _getGitUpstreamBranch.apply(this, arguments); } /** * 获取 git commit 哈希值 * @param options 选项 */ function _getGitUpstreamBranch() { _getGitUpstreamBranch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(options) { var upstream; return _regeneratorRuntime().wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.prev = 0; _context3.next = 3; return run('git', ['rev-parse', '--abbrev-ref', '@{u}'], options).then(function (data) { return data.stdout.trim(); }); case 3: upstream = _context3.sent; return _context3.abrupt("return", upstream); case 7: _context3.prev = 7; _context3.t0 = _context3["catch"](0); return _context3.abrupt("return", null); case 10: case "end": return _context3.stop(); } }, _callee3, null, [[0, 7]]); })); return _getGitUpstreamBranch.apply(this, arguments); } /** * 获取 git commit 哈希值 * @param short 是否截断 * @param options 选项 */ export function getGitCommitSha(_x5, _x6) { return _getGitCommitSha.apply(this, arguments); } /** * 获取 git 最新 tag * @param options 选项 */ function _getGitCommitSha() { _getGitCommitSha = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(short, options) { var cliArgs; return _regeneratorRuntime().wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: if (isObject(short)) { options = short; short = false; } cliArgs = ['rev-parse'].concat(_toConsumableArray(short ? ['--short'] : []), ['HEAD']).filter(Boolean); return _context4.abrupt("return", run('git', cliArgs, options).then(function (data) { return data.stdout.trim(); })); case 3: case "end": return _context4.stop(); } }, _callee4); })); return _getGitCommitSha.apply(this, arguments); } /** * 获取 git 最新 tag * @param match 匹配模式 * @param options 选项 */ /** * 获取 git 最新 tag * @param match 匹配模式 * @param args 命令行参数 * @param options 选项 */ export function getGitLatestTag(_x7, _x8, _x9) { return _getGitLatestTag.apply(this, arguments); } /** * 解析 git 地址 * @param url git 地址 */ function _getGitLatestTag() { _getGitLatestTag = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(match, args, options) { var cliArgs, _yield$run, stdout; return _regeneratorRuntime().wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: if (isObject(match)) { options = match; args = []; match = undefined; } if (isObject(args)) { options = args; args = []; } cliArgs = ['describe', '--tags', '--match', match || '*'].concat(_toConsumableArray(args ? args : [])).filter(Boolean); _context5.prev = 3; _context5.next = 6; return run('git', cliArgs, options); case 6: _yield$run = _context5.sent; stdout = _yield$run.stdout; return _context5.abrupt("return", stdout.trim()); case 11: _context5.prev = 11; _context5.t0 = _context5["catch"](3); return _context5.abrupt("return", null); case 14: case "end": return _context5.stop(); } }, _callee5, null, [[3, 11]]); })); return _getGitLatestTag.apply(this, arguments); } export function gitUrlAnalysis(url) { if (!url) { return null; } try { var repo = ''; var hostname = ''; if (url.startsWith('git')) { // git@git.corp.xxx.com:group/eljs.git // git@github.com:chnliquan/eljs.git var pieces = url.split(':'); hostname = pieces[0].split('@')[1]; repo = pieces[1].replace(/\.git$/, ''); } else if (url.startsWith('http')) { var _parsedUrl$pathname; // https://git.corp.xxx.com/group/eljs.git // https://github.com/chnliquan/eljs.git var parsedUrl = new URL(url); hostname = parsedUrl.hostname || ''; repo = ((_parsedUrl$pathname = parsedUrl.pathname) === null || _parsedUrl$pathname === void 0 || (_parsedUrl$pathname = _parsedUrl$pathname.slice(1)) === null || _parsedUrl$pathname === void 0 ? void 0 : _parsedUrl$pathname.replace(/\.git$/, '')) || ''; } else { return null; } var group = ''; var name = ''; repo.split('/').forEach(function (str, index, arr) { if (index === arr.length - 1) { name = str; } else { group += "/".concat(str); } }); group = group.substring(1); return { name: name, group: group, href: "https://".concat(hostname, "/").concat(group, "/").concat(name), https: "https://".concat(hostname, "/").concat(group, "/").concat(name, ".git"), ssh: "git@".concat(hostname, ":").concat(group, "/").concat(name, ".git") }; } catch (err) { return null; } } /** * 获取指定目录的 git 仓库信息 * @param dir 文件目录 * @param exact 是否在当前目录下提取 */ export function getGitRepository(_x10, _x11) { return _getGitRepository.apply(this, arguments); } /** * 获取指定目录的 git 仓库信息 * @param dir 文件目录 * @param exact 是否在当前目录下提取 */ function _getGitRepository() { _getGitRepository = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(dir, exact) { var gitDir, gitRepo, config, gitHead; return _regeneratorRuntime().wrap(function _callee6$(_context6) { while (1) switch (_context6.prev = _context6.next) { case 0: if (!exact) { _context6.next = 4; break; } _context6.t0 = path.join(dir, '.git'); _context6.next = 10; break; case 4: _context6.next = 6; return getProjectGitDir(dir); case 6: _context6.t1 = _context6.sent; if (_context6.t1) { _context6.next = 9; break; } _context6.t1 = ''; case 9: _context6.t0 = _context6.t1; case 10: gitDir = _context6.t0; _context6.next = 13; return isPathExists(gitDir); case 13: if (_context6.sent) { _context6.next = 15; break; } return _context6.abrupt("return", null); case 15: gitRepo = { name: '', group: '', href: '', https: '', ssh: '', branch: '', author: '', email: '' }; _context6.prev = 16; _context6.t2 = ini; _context6.next = 20; return readFile(path.join(gitDir, 'config')); case 20: _context6.t3 = _context6.sent; config = _context6.t2.parse.call(_context6.t2, _context6.t3); // remote if (config['remote "origin"']) { gitRepo.ssh = config['remote "origin"'].url; if (gitRepo.ssh) { Object.assign(gitRepo, gitUrlAnalysis(gitRepo.ssh)); } } if (config['user']) { gitRepo.author = config['user'].name; gitRepo.email = config['user'].email; } // branch _context6.next = 26; return readFile(path.join(gitDir, 'HEAD')); case 26: gitHead = _context6.sent; gitRepo.branch = gitHead.replace('ref: refs/heads/', '').replace(EOL, ''); _context6.next = 33; break; case 30: _context6.prev = 30; _context6.t4 = _context6["catch"](16); return _context6.abrupt("return", null); case 33: return _context6.abrupt("return", gitRepo); case 34: case "end": return _context6.stop(); } }, _callee6, null, [[16, 30]]); })); return _getGitRepository.apply(this, arguments); } export function getGitRepositorySync(dir, exact) { var gitDir = exact ? path.join(dir, '.git') : getProjectGitDirSync(dir) || ''; if (!isPathExistsSync(gitDir)) { return null; } var gitRepo = { name: '', group: '', href: '', https: '', ssh: '', branch: '', author: '', email: '' }; try { var config = ini.parse(readFileSync(path.join(gitDir, 'config'))); // remote if (config['remote "origin"']) { gitRepo.ssh = config['remote "origin"'].url; if (gitRepo.ssh) { Object.assign(gitRepo, gitUrlAnalysis(gitRepo.ssh)); } } if (config['user']) { gitRepo.author = config['user'].name; gitRepo.email = config['user'].email; } // branch var gitHead = readFileSync(path.join(gitDir, 'HEAD')); gitRepo.branch = gitHead.replace('ref: refs/heads/', '').replace(EOL, ''); } catch (err) { return null; } return gitRepo; } /** * git 用户 */ /** * 获取 git 用户 */ export function getGitUser() { return _getGitUser.apply(this, arguments); } /** * 获取 git 用户 */ function _getGitUser() { _getGitUser = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7() { var user, gitConfig, config, gitFile, parsed, _parsed$user2, name, email; return _regeneratorRuntime().wrap(function _callee7$(_context7) { while (1) switch (_context7.prev = _context7.next) { case 0: user = { name: '', email: '' }; // try to get config by git _context7.prev = 1; _context7.next = 4; return execa('git', ['config', '--list']); case 4: gitConfig = _context7.sent.stdout; if (gitConfig) { config = Object.create(null); gitConfig.split(EOL).forEach(function (line) { var _line$split3 = line.split('='), _line$split4 = _slicedToArray(_line$split3, 2), key = _line$split4[0], value = _line$split4[1]; config[key] = value; }); if (config['user.email']) { user = { name: config['user.email'].split('@')[0], email: config['user.email'] }; } else { user = { name: config['user.name'], email: '' }; } } _context7.next = 10; break; case 8: _context7.prev = 8; _context7.t0 = _context7["catch"](1); case 10: if (!user.email.match(/\.com$/)) { _context7.next = 12; break; } return _context7.abrupt("return", user); case 12: _context7.prev = 12; gitFile = path.join(os.homedir(), '.gitconfig'); _context7.t1 = ini; _context7.next = 17; return readFile(gitFile); case 17: _context7.t2 = _context7.sent; parsed = _context7.t1.parse.call(_context7.t1, _context7.t2); _parsed$user2 = parsed.user, name = _parsed$user2.name, email = _parsed$user2.email; if (email) { user = { name: email.split('@')[0], email: email }; } else { user = { name: name, email: '' }; } _context7.next = 25; break; case 23: _context7.prev = 23; _context7.t3 = _context7["catch"](12); case 25: return _context7.abrupt("return", user); case 26: case "end": return _context7.stop(); } }, _callee7, null, [[1, 8], [12, 23]]); })); return _getGitUser.apply(this, arguments); } export function getGitUserSync() { var user = { name: '', email: '' }; // try to get config by git try { var gitConfig = execa.sync('git', ['config', '--list']).stdout; if (gitConfig) { var config = Object.create(null); gitConfig.split(EOL).forEach(function (line) { var _line$split = line.split('='), _line$split2 = _slicedToArray(_line$split, 2), key = _line$split2[0], value = _line$split2[1]; config[key] = value; }); if (config['user.email']) { user = { name: config['user.email'].split('@')[0], email: config['user.email'] }; } else { user = { name: config['user.name'], email: '' }; } } } catch (err) { // ignore } if (user.email.match(/\.com$/)) { return user; } // try to read .gitconfig try { var gitFile = path.join(os.homedir(), '.gitconfig'); var parsed = ini.parse(readFileSync(gitFile)); var _parsed$user = parsed.user, name = _parsed$user.name, email = _parsed$user.email; if (email) { user = { name: email.split('@')[0], email: email }; } else { user = { name: name, email: '' }; } } catch (err) { // empty } return user; } /** * 获取工程 git 路径 * @param dir 文件夹目录 */ export function getProjectGitDir(_x12) { return _getProjectGitDir.apply(this, arguments); } /** * 获取工程 git 路径 * @param dir 文件目录 */ function _getProjectGitDir() { _getProjectGitDir = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(dir) { var cur, parent; return _regeneratorRuntime().wrap(function _callee8$(_context8) { while (1) switch (_context8.prev = _context8.next) { case 0: cur = dir; case 1: if (!cur) { _context8.next = 14; break; } _context8.next = 4; return isPathExists(path.join(cur, '.git', 'config')); case 4: if (!_context8.sent) { _context8.next = 6; break; } return _context8.abrupt("return", path.join(cur, '.git')); case 6: parent = path.dirname(cur); if (!(parent === cur)) { _context8.next = 11; break; } return _context8.abrupt("break", 14); case 11: cur = parent; case 12: _context8.next = 1; break; case 14: case "end": return _context8.stop(); } }, _callee8); })); return _getProjectGitDir.apply(this, arguments); } export function getProjectGitDirSync(dir) { var cur = dir; while (cur) { // 如果配置存在,说明是 .git 目录 if (isPathExistsSync(path.join(cur, '.git', 'config'))) { return path.join(cur, '.git'); } var parent = path.dirname(cur); if (parent === cur) { break; } else { cur = parent; } } }