Skip to content

Commit

Permalink
feat: init command add invalid options check (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
winchesHe committed Jun 14, 2024
1 parent 1beb52c commit 1573e48
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/actions/init-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import chalk from 'chalk';

import {downloadTemplate} from '@helpers/fetch';
import {fixPnpm} from '@helpers/fix';
import {checkInitOptions} from '@helpers/init';
import {getPackageManagerInfo} from '@helpers/utils';
import {selectClack, taskClack, textClack} from 'src/prompts/clack';
import {resolver} from 'src/scripts/path';
Expand All @@ -27,10 +28,10 @@ import {

export interface InitActionOptions {
template?: 'app' | 'pages' | 'vite';
package?: 'npm' | 'yarn' | 'pnpm';
package?: Agent;
}

const templatesMap: Record<Required<InitActionOptions>['template'], string> = {
export const templatesMap: Record<Required<InitActionOptions>['template'], string> = {
app: APP_NAME,
pages: PAGES_NAME,
vite: VITE_NAME
Expand All @@ -39,6 +40,9 @@ const templatesMap: Record<Required<InitActionOptions>['template'], string> = {
export async function initAction(_projectName: string, options: InitActionOptions) {
const {package: _package = 'npm', template: _template} = options;

/** ======================== Check invalid options ======================== */
checkInitOptions(_template, _package);

/** ======================== Welcome title ======================== */
p.intro(chalk.cyanBright('Create a new project'));

Expand Down
19 changes: 19 additions & 0 deletions src/helpers/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { SAFE_ANY } from './type';

import { type InitActionOptions, templatesMap } from 'src/actions/init-action';

import { AGENTS, type Agent } from './detect';
import { printMostMatchText } from './math-diff';

export function checkInitOptions(template: InitActionOptions['template'], agent: Agent) {
if (template) {
if (!Object.keys(templatesMap).includes(template)) {
printMostMatchText(Object.keys(templatesMap), template)
}
}
if (agent) {
if (!AGENTS.includes(agent)) {
printMostMatchText(AGENTS as SAFE_ANY, agent)
}
}
}
13 changes: 13 additions & 0 deletions src/helpers/math-diff.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Logger } from './logger';

function matchTextScore(text: string, pattern: string) {
let score = 0;
const textLength = text.length;
Expand Down Expand Up @@ -32,3 +34,14 @@ export function findMostMatchText(list: string[], pattern: string) {

return result !== '' ? result : null;
}

export function printMostMatchText(list: string[], pattern: string) {
const mathOption = findMostMatchText(list, pattern);

if (mathOption) {
Logger.error(`Unknown option '${pattern}', Did you mean '${mathOption}'?`);
} else {
Logger.error(`Unknown option '${pattern}'`);
}
process.exit(1);
}

0 comments on commit 1573e48

Please sign in to comment.