UNPKG

@vivo-minigame/cli-shared-utils

Version:

shared utilities for minigame-cli packages

124 lines (110 loc) 3.97 kB
"use strict";var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");_Object$defineProperty(exports, "__esModule", { value: true });exports.requiredVersion = exports.isWindows = exports.isMacintosh = exports.isLinux = exports.hasYarn = exports.hasProjectYarn = exports.hasProjectPnpm = exports.hasProjectGit = exports.hasPnpm3OrLater = exports.hasGit = void 0;var _child_process = require("child_process"); var _fsExtra = _interopRequireDefault(require("fs-extra")); var _path = _interopRequireDefault(require("path")); var _lruCache = _interopRequireDefault(require("lru-cache")); var _semver = _interopRequireDefault(require("semver")); let _hasYarn; const _yarnProjects = new _lruCache.default({ max: 10, maxAge: 1000 }); let _hasGit; const _gitProjects = new _lruCache.default({ max: 10, maxAge: 1000 }); // env detection const hasYarn = () => { if (_hasYarn != null) { return _hasYarn; } try { (0, _child_process.execSync)('yarnpkg --version', { stdio: 'ignore' }); return _hasYarn = true; } catch (e) { return _hasYarn = false; } };exports.hasYarn = hasYarn; const hasProjectYarn = (cwd) => { if (_yarnProjects.has(cwd)) { return checkYarn(_yarnProjects.get(cwd)); } const lockFile = _path.default.join(cwd, 'yarn.lock'); const result = _fsExtra.default.existsSync(lockFile); _yarnProjects.set(cwd, result); return checkYarn(result); };exports.hasProjectYarn = hasProjectYarn; function checkYarn(result) { if (result && !hasYarn()) throw new Error(`The project seems to require yarn but it's not installed.`); return result; } const hasGit = () => { if (_hasGit != null) { return _hasGit; } try { (0, _child_process.execSync)('git --version', { stdio: 'ignore' }); return _hasGit = true; } catch (e) { return _hasGit = false; } };exports.hasGit = hasGit; const hasProjectGit = (cwd) => { if (_gitProjects.has(cwd)) { return _gitProjects.get(cwd); } let result; try { (0, _child_process.execSync)('git status', { stdio: 'ignore', cwd }); result = true; } catch (e) { result = false; } _gitProjects.set(cwd, result); return result; };exports.hasProjectGit = hasProjectGit; let _hasPnpm; let _hasPnpm3orLater; const _pnpmProjects = new _lruCache.default({ max: 10, maxAge: 1000 }); const hasPnpm3OrLater = () => { if (_hasPnpm3orLater != null) { return _hasPnpm3orLater; } try { const pnpmVersion = (0, _child_process.execSync)('pnpm --version', { stdio: ['pipe', 'pipe', 'ignore'] }).toString(); // there's a critical bug in pnpm 2 // https://github.com/pnpm/pnpm/issues/1678#issuecomment-469981972 // so we only support pnpm >= 3.0.0 _hasPnpm = true; _hasPnpm3orLater = _semver.default.gte(pnpmVersion, '3.0.0'); return _hasPnpm3orLater; } catch (e) { return _hasPnpm3orLater = false; } };exports.hasPnpm3OrLater = hasPnpm3OrLater; const hasProjectPnpm = (cwd) => { if (_pnpmProjects.has(cwd)) { return checkPnpm(_pnpmProjects.get(cwd)); } const lockFile = _path.default.join(cwd, 'pnpm-lock.yaml'); const result = _fsExtra.default.existsSync(lockFile); _pnpmProjects.set(cwd, result); return checkPnpm(result); };exports.hasProjectPnpm = hasProjectPnpm; function checkPnpm(result) { if (result && !exports.hasPnpm3OrLater()) { throw new Error(`The project seems to require pnpm${_hasPnpm ? ' >= 3' : ''} but it's not installed.`); } return result; } // OS const isWindows = exports.isWindows = process.platform === 'win32'; const isMacintosh = exports.isMacintosh = process.platform === 'darwin'; const isLinux = exports.isLinux = process.platform === 'linux'; // 依赖的node版本可能会全局使用,提到shared模块 const requiredVersion = exports.requiredVersion = require('../../package.json').engines.node;