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

fix: throw error and exit when package.json not found #27

Merged
merged 5 commits into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 11 additions & 13 deletions lib/init/config-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>} Answer that indicates if user wants to install.
*/
function askInstallModules(modules, packageJsonExists) {
function askInstallModules(modules) {

// If no modules, do nothing.
if (modules.length === 0) {
Expand All @@ -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;
Expand Down Expand Up @@ -345,7 +344,11 @@ function askInstallModules(modules, packageJsonExists) {
* @returns {Promise<void>} 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",
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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.`);
}
Expand All @@ -498,7 +496,7 @@ function promptUser() {

const modules = getModulesList(config);

return askInstallModules(modules, earlyAnswers.packageJsonExists)
return askInstallModules(modules)
.then(() => writeFile(config, earlyAnswers.format));

}
Expand Down Expand Up @@ -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));
});
});
}
Expand Down
16 changes: 16 additions & 0 deletions tests/init/config-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}, "Cannot initialize ESLint outside a node project");
harish-sethuraman marked this conversation as resolved.
Show resolved Hide resolved
});

it("should create default config", () => {
const config = init.processAnswers(answers);
Expand Down