omni-tools/scripts/create-tool.mjs

158 lines
3.9 KiB
JavaScript
Raw Normal View History

2025-02-23 14:11:21 +00:00
import { readFile, writeFile } from 'fs/promises';
import fs from 'fs';
import { dirname, join, sep } from 'path';
import { fileURLToPath } from 'url';
const currentDirname = dirname(fileURLToPath(import.meta.url));
const toolName = process.argv[2];
const folder = process.argv[3];
const toolsDir = join(
currentDirname,
'..',
'src',
'pages',
'tools',
folder ?? ''
);
2024-06-23 19:57:58 +01:00
if (!toolName) {
2025-02-23 14:11:21 +00:00
throw new Error('Please specify a toolname.');
2024-06-23 19:57:58 +01:00
}
function capitalizeFirstLetter(string) {
2025-02-23 14:11:21 +00:00
return string.charAt(0).toUpperCase() + string.slice(1);
2024-06-23 19:57:58 +01:00
}
2024-06-23 20:37:17 +01:00
function createFolderStructure(basePath, foldersToCreateIndexCount) {
2025-02-23 14:11:21 +00:00
const folderArray = basePath.split(sep);
2024-06-23 20:37:17 +01:00
function recursiveCreate(currentBase, index) {
if (index >= folderArray.length) {
2025-02-23 14:11:21 +00:00
return;
2024-06-23 20:37:17 +01:00
}
2025-02-23 14:11:21 +00:00
const currentPath = join(currentBase, folderArray[index]);
2024-06-23 20:37:17 +01:00
if (!fs.existsSync(currentPath)) {
2025-02-23 14:11:21 +00:00
fs.mkdirSync(currentPath, { recursive: true });
2024-06-23 20:37:17 +01:00
}
2025-02-23 14:11:21 +00:00
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}`);
2024-06-23 20:37:17 +01:00
}
// Recursively create the next folder
2025-02-23 14:11:21 +00:00
recursiveCreate(currentPath, index + 1);
2024-06-23 20:37:17 +01:00
}
// Start the recursive folder creation
2025-02-23 14:11:21 +00:00
recursiveCreate('.', 0);
2024-06-23 20:37:17 +01:00
}
2025-02-23 14:11:21 +00:00
const toolNameCamelCase = toolName.replace(/-./g, (x) => x[1].toUpperCase());
2024-06-23 19:57:58 +01:00
const toolNameTitleCase =
2025-02-23 14:11:21 +00:00
toolName[0].toUpperCase() + toolName.slice(1).replace(/-/g, ' ');
const toolDir = join(toolsDir, toolName);
const type = folder.split(sep)[folder.split(sep).length - 1];
await createFolderStructure(toolDir, folder.split(sep).length);
console.log(`Directory created: ${toolDir}`);
2024-06-23 19:57:58 +01:00
const createToolFile = async (name, content) => {
2025-02-23 14:11:21 +00:00
const filePath = join(toolDir, name);
await writeFile(filePath, content.trim());
console.log(`File created: ${filePath}`);
};
2024-06-23 19:57:58 +01:00
createToolFile(
`index.tsx`,
`
import { Box } from '@mui/material';
import React from 'react';
import * as Yup from 'yup';
const initialValues = {};
const validationSchema = Yup.object({
// splitSeparator: Yup.string().required('The separator is required')
});
export default function ${capitalizeFirstLetter(toolNameCamelCase)}() {
return <Box>Lorem ipsum</Box>;
}
`
2025-02-23 14:11:21 +00:00
);
2024-06-23 19:57:58 +01:00
createToolFile(
`meta.ts`,
`
import { defineTool } from '@tools/defineTool';
import { lazy } from 'react';
2024-06-23 20:37:17 +01:00
// import image from '@assets/text.png';
2024-06-23 19:57:58 +01:00
2024-06-25 02:07:57 +01:00
export const tool = defineTool('${type}', {
2024-06-23 19:57:58 +01:00
name: '${toolNameTitleCase}',
2024-06-24 01:16:00 +01:00
path: '${toolName}',
2024-06-23 20:37:17 +01:00
// image,
2024-06-23 19:57:58 +01:00
description: '',
2024-06-25 08:39:29 +01:00
shortDescription: '',
2025-02-23 14:11:21 +00:00
keywords: ['${toolName.split('-').join("', '")}'],
2024-06-23 19:57:58 +01:00
component: lazy(() => import('./index'))
});
`
2025-02-23 14:11:21 +00:00
);
2024-06-23 19:57:58 +01:00
2025-02-23 14:11:21 +00:00
createToolFile(`service.ts`, ``);
2024-06-23 19:57:58 +01:00
createToolFile(
`${toolName}.service.test.ts`,
`
import { expect, describe, it } from 'vitest';
// import { } from './service';
//
// describe('${toolName}', () => {
//
// })
`
2025-02-23 14:11:21 +00:00
);
2024-06-23 19:57:58 +01:00
// createToolFile(
// `${toolName}.e2e.spec.ts`,
// `
// import { test, expect } from '@playwright/test';
//
// test.describe('Tool - ${toolNameTitleCase}', () => {
// test.beforeEach(async ({ page }) => {
// await page.goto('/${toolName}');
// });
//
// test('Has correct title', async ({ page }) => {
// await expect(page).toHaveTitle('${toolNameTitleCase} - IT Tools');
// });
//
// test('', async ({ page }) => {
//
// });
// });
//
// `
// )
2025-02-23 14:11:21 +00:00
const toolsIndex = join(toolsDir, 'index.ts');
2024-06-23 19:57:58 +01:00
const indexContent = await readFile(toolsIndex, { encoding: 'utf-8' }).then(
(r) => r.split('\n')
2025-02-23 14:11:21 +00:00
);
2024-06-23 19:57:58 +01:00
indexContent.splice(
0,
0,
2025-02-23 14:11:21 +00:00
`import { tool as ${type}${capitalizeFirstLetter(
toolNameCamelCase
)} } from './${toolName}/meta';`
);
writeFile(toolsIndex, indexContent.join('\n'));
console.log(`Added import in: ${toolsIndex}`);