-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e765f2
commit ffa9297
Showing
4 changed files
with
82 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,99 @@ | ||
#!/usr/bin/env node | ||
import { spawn } from 'child_process'; | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import fs from 'fs'; | ||
import chalk from 'chalk'; | ||
const inquirer = require('inquirer'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const chalk = require('chalk'); | ||
|
||
// Get the directory of the current script (bin/universal-box.js) | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
const templatesDir = path.resolve(__dirname, '../templates'); | ||
|
||
// Parse command | ||
const [,, command] = process.argv; | ||
console.log(`Command received: ${command}`); // Debugging statement | ||
const user_selection = process.argv.slice(2); | ||
const command = user_selection[0]; | ||
|
||
function getPackageJsonPath() { | ||
// Resolve the path to the node_modules directory | ||
const nodeModulesPath = path.join(__dirname, '../node_modules/universal-box'); | ||
const packageJsonPath = path.join(nodeModulesPath, 'package.json'); | ||
|
||
return packageJsonPath; | ||
switch (command) { | ||
case 'init': | ||
initProject(); | ||
break; | ||
case '--help': | ||
showHelp(); | ||
break; | ||
case 'deploy': | ||
console.log(chalk.yellow('Deployment utility function is still under development in an UAT environment and will be published soon.')); | ||
break; | ||
default: | ||
console.log(chalk.red('Unknown command. Use "universal-box --help" for available commands.')); | ||
} | ||
|
||
function getStartScript() { | ||
const packageJsonPath = getPackageJsonPath(); | ||
|
||
if (fs.existsSync(packageJsonPath)) { | ||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); | ||
return packageJson.scripts && packageJson.scripts.start; | ||
} | ||
return null; | ||
function getDirectoryContents(dir) { | ||
return fs.readdirSync(dir, { withFileTypes: true }) | ||
.filter(dirent => dirent.isDirectory() || (dirent.isFile() && dirent.name === 'README.md')) | ||
.map(dirent => dirent.name); | ||
} | ||
|
||
function runStartScript() { | ||
const startScript = getStartScript(); | ||
async function selectTemplate(currentDir) { | ||
const contents = getDirectoryContents(currentDir); | ||
|
||
if (startScript) { | ||
console.log(`Executing start script: ${startScript}`); // Debugging statement | ||
const child = spawn('npm', ['run', startScript], { stdio: 'inherit' }); | ||
|
||
child.on('error', (error) => { | ||
console.error(`Error: ${error.message}`); | ||
}); | ||
if (contents.includes('README.md') || contents.includes('server') || contents.includes('client')) { | ||
return currentDir; | ||
} | ||
|
||
const { selection } = await inquirer.prompt([ | ||
{ | ||
type: 'list', | ||
name: 'selection', | ||
message: 'Choose a template or subdirectory:', | ||
choices: contents, | ||
}, | ||
]); | ||
|
||
child.on('exit', (code) => { | ||
console.log(`Process exited with code ${code}`); | ||
}); | ||
} else { | ||
console.log(chalk.red('No start script found in package.json')); | ||
const newPath = path.join(currentDir, selection); | ||
if (fs.statSync(newPath).isDirectory()) { | ||
return selectTemplate(newPath); | ||
} | ||
return newPath; | ||
} | ||
|
||
function runInitCommand() { | ||
// Resolve the path to index.js relative to the package root | ||
const indexPath = path.join(__dirname, '../index.js'); | ||
console.log(`Executing: node ${indexPath}`); // Debugging statement | ||
|
||
// Execute index.js | ||
const child = spawn('node', [indexPath], { stdio: 'inherit' }); | ||
async function initProject() { | ||
const { projectName } = await inquirer.prompt([ | ||
{ | ||
type: 'input', | ||
name: 'projectName', | ||
message: 'Enter the project name:', | ||
}, | ||
]); | ||
|
||
const templatePath = await selectTemplate(templatesDir); | ||
const projectPath = path.resolve(process.cwd(), projectName); | ||
|
||
child.on('error', (error) => { | ||
console.error(`Error: ${error.message}`); | ||
}); | ||
fs.copySync(templatePath, projectPath); | ||
|
||
child.on('exit', (code) => { | ||
console.log(`Process exited with code ${code}`); | ||
}); | ||
console.log(chalk.green(`✅ Success! Project "${projectName}" has been initialized with the selected template.`)); | ||
console.log(chalk.yellow('\nNext steps:')); | ||
console.log(chalk.yellow(`1. Change to your new project directory:`)); | ||
console.log(chalk.cyan(` cd ${projectName}`)); | ||
console.log(chalk.yellow('2. Install dependencies (if required):')); | ||
console.log(chalk.cyan(' npm install') + ' or ' + chalk.cyan('yarn')); | ||
console.log(chalk.yellow('3. Start your development server (check package.json for specific commands)')); | ||
console.log(chalk.yellow('\nFor more information, refer to the README.md file in your project directory.')); | ||
console.log(chalk.blue('\n------ Happy Coding with Universal-Box 🚀 ------')); | ||
console.log(chalk.gray('Need help? Visit our documentation: https://universal-box.co/docs')); | ||
} | ||
|
||
switch (command) { | ||
case 'init': | ||
console.log('Initializing...'); // Debugging statement | ||
runInitCommand(); | ||
break; | ||
function showHelp() { | ||
console.log(` | ||
universal-box - The ultimate project scaffolding tool | ||
case 'start': | ||
console.log('Starting application...'); // Debugging statement | ||
runStartScript(); | ||
break; | ||
Usage: | ||
universal-box <command> | ||
case '--help': | ||
console.log(chalk.blue('For more information, visit the documentation at: https://universal-box.co/')); | ||
console.log(chalk.green('Available commands:')); | ||
console.log(chalk.green(' - universal-box init')); | ||
console.log(chalk.green(' - universal-box start')); | ||
console.log(chalk.green(' - universal-box deploy')); | ||
break; | ||
Commands: | ||
init Initialize a new project | ||
--help Show this help message | ||
deploy Triggers build and deployment pipeline | ||
case 'deploy': | ||
console.log(chalk.yellow('Deployment utility function is still under development in an UAT environment and will be published soon.')); | ||
break; | ||
|
||
default: | ||
console.log(chalk.red('Unknown command. Use "universal-box --help" for available commands.')); | ||
} | ||
Examples: | ||
universal-box --help | ||
universal-box init | ||
universal-box deploy | ||
`); | ||
console.log(chalk.blue('For more information, visit tahe documentation at: https://universal-box.co/docs')); | ||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters