Skip to content

Commit 0a72410

Browse files
refactor: create-webpack-app (#4352)
1 parent ce1e784 commit 0a72410

File tree

13 files changed

+59
-40
lines changed

13 files changed

+59
-40
lines changed

packages/create-webpack-app/package.json

-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
"lib",
4040
"!**/*__tests__"
4141
],
42-
"peerDependencies": {
43-
"webpack-cli": "^5.x.x"
44-
},
4542
"dependencies": {
4643
"@inquirer/prompts": "^7.2.0",
4744
"colorette": "^2.0.20",

packages/create-webpack-app/src/generators/init/default.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Answers, ActionType, FileRecord } from "../../types";
1+
import { type Answers, type ActionType, type FileRecord } from "../../types";
2+
import { type NodePlopAPI, type DynamicActionsFunction } from "node-plop";
23
import { dirname, join, resolve } from "path";
3-
import { NodePlopAPI, DynamicActionsFunction } from "node-plop";
44
import { fileURLToPath } from "url";
55

6-
const __dirname = dirname(fileURLToPath(import.meta.url));
7-
86
export default async function (plop: NodePlopAPI) {
7+
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
99
// dependencies to be installed
1010
const devDependencies: Array<string> = ["webpack", "webpack-cli"];
1111

packages/create-webpack-app/src/generators/init/react.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Answers, ActionType, FileRecord } from "../../types";
1+
import { type Answers, type ActionType, type FileRecord } from "../../types";
2+
import { type NodePlopAPI, type DynamicActionsFunction } from "node-plop";
23
import { dirname, resolve, join } from "path";
3-
import { NodePlopAPI, DynamicActionsFunction } from "node-plop";
44
import { fileURLToPath } from "url";
55

6-
const __dirname = dirname(fileURLToPath(import.meta.url));
7-
86
export default async function (plop: NodePlopAPI) {
7+
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
99
// dependencies to be installed
1010
const devDependencies: Array<string> = [
1111
"webpack",

packages/create-webpack-app/src/generators/init/svelte.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Answers, ActionType, FileRecord } from "../../types";
1+
import { type Answers, type ActionType, type FileRecord } from "../../types";
2+
import { type NodePlopAPI, type DynamicActionsFunction } from "node-plop";
23
import { dirname, join, resolve } from "path";
3-
import { NodePlopAPI, DynamicActionsFunction } from "node-plop";
44
import { fileURLToPath } from "url";
55

6-
const __dirname = dirname(fileURLToPath(import.meta.url));
7-
86
export default async function (plop: NodePlopAPI) {
7+
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
99
// dependencies to be installed
1010
const devDependencies: Array<string> = [
1111
"webpack",

packages/create-webpack-app/src/generators/init/vue.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Answers, ActionType, FileRecord } from "../../types";
1+
import { type Answers, type ActionType, type FileRecord } from "../../types";
2+
import { type NodePlopAPI, type DynamicActionsFunction } from "node-plop";
23
import { dirname, join, resolve } from "path";
3-
import { NodePlopAPI, DynamicActionsFunction } from "node-plop";
44
import { fileURLToPath } from "url";
55

6-
const __dirname = dirname(fileURLToPath(import.meta.url));
76
export default async function (plop: NodePlopAPI) {
7+
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
89
// dependencies to be installed
910
const devDependencies: Array<string> = [
1011
"webpack",

packages/create-webpack-app/src/generators/loader/default.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Answers, ActionType, FileRecord } from "../../types";
1+
import { type Answers, type ActionType, type FileRecord } from "../../types";
2+
import { type NodePlopAPI, type DynamicActionsFunction } from "node-plop";
23
import { dirname, join, resolve } from "path";
3-
import { NodePlopAPI, DynamicActionsFunction } from "node-plop";
44
import { fileURLToPath } from "url";
55
import { logger } from "../../utils/logger.js";
66

7-
const __dirname = dirname(fileURLToPath(import.meta.url));
8-
97
export default async function (plop: NodePlopAPI) {
8+
const __dirname = dirname(fileURLToPath(import.meta.url));
9+
1010
// dependencies to be installed
1111
const devDependencies: Array<string> = ["webpack-defaults"];
1212

packages/create-webpack-app/src/generators/plugin/default.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Answers, ActionType, FileRecord } from "../../types";
1+
import { type Answers, type ActionType, type FileRecord } from "../../types";
2+
import { type NodePlopAPI, type DynamicActionsFunction } from "node-plop";
23
import { dirname, join, resolve } from "path";
3-
import { NodePlopAPI, DynamicActionsFunction } from "node-plop";
44
import { fileURLToPath } from "url";
55
import { logger } from "../../utils/logger.js";
66

7-
const __dirname = dirname(fileURLToPath(import.meta.url));
8-
97
export default async function (plop: NodePlopAPI) {
8+
const __dirname = dirname(fileURLToPath(import.meta.url));
9+
1010
// dependencies to be installed
1111
const devDependencies: Array<string> = ["webpack-defaults"];
1212

packages/create-webpack-app/src/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Command } from "commander";
22
import { resolve, dirname } from "path";
33
import { select } from "@inquirer/prompts";
4-
import nodePlop, { PlopGenerator } from "node-plop";
4+
import nodePlop, { type PlopGenerator } from "node-plop";
55
import { fileURLToPath } from "url";
66

77
import { onSuccessHandler, onFailureHandler, logger } from "./utils/logger.js";
8-
import { Answers, InitOptions, LoaderOptions, PluginOptions } from "./types";
8+
import { type Answers, type InitOptions, type LoaderOptions, type PluginOptions } from "./types";
99

1010
const __dirname = dirname(fileURLToPath(import.meta.url));
1111

@@ -63,7 +63,6 @@ const pluginGenerators: Record<string, PlopGenerator> = {
6363
};
6464

6565
program
66-
.version("1.0.0", "-v, --version")
6766
.usage("[command] [options]")
6867
.helpOption("-h, --help", "Display help for command")
6968
.description("A CLI tool to generate a Webpack project");
@@ -127,6 +126,7 @@ program
127126
process.exit(2);
128127
}
129128
});
129+
130130
program
131131
.command("loader")
132132
.aliases(["l", "ld"])
@@ -167,6 +167,7 @@ program
167167
process.exit(2);
168168
}
169169
});
170+
170171
program
171172
.command("plugin")
172173
.aliases(["p", "pl"])

