diff --git a/scripts/cleanup-empty-directories.js b/scripts/cleanup-empty-directories.js new file mode 100644 index 0000000..e907d5a --- /dev/null +++ b/scripts/cleanup-empty-directories.js @@ -0,0 +1,94 @@ +const fs = require('fs'); +const path = require('path'); + +/** + * Recursively delete all empty folders in a directory + * @param {string} dirPath - The directory path to process + * @param {boolean} deleteRoot - Whether to delete the root directory if it becomes empty + * @returns {boolean} - Returns true if the directory is empty after processing + */ +function deleteEmptyFolders(dirPath, deleteRoot = false) { + if (!fs.existsSync(dirPath)) { + console.log(`Directory does not exist: ${dirPath}`); + return false; + } + + if (!fs.statSync(dirPath).isDirectory()) { + console.log(`Path is not a directory: ${dirPath}`); + return false; + } + + let files; + try { + files = fs.readdirSync(dirPath); + } catch (err) { + console.error(`Error reading directory ${dirPath}:`, err.message); + return false; + } + + // Process each item in the directory + for (const file of files) { + const fullPath = path.join(dirPath, file); + + try { + const stat = fs.statSync(fullPath); + + if (stat.isDirectory()) { + // Recursively process subdirectories + const isEmpty = deleteEmptyFolders(fullPath, true); + + // If subdirectory is empty, remove it from the files array + if (isEmpty) { + const index = files.indexOf(file); + if (index > -1) { + files.splice(index, 1); + } + } + } + } catch (err) { + console.error(`Error processing ${fullPath}:`, err.message); + } + } + + // Check if directory is empty after processing + const isEmpty = files.length === 0; + + if (isEmpty && deleteRoot) { + try { + fs.rmdirSync(dirPath); + console.log(`Deleted empty directory: ${dirPath}`); + return true; + } catch (err) { + console.error(`Error deleting directory ${dirPath}:`, err.message); + return false; + } + } + + return isEmpty; +} + +/** + * Main function to start the cleanup process + * @param {string} targetPath - The root directory to clean up + */ +function cleanupEmptyFolders(targetPath) { + console.log(`Starting cleanup of empty folders in: ${targetPath}`); + + const absolutePath = path.resolve(targetPath); + const result = deleteEmptyFolders(absolutePath, false); + + if (result) { + console.log( + 'Cleanup completed. Root directory is empty but was not deleted.' + ); + } else { + console.log('Cleanup completed.'); + } +} + +// Usage example +const targetDirectory = process.argv[2] || './target-folder'; +cleanupEmptyFolders(targetDirectory); + +// Export for use as a module +module.exports = { deleteEmptyFolders, cleanupEmptyFolders }; diff --git a/scripts/create-tool.mjs b/scripts/create-tool.mjs index 3032f95..c8bc14b 100644 --- a/scripts/create-tool.mjs +++ b/scripts/create-tool.mjs @@ -138,6 +138,40 @@ export default function ${capitalizeFirstLetter(toolNameCamelCase)}({ } ` ); +const validNamespaces = [ + 'string', + 'number', + 'video', + 'list', + 'json', + 'time', + 'csv', + 'pdf', + 'audio', + 'xml', + 'translation', + 'image' +]; +const isValidI18nNamespace = (value) => { + return validNamespaces.includes(value); +}; + +const getI18nNamespaceFromToolCategory = (category) => { + // Map image-related categories to 'image' + if (['png', 'image-generic'].includes(category)) { + return 'image'; + } else if (['gif'].includes(category)) { + return 'video'; + } + // Use type guard to check if category is a valid I18nNamespaces + if (isValidI18nNamespace(category)) { + return category; + } + + return 'translation'; +}; + +const i18nNamespace = getI18nNamespaceFromToolCategory(type); createToolFile( `meta.ts`, ` @@ -145,13 +179,15 @@ import { defineTool } from '@tools/defineTool'; import { lazy } from 'react'; export const tool = defineTool('${type}', { - name: '${toolNameTitleCase}', + i18n: { + name: '${i18nNamespace}:${toolNameCamelCase}.title', + description: '${i18nNamespace}:${toolNameCamelCase}.description', + shortDescription: '${i18nNamespace}:${toolNameCamelCase}.shortDescription', + longDescription: '${i18nNamespace}:${toolNameCamelCase}.longDescription' + }, path: '${toolName}', icon: '', - description: '', - shortDescription: '', keywords: ['${toolName.split('-').join("', '")}'], - longDescription: '', component: lazy(() => import('./index')) }); `