Skip to content

Commit a553d0f

Browse files
committed
:add: 新增 Env、openBrowser
🚀 0.1.6
1 parent 820aecd commit a553d0f

File tree

11 files changed

+584
-32
lines changed

11 files changed

+584
-32
lines changed

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import npa from 'npm-package-arg';
1515
import parseGitUrl from 'git-url-parse';
1616
import multimatch from 'multimatch';
1717
import stringifyObject from 'stringify-object';
18+
import LRU from 'lru-cache';
1819

1920
export function tryRequire( id: string, req?: Object): any | null;
2021
export function assert(value: any, message?: string | Error): void;
@@ -26,6 +27,8 @@ import * as loadFile from './src/loadFile';
2627
import * as logger from './src/logger';
2728
import * as smartMerge from './src/smartMerge';
2829
import * as virtualFile from './src/virtualFile';
30+
import * as openBrowser from './src/openBrowser';
31+
import * as env from './src/env';
2932

3033
export {
3134
moduleAlias,
@@ -35,6 +38,8 @@ export {
3538
smartMerge,
3639
virtualFile,
3740
logger,
41+
openBrowser,
42+
env,
3843
};
3944

4045
export {
@@ -54,4 +59,5 @@ export {
5459
parseGitUrl,
5560
multimatch,
5661
stringifyObject,
62+
LRU,
5763
}

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const internal = [
2020
'logger',
2121
'smartMerge',
2222
'virtualFile',
23+
'openBrowser',
24+
'Env',
2325
].reduce((obj, key) => {
2426
obj[key] = `./src/${key}`;
2527
return obj;
@@ -46,12 +48,16 @@ const thirdParty = {
4648
parseGitUrl: 'git-url-parse',
4749
multimatch: 'multimatch',
4850
stringifyObject: 'stringify-object',
51+
LRU: 'lru-cache',
4952
};
5053

5154
Object.keys(thirdParty).forEach(key => {
5255
defineProperty(key, thirdParty);
5356
});
5457

58+
59+
// ********************
60+
5561
const alias = {
5662
assert: () => {
5763
return output.logger.assert.bind(output.logger);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@micro-app/shared-utils",
3-
"version": "0.1.3",
3+
"version": "0.1.6",
44
"description": "[Shared] shared utilities for micro-app.",
55
"main": "index.js",
66
"scripts": {
@@ -62,6 +62,7 @@
6262
"import-fresh": "^3.2.1",
6363
"is-glob": "^4.0.1",
6464
"lodash": "^4.17.15",
65+
"lru-cache": "^5.1.1",
6566
"multimatch": "^4.0.0",
6667
"npm-package-arg": "^6.1.1",
6768
"npmlog": "^4.1.2",

src/Env/index.js

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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+
});

src/constants/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
module.exports = {
4+
NAME: 'Micro App',
45
SCOPE_NAME: '@micro-app',
56
INJECT_ID: '_MICRO_APP_INJECT_',
67
};

src/loadFile/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const DEFAULT_LOADERS = Object.freeze({
1212
'.json': loaders.loadJson,
1313
'.yaml': loaders.loadYaml,
1414
'.yml': loaders.loadYaml,
15-
// noExt: loaders.loadYaml,
15+
noExt: loaders.loadYaml,
1616
});
1717

1818
function loaderExt(filename) {

0 commit comments

Comments
 (0)