【问题标题】:If/Else If block not activating with node's 'fs' moduleIf/Else If 块未使用节点的“fs”模块激活
【发布时间】: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


【解决方案1】:

如果你的问题是这行代码:

fs.mkdirSync(`./src/components/${componentName}`);

因为目录已经存在而给你一个错误,那么你可以简单地捕获那个特定的错误,测试它并在它是那个特定的错误时忽略它,因为这不是你需要中止的问题。

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));

        try {
            fs.mkdirSync(`./src/components/${componentName}`);
        } catch(e) {
            // only throw error if the error is not because the directory
            // already exists
            if (e.code !== 'EEXIST') {
                throw e;
            }
        }

        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 :) 
    }
};

【讨论】:

  • 谢谢伙计!帮助很大。赞成。
【解决方案2】:

你得到的错误是File already exists for src/components/testing

失败的代码是:

fs.mkdirSync(`./src/components/${componentName}`);

修复

在写之前检查if not exists,即if (!fs.existsSync('./src/components/${componentName}'))应该是你尝试制作目录之前。

【讨论】:

  • 或者只是捕获该错误并检查它是否因为它已经存在(然后忽略它)。
猜你喜欢
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多