Skip to content

Commit debb857

Browse files
authored
refactor: union utils and generators (#2343)
1 parent 9100137 commit debb857

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+130
-217
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
plugins: ['node'],
77
settings: {
88
node: {
9-
allowModules: ['@webpack-cli/generators', '@webpack-cli/utils'],
9+
allowModules: ['@webpack-cli/generators'],
1010
},
1111
},
1212
env: {

packages/generators/__tests__/addon-generator.test.ts

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,14 @@ describe('addon generator', () => {
1818
// we call this unwanted path doubleGenPath
1919
const doubleGenPath = path.join(genPath, genName);
2020

21-
beforeAll(() => {
22-
rimraf.sync(testAssetsPath);
23-
fs.mkdirSync(genPath, { recursive: true });
24-
// set the working directory to here so that the addon directory is
25-
// generated in ./test-assets/test-addon
26-
process.chdir(genPath);
27-
packageMock = getPackageManager as jest.Mock;
28-
});
29-
3021
afterAll(() => {
3122
rimraf.sync(testAssetsPath);
3223
});
3324

3425
beforeEach(() => {
3526
// eslint-disable-next-line @typescript-eslint/no-empty-function
3627
const Gen = addonGenerator([], '', [], [], () => {});
28+
3729
gen = new Gen(null, null);
3830
gen.props = {
3931
name: genName,
@@ -43,9 +35,20 @@ describe('addon generator', () => {
4335
});
4436

4537
it('schedules install using npm', () => {
38+
const defaultCwd = process.cwd();
39+
40+
rimraf.sync(testAssetsPath);
41+
fs.mkdirSync(genPath, { recursive: true });
42+
43+
// set the working directory to here so that the addon directory is
44+
// generated in ./test-assets/test-addon
45+
process.chdir(genPath);
46+
47+
packageMock = getPackageManager as jest.Mock;
4648
packageMock.mockReturnValue('npm');
4749

4850
gen.install();
51+
4952
expect(installMock.mock.calls.length).toEqual(1);
5053
expect(installMock.mock.calls[0]).toEqual([
5154
'npm',
@@ -54,12 +57,24 @@ describe('addon generator', () => {
5457
'save-dev': true,
5558
},
5659
]);
60+
61+
process.chdir(defaultCwd);
5762
});
5863

5964
it('schedules install using yarn', () => {
65+
const defaultCwd = process.cwd();
66+
67+
rimraf.sync(testAssetsPath);
68+
fs.mkdirSync(genPath, { recursive: true });
69+
// set the working directory to here so that the addon directory is
70+
// generated in ./test-assets/test-addon
71+
process.chdir(genPath);
72+
73+
packageMock = getPackageManager as jest.Mock;
6074
packageMock.mockReturnValue('yarn');
6175

6276
gen.install();
77+
6378
expect(installMock.mock.calls.length).toEqual(1);
6479
expect(installMock.mock.calls[0]).toEqual([
6580
'yarn',
@@ -68,11 +83,26 @@ describe('addon generator', () => {
6883
dev: true,
6984
},
7085
]);
86+
87+
process.chdir(defaultCwd);
7188
});
7289

7390
it('does not create new directory when current directory matches addon name', () => {
91+
const defaultCwd = process.cwd();
92+
93+
rimraf.sync(testAssetsPath);
94+
fs.mkdirSync(genPath, { recursive: true });
95+
96+
// set the working directory to here so that the addon directory is
97+
// generated in ./test-assets/test-addon
98+
process.chdir(genPath);
99+
100+
packageMock = getPackageManager as jest.Mock;
101+
74102
expect(fs.existsSync(genPath)).toBeTruthy();
103+
75104
gen.default();
105+
76106
expect(fs.existsSync(genPath)).toBeTruthy();
77107
expect(fs.existsSync(doubleGenPath)).toBeFalsy();
78108

@@ -81,14 +111,26 @@ describe('addon generator', () => {
81111
// generator above
82112
// this is switching the working directory as follows:
83113
// ./test-assets/test-addon -> ./test-assets
84-
process.chdir(testAssetsPath);
85114
rimraf.sync(genPath);
115+
116+
process.chdir(defaultCwd);
86117
});
87118

88119
it('creates a new directory for the generated addon', () => {
89-
expect(fs.existsSync(genPath)).toBeFalsy();
120+
const defaultCwd = process.cwd();
121+
122+
rimraf.sync(testAssetsPath);
123+
fs.mkdirSync(genPath, { recursive: true });
124+
125+
// set the working directory to here so that the addon directory is
126+
// generated in ./test-assets/test-addon
127+
process.chdir(genPath);
128+
90129
gen.default();
130+
91131
expect(fs.existsSync(genPath)).toBeTruthy();
92132
expect(fs.existsSync(doubleGenPath)).toBeFalsy();
133+
134+
process.chdir(defaultCwd);
93135
});
94136
});

packages/generators/__tests__/utils/languageSupport.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import language, { LangType, getBabelLoader, getTypescriptLoader } from '../../lib/utils/languageSupport';
2-
import { CustomGenerator } from '../../lib/types';
1+
import language, { LangType, getBabelLoader, getTypescriptLoader } from '../../src/utils/languageSupport';
2+
import { CustomGenerator } from '../../src/types';
33

44
describe('languageSupport', () => {
55
const getMockGenerator = (): CustomGenerator => {

packages/generators/__tests__/utils/plugins.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { replaceAt, generatePluginName } from '../../lib/utils/plugins';
1+
import { replaceAt, generatePluginName } from '../../src/utils/plugins';
22

33
describe('generate plugin name', () => {
44
it('should return webpack Standard Plugin Name for Name : extract-text-webpack-plugin', () => {

packages/generators/__tests__/utils/styleSupport.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import style, { StylingType } from '../../lib/utils/styleSupport';
2-
import { CustomGenerator } from '../../lib/types';
1+
import style, { StylingType } from '../../src/utils/styleSupport';
2+
import { CustomGenerator } from '../../src/types';
33

44
describe('styleSupport', () => {
55
const getMockGenerator = (): CustomGenerator => {

packages/generators/package.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
"plugin-template"
1818
],
1919
"dependencies": {
20-
"@webpack-cli/utils": "^1.2.1",
2120
"colorette": "^1.2.1",
2221
"log-symbols": "^4.0.0",
2322
"yeoman-environment": "^2.10.3",
24-
"yeoman-generator": "^4.12.0"
23+
"yeoman-generator": "^4.12.0",
24+
"execa": "^4.1.0",
25+
"findup-sync": "^4.0.0",
26+
"global-modules": "^2.0.0",
27+
"got": "^11.8.0",
28+
"jscodeshift": "^0.11.0",
29+
"p-each-series": "^2.1.0"
2530
},
2631
"peerDependencies": {
2732
"webpack": "4.x.x || 5.x.x",
@@ -33,7 +38,14 @@
3338
"@types/yeoman-test": "^2.0.5",
3439
"rimraf": "^3.0.2",
3540
"yeoman-assert": "^3.1.1",
36-
"yeoman-test": "^2.3.0"
41+
"yeoman-test": "^2.3.0",
42+
"@types/got": "^9.6.11",
43+
"@types/prettier": "^2.1.5"
44+
},
45+
"peerDependenciesMeta": {
46+
"prettier": {
47+
"optional": true
48+
}
3749
},
3850
"gitHead": "fb50f766851f500ca12867a2aa9de81fa6e368f9"
3951
}

packages/generators/src/addon-generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs';
22
import path from 'path';
33
import Generator from 'yeoman-generator';
4-
import { generatorCopy, generatorCopyTpl } from '@webpack-cli/utils';
4+
import { generatorCopy, generatorCopyTpl } from './utils/copy-utils';
55

66
import { utils } from 'webpack-cli';
77

packages/generators/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,16 @@ class GeneratorsCommand {
5454

5555
export default GeneratorsCommand;
5656
export { addonGenerator, initGenerator };
57+
58+
export * from './utils/ast-utils';
59+
export * from './utils/copy-utils';
60+
export * from './utils/modify-config-helper';
61+
export * from './utils/npm-exists';
62+
export * from './utils/npm-packages-exists';
63+
export * from './utils/recursive-parser';
64+
export * from './utils/resolve-packages';
65+
export * from './utils/run-prettier';
66+
export * from './utils/scaffold';
67+
export * from './utils/validate-identifier';
68+
export * from './utils/prop-types';
69+
export * from './utils/global-packages-path';

packages/generators/src/loader-generator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import { toKebabCase } from './utils/helpers';
1111
*/
1212
export function makeLoaderName(name: string): string {
1313
name = toKebabCase(name);
14+
1415
if (!/loader$/.test(name)) {
1516
name += '-loader';
1617
}
18+
1719
return name;
1820
}
1921

packages/utils/__tests__/__snapshots__/ast-utils.test.ts.snap renamed to packages/generators/src/utils/__tests__/__snapshots__/ast-utils.test.ts.snap

File renamed without changes.

0 commit comments

Comments
 (0)