Skip to content

Commit

Permalink
Testing bin script
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek-Mallick committed Aug 13, 2024
1 parent 5e765f2 commit ffa9297
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 140 deletions.
154 changes: 79 additions & 75 deletions bin/universal-box.js
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'));
}
62 changes: 0 additions & 62 deletions bin/universalbox.js

This file was deleted.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "universal-box",
"version": "2.2.0",
"description": "universal-box(smart-bee)..a one stop package for all!",
"description": "universal-box..a one stop package for all!",
"license": "ISC",
"repository": {
"type": "git",
"url": "https://github.com/Abhishek-Mallick/universal-box.git"
},
"main": "index.js",
"bin": {
"universal-box": "./bin/universalbox.js"
"universal-box": "./bin/universal-box.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down

0 comments on commit ffa9297

Please sign in to comment.