From fe9c289cd54ac2f26d7c9690abc3d572e40feedf Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Wed, 27 Mar 2019 18:59:20 +0530 Subject: [PATCH] chore(cli): remove findup-sync from package dir and move to utils remove findup-sync from package dir and move to utils cli(utils): added path-util to be used after next release added path-util to be used after next release ISSUES CLOSED: #805 --- bin/utils/convert-argv.js | 2 + .../utils/__tests__/is-local-path.test.ts | 2 +- packages/utils/find-root.ts | 14 ------ packages/utils/is-local-path.ts | 17 -------- packages/utils/npm-packages-exists.ts | 2 +- packages/utils/path-utils.ts | 43 +++++++++++++++++++ packages/utils/resolve-packages.ts | 2 +- packages/utils/scaffold.ts | 2 +- 8 files changed, 49 insertions(+), 35 deletions(-) delete mode 100644 packages/utils/find-root.ts delete mode 100644 packages/utils/is-local-path.ts create mode 100644 packages/utils/path-utils.ts diff --git a/bin/utils/convert-argv.js b/bin/utils/convert-argv.js index d1c8d8d2f43..7f45aff70f3 100644 --- a/bin/utils/convert-argv.js +++ b/bin/utils/convert-argv.js @@ -7,6 +7,7 @@ const webpackConfigurationSchema = require("../config/webpackConfigurationSchema const validateSchema = require("webpack").validateSchema; const WebpackOptionsValidationError = require("webpack").WebpackOptionsValidationError; const findup = require("findup-sync"); +// const { webpackConfigPath } = require("@webpack-cli/utils/path-utils"); module.exports = function(...args) { const argv = args[1] || args[0]; @@ -72,6 +73,7 @@ module.exports = function(...args) { const defaultConfigFileNames = ["webpack.config", "webpackfile"].join("|"); const webpackConfigFileRegExp = `(${defaultConfigFileNames})(${extensions.join("|")})`; const pathToWebpackConfig = findup(webpackConfigFileRegExp); + // const pathToWebpackConfig = webpackConfigPath(extensions); if (pathToWebpackConfig) { const resolvedPath = path.resolve(pathToWebpackConfig); diff --git a/packages/utils/__tests__/is-local-path.test.ts b/packages/utils/__tests__/is-local-path.test.ts index ca34c0e7b66..654884b9e1a 100644 --- a/packages/utils/__tests__/is-local-path.test.ts +++ b/packages/utils/__tests__/is-local-path.test.ts @@ -1,7 +1,7 @@ "use strict"; import * as path from "path"; -import isLocalPath from "../is-local-path"; +import { isLocalPath } from "../path-utils"; describe("is-local-path", () => { it("returns true for paths beginning in the current directory", () => { diff --git a/packages/utils/find-root.ts b/packages/utils/find-root.ts deleted file mode 100644 index 0345da2db62..00000000000 --- a/packages/utils/find-root.ts +++ /dev/null @@ -1,14 +0,0 @@ -import * as findup from "findup-sync"; -import * as path from "path"; - -/** - * Returns the absolute path of the project directory - * Finds the package.json, by using findup-sync - * @returns {String} path of project directory - */ - -export function findProjectRoot(): string { - const rootFilePath = findup(`package.json`); - const projectRoot = path.dirname(rootFilePath); - return projectRoot; -} diff --git a/packages/utils/is-local-path.ts b/packages/utils/is-local-path.ts deleted file mode 100644 index 0efb8c6fc2c..00000000000 --- a/packages/utils/is-local-path.ts +++ /dev/null @@ -1,17 +0,0 @@ -import * as fs from "fs"; -import * as path from "path"; - -/** - * Attempts to detect whether the string is a local path regardless of its - * existence by checking its format. The point is to distinguish between - * paths and modules on the npm registry. This will fail for non-existent - * local Windows paths that begin with a drive letter, e.g. C:..\generator.js, - * but will succeed for any existing files and any absolute paths. - * - * @param {String} str - string to check - * @returns {Boolean} whether the string could be a path to a local file or directory - */ - -export default function(str: string): boolean { - return path.isAbsolute(str) || /^\./.test(str) || fs.existsSync(str); -} diff --git a/packages/utils/npm-packages-exists.ts b/packages/utils/npm-packages-exists.ts index a1987c3332d..2ad1d797510 100644 --- a/packages/utils/npm-packages-exists.ts +++ b/packages/utils/npm-packages-exists.ts @@ -1,7 +1,7 @@ import chalk from "chalk"; -import isLocalPath from "./is-local-path"; import npmExists from "./npm-exists"; +import { isLocalPath } from "./path-utils"; import { resolvePackages } from "./resolve-packages"; const WEBPACK_SCAFFOLD_PREFIX = "webpack-scaffold"; diff --git a/packages/utils/path-utils.ts b/packages/utils/path-utils.ts new file mode 100644 index 00000000000..468572a59b9 --- /dev/null +++ b/packages/utils/path-utils.ts @@ -0,0 +1,43 @@ +import * as findup from "findup-sync"; +import * as fs from "fs"; +import * as path from "path"; + +/** + * Attempts to detect whether the string is a local path regardless of its + * existence by checking its format. The point is to distinguish between + * paths and modules on the npm registry. This will fail for non-existent + * local Windows paths that begin with a drive letter, e.g. C:..\generator.js, + * but will succeed for any existing files and any absolute paths. + * + * @param {String} str - string to check + * @returns {Boolean} whether the string could be a path to a local file or directory + */ + +export function isLocalPath(str: string): boolean { + return path.isAbsolute(str) || /^\./.test(str) || fs.existsSync(str); +} + +/** + * Get absolute path of a webpack config in a project. + * + * @param {String[]} str - array of extensions to look for. + * @returns {String} Absolute path of the config. + */ + +export function webpackConfigPath(extensions: string[]): string { + const defaultConfigFileNames = ["webpack.config", "webpackfile"].join("|"); + const webpackConfigFileRegExp = `(${defaultConfigFileNames})(${extensions.join("|")})`; + return findup(webpackConfigFileRegExp); +} + +/** + * Find the root directory path of a project. + * + * @returns {String} Absolute path of the project root. + */ + +export function findProjectRoot(): string { + const rootFilePath = findup(`package.json`); + const projectRoot = path.dirname(rootFilePath); + return projectRoot; +} diff --git a/packages/utils/resolve-packages.ts b/packages/utils/resolve-packages.ts index b16823509a9..9cfbc21fa32 100644 --- a/packages/utils/resolve-packages.ts +++ b/packages/utils/resolve-packages.ts @@ -1,10 +1,10 @@ import chalk from "chalk"; import * as path from "path"; -import isLocalPath from "./is-local-path"; import modifyConfigHelper from "./modify-config-helper"; import { getPathToGlobalPackages } from "./package-manager"; import { spawnChild } from "./package-manager"; +import { isLocalPath } from "./path-utils"; interface IChildProcess { status: number; diff --git a/packages/utils/scaffold.ts b/packages/utils/scaffold.ts index a3add9ee769..637fdc5b83f 100644 --- a/packages/utils/scaffold.ts +++ b/packages/utils/scaffold.ts @@ -2,7 +2,7 @@ import chalk from "chalk"; import * as j from "jscodeshift"; import pEachSeries = require("p-each-series"); import * as path from "path"; -import { findProjectRoot } from "./find-root"; +import { findProjectRoot } from "./path-utils"; import { IError } from "../init/types"; import { IConfig, ITransformConfig } from "./modify-config-helper";