/
load-plugins.ts
307 lines (255 loc) · 7.34 KB
/
load-plugins.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* eslint-disable @typescript-eslint/no-explicit-any */
import { execSync } from "child_process";
import chalk from "chalk";
import endent from "endent";
import glob from "fast-glob";
import * as path from "path";
import { Auto } from "../auto";
import { ILogger } from "./logger";
import tryRequire from "./try-require";
import InteractiveInit from "../init";
import { LoadedAutoRc } from "../types";
import isBinary from "./is-binary";
export type IPluginConstructor = new (options?: any) => IPlugin;
/** A plugin to auto */
export interface IPlugin {
/** The name to identify the plugin by */
name: string;
/** Called when running `auto init`. gives plugin ability to add custom init experience. */
init?(initializer: InteractiveInit): void;
/** Called when registering the plugin with auto */
apply(auto: Auto): void;
}
const pluginPatterns = [
"@auto-it",
"@auto-canary",
"auto-plugin-",
"auto\\/plugins",
].map((p) => new RegExp(`${p}\\/([a-zA-Z-_\\.]+)$`));
const excluded = ["core", "cli", "bot-list"];
export interface InstalledModule {
/** The name of the module */
name: string;
/** The path to the module */
path: string;
}
/** Format a list of plugins */
const formatPluginList = (plugins: string[]): InstalledModule[] =>
plugins
.map((plugin) => {
if (!pluginPatterns.some((pattern) => plugin.match(pattern))) {
return undefined;
}
const parts = plugin.includes("node_modules/")
? plugin.split("node_modules/")
: plugin.split("auto/plugins/");
const name = parts[parts.length - 1]
.replace("@auto-it/", "")
.replace("@auto-canary/", "");
if (excluded.includes(name)) {
return undefined;
}
return {
name,
path: plugin,
};
})
.filter((m: InstalledModule | undefined): m is InstalledModule =>
Boolean(m)
);
/** Get the paths of available installed plugins. */
export const getInstalledPlugins = (global = false): InstalledModule[] => {
let modules: string[] = [];
try {
const stdout = execSync(`npm ls --parseable ${global ? "--global" : ""}`, {
encoding: "utf8",
stdio: ["pipe", "pipe", "ignore"],
});
modules = stdout.split("\n");
} catch (error) {
modules = error.stdout.split("\n");
}
return formatPluginList(modules);
};
/** Require a plugin and log where it was found. */
function requirePlugin(
pluginPath: string,
logger: ILogger,
extendedLocation?: string
) {
const plugin = tryRequire(pluginPath, extendedLocation);
if (plugin) {
logger.verbose.info(`Found plugin using: ${pluginPath}`);
}
return plugin as
| IPluginConstructor
| {
/** The plugin under the default export */
default: IPluginConstructor;
}
| undefined;
}
/** Try to load a plugin in various ways */
export function findPlugin(
pluginPath: string,
logger: ILogger,
extendedLocation?: string
): string | undefined {
const isLocal =
pluginPath.startsWith(".") ||
pluginPath.startsWith("/") ||
pluginPath.match(/^[A-Z]:\\/); // Support for windows paths
/** Attempt to require a plugin */
const exists = (p: string) =>
Boolean(requirePlugin(p, logger, extendedLocation));
// Try requiring a path
if (isLocal && exists(pluginPath)) {
return pluginPath;
}
// Try requiring a path from cwd
if (isLocal) {
let localPath = path.join(process.cwd(), pluginPath);
if (exists(localPath)) {
return localPath;
}
if (extendedLocation) {
localPath = path.join(
extendedLocation.endsWith("package.json")
? path.dirname(extendedLocation)
: extendedLocation,
pluginPath
);
if (exists(localPath)) {
return localPath;
}
}
}
// For pkg bundle
const pkgPath = path.join(
"/snapshot/auto/plugins/",
pluginPath,
"dist/index.js"
);
if (exists(pkgPath)) {
return pkgPath;
}
const userPlugin = `auto-plugin-${pluginPath}`;
// For a user created plugin
if (exists(userPlugin)) {
return userPlugin;
}
const officialPlugin = path.join("@auto-it", pluginPath);
// Try importing official plugin
if (exists(officialPlugin)) {
return officialPlugin;
}
const canaryPlugin = path.join("@auto-canary", pluginPath);
// Try importing official plugin
if (exists(canaryPlugin)) {
return canaryPlugin;
}
// Try requiring a package
if (
pluginPath.includes("/auto-plugin-") ||
pluginPath.startsWith("auto-plugin-") ||
pluginPath.startsWith("@auto-it")
) {
if (exists(pluginPath)) {
return pluginPath;
}
}
logger.log.warn(`Could not find plugin: ${pluginPath}`);
}
/** Try to load a plugin in various ways */
export function loadPlugin(
[pluginPath, options]: [string, any],
logger: ILogger,
extendedLocation?: string
): IPlugin | undefined {
const localPluginPath = findPlugin(pluginPath, logger, extendedLocation);
if (!localPluginPath) {
return;
}
const plugin = requirePlugin(localPluginPath, logger, extendedLocation);
if (!plugin) {
return;
}
try {
if ("default" in plugin && plugin.default) {
// eslint-disable-next-line new-cap
return new plugin.default(options);
}
return new (plugin as IPluginConstructor)(options);
} catch (error) {
logger.log.error(
`Plugin at the following path encountered an error: ${pluginPath}`
);
throw error;
}
}
/** Print a list of plugins */
const printPlugins = (title: string, modules: InstalledModule[]) => {
if (!modules.length) {
return;
}
console.log(endent`
${chalk.underline.white(title)}
${modules.map((plugin) => `- ${plugin.name} (${plugin.path})`).join("\n")}
`);
console.log("");
};
/** List some of the plugins available to auto */
export const listPlugins = async (
{ plugins = [] }: LoadedAutoRc,
logger: ILogger,
extendedLocation?: string
) => {
const rcPlugins = plugins.map((plugin) => {
const name = typeof plugin === "string" ? plugin : plugin[0];
const pluginPath = findPlugin(name, logger, extendedLocation) || "";
return {
name,
path: pluginPath.replace("/dist/index.js", ""),
};
});
printPlugins("Found the following plugins in your .autorc:", rcPlugins);
const bundledPlugins = isBinary()
? formatPluginList(
await glob("/snapshot/auto/plugins/**", {
onlyDirectories: true,
deep: 0,
})
)
: [];
printPlugins(
"Found the following plugins bundled with your binary:",
bundledPlugins
);
const localPlugins = getInstalledPlugins().map((installed) => ({
...installed,
path: path.relative(process.cwd(), installed.path),
}));
printPlugins(
"Found the following plugins installed in your project:",
localPlugins
);
const globalPlugins = getInstalledPlugins(true);
printPlugins(
"Found the following plugins globally installed in your environment:",
globalPlugins
);
if (
!rcPlugins.length &&
!localPlugins.length &&
!globalPlugins.length &&
!bundledPlugins.length
) {
logger.log.note(
"No plugins found through .autorc, npm, or binary. There might be other plugins available as files on your machine."
);
} else {
logger.log.note(
'There might be other plugins available as files on your machine. This flag can only list plugins from your .autorc or plugins managed with "npm".'
);
}
};