-
-
Notifications
You must be signed in to change notification settings - Fork 669
Expand file tree
/
Copy pathdefault.ts
More file actions
91 lines (81 loc) · 3.19 KB
/
default.ts
File metadata and controls
91 lines (81 loc) · 3.19 KB
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
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { type DynamicActionsFunction, type NodePlopAPI } from "node-plop";
import { type ActionType, type FileRecord, PluginAnswers } from "../../types.js";
import { logger } from "../../utils/logger.js";
export default async function pluginGenerator(plop: NodePlopAPI) {
const __dirname = dirname(fileURLToPath(import.meta.url));
// dependencies to be installed
const devDependencies: string[] = ["webpack-defaults"];
await plop.load("../../utils/install-dependencies.js", {}, true);
await plop.load("../../utils/generate-files.js", {}, true);
plop.setDefaultInclude({ generators: true, actionTypes: true });
plop.setPlopfilePath(resolve(__dirname, "../../plopfile.js"));
// Define a base generator for the project structure
plop.setGenerator("plugin-default", {
description: "Create a basic webpack plugin.",
prompts: [
{
type: "input",
name: "name",
message: "Plugin name?",
default: "my-webpack-plugin",
filter: (input) => plop.getHelper("kebabCase")(input),
validate: (str: string): boolean => str.length > 0,
},
{
type: "list",
name: "packageManager",
message: "Pick a package manager:",
choices: ["npm", "yarn", "pnpm"],
default: "npm",
validate(input) {
if (!input.trim()) {
return "Package manager cannot be empty";
}
return true;
},
},
],
actions: function actions(answers: PluginAnswers) {
const actions: ActionType[] = [];
answers.projectPath = join(answers.projectPath, answers.name);
logger.error(`
Your project must be inside a folder named ${answers.name}
I will create this folder for you.
`);
answers.pluginIdentifier = plop.getHelper("pascalCase")(answers.name);
const files: FileRecord[] = [
{ filePath: "./package.json", fileType: "text" },
{ filePath: "./examples/simple/src/index.js", fileType: "text" },
{ filePath: "./examples/simple/src/lazy-module.js", fileType: "text" },
{ filePath: "./examples/simple/src/static-esm-module.js", fileType: "text" },
{ filePath: "./examples/simple/webpack.config.js", fileType: "text" },
{ filePath: "./src/cjs.js", fileType: "text" },
{ filePath: "./test/fixtures/simple-file.js", fileType: "text" },
{ filePath: "./test/functional.test.js", fileType: "text" },
{ filePath: "./test/test-utils.js", fileType: "text" },
{ filePath: "./src/index.js", fileType: "text" },
];
for (const file of files) {
actions.push({
type: "generate-files",
path: join(answers.projectPath, file.filePath),
templateFile: join(
plop.getPlopfilePath(),
"../templates/plugin/default",
`${file.filePath}.tpl`,
),
fileType: file.fileType,
data: answers,
});
}
actions.push({
type: "install-dependencies",
path: answers.projectPath,
packages: devDependencies,
});
return actions;
} as DynamicActionsFunction,
});
}