If/Else 如果块没有用节点的 'fs' 模块激活
If/Else If block not activating with node's 'fs' module
当我尝试 运行 我的示例 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 :)
}
};
感谢您的帮助。
您收到的错误是 File already exists
for src/components/testing
失败的代码是:
fs.mkdirSync(`./src/components/${componentName}`);
修复
在写入之前检查 if not exists
即 if (!fs.existsSync('./src/components/${componentName}'))
应该在 之前 你尝试创建目录。
如果你的问题是这行代码:
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 :)
}
};
当我尝试 运行 我的示例 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 :)
}
};
感谢您的帮助。
您收到的错误是 File already exists
for src/components/testing
失败的代码是:
fs.mkdirSync(`./src/components/${componentName}`);
修复
在写入之前检查 if not exists
即 if (!fs.existsSync('./src/components/${componentName}'))
应该在 之前 你尝试创建目录。
如果你的问题是这行代码:
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 :)
}
};