@vivo-minigame/cli-shared-utils
Version:
shared utilities for minigame-cli packages
6 lines • 3.31 kB
JavaScript
;var _Object$defineProperty=require("@babel/runtime-corejs2/core-js/object/define-property"),_interopRequireDefault=require("@babel/runtime-corejs2/helpers/interopRequireDefault");_Object$defineProperty(exports,"__esModule",{value:!0}),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"),_fsExtra=_interopRequireDefault(require("fs-extra")),_path=_interopRequireDefault(require("path")),_lruCache=_interopRequireDefault(require("lru-cache")),_semver=_interopRequireDefault(require("semver"));let _hasYarn;const _yarnProjects=new _lruCache.default({max:10,maxAge:1e3});let _hasGit;const _gitProjects=new _lruCache.default({max:10,maxAge:1e3}),hasYarn=()=>{if(null!=_hasYarn)return _hasYarn;try{return(0,_child_process.execSync)("yarnpkg --version",{stdio:"ignore"}),_hasYarn=!0}catch(e){return _hasYarn=!1}};// env detection
exports.hasYarn=hasYarn;const hasProjectYarn=cwd=>{if(_yarnProjects.has(cwd))return checkYarn(_yarnProjects.get(cwd));const lockFile=_path.default.join(cwd,"yarn.lock"),result=_fsExtra.default.existsSync(lockFile);return _yarnProjects.set(cwd,result),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(null!=_hasGit)return _hasGit;try{return(0,_child_process.execSync)("git --version",{stdio:"ignore"}),_hasGit=!0}catch(e){return _hasGit=!1}};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=!0}catch(e){result=!1}return _gitProjects.set(cwd,result),result};exports.hasProjectGit=hasProjectGit;let _hasPnpm,_hasPnpm3orLater;const _pnpmProjects=new _lruCache.default({max:10,maxAge:1e3}),hasPnpm3OrLater=()=>{if(null!=_hasPnpm3orLater)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
return _hasPnpm=!0,_hasPnpm3orLater=_semver.default.gte(pnpmVersion,"3.0.0"),_hasPnpm3orLater}catch(e){return _hasPnpm3orLater=!1}};exports.hasPnpm3OrLater=hasPnpm3OrLater;const hasProjectPnpm=cwd=>{if(_pnpmProjects.has(cwd))return checkPnpm(_pnpmProjects.get(cwd));const lockFile=_path.default.join(cwd,"pnpm-lock.yaml"),result=_fsExtra.default.existsSync(lockFile);return _pnpmProjects.set(cwd,result),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="win32"===process.platform,isMacintosh=exports.isMacintosh="darwin"===process.platform,isLinux=exports.isLinux="linux"===process.platform,requiredVersion=exports.requiredVersion=require("../../package.json").engines.node;// 依赖的node版本可能会全局使用,提到shared模块