Skip to content

Commit 5cea5f1

Browse files
committed
feat(core): add support for plugins plugin in config
1 parent 9d2d2bd commit 5cea5f1

File tree

5 files changed

+83
-4
lines changed

5 files changed

+83
-4
lines changed

packages/core/__tests__/helpers/loaderdata.provider.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,15 @@ export default (flavor: string, root: string) => {
104104
() => {}
105105
],
106106
}`,
107+
108+
[tsp(
109+
"pluginsplugindir/package.json",
110+
)]: `{"name":"testpluginsplugin","main":"index.js"}`,
111+
[tsp(
112+
"pluginsplugindir/index.ts",
113+
)]: `export default { code:"PLUGINS_PLUGIN_CONTENT", id:2 }`,
114+
[tsp(
115+
"pluginsplugindir/hello.ts",
116+
)]: `export default { code:"PLUGINS_PLUGIN_HELLO", id:1 }`,
107117
};
108118
};

packages/core/__tests__/loader.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,38 @@ describe("LESY:Loader", () => {
354354
},
355355
});
356356
});
357+
358+
it("should load plugins plugin if provided in config", () => {
359+
jest.spyOn(loader, "loadPluginFromFile");
360+
loader["root"] = d``;
361+
loader.loadPlugins([
362+
[
363+
"plugindir",
364+
{ plugins: ["pluginsplugindir", p`pluginsplugindir/hello`] },
365+
],
366+
]);
367+
expect(loader.pluginConfigs).toEqual({
368+
plugindir: {
369+
plugins: [
370+
{
371+
module: {
372+
code: "PLUGINS_PLUGIN_CONTENT",
373+
id: 2,
374+
},
375+
src: "pluginsplugindir",
376+
},
377+
{
378+
module: {
379+
code: "PLUGINS_PLUGIN_HELLO",
380+
id: 1,
381+
},
382+
src: p`pluginsplugindir/hello`,
383+
},
384+
],
385+
},
386+
});
387+
});
388+
357389
it("should show error if plugin source in invalid", () => {
358390
const logSpy = jest.spyOn(console, "log");
359391
loader.loadPlugins(["abc"]);

packages/core/src/feature.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Feature, FeatureObj } from "./model";
2+
import { utils } from "./utilities";
23

34
class LesyFeature {
45
private feature = {};
56
constructor(private root: string) {}
67

78
add(featureFn: Feature) {
8-
featureFn(this.feature, this.root);
9+
featureFn(this.feature, this.root, utils);
910
}
1011
getFeatures(): FeatureObj {
1112
return this.feature;

packages/core/src/loader.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ class LesyLoader {
7474
loadPlugins(paths: Plugin[]): void {
7575
paths.forEach((pluginSource: any) => {
7676
if (Array.isArray(pluginSource)) {
77-
this.pluginConfigs[pluginSource[0]] = pluginSource[1] || {};
77+
const [pluginPath, pluginConfig = {}] = pluginSource;
78+
this.pluginConfigs[pluginPath] = this.loadPluginsPlugins(pluginConfig);
7879
// tslint:disable-next-line: no-parameter-reassignment
79-
pluginSource = pluginSource[0];
80+
pluginSource = pluginPath;
8081
}
8182
try {
8283
const isObject = typeof pluginSource === "object";
@@ -172,6 +173,41 @@ class LesyLoader {
172173
const isDir = lstatSync(fileOrDir).isDirectory();
173174
if (isDir) return this.loadFromDir(fileOrDir, name);
174175
}
176+
177+
private loadPluginsPlugins(pluginConfig = {}) {
178+
const pluginsPlugins = pluginConfig["plugins"];
179+
if (!pluginsPlugins || !Array.isArray(pluginsPlugins)) return pluginConfig;
180+
const pluginsPluginsData = [];
181+
pluginsPlugins.forEach((pluginSource: string) => {
182+
try {
183+
const isFile = lstatSync(pluginSource).isFile();
184+
if (isFile) {
185+
pluginsPluginsData.push({
186+
src: pluginSource,
187+
module: this.getModuleFromFile(pluginSource),
188+
});
189+
return;
190+
}
191+
} catch (e) {}
192+
193+
try {
194+
const paths = [this.root];
195+
pluginsPluginsData.push({
196+
src: pluginSource,
197+
module: this.getModuleFromFile(
198+
require.resolve(pluginSource, { paths }),
199+
),
200+
});
201+
} catch (e) {
202+
console.log(`${pluginSource} is not loaded`);
203+
console.log(`Error: ${e.message}`);
204+
}
205+
});
206+
return {
207+
...pluginConfig,
208+
plugins: pluginsPluginsData,
209+
};
210+
}
175211
}
176212

177213
export { LesyLoader };

packages/core/src/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export interface FeatureObj {
114114
[key: string]: any;
115115
}
116116

117-
export type Feature = (feature: FeatureObj, opts: any) => void;
117+
export type Feature = (feature: FeatureObj, opts: any, utils: any) => void;
118118

119119
export interface Constructor<T> extends Function {
120120
new (...args: any[]): T;

0 commit comments

Comments
 (0)