Skip to content

Commit

Permalink
Creacion de base de datos desde cli
Browse files Browse the repository at this point in the history
  • Loading branch information
francis560 committed Jun 19, 2022
1 parent f18e633 commit f1853c2
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 20 deletions.
52 changes: 32 additions & 20 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import shell from "shelljs";
import gradient from "gradient-string";
import { createSpinner } from "nanospinner";
import { createBasicFiles, createFolderProject } from "./createInitFolders.js";
import { createDbConnection } from "./createdb.js";
import welcome from "./welcome.js";
import finalMessage from "./finalProjectMessages.js";
import "./commands.js";
Expand All @@ -22,36 +23,47 @@ const generateProject = () => {
name: "type",
message: "Select your type of project",
choices: ["javascript", "typescript"]
}
// {
// type: "confirm",
// name: "database",
// message: "Do you want to use a database?"
// }
},
{
type: "confirm",
name: "database",
message: "Do you want to use a database?"
}
]).then(({ name, type, database }) => {

try {

console.log("\n");

shell.config.silent = true;

// if (database) {
// inquirer.prompt({
// type: "list",
// name: "database_type",
// message: "Select your database type",
// choices: ["mongodb"]
// }).then(({ database_type }) => {

if (database) {
inquirer.prompt({
type: "list",
name: "database_type",
message: "Select your database type",
choices: ["mongodb", "postgresql"]
}).then(({ database_type }) => {

console.log("\n");

const spinner = createSpinner(gradient.rainbow(`Creating a new Express project in ${process.cwd()} \n`)).start();

createFolderProject(name);

createBasicFiles(name, type);

createDbConnection(database_type, type, name);

spinner.stop();

finalMessage(name, spinner);

// });
});

// return;
// }

return;
}

console.log("\n");

const spinner = createSpinner(gradient.rainbow(`Creating a new Express project in ${process.cwd()} \n`)).start();

createFolderProject(name);
Expand Down
197 changes: 197 additions & 0 deletions app/createdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import path from "path";
import fs from "fs";
import shell from "shelljs";
import { mongoConnectionJs, postgresqlConnectionJs } from "./templates/javascript/contentdbJs.js";
import { mongoConnectionTs, postgresqlConnectionTs } from "./templates/typescript/contentdbTs.js";


const contentdbFile = (database_type, type, name) => {

if (database_type === "mongodb") {

fs.appendFile(path.join(process.cwd(), type === "javascript" ? `/${name}/config/database.js` : `/${name}/config/database.ts`), type === "javascript" ? mongoConnectionJs : mongoConnectionTs, (err) => {

if (err) {
console.error(err)
return
}

});

if (type === "javascript") {
fs.writeFile(path.join(process.cwd(), `/${name}/package.json`), `{
"name": "${name}",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "app.js",
"scripts": {
"start": "node app.js",
"build": "npm i",
"dev": "nodemon app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"morgan": "^1.10.0",
"dotenv": "^16.0.1",
"mongoose": "^6.3.1"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}
`, (err) => {
if (err) {
console.error(err)
return
}
});

}

if (type === "typescript") {
fs.appendFile(path.join(process.cwd(), `/${name}/package.json`), `{
"name": "${name}",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js",
"build": "npx tsc -b",
"dev": "nodemon"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"morgan": "^1.10.0",
"dotenv": "^16.0.1",
"mongoose": "^6.3.1"
},
"devDependencies": {
"nodemon": "^2.0.7",
"ts-node": "^10.7.0",
"typescript": "^4.6.3",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.13",
"@types/morgan": "^1.9.3",
"@types/nodemon": "^1.19.1",
"@types/mongoose": "^5.11.97"
}
}
`, (err) => {
if (err) {
console.error(err)
return
}
});

}

}

if (database_type === "postgresql") {

fs.appendFile(path.join(process.cwd(), type === "javascript" ? `/${name}/config/database.js` : `/${name}/config/database.ts`), type === "javascript" ? postgresqlConnectionJs : postgresqlConnectionTs, (err) => {

if (err) {
console.error(err)
return
}

});

if (type === "javascript") {
fs.writeFile(path.join(process.cwd(), `/${name}/package.json`), `{
"name": "${name}",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "app.js",
"scripts": {
"start": "node app.js",
"build": "npm i",
"dev": "nodemon app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"morgan": "^1.10.0",
"dotenv": "^16.0.1",
"pg": "^8.7.3"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}
`, (err) => {
if (err) {
console.error(err)
return
}
});

}

if (type === "typescript") {
fs.appendFile(path.join(process.cwd(), `/${name}/package.json`), `{
"name": "${name}",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js",
"build": "npx tsc -b",
"dev": "nodemon"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"morgan": "^1.10.0",
"dotenv": "^16.0.1",
"pg": "^8.7.3"
},
"devDependencies": {
"nodemon": "^2.0.7",
"ts-node": "^10.7.0",
"typescript": "^4.6.3",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.13",
"@types/morgan": "^1.9.3",
"@types/nodemon": "^1.19.1"
}
}
`, (err) => {
if (err) {
console.error(err)
return
}
});

}

}

}

export const createDbConnection = (database_type, type, name) => {

if (type === "javascript") {
shell.touch(path.join(process.cwd(), `/${name}/config/database.js`));
}

if (type === "typescript") {
shell.touch(path.join(process.cwd(), `/${name}/config/database.ts`));
}

contentdbFile(database_type, type, name);

}
32 changes: 32 additions & 0 deletions app/templates/javascript/contentdbJs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const mongoConnectionJs = `import mongoose from "mongoose";
const connection = () => {
mongoose.connect("mongodb_uri").then(res => {
console.log("Database is conected");
}).catch(err => {
console.error(err);
});
}
export default connection;
`

export const postgresqlConnectionJs = `import { Pool } from "pg";
const pool = new Pool({
user: "",
password: "",
host: "localhost",
port: 5432,
database: ""
});
export default pool;
`
32 changes: 32 additions & 0 deletions app/templates/typescript/contentdbTs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const mongoConnectionTs = `import mongoose from "mongoose";
const connection = () => {
mongoose.connect("mongodb_uri").then(res => {
console.log("Database is conected");
}).catch(err => {
console.error(err);
});
}
export default connection;
`

export const postgresqlConnectionTs = `import { Pool } from "pg";
const pool = new Pool({
user: "",
password: "",
host: "localhost",
port: 5432,
database: ""
});
export default pool;
`

0 comments on commit f1853c2

Please sign in to comment.