Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): remove findup-sync from root move to utils package && format ts files #806

Merged
merged 13 commits into from
May 30, 2019
Merged
2 changes: 2 additions & 0 deletions bin/utils/convert-argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
anshumanv marked this conversation as resolved.
Show resolved Hide resolved

module.exports = function(...args) {
const argv = args[1] || args[0];
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/__tests__/is-local-path.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import * as path from "path";
import isLocalPath from "../is-local-path";
import { isLocalPath } from "../path-utils";
evenstensberg marked this conversation as resolved.
Show resolved Hide resolved

describe("is-local-path", () => {
it("returns true for paths beginning in the current directory", () => {
Expand Down
14 changes: 0 additions & 14 deletions packages/utils/find-root.ts

This file was deleted.

17 changes: 0 additions & 17 deletions packages/utils/is-local-path.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/utils/npm-packages-exists.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
43 changes: 43 additions & 0 deletions packages/utils/path-utils.ts
Original file line number Diff line number Diff line change
@@ -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 {
evenstensberg marked this conversation as resolved.
Show resolved Hide resolved
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`);
evenstensberg marked this conversation as resolved.
Show resolved Hide resolved
const projectRoot = path.dirname(rootFilePath);
return projectRoot;
}
2 changes: 1 addition & 1 deletion packages/utils/resolve-packages.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down