Skip to content

Commit

Permalink
feat: export utils from core for other packages (#2011)
Browse files Browse the repository at this point in the history
Export utils from CLI core for other packages which were relying on absolute path of utils.
  • Loading branch information
anshumanv authored Nov 2, 2020
1 parent bd24aa1 commit 3004549
Show file tree
Hide file tree
Showing 23 changed files with 74 additions and 37 deletions.
6 changes: 4 additions & 2 deletions packages/generate-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { loaderGenerator } from '@webpack-cli/generators';
import yeoman from 'yeoman-environment';
import logger from 'webpack-cli/lib/utils/logger';
import { loaderGenerator } from '@webpack-cli/generators';
import { utils } from 'webpack-cli';

const { logger } = utils;

/**
* Runs a yeoman generator to create a new webpack loader project
Expand Down
4 changes: 3 additions & 1 deletion packages/generate-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { pluginGenerator } from '@webpack-cli/generators';
import yeoman from 'yeoman-environment';
import logger from 'webpack-cli/lib/utils/logger';
import { utils } from 'webpack-cli';

const { logger } = utils;

/**
* Runs a yeoman generator to create a new webpack plugin project
Expand Down
4 changes: 3 additions & 1 deletion packages/generators/__tests__/addon-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ jest.setMock('@webpack-cli/utils', {
import fs from 'fs';
import path from 'path';
import rimraf from 'rimraf';
import { getPackageManager } from 'webpack-cli/lib/utils/get-package-manager';
import { utils } from 'webpack-cli';
import addonGenerator from '../src/addon-generator';

const { getPackageManager } = utils;

// TODO: enable after jest release
describe.skip('addon generator', () => {
let gen, installMock, packageMock;
Expand Down
6 changes: 4 additions & 2 deletions packages/generators/src/addon-generator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logger from 'webpack-cli/lib/utils/logger';
import fs from 'fs';
import path from 'path';
import Generator from 'yeoman-generator';
import { generatorCopy, generatorCopyTpl } from '@webpack-cli/utils';
import { getPackageManager } from 'webpack-cli/lib/utils/get-package-manager';

import { utils } from 'webpack-cli';

const { logger, getPackageManager } = utils;

/**
* Creates a Yeoman Generator that generates a project conforming
Expand Down
5 changes: 3 additions & 2 deletions packages/generators/src/init-generator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { blue, green, bold } from 'colorette';
import logger from 'webpack-cli/lib/utils/logger';
import { utils } from 'webpack-cli';
import logSymbols from 'log-symbols';
import path from 'path';
import { getPackageManager } from 'webpack-cli/lib/utils/get-package-manager';
import { Confirm, Input, List } from '@webpack-cli/webpack-scaffold';

import {
Expand All @@ -17,6 +16,8 @@ import {
} from './utils';
import { CustomGenerator } from './types';

const { logger, getPackageManager } = utils;

/**
*
* Generator for initializing a webpack config
Expand Down
5 changes: 3 additions & 2 deletions packages/info/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import envinfo from 'envinfo';
import logger from 'webpack-cli/lib/utils/logger';
import { commands } from 'webpack-cli/lib/utils/cli-flags';
import WebpackCLI from 'webpack-cli';
import { utils } from 'webpack-cli';

const { logger, commands } = utils;

interface Information {
Binaries?: string[];
Expand Down
4 changes: 3 additions & 1 deletion packages/migrate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { green, red } from 'colorette';
import { Change, diffLines } from 'diff';
import fs from 'fs';
import inquirer from 'inquirer';
import logger from 'webpack-cli/lib/utils/logger';
import Listr from 'listr';
import pLazy = require('p-lazy');
import path from 'path';
Expand All @@ -11,6 +10,9 @@ import { runPrettier } from '@webpack-cli/utils';
import { transformations } from './migrate';
import { Node } from './types/NodePath';
import jscodeshift from 'jscodeshift';
import { utils } from 'webpack-cli';

const { logger } = utils;

declare let process: {
cwd: Function;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { red, underline } from 'colorette';

import logger from 'webpack-cli/lib/utils/logger';

import { findPluginsByName, isType, safeTraverse } from '@webpack-cli/utils';

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

import { utils } from 'webpack-cli';

const { logger } = utils;

/**
*
* Find deprecated plugins and remove them from the `plugins` array, if possible.
Expand Down
4 changes: 3 additions & 1 deletion packages/serve/src/parseArgs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logger from 'webpack-cli/lib/utils/logger';
import { utils } from 'webpack-cli';

const { logger } = utils;

type WebpackCLIType = {
getCoreFlags: Function;
Expand Down
9 changes: 5 additions & 4 deletions packages/utils/__tests__/global-packages-path.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';
jest.setMock('webpack-cli/lib/utils/get-package-manager', {
getPackageManager: jest.fn(),
});
jest.setMock('webpack-cli/lib/utils/get-package-manager', jest.fn());

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

const { getPackageManager } = utils;

jest.mock('execa');
jest.mock('cross-spawn');
const globalModulesNpmValue = 'test-npm';
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/src/global-packages-path.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import spawn from 'cross-spawn';
import path from 'path';
import { getPackageManager } from 'webpack-cli/lib/utils/get-package-manager';
import { utils } from 'webpack-cli';

const { getPackageManager } = utils;

/**
*
Expand Down
5 changes: 3 additions & 2 deletions packages/utils/src/modify-config-helper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { green } from 'colorette';
import fs from 'fs';
import logger from 'webpack-cli/lib/utils/logger';
import path from 'path';
import yeoman from 'yeoman-environment';
import Generator from 'yeoman-generator';
import { runTransform } from './scaffold';
import { getPackageManager } from 'webpack-cli/lib/utils/get-package-manager';
import { utils } from 'webpack-cli';

const { logger, getPackageManager } = utils;

export interface Config extends Object {
item?: {
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/src/recursive-parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { parseTopScope, findRootNodesByName, addProperty, removeProperty, parseMerge, safeTraverse } from './ast-utils';
import { JSCodeshift, Node, valueType } from './types/NodePath';
import { utils } from 'webpack-cli';

import logger from 'webpack-cli/lib/utils/logger';
const { logger } = utils;

export function recursiveTransform(j: JSCodeshift, ast: Node, key: string, value: valueType, action: string): boolean | Node {
if (key === 'topScope') {
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/src/resolve-packages.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { bold } from 'colorette';
import logger from 'webpack-cli/lib/utils/logger';
import path from 'path';
import { modifyHelperUtil } from './modify-config-helper';
import { getPathToGlobalPackages } from './global-packages-path';
import { spawnChild } from './spawn-child';
import { isLocalPath } from './path-utils';
import { ExecaSyncReturnValue } from 'execa';
import { utils } from 'webpack-cli';

const { logger } = utils;

interface ChildProcess {
status: number;
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/src/run-prettier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fs from 'fs';
import logger from 'webpack-cli/lib/utils/logger';
import { utils } from 'webpack-cli';

const { logger } = utils;

/**
*
Expand Down
5 changes: 3 additions & 2 deletions packages/utils/src/scaffold.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { green } from 'colorette';
import j from 'jscodeshift';
import logger from 'webpack-cli/lib/utils/logger';
import pEachSeries = require('p-each-series');
import path from 'path';
import { getPackageManager } from 'webpack-cli/lib/utils/get-package-manager';
import { findProjectRoot } from './path-utils';
import { Error } from './types';
import { Config, TransformConfig } from './types';
import { PROP_TYPES } from './prop-types';
import { recursiveTransform } from './recursive-parser';
import { runPrettier } from './run-prettier';
import { Node } from './types/NodePath';
import { utils } from 'webpack-cli';

const { logger, getPackageManager } = utils;

/**
*
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/src/spawn-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import path from 'path';
import fs from 'fs';
import { ExecaSyncReturnValue, sync } from 'execa';
import { getPathToGlobalPackages } from './global-packages-path';
import { getPackageManager } from 'webpack-cli/lib/utils/get-package-manager';
import { utils } from 'webpack-cli';

const { getPackageManager } = utils;

/**
*
Expand Down
13 changes: 13 additions & 0 deletions packages/webpack-cli/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const WebpackCLI = require('./webpack-cli');
const { commands } = require('./utils/cli-flags');
const logger = require('./utils/logger');
const getPackageManager = require('./utils/get-package-manager');

module.exports = WebpackCLI;

// export additional utils used by other packages
module.exports.utils = {
logger,
commands,
getPackageManager,
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const syncMock = jest.fn(() => {
jest.setMock('execa', {
sync: syncMock,
});
const { getPackageManager } = require('../get-package-manager');
const getPackageManager = require('../get-package-manager');

jest.mock('cross-spawn');
const globalModulesNpmValue = 'test-npm';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ jest.setMock('../package-exists', {
packageExists: jest.fn(),
});

jest.setMock('../get-package-manager', {
getPackageManager: jest.fn(),
});
jest.setMock('../get-package-manager', jest.fn());

const { getPackageManager } = require('../get-package-manager');
const getPackageManager = require('../get-package-manager');
const { packageExists } = require('../package-exists');
const { promptInstallation } = require('../prompt-installation');
const { runCommand } = require('../run-command');
Expand Down
4 changes: 1 addition & 3 deletions packages/webpack-cli/lib/utils/get-package-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,4 @@ function getPackageManager() {
return 'npm';
}

module.exports = {
getPackageManager,
};
module.exports = getPackageManager;
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/utils/prompt-installation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { prompt } = require('enquirer');
const { green } = require('colorette');
const { runCommand } = require('./run-command');
const { getPackageManager } = require('./get-package-manager');
const getPackageManager = require('./get-package-manager');
const { packageExists } = require('./package-exists');

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"bin": {
"webpack-cli": "./bin/cli.js"
},
"main": "./lib/webpack-cli.js",
"main": "./lib/index.js",
"engines": {
"node": ">=10.13.0"
},
Expand Down

0 comments on commit 3004549

Please sign in to comment.