Fixed create-tool script

- It was creating all my pwd inside the project before and exiting with an error
- Now it work as intened
This commit is contained in:
C043
2025-05-22 22:47:27 +02:00
parent 7e4c18c7dc
commit 2a9d3c4fda

View File

@@ -1,7 +1,7 @@
import { readFile, writeFile } from 'fs/promises'; import { readFile, writeFile } from "fs/promises";
import fs from 'fs'; import fs from "fs";
import { dirname, join, sep } from 'path'; import { dirname, join, sep } from "path";
import { fileURLToPath } from 'url'; import { fileURLToPath } from "url";
const currentDirname = dirname(fileURLToPath(import.meta.url)); const currentDirname = dirname(fileURLToPath(import.meta.url));
@@ -10,14 +10,14 @@ const folder = process.argv[3];
const toolsDir = join( const toolsDir = join(
currentDirname, currentDirname,
'..', "..",
'src', "src",
'pages', "pages",
'tools', "tools",
folder ?? '' folder ?? ""
); );
if (!toolName) { if (!toolName) {
throw new Error('Please specify a toolname.'); throw new Error("Please specify a toolname.");
} }
function capitalizeFirstLetter(string) { function capitalizeFirstLetter(string) {
@@ -35,7 +35,7 @@ function createFolderStructure(basePath, foldersToCreateIndexCount) {
if (!fs.existsSync(currentPath)) { if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath, { recursive: true }); fs.mkdirSync(currentPath, { recursive: true });
} }
const indexPath = join(currentPath, 'index.ts'); const indexPath = join(currentPath, "index.ts");
if ( if (
!fs.existsSync(indexPath) && !fs.existsSync(indexPath) &&
index < folderArray.length - 1 && index < folderArray.length - 1 &&
@@ -54,15 +54,15 @@ function createFolderStructure(basePath, foldersToCreateIndexCount) {
} }
// Start the recursive folder creation // Start the recursive folder creation
recursiveCreate('.', 0); recursiveCreate(toolsDir, 0);
} }
const toolNameCamelCase = toolName.replace(/-./g, (x) => x[1].toUpperCase()); const toolNameCamelCase = toolName.replace(/-./g, x => x[1].toUpperCase());
const toolNameTitleCase = 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(toolName, folder.split(sep).length);
console.log(`Directory created: ${toolDir}`); console.log(`Directory created: ${toolDir}`);
const createToolFile = async (name, content) => { const createToolFile = async (name, content) => {
@@ -150,7 +150,7 @@ export const tool = defineTool('${type}', {
icon: '', icon: '',
description: '', description: '',
shortDescription: '', shortDescription: '',
keywords: ['${toolName.split('-').join("', '")}'], keywords: ['${toolName.split("-").join("', '")}'],
longDescription: '', longDescription: '',
component: lazy(() => import('./index')) component: lazy(() => import('./index'))
}); });
@@ -209,9 +209,9 @@ import { expect, describe, it } from 'vitest';
// ` // `
// ) // )
const toolsIndex = join(toolsDir, 'index.ts'); const toolsIndex = join(toolsDir, "index.ts");
const indexContent = await readFile(toolsIndex, { encoding: 'utf-8' }).then( const indexContent = await readFile(toolsIndex, { encoding: "utf-8" }).then(r =>
(r) => r.split('\n') r.split("\n")
); );
indexContent.splice( indexContent.splice(
@@ -221,5 +221,5 @@ indexContent.splice(
toolNameCamelCase toolNameCamelCase
)} } from './${toolName}/meta';` )} } from './${toolName}/meta';`
); );
writeFile(toolsIndex, indexContent.join('\n')); writeFile(toolsIndex, indexContent.join("\n"));
console.log(`Added import in: ${toolsIndex}`); console.log(`Added import in: ${toolsIndex}`);