Skip to content

Commit

Permalink
refactor: union utils and generators (#2343)
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv authored Jan 12, 2021
1 parent 9100137 commit debb857
Show file tree
Hide file tree
Showing 57 changed files with 130 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
plugins: ['node'],
settings: {
node: {
allowModules: ['@webpack-cli/generators', '@webpack-cli/utils'],
allowModules: ['@webpack-cli/generators'],
},
},
env: {
Expand Down
64 changes: 53 additions & 11 deletions packages/generators/__tests__/addon-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@ describe('addon generator', () => {
// we call this unwanted path doubleGenPath
const doubleGenPath = path.join(genPath, genName);

beforeAll(() => {
rimraf.sync(testAssetsPath);
fs.mkdirSync(genPath, { recursive: true });
// set the working directory to here so that the addon directory is
// generated in ./test-assets/test-addon
process.chdir(genPath);
packageMock = getPackageManager as jest.Mock;
});

afterAll(() => {
rimraf.sync(testAssetsPath);
});

beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const Gen = addonGenerator([], '', [], [], () => {});

gen = new Gen(null, null);
gen.props = {
name: genName,
Expand All @@ -43,9 +35,20 @@ describe('addon generator', () => {
});

it('schedules install using npm', () => {
const defaultCwd = process.cwd();

rimraf.sync(testAssetsPath);
fs.mkdirSync(genPath, { recursive: true });

// set the working directory to here so that the addon directory is
// generated in ./test-assets/test-addon
process.chdir(genPath);

packageMock = getPackageManager as jest.Mock;
packageMock.mockReturnValue('npm');

gen.install();

expect(installMock.mock.calls.length).toEqual(1);
expect(installMock.mock.calls[0]).toEqual([
'npm',
Expand All @@ -54,12 +57,24 @@ describe('addon generator', () => {
'save-dev': true,
},
]);

process.chdir(defaultCwd);
});

it('schedules install using yarn', () => {
const defaultCwd = process.cwd();

rimraf.sync(testAssetsPath);
fs.mkdirSync(genPath, { recursive: true });
// set the working directory to here so that the addon directory is
// generated in ./test-assets/test-addon
process.chdir(genPath);

packageMock = getPackageManager as jest.Mock;
packageMock.mockReturnValue('yarn');

gen.install();

expect(installMock.mock.calls.length).toEqual(1);
expect(installMock.mock.calls[0]).toEqual([
'yarn',
Expand All @@ -68,11 +83,26 @@ describe('addon generator', () => {
dev: true,
},
]);

process.chdir(defaultCwd);
});

it('does not create new directory when current directory matches addon name', () => {
const defaultCwd = process.cwd();

rimraf.sync(testAssetsPath);
fs.mkdirSync(genPath, { recursive: true });

// set the working directory to here so that the addon directory is
// generated in ./test-assets/test-addon
process.chdir(genPath);

packageMock = getPackageManager as jest.Mock;

expect(fs.existsSync(genPath)).toBeTruthy();

gen.default();

expect(fs.existsSync(genPath)).toBeTruthy();
expect(fs.existsSync(doubleGenPath)).toBeFalsy();

Expand All @@ -81,14 +111,26 @@ describe('addon generator', () => {
// generator above
// this is switching the working directory as follows:
// ./test-assets/test-addon -> ./test-assets
process.chdir(testAssetsPath);
rimraf.sync(genPath);

process.chdir(defaultCwd);
});

it('creates a new directory for the generated addon', () => {
expect(fs.existsSync(genPath)).toBeFalsy();
const defaultCwd = process.cwd();

rimraf.sync(testAssetsPath);
fs.mkdirSync(genPath, { recursive: true });

// set the working directory to here so that the addon directory is
// generated in ./test-assets/test-addon
process.chdir(genPath);

gen.default();

expect(fs.existsSync(genPath)).toBeTruthy();
expect(fs.existsSync(doubleGenPath)).toBeFalsy();

process.chdir(defaultCwd);
});
});
4 changes: 2 additions & 2 deletions packages/generators/__tests__/utils/languageSupport.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import language, { LangType, getBabelLoader, getTypescriptLoader } from '../../lib/utils/languageSupport';
import { CustomGenerator } from '../../lib/types';
import language, { LangType, getBabelLoader, getTypescriptLoader } from '../../src/utils/languageSupport';
import { CustomGenerator } from '../../src/types';

describe('languageSupport', () => {
const getMockGenerator = (): CustomGenerator => {
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/__tests__/utils/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { replaceAt, generatePluginName } from '../../lib/utils/plugins';
import { replaceAt, generatePluginName } from '../../src/utils/plugins';

describe('generate plugin name', () => {
it('should return webpack Standard Plugin Name for Name : extract-text-webpack-plugin', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/generators/__tests__/utils/styleSupport.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import style, { StylingType } from '../../lib/utils/styleSupport';
import { CustomGenerator } from '../../lib/types';
import style, { StylingType } from '../../src/utils/styleSupport';
import { CustomGenerator } from '../../src/types';

describe('styleSupport', () => {
const getMockGenerator = (): CustomGenerator => {
Expand Down
18 changes: 15 additions & 3 deletions packages/generators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
"plugin-template"
],
"dependencies": {
"@webpack-cli/utils": "^1.2.1",
"colorette": "^1.2.1",
"log-symbols": "^4.0.0",
"yeoman-environment": "^2.10.3",
"yeoman-generator": "^4.12.0"
"yeoman-generator": "^4.12.0",
"execa": "^4.1.0",
"findup-sync": "^4.0.0",
"global-modules": "^2.0.0",
"got": "^11.8.0",
"jscodeshift": "^0.11.0",
"p-each-series": "^2.1.0"
},
"peerDependencies": {
"webpack": "4.x.x || 5.x.x",
Expand All @@ -33,7 +38,14 @@
"@types/yeoman-test": "^2.0.5",
"rimraf": "^3.0.2",
"yeoman-assert": "^3.1.1",
"yeoman-test": "^2.3.0"
"yeoman-test": "^2.3.0",
"@types/got": "^9.6.11",
"@types/prettier": "^2.1.5"
},
"peerDependenciesMeta": {
"prettier": {
"optional": true
}
},
"gitHead": "fb50f766851f500ca12867a2aa9de81fa6e368f9"
}
2 changes: 1 addition & 1 deletion packages/generators/src/addon-generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import Generator from 'yeoman-generator';
import { generatorCopy, generatorCopyTpl } from '@webpack-cli/utils';
import { generatorCopy, generatorCopyTpl } from './utils/copy-utils';

import { utils } from 'webpack-cli';

Expand Down
13 changes: 13 additions & 0 deletions packages/generators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ class GeneratorsCommand {

export default GeneratorsCommand;
export { addonGenerator, initGenerator };

export * from './utils/ast-utils';
export * from './utils/copy-utils';
export * from './utils/modify-config-helper';
export * from './utils/npm-exists';
export * from './utils/npm-packages-exists';
export * from './utils/recursive-parser';
export * from './utils/resolve-packages';
export * from './utils/run-prettier';
export * from './utils/scaffold';
export * from './utils/validate-identifier';
export * from './utils/prop-types';
export * from './utils/global-packages-path';
2 changes: 2 additions & 0 deletions packages/generators/src/loader-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { toKebabCase } from './utils/helpers';
*/
export function makeLoaderName(name: string): string {
name = toKebabCase(name);

if (!/loader$/.test(name)) {
name += '-loader';
}

return name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
getRequire,
safeTraverse,
safeTraverseAndGetType,
} from '../src/ast-utils';
import { Node } from '../src/types/NodePath';
} from '../ast-utils';
import { Node } from '../types/NodePath';

describe('utils', () => {
describe('createProperty', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line node/no-unpublished-import
import { findProjectRoot } from '../../../src/path-utils';
import { findProjectRoot } from '../../../path-utils';
import { join } from 'path';

beforeAll(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
jest.setMock('webpack-cli/lib/utils/get-package-manager', jest.fn());

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

const { getPackageManager } = utils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import path from 'path';
import { isLocalPath } from '../src/path-utils';
import { isLocalPath } from '../path-utils';

describe('is-local-path', () => {
it('returns true for paths beginning in the current directory', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
import { npmExists } from '../src/npm-exists';
import { npmExists } from '../npm-exists';

describe('npm-exists', () => {
it('should successfully existence of a published module', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { npmPackagesExists } from '../src/npm-packages-exists';
import { resolvePackages } from '../src/resolve-packages';
import { npmPackagesExists } from '../npm-packages-exists';
import { resolvePackages } from '../resolve-packages';

jest.mock('../src/npm-exists');
jest.mock('../src/resolve-packages');
jest.mock('../npm-exists');
jest.mock('../resolve-packages');

// TS is not aware that jest changes the type of resolvePackages
const mockResolvePackages = resolvePackages as jest.Mock<typeof resolvePackages>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ exports[`recursive parser remove transforms correctly using "fixture-3" data 1`]
symlinks: true
},
module: {
noParse: function(content) {
noParse: function (content) {
return /jquery|lodash/.test(content);
},
rules: [
Expand Down Expand Up @@ -167,7 +167,7 @@ exports[`recursive parser remove transforms correctly using "fixture-3" data 2`]
symlinks: true,
},
module: {
noParse: function(content) {
noParse: function (content) {
return /jquery|lodash/.test(content);
},
rules: [
Expand Down Expand Up @@ -287,7 +287,7 @@ exports[`recursive parser remove transforms correctly using "fixture-3" data 4`]
symlinks: true,
},
module: {
noParse: function(content) {
noParse: function (content) {
return /jquery|lodash/.test(content);
},
rules: [
Expand Down Expand Up @@ -349,7 +349,7 @@ exports[`recursive parser remove transforms correctly using "fixture-3" data 5`]
symlinks: true,
},
module: {
noParse: function(content) {
noParse: function (content) {
return /jquery|lodash/.test(content);
},
rules: [{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
symlinks: true,
},
module: {
noParse: function(content) {
noParse: function (content) {
return /jquery|lodash/.test(content);
},
rules: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { join } from 'path';
import defineTest from '../defineTest';
import defineTest from '../../defineTest';

describe('recursive parser', () => {
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs';
import path from 'path';
//eslint-disable-next-line node/no-extraneous-import
import rimraf from 'rimraf';
import { runPrettier } from '../src/run-prettier';
import { runPrettier } from '../run-prettier';

const outputPath = path.join(__dirname, 'test-assets');
const outputFile = path.join(outputPath, 'test.js');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

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

describe('validate-identifier', () => {
it('should return true for reserved keyword', () => {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';

import { JSCodeshift, Node } from '../src/types/NodePath';
import { JSCodeshift, Node } from './types/NodePath';

interface Module {
(jscodeshift: JSCodeshift, ast: Node, initOptions: string | boolean | object, action: string, transformName?: string): Node;
Expand Down Expand Up @@ -58,9 +58,9 @@ function runSingleTransform(
let module: Module;
// Assumes transform and test are on the same level
if (action) {
module = require(path.join(dirName, '../../src', 'recursive-parser.ts'));
module = require(path.join(dirName, '../../', 'recursive-parser.ts'));
} else {
module = require(path.join(dirName, '../../src', transformName, `${transformName}.ts`));
module = require(path.join(dirName, '../../src/', transformName, `${transformName}.ts`));
}
// Handle ES6 modules using default export for the transform
const transform = module.default ? module.default : module;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions packages/generators/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"extends": "../../tsconfig.json",
"exclude": ["src/utils/__tests__"],
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"include": ["src"],
"references": [{ "path": "../utils" }]
"include": ["src"]
}
3 changes: 1 addition & 2 deletions packages/init/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"lib"
],
"dependencies": {
"@webpack-cli/generators": "^1.2.1",
"@webpack-cli/utils": "^1.2.1"
"@webpack-cli/generators": "^1.2.1"
},
"peerDependencies": {
"webpack": "4.x.x || 5.x.x",
Expand Down
Loading

0 comments on commit debb857

Please sign in to comment.