/
plugins-loader.ts
117 lines (103 loc) · 3.43 KB
/
plugins-loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { join } from 'path';
import * as ts from 'typescript';
import { CLI_ERRORS } from '../../ui';
import { ReadonlyVisitor } from '../interfaces/readonly-visitor.interface';
const PLUGIN_ENTRY_FILENAME = 'plugin';
type Transformer = ts.TransformerFactory<any> | ts.CustomTransformerFactory;
type PluginEntry = string | PluginAndOptions;
type PluginOptions = Record<string, any>;
interface PluginAndOptions {
name: 'string';
options: PluginOptions;
}
export interface NestCompilerPlugin {
before?: (options?: PluginOptions, program?: ts.Program) => Transformer;
after?: (options?: PluginOptions, program?: ts.Program) => Transformer;
afterDeclarations?: (
options?: PluginOptions,
program?: ts.Program,
) => Transformer;
ReadonlyVisitor?: { new (options: PluginOptions): ReadonlyVisitor };
}
export interface MultiNestCompilerPlugins {
beforeHooks: Array<(program?: ts.Program) => Transformer>;
afterHooks: Array<(program?: ts.Program) => Transformer>;
afterDeclarationsHooks: Array<(program?: ts.Program) => Transformer>;
readonlyVisitors: Array<ReadonlyVisitor>;
}
export class PluginsLoader {
public load(
plugins: PluginEntry[] = [],
extras: { pathToSource?: string } = {},
): MultiNestCompilerPlugins {
const pluginNames = plugins.map((entry) =>
typeof entry === 'object'
? (entry as PluginAndOptions).name
: (entry as string),
);
const pluginRefs = this.resolvePluginReferences(pluginNames);
const multiCompilerPlugins: MultiNestCompilerPlugins = {
afterHooks: [],
afterDeclarationsHooks: [],
beforeHooks: [],
readonlyVisitors: [],
};
pluginRefs.forEach((plugin, index) => {
if (!plugin.before && !plugin.after && !plugin.afterDeclarations) {
throw new Error(CLI_ERRORS.WRONG_PLUGIN(pluginNames[index]));
}
const options =
typeof plugins[index] === 'object'
? (plugins[index] as PluginAndOptions).options || {}
: {};
if (plugin.before) {
multiCompilerPlugins.beforeHooks.push(
plugin.before.bind(plugin.before, options),
);
}
if (plugin.after) {
multiCompilerPlugins.afterHooks.push(
plugin.after.bind(plugin.after, options),
);
}
if (plugin.afterDeclarations) {
multiCompilerPlugins.afterDeclarationsHooks.push(
plugin.afterDeclarations.bind(plugin.afterDeclarations, options),
);
}
if (plugin.ReadonlyVisitor) {
const instance = new plugin.ReadonlyVisitor({
...options,
...extras,
readonly: true,
});
instance.key = pluginNames[index];
multiCompilerPlugins.readonlyVisitors.push(instance);
}
});
return multiCompilerPlugins;
}
private resolvePluginReferences(pluginNames: string[]): NestCompilerPlugin[] {
const nodeModulePaths = [
join(process.cwd(), 'node_modules'),
...module.paths,
];
return pluginNames.map((item) => {
try {
try {
const binaryPath = require.resolve(
join(item, PLUGIN_ENTRY_FILENAME),
{
paths: nodeModulePaths,
},
);
return require(binaryPath);
} catch {}
const binaryPath = require.resolve(item, { paths: nodeModulePaths });
return require(binaryPath);
} catch (e) {
throw new Error(`"${item}" plugin is not installed.`);
}
});
}
}