diff --git a/lib/init/config-initializer.js b/lib/init/config-initializer.js index b4f436a2..f0548b0f 100644 --- a/lib/init/config-initializer.js +++ b/lib/init/config-initializer.js @@ -295,10 +295,9 @@ function installModules(modules, packageManager) { /** * Ask user to install modules. * @param {string[]} modules Array of modules to be installed. - * @param {boolean} packageJsonExists Indicates if package.json is existed. * @returns {Promise} Answer that indicates if user wants to install. */ -function askInstallModules(modules, packageJsonExists) { +function askInstallModules(modules) { // If no modules, do nothing. if (modules.length === 0) { @@ -316,7 +315,7 @@ function askInstallModules(modules, packageJsonExists) { disabled: "No", initial: 1, skip() { - return !(modules.length && packageJsonExists); + return !modules.length; }, result(input) { return this.skipped ? null : input; @@ -345,7 +344,11 @@ function askInstallModules(modules, packageJsonExists) { * @returns {Promise} The promise with the result of the prompt */ function promptUser() { + const packageJsonExists = npmUtils.checkPackageJson(); + if (!packageJsonExists) { + throw new Error("A package.json file is necessary to initialize ESLint. Run `npm init` to create a package.json file and try again."); + } return enquirer.prompt([ { type: "select", @@ -427,8 +430,7 @@ function promptUser() { { message: "XO: https://github.com/xojs/eslint-config-xo", name: "xo" } ], skip() { - this.state.answers.packageJsonExists = npmUtils.checkPackageJson(); - return !(this.state.answers.source === "guide" && this.state.answers.packageJsonExists); + return this.state.answers.source !== "guide"; }, result(input) { return this.skipped ? null : input; @@ -456,7 +458,7 @@ function promptUser() { disabled: "No", initial: 1, skip() { - return !(this.state.answers.source === "guide" && this.state.answers.packageJsonExists && hasESLintVersionConflict(this.state.answers)); + return !(this.state.answers.source === "guide" && hasESLintVersionConflict(this.state.answers)); }, result(input) { return this.skipped ? null : input; @@ -469,16 +471,12 @@ function promptUser() { const config = processAnswers(earlyAnswers); const modules = getModulesList(config); - return askInstallModules(modules, earlyAnswers.packageJsonExists) + return askInstallModules(modules) .then(() => writeFile(config, earlyAnswers.format)); } // early exit if you are using a style guide if (earlyAnswers.source === "guide") { - if (!earlyAnswers.packageJsonExists) { - info("A package.json is necessary to install plugins such as style guides. Run `npm init` to create a package.json file and try again."); - return void 0; - } if (earlyAnswers.installESLint === false && !semver.satisfies(earlyAnswers.localESLintVersion, earlyAnswers.requiredESLintVersionRange)) { info(`Note: it might not work since ESLint's version is mismatched with the ${earlyAnswers.styleguide} config.`); } @@ -498,7 +496,7 @@ function promptUser() { const modules = getModulesList(config); - return askInstallModules(modules, earlyAnswers.packageJsonExists) + return askInstallModules(modules) .then(() => writeFile(config, earlyAnswers.format)); } @@ -540,7 +538,7 @@ function promptUser() { const config = processAnswers(totalAnswers); const modules = getModulesList(config); - return askInstallModules(modules, earlyAnswers.packageJsonExists).then(() => writeFile(config, earlyAnswers.format)); + return askInstallModules(modules).then(() => writeFile(config, earlyAnswers.format)); }); }); } diff --git a/tests/init/config-initializer.js b/tests/init/config-initializer.js index db643602..9e867737 100644 --- a/tests/init/config-initializer.js +++ b/tests/init/config-initializer.js @@ -16,6 +16,7 @@ import sh from "shelljs"; import esmock from "esmock"; import { fileURLToPath } from "url"; import * as npmUtils from "../../lib/init/npm-utils.js"; +import { defineInMemoryFs } from "../_utils/index.js"; const originalDir = process.cwd(); const { assert } = chai; @@ -137,6 +138,21 @@ describe("configInitializer", () => { format: "JSON" }; }); + it("should throw error with message when no package.json", async () => { + const requireStubs = { + "../../lib/shared/logging.js": log, + "../../lib/init/npm-utils.js": { + ...await esmock("../../lib/init/npm-utils.js", { fs: defineInMemoryFs({}) }) + } + }; + + + init = await esmock("../../lib/init/config-initializer.js", requireStubs, { }); + + assert.throws(() => { + init.initializeConfig(); + }, "A package.json file is necessary to initialize ESLint. Run `npm init` to create a package.json file and try again."); + }); it("should create default config", () => { const config = init.processAnswers(answers);