Skip to content

Commit

Permalink
Update changes
Browse files Browse the repository at this point in the history
  • Loading branch information
owellandry committed Sep 14, 2024
1 parent 88a93f0 commit 96bca24
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

npm uninstall -g pyrex-cli
npm install -g .
pyx create-pyrex-app app
pyx create-pyrex-app My-App
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node

// index.js

const { Command } = require('commander');
const { createApp } = require('./modules/createApp');
const { promptStructure } = require('./modules/promptStructure');
const { promptLanguage } = require('./modules/promptLanguage'); // Nueva función para lenguaje
const path = require('path');
const { execSync } = require('child_process');

const program = new Command();
Expand All @@ -14,15 +14,16 @@ program
.command('create-pyrex-app <appName>')
.description('Crea una nueva aplicación Pyrex')
.action(async (appName) => {
await createApp(appName, promptStructure);
await createApp(appName, promptStructure, promptLanguage);
});

program
.command('start')
.description('Inicia el servidor de desarrollo')
.description('Inicia el servidor en el puerto 3000')
.action(() => {
const serverPath = path.join(__dirname, 'server.js');
try {
execSync('node modules/server.js', { stdio: 'inherit' });
execSync(`node ${serverPath}`, { stdio: 'inherit' });
} catch (error) {
console.error('Error al iniciar el servidor:', error.message);
}
Expand Down
39 changes: 19 additions & 20 deletions modules/createApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ const { execSync } = require('child_process');
const { createPublicFiles } = require('./fileManager');
const { createReadme } = require('./readme');

async function createApp(appName, promptStructure) {
async function createApp(appName, promptStructure, promptLanguage) {
const appDir = path.join(process.cwd(), appName);
if (fs.existsSync(appDir)) {
console.error('El directorio ya existe.');
process.exit(1);
}

// Crear el directorio principal
fs.mkdirSync(appDir);

// Preguntar al usuario sobre la estructura del proyecto
const answers = await promptStructure();

// Crear la estructura de carpetas según la elección del usuario
if (answers.structure === 'src') {
fs.mkdirSync(path.join(appDir, 'src'));
fs.writeFileSync(path.join(appDir, 'src', 'index.js'), '// Tu código aquí');
Expand All @@ -29,27 +26,29 @@ async function createApp(appName, promptStructure) {
fs.writeFileSync(path.join(appDir, 'pages', 'index.js'), '// Tu código aquí');
}

// Crear archivos públicos y README
createPublicFiles(appDir);
createReadme(appDir);

// Ejecutar npm init -y para crear package.json
try {
execSync('npm init -y', { cwd: appDir, stdio: 'inherit' });
console.log('Proyecto inicializado con npm.');
} catch (error) {
console.error('Error al ejecutar npm init:', error.message);
}

// Instalar TypeScript
try {
execSync('npm install typescript --save-dev', { cwd: appDir, stdio: 'inherit' });
console.log('TypeScript instalado.');
} catch (error) {
console.error('Error al instalar TypeScript:', error.message);
const langAnswer = await promptLanguage();
if (langAnswer.language === 'typescript') {
try {
execSync('npm init -y', { cwd: appDir, stdio: 'inherit' });
console.log('Proyecto inicializado con npm.');

execSync('npm install typescript --save-dev', { cwd: appDir, stdio: 'inherit' });
console.log('TypeScript instalado.');
} catch (error) {
console.error('Error al configurar el proyecto:', error.message);
}
} else {
try {
execSync('npm init -y', { cwd: appDir, stdio: 'inherit' });
console.log('Proyecto inicializado con npm.');
} catch (error) {
console.error('Error al ejecutar npm init:', error.message);
}
}

// Inicializar Git
try {
execSync('git init', { cwd: appDir, stdio: 'inherit' });
console.log('Repositorio Git inicializado.');
Expand Down
2 changes: 0 additions & 2 deletions modules/fileManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//modules/fileManager.js

const fs = require('fs');
const path = require('path');

Expand Down
8 changes: 3 additions & 5 deletions modules/promptLanguage.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// modules/promptLanguage.js

const inquirer = require('inquirer').default;

async function promptLanguage() {
const answer = await inquirer.prompt([
const answers = await inquirer.prompt([
{
type: 'list',
name: 'language',
message: '¿Qué lenguaje de programación prefieres?',
message: 'Elige el lenguaje de programación:',
choices: ['javascript', 'typescript'],
},
]);

return answer;
return answers;
}

module.exports = { promptLanguage };
6 changes: 0 additions & 6 deletions modules/promptStructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ async function promptStructure() {
message: 'Elige la estructura del proyecto:',
choices: ['src', 'app', 'page'],
},
{
type: 'list',
name: 'language',
message: 'Elige el lenguaje del proyecto:',
choices: ['JavaScript', 'TypeScript'],
},
]);

return answers;
Expand Down
2 changes: 0 additions & 2 deletions modules/readme.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//modules/readme.md

const fs = require('fs');
const path = require('path');

Expand Down
19 changes: 0 additions & 19 deletions modules/server.js

This file was deleted.

18 changes: 17 additions & 1 deletion package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pyrex-cli",
"version": "1.0.0",
"version": "2.0.0",
"bin": {
"pyx": "./index.js"
},
Expand All @@ -11,6 +11,7 @@
"license": "MIT",
"dependencies": {
"commander": "^12.1.0",
"inquirer": "^10.2.2"
"inquirer": "^10.2.2",
"mime": "^4.0.4"
}
}
27 changes: 27 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const http = require('http');
const path = require('path');
const fs = require('fs');
const mime = require('mime');

const port = 3000;
const publicDir = path.join(__dirname, 'public');

const server = http.createServer((req, res) => {
let filePath = path.join(publicDir, req.url === '/' ? 'index.html' : req.url);

fs.stat(filePath, (err, stat) => {
if (err) {
res.statusCode = 404;
res.end('Not Found');
return;
}

const contentType = mime.getType(filePath) || 'text/plain';
res.setHeader('Content-Type', contentType);
fs.createReadStream(filePath).pipe(res);
});
});

server.listen(port, () => {
console.log(`Servidor escuchando en http://localhost:${port}`);
});

0 comments on commit 96bca24

Please sign in to comment.