|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const LRU = require('./node_modules/lru-cache'); |
| 7 | +const semver = require('./node_modules/semver'); |
| 8 | + |
| 9 | +let _hasYarn; |
| 10 | +const _yarnProjects = new LRU({ |
| 11 | + max: 10, |
| 12 | + maxAge: 1000, |
| 13 | +}); |
| 14 | +let _hasGit; |
| 15 | +const _gitProjects = new LRU({ |
| 16 | + max: 10, |
| 17 | + maxAge: 1000, |
| 18 | +}); |
| 19 | + |
| 20 | +// env detection |
| 21 | +exports.hasYarn = () => { |
| 22 | + if (process.env.MICRO_APP_TEST) { |
| 23 | + return true; |
| 24 | + } |
| 25 | + if (_hasYarn != null) { |
| 26 | + return _hasYarn; |
| 27 | + } |
| 28 | + try { |
| 29 | + execSync('yarn --version', { stdio: 'ignore' }); |
| 30 | + return (_hasYarn = true); |
| 31 | + } catch (e) { |
| 32 | + return (_hasYarn = false); |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +exports.hasProjectYarn = cwd => { |
| 37 | + if (_yarnProjects.has(cwd)) { |
| 38 | + return checkYarn(_yarnProjects.get(cwd)); |
| 39 | + } |
| 40 | + |
| 41 | + const lockFile = path.join(cwd, 'yarn.lock'); |
| 42 | + const result = fs.existsSync(lockFile); |
| 43 | + _yarnProjects.set(cwd, result); |
| 44 | + return checkYarn(result); |
| 45 | +}; |
| 46 | + |
| 47 | +function checkYarn(result) { |
| 48 | + if (result && !exports.hasYarn()) throw new Error('The project seems to require yarn but it\'s not installed.'); |
| 49 | + return result; |
| 50 | +} |
| 51 | + |
| 52 | +exports.hasGit = () => { |
| 53 | + if (process.env.MICRO_APP_TEST) { |
| 54 | + return true; |
| 55 | + } |
| 56 | + if (_hasGit != null) { |
| 57 | + return _hasGit; |
| 58 | + } |
| 59 | + try { |
| 60 | + execSync('git --version', { stdio: 'ignore' }); |
| 61 | + return (_hasGit = true); |
| 62 | + } catch (e) { |
| 63 | + return (_hasGit = false); |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +exports.hasProjectGit = cwd => { |
| 68 | + if (_gitProjects.has(cwd)) { |
| 69 | + return _gitProjects.get(cwd); |
| 70 | + } |
| 71 | + |
| 72 | + let result; |
| 73 | + try { |
| 74 | + execSync('git status', { stdio: 'ignore', cwd }); |
| 75 | + result = true; |
| 76 | + } catch (e) { |
| 77 | + result = false; |
| 78 | + } |
| 79 | + _gitProjects.set(cwd, result); |
| 80 | + return result; |
| 81 | +}; |
| 82 | + |
| 83 | +let _hasPnpm; |
| 84 | +let _pnpmVersion; |
| 85 | +const _pnpmProjects = new LRU({ |
| 86 | + max: 10, |
| 87 | + maxAge: 1000, |
| 88 | +}); |
| 89 | + |
| 90 | +function getPnpmVersion() { |
| 91 | + if (_pnpmVersion != null) { |
| 92 | + return _pnpmVersion; |
| 93 | + } |
| 94 | + try { |
| 95 | + _pnpmVersion = execSync('pnpm --version', { |
| 96 | + stdio: [ 'pipe', 'pipe', 'ignore' ], |
| 97 | + }).toString(); |
| 98 | + // there's a critical bug in pnpm 2 |
| 99 | + // https://github.com/pnpm/pnpm/issues/1678#issuecomment-469981972 |
| 100 | + // so we only support pnpm >= 3.0.0 |
| 101 | + _hasPnpm = true; |
| 102 | + } catch (e) {} |
| 103 | + return _pnpmVersion || '0.0.0'; |
| 104 | +} |
| 105 | + |
| 106 | +exports.hasPnpmVersionOrLater = version => { |
| 107 | + if (process.env.MICRO_APP_TEST) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + return semver.gte(getPnpmVersion(), version); |
| 111 | +}; |
| 112 | + |
| 113 | +exports.hasPnpm3OrLater = () => { |
| 114 | + return this.hasPnpmVersionOrLater('3.0.0'); |
| 115 | +}; |
| 116 | + |
| 117 | +exports.hasProjectPnpm = cwd => { |
| 118 | + if (_pnpmProjects.has(cwd)) { |
| 119 | + return checkPnpm(_pnpmProjects.get(cwd)); |
| 120 | + } |
| 121 | + |
| 122 | + const lockFile = path.join(cwd, 'pnpm-lock.yaml'); |
| 123 | + const result = fs.existsSync(lockFile); |
| 124 | + _pnpmProjects.set(cwd, result); |
| 125 | + return checkPnpm(result); |
| 126 | +}; |
| 127 | + |
| 128 | +function checkPnpm(result) { |
| 129 | + if (result && !exports.hasPnpm3OrLater()) { |
| 130 | + throw new Error(`The project seems to require pnpm${_hasPnpm ? ' >= 3' : ''} but it's not installed.`); |
| 131 | + } |
| 132 | + return result; |
| 133 | +} |
| 134 | + |
| 135 | +// OS |
| 136 | +exports.isWindows = process.platform === 'win32'; |
| 137 | +exports.isMacintosh = process.platform === 'darwin'; |
| 138 | +exports.isLinux = process.platform === 'linux'; |
| 139 | + |
| 140 | +const browsers = {}; |
| 141 | +let hasCheckedBrowsers = false; |
| 142 | + |
| 143 | +function tryRun(cmd) { |
| 144 | + try { |
| 145 | + return execSync(cmd, { |
| 146 | + stdio: [ 0, 'pipe', 'ignore' ], |
| 147 | + }).toString().trim(); |
| 148 | + } catch (e) { |
| 149 | + return ''; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +function getLinuxAppVersion(binary) { |
| 154 | + return tryRun(`${binary} --version`).replace(/^.* ([^ ]*)/g, '$1'); |
| 155 | +} |
| 156 | + |
| 157 | +function getMacAppVersion(bundleIdentifier) { |
| 158 | + const bundlePath = tryRun(`mdfind "kMDItemCFBundleIdentifier=='${bundleIdentifier}'"`); |
| 159 | + |
| 160 | + if (bundlePath) { |
| 161 | + return tryRun(`/usr/libexec/PlistBuddy -c Print:CFBundleShortVersionString ${ |
| 162 | + bundlePath.replace(/(\s)/g, '\\ ') |
| 163 | + }/Contents/Info.plist`); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +Object.defineProperty(exports, 'installedBrowsers', { |
| 168 | + enumerable: true, |
| 169 | + get() { |
| 170 | + if (hasCheckedBrowsers) { |
| 171 | + return browsers; |
| 172 | + } |
| 173 | + hasCheckedBrowsers = true; |
| 174 | + |
| 175 | + if (exports.isLinux) { |
| 176 | + browsers.chrome = getLinuxAppVersion('google-chrome'); |
| 177 | + browsers.firefox = getLinuxAppVersion('firefox'); |
| 178 | + } else if (exports.isMacintosh) { |
| 179 | + browsers.chrome = getMacAppVersion('com.google.Chrome'); |
| 180 | + browsers.firefox = getMacAppVersion('org.mozilla.firefox'); |
| 181 | + } else if (exports.isWindows) { |
| 182 | + // get chrome stable version |
| 183 | + // https://stackoverflow.com/a/51773107/2302258 |
| 184 | + const chromeQueryResult = tryRun( |
| 185 | + 'reg query "HKLM\\Software\\Google\\Update\\Clients\\{8A69D345-D564-463c-AFF1-A69D9E530F96}" /v pv /reg:32' |
| 186 | + ) || tryRun( |
| 187 | + 'reg query "HKCU\\Software\\Google\\Update\\Clients\\{8A69D345-D564-463c-AFF1-A69D9E530F96}" /v pv /reg:32' |
| 188 | + ); |
| 189 | + if (chromeQueryResult) { |
| 190 | + const matched = chromeQueryResult.match(/REG_SZ\s+(\S*)$/); |
| 191 | + browsers.chrome = matched && matched[1]; |
| 192 | + } |
| 193 | + |
| 194 | + // get firefox version |
| 195 | + // https://community.spiceworks.com/topic/111518-how-to-determine-version-of-installed-firefox-in-windows-batchscript |
| 196 | + const ffQueryResult = tryRun( |
| 197 | + 'reg query "HKLM\\Software\\Mozilla\\Mozilla Firefox" /v CurrentVersion' |
| 198 | + ); |
| 199 | + if (ffQueryResult) { |
| 200 | + const matched = ffQueryResult.match(/REG_SZ\s+(\S*)$/); |
| 201 | + browsers.firefox = matched && matched[1]; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return browsers; |
| 206 | + }, |
| 207 | +}); |
0 commit comments