Skip to content

Commit 104b9c4

Browse files
refactor(generators): leverage helpers (#2842)
1 parent 85a9f20 commit 104b9c4

7 files changed

Lines changed: 125 additions & 71 deletions

File tree

packages/generators/src/addon-generator.ts

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "fs";
22
import path from "path";
33
import Generator from "yeoman-generator";
44

5-
import { List } from "./utils/scaffold-utils";
5+
import { getInstaller, getTemplate } from "./utils/helpers";
66

77
// Helper to get the template-directory content
88

@@ -57,42 +57,12 @@ const addonGenerator = (
5757
public copyTpl: (value: string, index: number, array: string[]) => void;
5858

5959
public async prompting(): Promise<void> {
60-
if (!this.supportedTemplates.includes(this.template)) {
61-
this.utils.logger.warn(
62-
`⚠ ${this.template} is not a valid template, please select one from below`,
63-
);
64-
65-
const { selectedTemplate } = await List(
66-
this,
67-
"selectedTemplate",
68-
"Select a valid template from below:",
69-
this.supportedTemplates,
70-
"default",
71-
false,
72-
);
73-
74-
this.template = selectedTemplate;
75-
}
60+
this.template = await getTemplate.call(this);
7661
this.resolvedTemplatePath = path.join(templateDir, this.template);
7762

7863
this.props = await this.prompt(prompts);
7964

80-
const installers = this.utils.getAvailableInstallers();
81-
if (installers.length === 1) {
82-
return ([this.packageManager] = installers);
83-
}
84-
85-
// Prompt for the package manager of choice
86-
const defaultPackager = this.utils.getPackageManager();
87-
const { packager } = await List(
88-
this,
89-
"packager",
90-
"Pick a package manager:",
91-
installers,
92-
defaultPackager,
93-
false,
94-
);
95-
this.packageManager = packager;
65+
this.packageManager = await getInstaller.call(this);
9666
}
9767

9868
public default(): void {

packages/generators/src/init-generator.ts

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { blue, yellow } from "colorette";
2+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
23
import path from "path";
3-
import * as Question from "./utils/scaffold-utils";
44

55
import { CustomGenerator } from "./types";
6-
import { existsSync, mkdirSync } from "fs";
6+
import { getInstaller, getTemplate } from "./utils/helpers";
7+
import * as Question from "./utils/scaffold-utils";
78
import handlers from "./handlers";
89

9-
import { readFileSync, writeFileSync } from "fs";
10-
1110
/**
1211
*
1312
* Generator for initializing a webpack config
@@ -62,22 +61,7 @@ export default class InitGenerator extends CustomGenerator {
6261
}
6362
}
6463

65-
if (!this.supportedTemplates.includes(this.template)) {
66-
this.utils.logger.warn(
67-
`⚠ ${this.template} is not a valid template, please select one from below`,
68-
);
69-
70-
const { selectedTemplate } = await Question.List(
71-
this,
72-
"selectedTemplate",
73-
"Select a valid template from below:",
74-
this.supportedTemplates,
75-
"default",
76-
false,
77-
);
78-
79-
this.template = selectedTemplate;
80-
}
64+
this.template = await getTemplate.call(this);
8165

8266
await handlers[this.template].questions(this, Question);
8367

@@ -101,23 +85,7 @@ export default class InitGenerator extends CustomGenerator {
10185
}
10286

10387
public async installPlugins(): Promise<void> {
104-
const installers = this.utils.getAvailableInstallers();
105-
106-
if (installers.length === 1) {
107-
[this.packageManager] = installers;
108-
} else {
109-
// Prompt for the package manager of choice
110-
const defaultPackager = this.utils.getPackageManager();
111-
const { packager } = await Question.List(
112-
this,
113-
"packager",
114-
"Pick a package manager:",
115-
installers,
116-
defaultPackager,
117-
this.force,
118-
);
119-
this.packageManager = packager;
120-
}
88+
this.packageManager = await getInstaller.call(this);
12189

12290
const opts: {
12391
dev?: boolean;

packages/generators/src/utils/helpers.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { List } from "./scaffold-utils";
2+
13
const regex = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
24

35
/**
@@ -20,3 +22,44 @@ export function toUpperCamelCase(str: string): string {
2022
.map((x) => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
2123
.join("");
2224
}
25+
26+
export async function getInstaller(): Promise<string> {
27+
const installers = this.utils.getAvailableInstallers();
28+
29+
if (installers.length === 1) {
30+
return installers[0];
31+
}
32+
33+
// Prompt for the package manager of choice
34+
const defaultPackager = this.utils.getPackageManager();
35+
const { packager } = await List(
36+
this,
37+
"packager",
38+
"Pick a package manager:",
39+
installers,
40+
defaultPackager,
41+
this.force,
42+
);
43+
return packager;
44+
}
45+
46+
export async function getTemplate(): Promise<string> {
47+
if (this.supportedTemplates.includes(this.template)) {
48+
return this.template;
49+
}
50+
51+
this.utils.logger.warn(
52+
`⚠ ${this.template} is not a valid template, please select one from below`,
53+
);
54+
55+
const { selectedTemplate } = await List(
56+
this,
57+
"selectedTemplate",
58+
"Select a valid template from below:",
59+
this.supportedTemplates,
60+
"default",
61+
false,
62+
);
63+
64+
return selectedTemplate;
65+
}

test/api/__snapshots__/scaffold-utils.test.js.snap.webpack4 renamed to test/api/generators/__snapshots__/scaffold-utils.test.js.snap.webpack4

File renamed without changes.

test/api/__snapshots__/scaffold-utils.test.js.snap.webpack5 renamed to test/api/generators/__snapshots__/scaffold-utils.test.js.snap.webpack5

File renamed without changes.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const path = require("path");
2+
3+
const utilsDirectory = {
4+
cli: "../../../packages/webpack-cli/lib/utils",
5+
generators: "../../../packages/generators/src/utils",
6+
};
7+
8+
jest.setMock(path.join(utilsDirectory.cli, "get-available-installers"), jest.fn());
9+
jest.mock(path.join(utilsDirectory.generators, "scaffold-utils"), () => ({
10+
List: jest.fn(),
11+
}));
12+
13+
const getAvailableInstallers = require(path.join(utilsDirectory.cli, "get-available-installers"));
14+
const getPackageManager = require(path.join(utilsDirectory.cli, "get-package-manager"));
15+
const logger = require(path.join(utilsDirectory.cli, "logger"));
16+
17+
const { getInstaller, getTemplate } = require(path.join(utilsDirectory.generators, "helpers"));
18+
const { List } = require(path.join(utilsDirectory.generators, "scaffold-utils"));
19+
20+
const context = {
21+
prompt: () => {},
22+
supportedTemplates: ["default"],
23+
utils: {
24+
getAvailableInstallers,
25+
getPackageManager,
26+
logger,
27+
},
28+
};
29+
30+
describe("helpers", () => {
31+
it("getInstaller() returns the available installer", async () => {
32+
// Multiple installers are not available
33+
getAvailableInstallers.mockReturnValue(["npm"]);
34+
35+
// Invoke the helper function
36+
const installer = await getInstaller.call(context);
37+
expect(installer).toBe("npm");
38+
});
39+
40+
it("getInstaller() invokes a List prompt if multiple installers are available", async () => {
41+
// Multiple installers are available
42+
getAvailableInstallers.mockReturnValue(["npm", "yarn", "pnpm"]);
43+
44+
// User chose "pnpm"
45+
List.mockReturnValue({ packager: "pnpm" });
46+
47+
// Invoke the helper function
48+
const installer = await getInstaller.call(context);
49+
expect(installer).toBe("pnpm");
50+
});
51+
52+
it("getTemplate() returns with the valid template", async () => {
53+
context.template = "default";
54+
55+
// Invoke the helper function
56+
const template = await getTemplate.call(context);
57+
expect(template).toBe("default");
58+
});
59+
60+
it("getTemplate() invokes a List prompt on supplying an invalid template", async () => {
61+
context.template = "unknown";
62+
63+
// User chose "default"
64+
List.mockReturnValue({ selectedTemplate: "default" });
65+
66+
const loggerMock = jest.spyOn(logger, "warn").mockImplementation(() => {});
67+
68+
// Invoke the helper function`
69+
const template = await getTemplate.call(context);
70+
expect(template).toBe("default");
71+
expect(loggerMock).toHaveBeenCalled();
72+
});
73+
});

test/api/scaffold-utils.test.js renamed to test/api/generators/scaffold-utils.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
InputValidate,
55
Input,
66
// eslint-disable-next-line node/no-missing-require
7-
} = require("../../packages/generators/src/utils/scaffold-utils");
7+
} = require("../../../packages/generators/src/utils/scaffold-utils");
88

99
describe("utils", () => {
1010
let mockSelf;

0 commit comments

Comments
 (0)