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__/ast-utils.test.ts renamed to packages/generators/src/utils/__tests__/ast-utils.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
getRequire,
1616
safeTraverse,
1717
safeTraverseAndGetType,
18-
} from '../src/ast-utils';
19-
import { Node } from '../src/types/NodePath';
18+
} from '../ast-utils';
19+
import { Node } from '../types/NodePath';
2020

2121
describe('utils', () => {
2222
describe('createProperty', () => {

packages/utils/__tests__/find-project-root/project/find-project-root.test.ts renamed to packages/generators/src/utils/__tests__/find-project-root/project/find-project-root.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// eslint-disable-next-line node/no-unpublished-import
2-
import { findProjectRoot } from '../../../src/path-utils';
2+
import { findProjectRoot } from '../../../path-utils';
33
import { join } from 'path';
44

55
beforeAll(() => {

packages/utils/__tests__/global-packages-path.test.ts renamed to packages/generators/src/utils/__tests__/global-packages-path.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
jest.setMock('webpack-cli/lib/utils/get-package-manager', jest.fn());
33

4-
import { getPathToGlobalPackages } from '../lib/global-packages-path';
4+
import { getPathToGlobalPackages } from '../global-packages-path';
55
import { utils } from 'webpack-cli';
66

77
const { getPackageManager } = utils;

packages/utils/__tests__/is-local-path.test.ts renamed to packages/generators/src/utils/__tests__/is-local-path.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
import path from 'path';
4-
import { isLocalPath } from '../src/path-utils';
4+
import { isLocalPath } from '../path-utils';
55

66
describe('is-local-path', () => {
77
it('returns true for paths beginning in the current directory', () => {

packages/utils/__tests__/npm-exists.test.ts renamed to packages/generators/src/utils/__tests__/npm-exists.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
import { npmExists } from '../src/npm-exists';
2+
import { npmExists } from '../npm-exists';
33

44
describe('npm-exists', () => {
55
it('should successfully existence of a published module', () => {

packages/utils/__tests__/npm-packages-exists.test.ts renamed to packages/generators/src/utils/__tests__/npm-packages-exists.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { npmPackagesExists } from '../src/npm-packages-exists';
2-
import { resolvePackages } from '../src/resolve-packages';
1+
import { npmPackagesExists } from '../npm-packages-exists';
2+
import { resolvePackages } from '../resolve-packages';
33

4-
jest.mock('../src/npm-exists');
5-
jest.mock('../src/resolve-packages');
4+
jest.mock('../npm-exists');
5+
jest.mock('../resolve-packages');
66

77
// TS is not aware that jest changes the type of resolvePackages
88
const mockResolvePackages = resolvePackages as jest.Mock<typeof resolvePackages>;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ exports[`recursive parser remove transforms correctly using "fixture-3" data 1`]
105105
symlinks: true
106106
},
107107
module: {
108-
noParse: function(content) {
108+
noParse: function (content) {
109109
return /jquery|lodash/.test(content);
110110
},
111111
rules: [
@@ -167,7 +167,7 @@ exports[`recursive parser remove transforms correctly using "fixture-3" data 2`]
167167
symlinks: true,
168168
},
169169
module: {
170-
noParse: function(content) {
170+
noParse: function (content) {
171171
return /jquery|lodash/.test(content);
172172
},
173173
rules: [
@@ -287,7 +287,7 @@ exports[`recursive parser remove transforms correctly using "fixture-3" data 4`]
287287
symlinks: true,
288288
},
289289
module: {
290-
noParse: function(content) {
290+
noParse: function (content) {
291291
return /jquery|lodash/.test(content);
292292
},
293293
rules: [
@@ -349,7 +349,7 @@ exports[`recursive parser remove transforms correctly using "fixture-3" data 5`]
349349
symlinks: true,
350350
},
351351
module: {
352-
noParse: function(content) {
352+
noParse: function (content) {
353353
return /jquery|lodash/.test(content);
354354
},
355355
rules: [{

packages/utils/__tests__/recursive-parser/__testfixtures__/fixture-3.input.js renamed to packages/generators/src/utils/__tests__/recursive-parser/__testfixtures__/fixture-3.input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = {
3535
symlinks: true,
3636
},
3737
module: {
38-
noParse: function(content) {
38+
noParse: function (content) {
3939
return /jquery|lodash/.test(content);
4040
},
4141
rules: [

packages/utils/__tests__/recursive-parser/recursive-parser.test.ts renamed to packages/generators/src/utils/__tests__/recursive-parser/recursive-parser.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
import { join } from 'path';
4-
import defineTest from '../defineTest';
4+
import defineTest from '../../defineTest';
55

66
describe('recursive parser', () => {
77
{

packages/utils/__tests__/run-prettier.test.ts renamed to packages/generators/src/utils/__tests__/run-prettier.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from 'fs';
44
import path from 'path';
55
//eslint-disable-next-line node/no-extraneous-import
66
import rimraf from 'rimraf';
7-
import { runPrettier } from '../src/run-prettier';
7+
import { runPrettier } from '../run-prettier';
88

99
const outputPath = path.join(__dirname, 'test-assets');
1010
const outputFile = path.join(outputPath, 'test.js');

packages/utils/__tests__/validate-identifier.test.ts renamed to packages/generators/src/utils/__tests__/validate-identifier.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import { isKeyword, isIdentifierChar, isIdentifierStart } from '../src/validate-identifier';
3+
import { isKeyword, isIdentifierChar, isIdentifierStart } from '../validate-identifier';
44

55
describe('validate-identifier', () => {
66
it('should return true for reserved keyword', () => {

packages/utils/__tests__/defineTest.ts renamed to packages/generators/src/utils/defineTest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33

4-
import { JSCodeshift, Node } from '../src/types/NodePath';
4+
import { JSCodeshift, Node } from './types/NodePath';
55

66
interface Module {
77
(jscodeshift: JSCodeshift, ast: Node, initOptions: string | boolean | object, action: string, transformName?: string): Node;
@@ -58,9 +58,9 @@ function runSingleTransform(
5858
let module: Module;
5959
// Assumes transform and test are on the same level
6060
if (action) {
61-
module = require(path.join(dirName, '../../src', 'recursive-parser.ts'));
61+
module = require(path.join(dirName, '../../', 'recursive-parser.ts'));
6262
} else {
63-
module = require(path.join(dirName, '../../src', transformName, `${transformName}.ts`));
63+
module = require(path.join(dirName, '../../src/', transformName, `${transformName}.ts`));
6464
}
6565
// Handle ES6 modules using default export for the transform
6666
const transform = module.default ? module.default : module;

packages/generators/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"extends": "../../tsconfig.json",
3+
"exclude": ["src/utils/__tests__"],
34
"compilerOptions": {
45
"outDir": "lib",
56
"rootDir": "src"
67
},
7-
"include": ["src"],
8-
"references": [{ "path": "../utils" }]
8+
"include": ["src"]
99
}

packages/init/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
"lib"
1313
],
1414
"dependencies": {
15-
"@webpack-cli/generators": "^1.2.1",
16-
"@webpack-cli/utils": "^1.2.1"
15+
"@webpack-cli/generators": "^1.2.1"
1716
},
1817
"peerDependencies": {
1918
"webpack": "4.x.x || 5.x.x",

0 commit comments

Comments
 (0)