Skip to content

Commit 082332b

Browse files
committed
feat: add pluginResolution
1 parent 784ff23 commit 082332b

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ export * as openBrowser from './src/openBrowser';
4141
export * as Env from './src/Env';
4242
export * as validateSchema from './src/validateSchema';
4343
export * as loadFile from './src/loadFile';
44+
export * as pluginResolution from './src/pluginResolution';

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const internal = [
2424
'openBrowser',
2525
'Env',
2626
'validateSchema',
27+
'pluginResolution',
2728
].reduce((obj, key) => {
2829
obj[key] = `./src/${key}`;
2930
return obj;

src/pluginResolution/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const pluginRE = /^(@micro-app\/|microapp-|@[\w-]+(\.)?[\w-]+\/microapp-)plugin-/;
4+
const officialRE = /^@micro-app\//;
5+
6+
exports.isPlugin = id => pluginRE.test(id);
7+
8+
exports.isOfficialPlugin = id => exports.isPlugin(id) && officialRE.test(id);
9+
10+
exports.getPluginLink = id => {
11+
let pkg = {};
12+
try {
13+
pkg = require(`${id}/package.json`);
14+
} catch (e) { /* nothing */ }
15+
return (
16+
pkg.homepage ||
17+
(pkg.repository && pkg.repository.url) ||
18+
`https://www.npmjs.com/package/${id.replace('/', '%2F')}`
19+
);
20+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
/* global expect */
4+
5+
const { pluginResolution } = require('../..');
6+
7+
describe('pluginResolution', () => {
8+
9+
it('isPlugin', () => {
10+
const pname = 'microapp-plugin-abc';
11+
expect(pluginResolution.isPlugin(pname)).toBeTruthy();
12+
expect(pluginResolution.isOfficialPlugin(pname)).toBeFalsy();
13+
});
14+
15+
it('isOfficialPlugin', () => {
16+
const pname = '@micro-app/plugin-abc';
17+
expect(pluginResolution.isPlugin(pname)).toBeTruthy();
18+
expect(pluginResolution.isOfficialPlugin(pname)).toBeTruthy();
19+
});
20+
21+
it('getPluginLink', () => {
22+
const pname = 'microapp-plugin-abc';
23+
expect(pluginResolution.getPluginLink(pname)).toEqual(`https://www.npmjs.com/package/${pname.replace('/', '%2F')}`);
24+
});
25+
26+
});

0 commit comments

Comments
 (0)