Commit 82d17ebc authored by David Sveningsson's avatar David Sveningsson
Browse files

feat: replace `inquirer` with `prompts`

Saves about 25 dependencies and roughly 50% of downloaded size (49MB to 26MB)
parent 3df8a0a8
This diff is collapsed.
......@@ -97,9 +97,9 @@
"deepmerge": "^4.2.2",
"espree": "^7.3.0",
"glob": "^7.1.6",
"inquirer": "^7.3.3",
"json-merge-patch": "^1.0.1",
"minimist": "^1.2.5"
"minimist": "^1.2.5",
"prompts": "^2.0.0"
},
"devDependencies": {
"@babel/core": "7.12.9",
......@@ -119,6 +119,7 @@
"@types/json-merge-patch": "0.0.5",
"@types/minimist": "1.2.1",
"@types/node": "11.15.38",
"@types/prompts": "2.0.9",
"autoprefixer": "10.0.2",
"babelify": "10.0.0",
"bootstrap-sass": "3.4.1",
......
......@@ -3,11 +3,9 @@ const fs = {
writeFile: jest.fn().mockImplementation((fn, data, cb) => cb()),
};
const inquirer = {
prompt: jest.fn(),
};
const prompts = jest.fn();
jest.mock("inquirer", () => inquirer);
jest.mock("prompts", () => prompts);
jest.mock("fs", () => fs);
import { CLI } from "./cli";
......@@ -27,7 +25,7 @@ it.each([
])("should generate configuration for %s", async (name, frameworks) => {
expect.assertions(2);
fs.existsSync.mockReturnValue(false);
inquirer.prompt.mockResolvedValue({
prompts.mockResolvedValue({
write: true,
frameworks,
});
......@@ -43,8 +41,8 @@ it.each([
it("should not overwrite configuration unless requested", async () => {
expect.assertions(1);
fs.existsSync.mockReturnValue(true);
inquirer.prompt.mockResolvedValue({
write: false,
prompts.mockResolvedValue({
overwrite: false,
});
try {
await cli.init(".");
......@@ -54,11 +52,21 @@ it("should not overwrite configuration unless requested", async () => {
expect(fs.writeFile).not.toHaveBeenCalled();
});
it("should overwrite configuration when requested", async () => {
expect.assertions(1);
fs.existsSync.mockReturnValue(true);
prompts.mockResolvedValue({
overwrite: true,
frameworks: [],
});
await cli.init(".");
expect(fs.writeFile).toHaveBeenCalled();
});
it("should always create configuration when config is missing", async () => {
expect.assertions(1);
fs.existsSync.mockReturnValue(false);
inquirer.prompt.mockResolvedValue({
write: undefined,
prompts.mockResolvedValue({
frameworks: [],
});
await cli.init(".");
......@@ -73,8 +81,7 @@ it("should propagate errors from fs.writeFile", async () => {
expect.assertions(1);
fs.existsSync.mockReturnValue(false);
fs.writeFile.mockImplementationOnce((fn, data, cb) => cb("mock error"));
inquirer.prompt.mockResolvedValue({
write: true,
prompts.mockResolvedValue({
frameworks: [],
});
await expect(cli.init(".")).rejects.toEqual("mock error");
......
import fs from "fs";
import deepmerge from "deepmerge";
import inquirer from "inquirer";
import prompts from "prompts";
import { ConfigData } from "../config";
export interface InitResult {
......@@ -58,33 +58,34 @@ export async function init(cwd: string): Promise<InitResult> {
elements: ["html5"],
extends: ["html-validate:recommended"],
};
const when = /* istanbul ignore next */ (answers: any): boolean => {
return !exists || answers.write;
};
const questions: inquirer.QuestionCollection = [
{
name: "write",
/* confirm overwrite */
if (exists) {
const result = await prompts({
name: "overwrite",
type: "confirm",
default: false,
when: exists,
message: "A .htmlvalidate.json file already exists, do you want to overwrite it?",
},
});
if (!result.overwrite) {
return Promise.reject();
}
}
const questions: prompts.PromptObject[] = [
{
name: "frameworks",
type: "checkbox",
choices: [Frameworks.angularjs, Frameworks.vuejs, Frameworks.markdown],
type: "multiselect",
choices: [
{ title: Frameworks.angularjs, value: Frameworks.angularjs },
{ title: Frameworks.vuejs, value: Frameworks.vuejs },
{ title: Frameworks.markdown, value: Frameworks.markdown },
],
message: "Support additional frameworks?",
when,
},
];
/* prompt user for questions */
const answers = await inquirer.prompt(questions);
/* dont overwrite configuration unless explicitly requested */
if (exists && !answers.write) {
return Promise.reject();
}
const answers = await prompts(questions);
/* write configuration to file */
let config = initialConfig;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment