【发布时间】:2021-10-07 21:11:10
【问题描述】:
当我尝试运行我的示例 CLI 应用程序(使用 yargs 构建的用于解析 optstrings 的参数)时,它会在胖堆栈跟踪中出现此错误: https://streamable.com/v4sih6(视频录制这么大)
我不知道我做错了什么。这是我的 ts 代码:
import type { Arguments, CommandBuilder } from 'yargs';
import chalk from 'chalk';
import * as logos from '../logos';
import fs from 'fs';
type Options = {
componentName?: string;
};
export const command: string[] = ["component", "c", "comp"];
export const desc: string = 'Creates a component. Use: "vcli component title" - this makes a new title component (optional).';
export const builder: CommandBuilder<Options, Options> = (yargs) =>
yargs
.options({
componentName: { type: 'string' },
});
//TODO: Check if component exists
export const handler = (argv: Arguments<Options>): void => {
const { componentName } = argv;
if (componentName !== undefined) {
const timerMsg: string = `${logos.prefix} Component with name '${componentName}' has been successfully created in`
console.time(chalk.green(timerMsg));
fs.mkdirSync(`./src/components/${componentName}`);
fs.writeFileSync(`./src/components/${componentName}/index.tsx`, `import React from 'react';\n\nimport './${componentName}Styles.scss'`);
console.timeEnd(chalk.green(timerMsg));
} else if (fs.existsSync(`./src/components/${componentName}`)) {
console.log(chalk.red(`${logos.prefix} Component ${componentName} already exists!`));
} else {
// Write an inquirer.js prompt here to ask for the name of the component :)
}
};
感谢您的帮助。
【问题讨论】:
-
你的意思是它不会这样吗? if (componentName !== undefined)
-
您的视频中的错误表明您正在尝试创建一个已经存在的目录....
标签: javascript node.js typescript yargs