Skip to content

Commit cf7f3e0

Browse files
authored
feat: update ui (#202)
* fix: remove double blank lines when not use eslintrc style * add ansi-colors * add style for questions * add style for logg util * change error color back to magenta * update console * some changes * some changes * change jsdoc type
1 parent 5db63e2 commit cf7f3e0

File tree

5 files changed

+100
-22
lines changed

5 files changed

+100
-22
lines changed

bin/create-config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77

88
import { ConfigGenerator } from "../lib/config-generator.js";
99
import { findPackageJson } from "../lib/utils/npm-utils.js";
10-
import { info } from "../lib/utils/logging.js";
10+
import * as log from "../lib/utils/logging.js";
1111
import process from "node:process";
1212
import fs from "node:fs/promises";
1313

1414
const pkg = JSON.parse(await fs.readFile(new URL("../package.json", import.meta.url), "utf8"));
1515

16-
info(`${pkg.name}: v${pkg.version}\n`);
16+
log.log(`${pkg.name}: v${pkg.version}\n`);
1717

1818
process.on("uncaughtException", error => {
1919
if (error instanceof Error && error.code === "ERR_USE_AFTER_CLOSE") {
20-
info("Operation canceled");
20+
log.error("Operation canceled");
2121
// eslint-disable-next-line n/no-process-exit -- exit gracefully on Ctrl+C
2222
process.exit(1);
2323
} else {

lib/config-generator.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,14 @@ export default defineConfig([\n${exportContent || " {}\n"}]);\n`; // defaults t
355355
async output() {
356356

357357
log.info("The config that you've selected requires the following dependencies:\n");
358-
log.info(this.result.devDependencies.join(", "));
358+
log.log(this.result.devDependencies.join(", "));
359359

360360

361361
const { executeInstallation, packageManager } = (await enquirer.prompt(installationQuestions));
362362
const configPath = path.join(this.cwd, this.result.configFilename);
363363

364364
if (executeInstallation === true) {
365-
log.info("☕️Installing...");
365+
log.log("☕️Installing...");
366366
installSyncSaveDev(this.result.devDependencies, packageManager, this.result.installFlags);
367367
await writeFile(configPath, this.result.configContent);
368368

@@ -374,12 +374,12 @@ export default defineConfig([\n${exportContent || " {}\n"}]);\n`; // defaults t
374374
if (result.error || result.status !== 0) {
375375
log.error("A config file was generated, but the config file itself may not follow your linting rules.");
376376
} else {
377-
log.info(`Successfully created ${configPath} file.`);
377+
log.success(`Successfully created ${configPath} file.`);
378378
}
379379
} else {
380380
await writeFile(configPath, this.result.configContent);
381381

382-
log.info(`Successfully created ${configPath} file.`);
382+
log.success(`Successfully created ${configPath} file.`);
383383
log.warn("You will need to install the dependencies yourself.");
384384
}
385385
}

lib/questions.js

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,44 @@
33
* @author 唯然<weiran.zsd@outlook.com>
44
*/
55

6-
export const langQuestions = [{
6+
/**
7+
* @typedef {Record<string, any>} PlainObject
8+
*/
9+
10+
// ------------------------------------------------------------------------------
11+
// Imports
12+
// ------------------------------------------------------------------------------
13+
14+
import colors from "ansi-colors";
15+
16+
// ------------------------------------------------------------------------------
17+
// Helpers
18+
// ------------------------------------------------------------------------------
19+
20+
/**
21+
* Set questions prompt style options in here.
22+
* @param {PlainObject[]} questionsPromptArray Array of questions prompt.
23+
* @returns {PlainObject[]} Questions prompt with style options.
24+
*/
25+
function setQuestionsPromptStyle(questionsPromptArray) {
26+
return questionsPromptArray.map(opts => ({
27+
...opts,
28+
symbols: {
29+
30+
// For option symbol in select and multiselect
31+
indicator: {
32+
on: colors.cyan(colors.symbols.radioOn),
33+
off: colors.gray(colors.symbols.radioOff)
34+
}
35+
}
36+
}));
37+
}
38+
39+
// ------------------------------------------------------------------------------
40+
// Exports
41+
// ------------------------------------------------------------------------------
42+
43+
export const langQuestions = setQuestionsPromptStyle([{
744
type: "multiselect",
845
name: "languages",
946
message: "What do you want to lint?",
@@ -25,9 +62,9 @@ export const langQuestions = [{
2562
{ message: "To check syntax only", name: "syntax" },
2663
{ message: "To check syntax and find problems", name: "problems" }
2764
]
28-
}];
65+
}]);
2966

30-
export const jsQuestions = [
67+
export const jsQuestions = setQuestionsPromptStyle([
3168
{
3269
type: "select",
3370
name: "moduleType",
@@ -82,9 +119,9 @@ export const jsQuestions = [
82119
return !this.state.answers.useTs;
83120
}
84121
}
85-
];
122+
]);
86123

87-
export const mdQuestions = [{
124+
export const mdQuestions = setQuestionsPromptStyle([{
88125
type: "select",
89126
name: "mdType",
90127
message: "What flavor of Markdown do you want to lint?",
@@ -93,9 +130,9 @@ export const mdQuestions = [{
93130
{ message: "CommonMark", name: "commonmark" },
94131
{ message: "GitHub Flavored Markdown", name: "gfm" }
95132
]
96-
}];
133+
}]);
97134

98-
export const installationQuestions = [
135+
export const installationQuestions = setQuestionsPromptStyle([
99136
{
100137
type: "toggle",
101138
name: "executeInstallation",
@@ -113,13 +150,13 @@ export const installationQuestions = [
113150
return this.state.answers.executeInstallation === false;
114151
}
115152
}
116-
];
153+
]);
117154

118-
export const addJitiQuestion = {
155+
export const addJitiQuestion = setQuestionsPromptStyle([{
119156
type: "toggle",
120157
name: "addJiti",
121158
message: "Would you like to add Jiti as a devDependency?",
122159
disabled: "No",
123160
enabled: "Yes",
124161
initial: 1
125-
};
162+
}]);

lib/utils/logging.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33
* @author Gyandeep Singh
44
*/
55

6+
// ------------------------------------------------------------------------------
7+
// Imports
8+
// ------------------------------------------------------------------------------
9+
10+
import colors from "ansi-colors";
11+
12+
// ------------------------------------------------------------------------------
13+
// Helpers
14+
// ------------------------------------------------------------------------------
15+
16+
/**
17+
* Used for joining and add bold style to an array of arguments.
18+
* @param {any[]} args Array of arguments.
19+
* @returns {string} Joined and bolded string.
20+
*/
21+
function boldArgs(args) {
22+
return colors.bold(args.join(" "));
23+
}
24+
25+
// ------------------------------------------------------------------------------
26+
// Exports
27+
// ------------------------------------------------------------------------------
628

729
/* eslint no-console: "off" -- Logging util */
830

@@ -11,24 +33,42 @@
1133
* @param {...any} args The elements to log.
1234
* @returns {void}
1335
*/
36+
export function log(...args) {
37+
console.log(boldArgs(args));
38+
}
39+
40+
/**
41+
* Cover for console.log with check symbol
42+
* @param {...any} args The elements to log.
43+
* @returns {void}
44+
*/
45+
export function success(...args) {
46+
console.log(colors.green(colors.symbols.check), boldArgs(args));
47+
}
48+
49+
/**
50+
* Cover for console.info with info symbol
51+
* @param {...any} args The elements to log.
52+
* @returns {void}
53+
*/
1454
export function info(...args) {
15-
console.log(...args);
55+
console.info(colors.blue(colors.symbols.info), boldArgs(args));
1656
}
1757

1858
/**
19-
* Cover for console.warn
59+
* Cover for console.warn with warn symbol
2060
* @param {...any} args The elements to log.
2161
* @returns {void}
2262
*/
2363
export function warn(...args) {
24-
console.warn(...args);
64+
console.warn(colors.yellow(colors.symbols.warning), boldArgs(args));
2565
}
2666

2767
/**
28-
* Cover for console.error
68+
* Cover for console.error with cross symbol
2969
* @param {...any} args The elements to log.
3070
* @returns {void}
3171
*/
3272
export function error(...args) {
33-
console.error(...args);
73+
console.error(colors.magenta(colors.symbols.cross), boldArgs(args));
3474
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"test:snapshots:update": "vitest -u run snapshots"
3737
},
3838
"dependencies": {
39+
"ansi-colors": "^4.1.3",
3940
"cross-spawn": "^7.0.2",
4041
"enquirer": "^2.3.5",
4142
"semver": "^7.7.1"

0 commit comments

Comments
 (0)