|
| 1 | +import arg from 'arg'; |
| 2 | +import chalk from 'chalk'; |
| 3 | +import fs from 'fs'; |
| 4 | +import fse from 'fs-extra'; |
| 5 | +import inquirer from 'inquirer'; |
| 6 | +import path from 'path'; |
| 7 | +import { Listr } from 'listr2'; |
| 8 | +import { fileURLToPath } from 'url'; |
| 9 | +import { execa } from 'execa'; |
| 10 | +import Handlebars from 'handlebars'; |
| 11 | + |
| 12 | +export function parseArgumentsIntoOptions(rawArgs) { |
| 13 | + const args = arg( |
| 14 | + { |
| 15 | + "--plugin-name": String, |
| 16 | + // you can add more flags here if needed |
| 17 | + }, |
| 18 | + { |
| 19 | + argv: rawArgs.slice(1), // skip "create-plugin" |
| 20 | + } |
| 21 | + ); |
| 22 | + |
| 23 | + return { |
| 24 | + pluginName: args["--plugin-name"], |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +export async function promptForMissingOptions(options) { |
| 29 | + const questions = []; |
| 30 | + |
| 31 | + if (!options.pluginName) { |
| 32 | + questions.push({ |
| 33 | + type: "input", |
| 34 | + name: "pluginName", |
| 35 | + message: "Please specify the name of the plugin >", |
| 36 | + default: "adminforth-plugin", |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + const answers = await inquirer.prompt(questions); |
| 41 | + return { |
| 42 | + ...options, |
| 43 | + pluginName: options.pluginName || answers.pluginName, |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +function checkNodeVersion(minRequiredVersion = 20) { |
| 48 | + const current = process.versions.node.split("."); |
| 49 | + const major = parseInt(current[0], 10); |
| 50 | + |
| 51 | + if (isNaN(major) || major < minRequiredVersion) { |
| 52 | + throw new Error( |
| 53 | + `Node.js v${minRequiredVersion}+ is required. You have ${process.versions.node}. ` + |
| 54 | + `Please upgrade Node.js. We recommend using nvm for managing multiple Node.js versions.` |
| 55 | + ); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function checkForExistingPackageJson() { |
| 60 | + if (fs.existsSync(path.join(process.cwd(), "package.json"))) { |
| 61 | + throw new Error( |
| 62 | + `A package.json already exists in this directory.\n` + |
| 63 | + `Please remove it or use an empty directory.` |
| 64 | + ); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function initialChecks() { |
| 69 | + return [ |
| 70 | + { |
| 71 | + title: "👀 Checking Node.js version...", |
| 72 | + task: () => checkNodeVersion(20), |
| 73 | + }, |
| 74 | + { |
| 75 | + title: "👀 Validating current working directory...", |
| 76 | + task: () => checkForExistingPackageJson(), |
| 77 | + }, |
| 78 | + ]; |
| 79 | +} |
| 80 | + |
| 81 | +function renderHBSTemplate(templatePath, data) { |
| 82 | + const template = fs.readFileSync(templatePath, "utf-8"); |
| 83 | + const compiled = Handlebars.compile(template); |
| 84 | + return compiled(data); |
| 85 | +} |
| 86 | + |
| 87 | +async function scaffoldProject(ctx, options, cwd) { |
| 88 | + const pluginName = options.pluginName; |
| 89 | + |
| 90 | + const filename = fileURLToPath(import.meta.url); |
| 91 | + const dirname = path.dirname(filename); |
| 92 | + |
| 93 | + // Prepare directories |
| 94 | + ctx.customDir = path.join(cwd, "custom"); |
| 95 | + await fse.ensureDir(ctx.customDir); |
| 96 | + |
| 97 | + // Write templated files |
| 98 | + await writeTemplateFiles(dirname, cwd, { |
| 99 | + pluginName, |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +async function writeTemplateFiles(dirname, cwd, options) { |
| 104 | + const { pluginName } = options; |
| 105 | + |
| 106 | + // Build a list of files to generate |
| 107 | + const templateTasks = [ |
| 108 | + { |
| 109 | + src: "tsconfig.json.hbs", |
| 110 | + dest: "tsconfig.json", |
| 111 | + data: {}, |
| 112 | + }, |
| 113 | + { |
| 114 | + src: "package.json.hbs", |
| 115 | + dest: "package.json", |
| 116 | + data: { pluginName }, |
| 117 | + }, |
| 118 | + { |
| 119 | + src: "index.ts.hbs", |
| 120 | + dest: "index.ts", |
| 121 | + data: {}, |
| 122 | + }, |
| 123 | + { |
| 124 | + src: ".gitignore.hbs", |
| 125 | + dest: ".gitignore", |
| 126 | + data: {}, |
| 127 | + }, |
| 128 | + { |
| 129 | + src: "types.ts.hbs", |
| 130 | + dest: "types.ts", |
| 131 | + data: {}, |
| 132 | + }, |
| 133 | + { |
| 134 | + src: "custom/tsconfig.json.hbs", |
| 135 | + dest: "custom/tsconfig.json", |
| 136 | + data: {}, |
| 137 | + }, |
| 138 | + ]; |
| 139 | + |
| 140 | + for (const task of templateTasks) { |
| 141 | + // If a condition is specified and false, skip this file |
| 142 | + if (task.condition === false) continue; |
| 143 | + |
| 144 | + const destPath = path.join(cwd, task.dest); |
| 145 | + fse.ensureDirSync(path.dirname(destPath)); |
| 146 | + |
| 147 | + if (task.empty) { |
| 148 | + fs.writeFileSync(destPath, ""); |
| 149 | + } else { |
| 150 | + const templatePath = path.join(dirname, "templates", task.src); |
| 151 | + const compiled = renderHBSTemplate(templatePath, task.data); |
| 152 | + fs.writeFileSync(destPath, compiled); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +async function installDependencies(ctx, cwd) { |
| 158 | + const customDir = ctx.customDir; |
| 159 | + |
| 160 | + await Promise.all([ |
| 161 | + await execa("npm", ["install", "--no-package-lock"], { cwd }), |
| 162 | + await execa("npm", ["install"], { cwd: customDir }), |
| 163 | + ]); |
| 164 | +} |
| 165 | + |
| 166 | +function generateFinalInstructions() { |
| 167 | + let instruction = "⏭️ Your plugin is ready! Next steps:\n"; |
| 168 | + |
| 169 | + instruction += ` |
| 170 | + ${chalk.dim("// Build your plugin")} |
| 171 | + ${chalk.cyan("$ npm run build")}\n`; |
| 172 | + |
| 173 | + instruction += ` |
| 174 | + ${chalk.dim("// To test your plugin locally")} |
| 175 | + ${chalk.cyan("$ npm link")}\n`; |
| 176 | + |
| 177 | + instruction += ` |
| 178 | + ${chalk.dim("// In your AdminForth project")} |
| 179 | + ${chalk.cyan("$ npm link " + chalk.italic("your-plugin-name"))}\n`; |
| 180 | + |
| 181 | + instruction += "\n😉 Happy coding!"; |
| 182 | + |
| 183 | + return instruction; |
| 184 | +} |
| 185 | + |
| 186 | +export function prepareWorkflow(options) { |
| 187 | + const cwd = process.cwd(); |
| 188 | + const tasks = new Listr( |
| 189 | + [ |
| 190 | + { |
| 191 | + title: "🔍 Initial checks...", |
| 192 | + task: (_, task) => task.newListr(initialChecks(), { concurrent: true }), |
| 193 | + }, |
| 194 | + { |
| 195 | + title: "🚀 Scaffolding your plugin...", |
| 196 | + task: async (ctx) => scaffoldProject(ctx, options, cwd), |
| 197 | + }, |
| 198 | + { |
| 199 | + title: "📦 Installing dependencies...", |
| 200 | + task: async (ctx) => installDependencies(ctx, cwd), |
| 201 | + }, |
| 202 | + { |
| 203 | + title: "📝 Preparing final instructions...", |
| 204 | + task: (ctx) => { |
| 205 | + console.log( |
| 206 | + chalk.green(`✅ Successfully created your new AdminForth plugin!\n`) |
| 207 | + ); |
| 208 | + console.log(generateFinalInstructions()); |
| 209 | + console.log("\n\n"); |
| 210 | + }, |
| 211 | + }, |
| 212 | + ], |
| 213 | + { |
| 214 | + rendererOptions: { collapseSubtasks: false }, |
| 215 | + concurrent: false, |
| 216 | + exitOnError: true, |
| 217 | + collectErrors: true, |
| 218 | + } |
| 219 | + ); |
| 220 | + |
| 221 | + return tasks; |
| 222 | +} |
0 commit comments