Skip to content

Commit

Permalink
refactor(projects): use enquirer replace prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Mar 11, 2024
1 parent 6f004ce commit 64b272c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 35 deletions.
3 changes: 1 addition & 2 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
},
"devDependencies": {
"@soybeanjs/changelog": "0.3.15",
"@types/prompts": "2.4.9",
"bumpp": "9.4.0",
"c12": "1.10.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"
}
}
20 changes: 13 additions & 7 deletions packages/scripts/src/commands/git-commit.ts
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -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',
Expand Down
31 changes: 21 additions & 10 deletions packages/scripts/src/commands/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,42 @@ 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),
}
]);

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]+$/;

Expand All @@ -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 "_".'));
}

Expand Down
111 changes: 95 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 64b272c

Please sign in to comment.