mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-10 01:29:30 +02:00
fix: create-tool.mjs to use i18n object
This commit is contained in:
94
scripts/cleanup-empty-directories.js
Normal file
94
scripts/cleanup-empty-directories.js
Normal file
@@ -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 };
|
@@ -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(
|
createToolFile(
|
||||||
`meta.ts`,
|
`meta.ts`,
|
||||||
`
|
`
|
||||||
@@ -145,13 +179,15 @@ import { defineTool } from '@tools/defineTool';
|
|||||||
import { lazy } from 'react';
|
import { lazy } from 'react';
|
||||||
|
|
||||||
export const tool = defineTool('${type}', {
|
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}',
|
path: '${toolName}',
|
||||||
icon: '',
|
icon: '',
|
||||||
description: '',
|
|
||||||
shortDescription: '',
|
|
||||||
keywords: ['${toolName.split('-').join("', '")}'],
|
keywords: ['${toolName.split('-').join("', '")}'],
|
||||||
longDescription: '',
|
|
||||||
component: lazy(() => import('./index'))
|
component: lazy(() => import('./index'))
|
||||||
});
|
});
|
||||||
`
|
`
|
||||||
|
Reference in New Issue
Block a user