UNPKG

ganjineh

Version:

A command-line interface (CLI) tool for scaffolding new projects with various languages and frameworks.

313 lines (276 loc) 13.4 kB
#!/usr/bin/env node import inquirer from 'inquirer'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { execa } from 'execa'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const copyDir = (src, dest) => { fs.mkdirSync(dest, { recursive: true }); let entries = fs.readdirSync(src, { withFileTypes: true }); for (let entry of entries) { let srcPath = path.join(src, entry.name); let destPath = path.join(dest, entry.name); entry.isDirectory() ? copyDir(srcPath, destPath) : fs.copyFileSync(srcPath, destPath); } }; const mitLicense = (author, year) => `MIT License Copyright (c) ${year} ${author} Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.`; const apacheLicense = (author, year) => `Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ Copyright (c) ${year} ${author} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.`; const gplLicense = (author, year) => `GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (c) ${year} ${author} This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.`; const questions = [ { type: 'input', name: 'projectName', message: 'What is the name of your project?', validate: input => input ? true : 'Please enter a project name.' }, { type: 'list', name: 'language', message: 'Which is the main programming language?', choices: ['Node.js', 'Python', 'Mobile (Cross-platform)', 'Other'] }, { type: 'list', name: 'jsProjectCreator', message: 'Which tool/framework do you want to use for your Node.js project?', choices: ['Express.js', 'React (CRA)', 'Vite (React)', 'Next.js', 'NestJS', 'Fastify', 'None'], when: (answers) => answers.language === 'Node.js' }, { type: 'list', name: 'pythonProjectCreator', message: 'Which tool/framework do you want to use for your Python project?', choices: ['Django', 'Flask', 'FastAPI', 'None'], when: (answers) => answers.language === 'Python' }, { type: 'list', name: 'mobileProjectCreator', message: 'Which mobile framework do you want to use?', choices: ['React Native', 'Flutter', 'None'], when: (answers) => answers.language === 'Mobile (Cross-platform)' }, { type: 'list', name: 'license', message: 'Choose a license:', choices: ['MIT', 'Apache-2.0', 'GPL-3.0', 'No License'] }, { type: 'confirm', name: 'includeDocs', message: 'Include initial documentation?', default: true }, { type: 'input', name: 'author', message: 'Author name?', validate: input => input ? true : 'Please enter author name.' }, { type: 'input', name: 'github', message: 'GitHub URL (optional)?' } ]; inquirer.prompt(questions).then(async answers => { const dir = path.join(process.cwd(), answers.projectName); if (fs.existsSync(dir)) { console.error(`\n❌ Folder "${answers.projectName}" already exists. Please choose another name.`); return; } fs.mkdirSync(dir); const year = new Date().getFullYear(); // Handle templates and dependency installation const { language, jsProjectCreator, pythonProjectCreator, mobileProjectCreator } = answers; let templatePath = ''; let installCommand = ''; let installArgs = []; if (language === 'Node.js') { if (jsProjectCreator === 'Express.js') { templatePath = path.join(__dirname, 'templates', 'nodejs', 'express'); installCommand = 'npm'; installArgs = ['install']; } else if (jsProjectCreator === 'React (CRA)') { console.log('\n💡 Using create-react-app for React (CRA). This might take a while...'); installCommand = 'npx'; installArgs = ['create-react-app', answers.projectName]; } else if (jsProjectCreator === 'Vite (React)') { console.log('\n💡 Using Vite for React. This might take a while...'); installCommand = 'npm'; installArgs = ['create', 'vite@latest', answers.projectName, '--template', 'react-ts']; } else if (jsProjectCreator === 'Next.js') { console.log('\n💡 Using create-next-app for Next.js. This might take a while...'); installCommand = 'npx'; installArgs = ['create-next-app', answers.projectName, '--use-npm', '--ts', '--eslint', '--app', '--src-dir', '--tailwind', '--no-import-alias', '--no-experimental-app', '--no-browserslist']; } else if (jsProjectCreator === 'NestJS') { console.log('\n💡 Using NestJS for Node.js. This might take a while...'); installCommand = 'npm'; installArgs = ['create', 'nestjs', answers.projectName]; } else if (jsProjectCreator === 'Fastify') { console.log('\n💡 Using Fastify for Node.js. This might take a while...'); installCommand = 'npm'; installArgs = ['create', 'fastify', answers.projectName]; } } else if (language === 'Python') { if (pythonProjectCreator === 'Django') { console.log('\n💡 Using django-admin to create Django project. This might take a while...'); // Django creates its own directory and requires django to be installed first installCommand = 'python'; installArgs = ['-m', 'pip', 'install', 'django']; // The actual project creation will happen after this pip install } else if (pythonProjectCreator === 'Flask') { templatePath = path.join(__dirname, 'templates', 'python', 'flask'); installCommand = 'pip'; installArgs = ['install', '-r', 'requirements.txt']; } else if (pythonProjectCreator === 'FastAPI') { console.log('\n💡 Using FastAPI for Python. This might take a while...'); installCommand = 'pip'; installArgs = ['install', 'fastapi']; } } else if (language === 'Mobile (Cross-platform)') { if (mobileProjectCreator === 'React Native') { console.log('\n💡 Using React Native for Mobile (Cross-platform). This might take a while...'); installCommand = 'npx'; installArgs = ['create-react-native-app', answers.projectName]; } else if (mobileProjectCreator === 'Flutter') { console.log('\n💡 Using Flutter for Mobile (Cross-platform). This might take a while...'); installCommand = 'flutter'; installArgs = ['create', answers.projectName]; } } if (templatePath) { copyDir(templatePath, dir); } // After template/project creation, run Django-specific command if selected if (language === 'Python' && pythonProjectCreator === 'Django') { console.log(`\n📦 Creating Django project...`); try { await execa('django-admin', ['startproject', answers.projectName, dir], { stdio: 'inherit' }); console.log(`\n✅ Django project created successfully.`); } catch (error) { console.error(`\n❌ Error creating Django project:`, error); } } // Write common files (README, LICENSE, CONTRIBUTING) let projectDesc = `A ${answers.projectType || answers.language} project`; let runCommand = ''; if (answers.language === 'Node.js') { projectDesc = `A ${answers.jsProjectCreator} project built with Node.js`; if (answers.jsProjectCreator === 'Express.js') { runCommand = `cd ${answers.projectName}\\nnpm install\\nnpm start`; } else if (answers.jsProjectCreator === 'React (CRA)') { runCommand = `cd ${answers.projectName}\\nnpm start`; } else if (answers.jsProjectCreator === 'Vite (React)') { runCommand = `cd ${answers.projectName}\\nnpm run dev`; } else if (answers.jsProjectCreator === 'Next.js') { runCommand = `cd ${answers.projectName}\\nnpm run dev`; } else if (answers.jsProjectCreator === 'NestJS') { runCommand = `cd ${answers.projectName}\\nnpm run start:dev`; } else if (answers.jsProjectCreator === 'Fastify') { runCommand = `cd ${answers.projectName}\\nnpm start`; } } else if (answers.language === 'Python') { projectDesc = `A ${answers.pythonProjectCreator} project built with Python`; if (answers.pythonProjectCreator === 'Django') { runCommand = `cd ${answers.projectName}\\npip install -r requirements.txt\\npython manage.py runserver`; } else if (answers.pythonProjectCreator === 'Flask') { runCommand = `cd ${answers.projectName}\\npip install -r requirements.txt\\nflask run`; } else if (answers.pythonProjectCreator === 'FastAPI') { runCommand = `cd ${answers.projectName}\\npip install -r requirements.txt\\nuvicorn main:app --reload`; } } else if (answers.language === 'Mobile (Cross-platform)') { projectDesc = `A ${answers.mobileProjectCreator} mobile application`; if (answers.mobileProjectCreator === 'React Native') { runCommand = `cd ${answers.projectName}\\nnpm start`; } else if (answers.mobileProjectCreator === 'Flutter') { runCommand = `cd ${answers.projectName}\\nflutter run`; } } fs.writeFileSync( path.join(dir, 'README.md'), `# ${answers.projectName}\n\n${projectDesc}.\n\n## Author\n[${answers.author}](${answers.github || '#'})\n\n## Getting Started\n\nTo get started with this project, run the following commands:\n\n\`\`\`bash\n${runCommand}\n\`\`\`\n` ); if (answers.license !== 'No License') { let licenseText = ''; switch (answers.license) { case 'MIT': licenseText = mitLicense(answers.author, year); break; case 'Apache-2.0': licenseText = apacheLicense(answers.author, year); break; case 'GPL-3.0': licenseText = gplLicense(answers.author, year); break; } fs.writeFileSync(path.join(dir, 'LICENSE'), licenseText); } fs.writeFileSync( path.join(dir, 'CONTRIBUTING.md'), `# Contribution Guidelines for ${answers.projectName}\n\nThank you for considering contributing to this ${projectDesc}.\n\n## How to Contribute\n\n1. Fork the repository\n2. Create a new branch\n3. Make your changes\n4. Submit a pull request` ); if (answers.includeDocs) { const docsPath = path.join(dir, 'docs'); fs.mkdirSync(docsPath, { recursive: true }); fs.writeFileSync( path.join(docsPath, 'index.md'), `# Documentation for ${answers.projectName}\n\nWelcome to the documentation for your ${projectDesc}.` ); } // Run install command if exists if (installCommand) { console.log(`\n📦 Installing dependencies...`); try { const cwd = (jsProjectCreator === 'React (CRA)' || jsProjectCreator === 'Next.js' || jsProjectCreator === 'Vite (React)' || pythonProjectCreator === 'Django' || pythonProjectCreator === 'FastAPI' || mobileProjectCreator === 'React Native' || mobileProjectCreator === 'Flutter') ? process.cwd() : dir; // CRA, Next.js, Vite, Django, FastAPI, React Native, Flutter create their own directory await execa(installCommand, installArgs, { stdio: 'inherit', cwd }); console.log(`\n✅ Dependencies installed successfully.`); } catch (error) { console.error(`\n❌ Error installing dependencies:`, error); } } console.log(`\n✅ Project "${answers.projectName}" created successfully.`); });