From fdb7b54333f49bc168309d12bb08f404f39cec05 Mon Sep 17 00:00:00 2001 From: ny <64143453+nyaoouo@users.noreply.github.com> Date: Mon, 30 Jun 2025 11:10:20 +0800 Subject: [PATCH] feat(scripts): add TypeScript and JavaScript plugin generator wrappers --- package.json | 4 +- scripts/create-plugin-core.js | 238 ++++++++++++++++++++++++++++++++++ scripts/new-plugin-js.js | 19 +++ scripts/new-plugin-ts.js | 19 +++ 4 files changed, 279 insertions(+), 1 deletion(-) create mode 100644 scripts/create-plugin-core.js create mode 100644 scripts/new-plugin-js.js create mode 100644 scripts/new-plugin-ts.js diff --git a/package.json b/package.json index f17853ec..8b771442 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,9 @@ "lint:ci": "eslint --ext .ts --rule \"prettier/prettier: off\" .", "lint:fix": "eslint --fix --ext .ts .", "prettier": "prettier --write .", - "update-translations": "cd scripts && node update-translations.js" + "update-translations": "cd scripts && node update-translations.js", + "new-plugin-ts": "node scripts/new-plugin-ts.js", + "new-plugin-js": "node scripts/new-plugin-js.js" }, "license": "GNU", "dependencies": { diff --git a/scripts/create-plugin-core.js b/scripts/create-plugin-core.js new file mode 100644 index 00000000..61ddfc84 --- /dev/null +++ b/scripts/create-plugin-core.js @@ -0,0 +1,238 @@ +/** + * Plugin Generator Script + * Creates new plugin templates for both TypeScript and JavaScript + */ + +const fs = require("fs"); +const path = require("path"); + +class PluginGenerator { + constructor() { + this.pluginsDir = path.resolve("plugins"); + } + + /** + * Create a new plugin + */ + createPlugin(name, language = "ts") { + if (!name) { + console.error("❌ Plugin name is required!"); + console.log("Usage: node create-plugin.js [ts|js]"); + process.exit(1); + } + + if (!["ts", "js"].includes(language)) { + console.error("❌ Language must be 'ts' or 'js'"); + process.exit(1); + } + + const pluginDir = path.join(this.pluginsDir, name); + + // Check if plugin already exists + if (fs.existsSync(pluginDir)) { + console.error(`❌ Plugin '${name}' already exists!`); + process.exit(1); + } + + console.log(`🚀 Creating ${language.toUpperCase()} plugin: ${name}`); + + // Create plugin directory + this.createDirectory(pluginDir); + + // Create plugin files + this.createPluginManifest(pluginDir, name); + + if (language === "ts") { + this.createTypeScriptPlugin(pluginDir, name); + } else { + this.createJavaScriptPlugin(pluginDir, name); + } + + console.log(`✅ Plugin '${name}' created successfully!`); + console.log(`📁 Location: ${pluginDir}`); + console.log(`🔧 Next steps:`); + console.log(` 1. Edit ${path.join(pluginDir, `index.${language}`)} to implement your plugin logic`); + console.log(` 2. Update ${path.join(pluginDir, "plugin.json")} if needed`); + console.log(` 3. Run 'npm run build' to build the plugin`); + } + + /** + * Create directory if it doesn't exist + */ + createDirectory(dirPath) { + if (!fs.existsSync(dirPath)) { + fs.mkdirSync(dirPath, { recursive: true }); + } + } + + /** + * Create plugin manifest file + */ + createPluginManifest(pluginDir, name) { + const manifest = { + name: name, + version: "1.0.0", + description: `${name} plugin for Warframe Emulator`, + main: "index.js", + author: "Your Name", + license: "GNU", + config: { + enabled: true + }, + dependencies: {}, + tags: ["custom"] + }; + + const manifestPath = path.join(pluginDir, "plugin.json"); + fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2)); + console.log(`📝 Created: plugin.json`); + } + + /** + * Create TypeScript plugin template + */ + createTypeScriptPlugin(pluginDir, name) { + const template = `import { IPlugin } from "@/src/types/pluginTypes"; +import { logger } from "@/src/utils/logger"; + +export default class ${name} implements IPlugin { + public name = "${name}"; + public version = "1.0.0"; + public description = "${name} plugin for Warframe Emulator"; + + async initialize(): Promise { + logger.info(\`[\${this.name}] Plugin initialized successfully!\`); + + // Add your plugin initialization logic here + // For example: + // - Register new routes + // - Add new API endpoints + // - Set up event listeners + // - Connect to external services + + await Promise.resolve(); // Remove this line and add your actual logic + } + + async cleanup(): Promise { + logger.info(\`[\${this.name}] Plugin cleanup completed\`); + + // Add your cleanup logic here + // For example: + // - Close database connections + // - Clear timers/intervals + // - Remove event listeners + // - Cleanup resources + + await Promise.resolve(); // Remove this line and add your actual logic + } + + // Add your custom methods here + // Example: + // public async customMethod(): Promise { + // logger.info(\`[\${this.name}] Custom method called\`); + // } +} +`; + + const pluginPath = path.join(pluginDir, "index.ts"); + fs.writeFileSync(pluginPath, template); + console.log(`📝 Created: index.ts`); + } + + /** + * Create JavaScript plugin template + */ + createJavaScriptPlugin(pluginDir, name) { + const template = `/** + * ${name} Plugin + * ${name} plugin for Warframe Emulator + */ +import {logger} from "../../src/utils/logger.js"; + +class ${name} { + constructor() { + this.name = "${name}"; + this.version = "1.0.0"; + this.description = "${name} plugin for Warframe Emulator"; + } + + async initialize() { + logger.info(\`[\${this.name}] Plugin initialized successfully!\`); + + // Add your plugin initialization logic here + // For example: + // - Register new routes + // - Add new API endpoints + // - Set up event listeners + // - Connect to external services + + return Promise.resolve(); // Remove this line and add your actual logic + } + + async cleanup() { + logger.info(\`[\${this.name}] Plugin cleanup completed\`); + + // Add your cleanup logic here + // For example: + // - Close database connections + // - Clear timers/intervals + // - Remove event listeners + // - Cleanup resources + + return Promise.resolve(); // Remove this line and add your actual logic + } + + // Add your custom methods here + // Example: + // async customMethod() { + // console.log(\`[\${this.name}] Custom method called\`); + // } +} + +export default ${name}; +`; + + const pluginPath = path.join(pluginDir, "index.js"); + fs.writeFileSync(pluginPath, template); + console.log(`📝 Created: index.js`); + + // Create package.json for ES module support + const packageJson = { + "type": "module" + }; + const packagePath = path.join(pluginDir, "package.json"); + fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2)); + console.log(`📝 Created: package.json`); + } +} + +// Parse command line arguments +const args = process.argv.slice(2); +const pluginName = args[0]; +const language = args[1] || "ts"; + +// Validate plugin name +if (!pluginName) { + console.error("❌ Plugin name is required!"); + console.log("Usage:"); + console.log(" node scripts/create-plugin.js [ts|js]"); + console.log(""); + console.log("Examples:"); + console.log(" node scripts/create-plugin.js MyAwesomePlugin ts"); + console.log(" node scripts/create-plugin.js MyJSPlugin js"); + process.exit(1); +} + +// Validate plugin name format +if (!/^[A-Za-z][A-Za-z0-9_]*$/.test(pluginName)) { + console.error("❌ Plugin name must start with a letter and contain only letters, numbers, and underscores!"); + process.exit(1); +} + +// Create the plugin +if (require.main === module) { + const generator = new PluginGenerator(); + generator.createPlugin(pluginName, language); +} + +module.exports = { PluginGenerator }; diff --git a/scripts/new-plugin-js.js b/scripts/new-plugin-js.js new file mode 100644 index 00000000..9e28f0ab --- /dev/null +++ b/scripts/new-plugin-js.js @@ -0,0 +1,19 @@ +/** + * JavaScript Plugin Generator Wrapper + */ + +const { PluginGenerator } = require('./create-plugin-core'); + +// Get plugin name from command line arguments +const pluginName = process.argv[2]; + +if (!pluginName) { + console.error("❌ Plugin name is required!"); + console.log("Usage: npm run new-plugin-js "); + console.log("Example: npm run new-plugin-js MyJSPlugin"); + process.exit(1); +} + +// Create JavaScript plugin +const generator = new PluginGenerator(); +generator.createPlugin(pluginName, "js"); diff --git a/scripts/new-plugin-ts.js b/scripts/new-plugin-ts.js new file mode 100644 index 00000000..04ca4c8c --- /dev/null +++ b/scripts/new-plugin-ts.js @@ -0,0 +1,19 @@ +/** + * TypeScript Plugin Generator Wrapper + */ + +const { PluginGenerator } = require('./create-plugin-core'); + +// Get plugin name from command line arguments +const pluginName = process.argv[2]; + +if (!pluginName) { + console.error("❌ Plugin name is required!"); + console.log("Usage: npm run new-plugin-ts "); + console.log("Example: npm run new-plugin-ts MyAwesomePlugin"); + process.exit(1); +} + +// Create TypeScript plugin +const generator = new PluginGenerator(); +generator.createPlugin(pluginName, "ts");