Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions bin/create-config.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, could we add a brief test to check whether SIGINT and SIGTERM work as expected?

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { ConfigGenerator } from "../lib/config-generator.js";
import { findPackageJson } from "../lib/utils/npm-utils.js";
import { info } from "../lib/utils/logging.js";
import { info, error } from "../lib/utils/logging.js";
import process from "node:process";
import fs from "node:fs/promises";

Expand All @@ -22,15 +22,42 @@ if (packageJsonPath === null) {
throw new Error("A package.json file is necessary to initialize ESLint. Run `npm init` to create a package.json file and try again.");
}

/**
* Used for handle exit & error and show exit message.
* @param {string} message Message to show.
* @returns {void}
*/
function gracefullyExit(message) {
if (message) {
error(message);
}
/* eslint-disable-next-line n/no-process-exit -- exit gracefully */
process.exit(1);
}

process.on("uncaughtException", err => {
if (err instanceof Error && err.toString() === "Error [ERR_USE_AFTER_CLOSE]: readline was closed") {
gracefullyExit();
} else {
gracefullyExit(err.message || err);
}
});

const argv = process.argv;
const sharedConfigIndex = process.argv.indexOf("--config");

if (sharedConfigIndex === -1) {
const generator = new ConfigGenerator({ cwd, packageJsonPath });

await generator.prompt();
await generator.calc();
await generator.output();
(async () => {
try {
await generator.prompt();
await generator.calc();
await generator.output();
} catch (err) {
gracefullyExit(err.message || err);
}
})();
} else {

// passed "--config"
Expand All @@ -39,6 +66,12 @@ if (sharedConfigIndex === -1) {
const answers = { config: { packageName, type } };
const generator = new ConfigGenerator({ cwd, packageJsonPath, answers });

await generator.calc();
await generator.output();
(async () => {
try {
await generator.calc();
await generator.output();
} catch (err) {
gracefullyExit(err.message || err);
}
})();
}
Loading