refactor(projects): use enquirer replace prompts

This commit is contained in:
Soybean
2024-03-03 20:43:21 +08:00
parent 14aa856a0f
commit b546ff8625
4 changed files with 131 additions and 36 deletions

View File

@ -14,15 +14,14 @@
},
"devDependencies": {
"@soybeanjs/changelog": "0.3.15",
"@types/prompts": "2.4.9",
"bumpp": "9.3.0",
"c12": "1.9.0",
"cac": "6.7.14",
"consola": "3.2.3",
"enquirer": "2.4.1",
"execa": "8.0.1",
"kolorist": "1.8.0",
"npm-check-updates": "16.14.15",
"prompts": "2.4.2",
"rimraf": "5.0.5"
}
}

View File

@ -1,10 +1,16 @@
import path from 'node:path';
import { readFileSync } from 'node:fs';
import prompts from 'prompts';
import { prompt } from 'enquirer';
import { bgRed, green, red, yellow } from 'kolorist';
import { execCommand } from '../shared';
import type { CliOption } from '../types';
interface PromptObject {
types: string;
scopes: string;
description: string;
}
/**
* Git commit with Conventional Commits standard
*
@ -18,20 +24,20 @@ export async function gitCommit(
const typesChoices = gitCommitTypes.map(([value, msg]) => {
const nameWithSuffix = `${value}:`;
const title = `${nameWithSuffix.padEnd(12)}${msg}`;
const message = `${nameWithSuffix.padEnd(12)}${msg}`;
return {
value,
title
name: value,
message
};
});
const scopesChoices = gitCommitScopes.map(([value, msg]) => ({
value,
title: `${value.padEnd(30)} (${msg})`
name: value,
message: `${value.padEnd(30)} (${msg})`
}));
const result = await prompts([
const result = await prompt<PromptObject>([
{
name: 'types',
type: 'select',

View File

@ -2,32 +2,43 @@ import process from 'node:process';
import path from 'node:path';
import { writeFile } from 'node:fs/promises';
import { existsSync, mkdirSync } from 'node:fs';
import prompts from 'prompts';
import { prompt } from 'enquirer';
import { green, red } from 'kolorist';
interface PromptObject {
routeName: string;
addRouteParams: boolean;
routeParams: string;
}
/** generate route */
export async function generateRoute() {
const result = await prompts([
const result = await prompt<PromptObject>([
{
type: 'text',
name: 'routeName',
type: 'text',
message: 'please enter route name',
initial: 'demo-route_child'
},
{
type: 'confirm',
name: 'addRouteParams',
type: 'confirm',
message: 'add route params?',
initial: false
},
{
type: pre => (pre ? 'text' : null),
name: 'routeParams',
message: 'please enter route params',
initial: 'id'
}
]);
if (result.addRouteParams) {
const answers = await prompt<PromptObject>({
name: 'routeParams',
type: 'text',
message: 'please enter route params',
initial: 'id'
});
Object.assign(result, answers);
}
const PAGE_DIR_NAME_PATTERN = /^[\w-]+[0-9a-zA-Z]+$/;
if (!PAGE_DIR_NAME_PATTERN.test(result.routeName)) {
@ -42,7 +53,7 @@ For example:
const PARAM_REG = /^\w+$/g;
if (!PARAM_REG.test(result.routeParams)) {
if (result.routeParams && !PARAM_REG.test(result.routeParams)) {
throw new Error(red('route params is invalid, it only allow letters, numbers or "_".'));
}