nextdevkit
Version:
A Comprehensive CLI Toolkit for Next.js Development
49 lines (48 loc) • 2.06 kB
JavaScript
import chalk from 'chalk';
import fs from 'fs-extra';
import files from '../files.js';
import getAliasValue from '../utils/getAliasValue.js';
import getDestinationPaths from '../utils/getDestinationPaths.js';
import { handleError, handleSuccess } from '../utils/handleMessages.js';
import isTypeScriptProject from '../utils/isTypescriptProject.js';
import loadNextdevkitConfig from '../utils/loadNextdevkitConfig.js';
const removeFile = async (fileName) => {
if (!fileName) {
console.log(chalk.red('Please specify the name of the file to remove.'));
console.log(chalk.yellow('Usage: npx nextdevkit@latest remove <name>'));
return;
}
const currentDirectory = process.cwd();
const config = await loadNextdevkitConfig(currentDirectory);
if (!config) {
handleError('Failed to load configuration file.', {
message: 'Ensure the configuration file exists.',
exit: true
});
return;
}
const isTs = isTypeScriptProject(currentDirectory);
const fileConfig = files.find((f) => f.command.toLowerCase() === fileName.toLowerCase());
if (!fileConfig) {
console.log(chalk.red(`File name '${fileName}' not found.`));
console.log(`\nUse ${chalk.cyan('npx nextdevkit@latest list')} to list all available utility and hook files.\n`);
return;
}
try {
const aliasValue = await getAliasValue(currentDirectory, isTs);
const { destFilePath } = getDestinationPaths(currentDirectory, config.aliases, fileConfig, aliasValue, fileName, isTs);
if (!(await fs.pathExists(destFilePath))) {
console.log(chalk.yellow(`File '${fileName}' does not exist at the expected path.`));
return;
}
await fs.remove(destFilePath);
handleSuccess(`File '${fileName}' removed successfully.`);
}
catch (error) {
handleError(error, {
message: `Failed to remove file '${fileName}'. Please try again.`,
verbose: true
});
}
};
export default removeFile;