packages/create-webpack-app/src/plopfile.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NodePlopAPI } from "node-plop";
1+
import { type NodePlopAPI } from "node-plop";
22

33
export default async function (plop: NodePlopAPI) {
44
//init generators

packages/create-webpack-app/src/types.ts

+12
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ export type FileRecord = {
2424
filePath: string;
2525
fileType: string;
2626
};
27+
28+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
29+
type LogHandler = (value: any) => void;
30+
31+
export interface Logger {
32+
error: LogHandler;
33+
warn: LogHandler;
34+
info: LogHandler;
35+
success: LogHandler;
36+
log: LogHandler;
37+
raw: LogHandler;
38+
}

packages/create-webpack-app/src/utils/fileGenerator.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { spawn, sync } from "cross-spawn";
55
import * as path from "path";
66
import { fileURLToPath } from "url";
77
import { logger } from "./logger.js";
8-
import { NodePlopAPI } from "node-plop";
9-
import { Answers } from "../types";
8+
import { type NodePlopAPI } from "node-plop";
9+
import { type Answers } from "../types";
1010

1111
export interface AddConfig {
1212
type: string; // Type of action
@@ -32,7 +32,6 @@ export interface GlobalConfig {
3232
overwriteAll: boolean;
3333
}
3434

35-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
3635
const globalConfig: GlobalConfig = { overwriteAll: false };
3736

3837
async function doesFileExists(filePath: string): Promise<boolean> {
@@ -236,6 +235,8 @@ async function checkAndPrepareContent(config: AddConfig, isTemplate: boolean): P
236235
}
237236
}
238237
export default async function (plop: NodePlopAPI) {
238+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
239+
239240
plop.setPlopfilePath(path.resolve(__dirname, "../plopfile.js"));
240241
plop.setDefaultInclude({ actions: true });
241242

packages/create-webpack-app/src/utils/logger.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { WebpackCLILogger } from "webpack-cli";
21
import { green, yellow, Color, red, cyan, blue, blueBright, greenBright } from "colorette";
3-
import { PlopActionHooksChanges, PlopActionHooksFailures } from "../types";
2+
import { type Logger, type PlopActionHooksChanges, type PlopActionHooksFailures } from "../types";
43
import { relative, normalize } from "path";
54

65
const prefix: string = blueBright("create-webpack");
7-
const getLogger = (): WebpackCLILogger => {
6+
const getLogger = (): Logger => {
87
return {
98
error: (val) => console.error(`[${prefix}] ⛔${red(val)}`),
109
warn: (val) => console.warn(`[${prefix}] ⚠️${yellow(val)}`),
@@ -15,6 +14,7 @@ const getLogger = (): WebpackCLILogger => {
1514
};
1615
};
1716
const logger = getLogger();
17+
1818
const typeDisplay: Record<string, Color | string> = {
1919
function: yellow("-> "),
2020
add: green("create "),
@@ -26,6 +26,7 @@ const typeDisplay: Record<string, Color | string> = {
2626
identical: greenBright("identical "),
2727
create: green("create "),
2828
};
29+
2930
function onSuccessHandler(change: PlopActionHooksChanges): void {
3031
change.path.split("\n").forEach((line) => {
3132
const [operationType = "", renderPath = ""] = line.split("|") as [string, string];
@@ -34,6 +35,7 @@ function onSuccessHandler(change: PlopActionHooksChanges): void {
3435
);
3536
});
3637
}
38+
3739
function onFailureHandler(failure: PlopActionHooksFailures): void {
3840
throw new Error(failure.error);
3941
}

packages/create-webpack-app/src/utils/pkgInstallAction.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import { NodePlopAPI } from "node-plop";
1+
import { type NodePlopAPI } from "node-plop";
22
import { dirname, resolve } from "path";
33
import { spawn } from "cross-spawn";
4-
import { ChildProcess, SpawnOptionsWithStdioTuple, StdioNull, StdioPipe } from "child_process";
4+
import {
5+
type ChildProcess,
6+
type SpawnOptionsWithStdioTuple,
7+
type StdioNull,
8+
type StdioPipe,
9+
} from "child_process";
510
import { fileURLToPath } from "url";
611

7-
const __dirname = dirname(fileURLToPath(import.meta.url));
8-
912
export default async function (plop: NodePlopAPI) {
13+
const __dirname = dirname(fileURLToPath(import.meta.url));
14+
1015
plop.setPlopfilePath(resolve(__dirname, "../plopfile.js"));
1116
plop.setDefaultInclude({ actions: true });
1217
plop.setActionType("pkgInstall", (answers, config) => {

0 commit comments

Comments
 (0)