fix: robust tool creation script

This commit is contained in:
ShahafShavit 2025-07-11 14:05:15 +03:00
commit 80e0ecab9b

View file

@ -24,37 +24,25 @@ function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1); return string.charAt(0).toUpperCase() + string.slice(1);
} }
/**
* Creates the folder structure for the new tool and adds index.ts files in parent directories.
* @param {string} basePath - The full path to the new tool's directory.
* @param {number} foldersToCreateIndexCount - The number of parent levels to create an index.ts file in.
*/
function createFolderStructure(basePath, foldersToCreateIndexCount) { function createFolderStructure(basePath, foldersToCreateIndexCount) {
const folderArray = basePath.split(sep); fs.mkdirSync(basePath, { recursive: true });
let parentDir = dirname(basePath);
function recursiveCreate(currentBase, index) { for (let i = 0; i < foldersToCreateIndexCount; i++) {
if (index >= folderArray.length) { if (!parentDir || parentDir === dirname(parentDir)) break;
return; const indexPath = join(parentDir, 'index.ts');
} if (!fs.existsSync(indexPath)) {
const currentPath = join(currentBase, folderArray[index]); const dirName = parentDir.split(sep).pop() || '';
if (!fs.existsSync(currentPath)) { const content = `export const ${dirName}Tools = [];\n`;
fs.mkdirSync(currentPath, { recursive: true }); fs.writeFileSync(indexPath, content);
}
const indexPath = join(currentPath, 'index.ts');
if (
!fs.existsSync(indexPath) &&
index < folderArray.length - 1 &&
index >= folderArray.length - 1 - foldersToCreateIndexCount
) {
fs.writeFileSync(
indexPath,
`export const ${
currentPath.split(sep)[currentPath.split(sep).length - 1]
}Tools = [];\n`
);
console.log(`File created: ${indexPath}`); console.log(`File created: ${indexPath}`);
} }
// Recursively create the next folder parentDir = dirname(parentDir);
recursiveCreate(currentPath, index + 1);
} }
// Start the recursive folder creation
recursiveCreate('.', 0);
} }
const toolNameCamelCase = toolName.replace(/-./g, (x) => x[1].toUpperCase()); const toolNameCamelCase = toolName.replace(/-./g, (x) => x[1].toUpperCase());
@ -62,7 +50,7 @@ const toolNameTitleCase =
toolName[0].toUpperCase() + toolName.slice(1).replace(/-/g, ' '); toolName[0].toUpperCase() + toolName.slice(1).replace(/-/g, ' ');
const toolDir = join(toolsDir, toolName); const toolDir = join(toolsDir, toolName);
const type = folder.split(sep)[folder.split(sep).length - 1]; const type = folder.split(sep)[folder.split(sep).length - 1];
await createFolderStructure(toolDir, folder.split(sep).length); await createFolderStructure(toolDir, folder ? folder.split(sep).length : 0);
console.log(`Directory created: ${toolDir}`); console.log(`Directory created: ${toolDir}`);
const createToolFile = async (name, content) => { const createToolFile = async (name, content) => {