-
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.
feat: Added generate functionality and update deps [2.6.1]
- Loading branch information
1 parent
e1f57b5
commit 82a87b1
Showing
9 changed files
with
361 additions
and
12 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 |
---|---|---|
|
@@ -18,6 +18,7 @@ template/ | |
.github/ | ||
.vscode/ | ||
.releaserc.json | ||
LICENSE | ||
|
||
# for 2.4.5 release | ||
bin/format/ |
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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module.exports = { | ||
modelsContent: function (item) { | ||
return ` | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
const ${item}Schema = new Schema({}); // Write your schema here | ||
const ${item} = mongoose.model('${item}', ${item}Schema); | ||
module.exports = ${item}; | ||
`; | ||
}, | ||
|
||
viewsContent: function (item) { | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>${item}</title> | ||
</head> | ||
<body> | ||
</body> | ||
</html> | ||
`; | ||
}, | ||
|
||
controllerContent: function (functions_array) { | ||
return `module.exports = {\n${functions_array | ||
.map((item) => `${item}: function() {}, // Add function logic here`) | ||
.join("\n")}\n};`; | ||
}, | ||
|
||
routesContent: function (config) { | ||
let base_content = `const router = require('express').Router();\n\n`; | ||
|
||
Object.entries(config).forEach(([type, routes]) => { | ||
routes.forEach((route) => { | ||
base_content += `router.${type}('${route}', (req, res) => {}); // Add your route logic here\n`; | ||
}); | ||
}); | ||
|
||
base_content += `\nmodule.exports = router;`; | ||
return base_content; | ||
}, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const fs = require("fs"); | ||
const util = require("util"); | ||
|
||
const fsWritePromisified = util.promisify(fs.writeFile); | ||
|
||
const errorHandler = (err) => console.log("\x1b[31m", err); | ||
const successHandler = (fileName) => () => | ||
console.log("\x1b[32m", `Generated file ${fileName}`); | ||
|
||
module.exports = { | ||
makeModels: function (models) { | ||
return models.map((item) => | ||
fsWritePromisified( | ||
`./models/${item}.js`, | ||
` | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
const ${item}Schema = new Schema({}); // Write your schema here | ||
const ${item} = mongoose.model('${item}', ${item}Schema); | ||
module.exports = ${item}; | ||
` | ||
) | ||
.then(successHandler(`${item}.js`)) | ||
.catch(errorHandler) | ||
); | ||
}, | ||
|
||
makeViews: function (views) { | ||
return views.map((item) => | ||
fsWritePromisified( | ||
`./views/${item}.html`, | ||
` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>${item}</title> | ||
</head> | ||
<body> | ||
</body> | ||
</html> | ||
` | ||
) | ||
.then(successHandler(`${item}.html`)) | ||
.catch(errorHandler) | ||
); | ||
}, | ||
|
||
makeControllers: function (controllers) { | ||
return Object.entries(controllers).map(([fileName, methods]) => | ||
fsWritePromisified( | ||
`./controllers/${fileName}.js`, | ||
` | ||
module.exports = { | ||
${methods | ||
.map((method) => `${method}: function() {}, // Add function logic here`) | ||
.join("\n")} | ||
}; | ||
` | ||
) | ||
.then(successHandler(`${fileName}.js`)) | ||
.catch(errorHandler) | ||
); | ||
}, | ||
|
||
makeRoutes: function (routes) { | ||
return Object.entries(routes).map(([fileName, methods]) => | ||
fsWritePromisified( | ||
`./routes/${fileName}.js`, | ||
` | ||
const router = require('express').Router(); | ||
${Object.entries(methods) | ||
.map(([methodType, paths]) => | ||
paths | ||
.map( | ||
(path) => | ||
`router.${methodType}('${path}', (req, res) => {}); // Add your route logic here` | ||
) | ||
.join("\n") | ||
) | ||
.join("\n")} | ||
module.exports = router; | ||
` | ||
) | ||
.then(successHandler(`${fileName}.js`)) | ||
.catch(errorHandler) | ||
); | ||
}, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,152 @@ | ||
const fs = require("fs"); | ||
const yaml = require("js-yaml"); | ||
const utils = require("./utils"); | ||
const chalk = require("chalk"); | ||
|
||
function generateFromConfig(configFileName) { | ||
const configPath = `${process.cwd()}/${configFileName}`; | ||
|
||
// Check if the config file exists | ||
if (!fs.existsSync(configPath)) { | ||
console.error(`Configuration file "${configFileName}" not found.`); | ||
process.exit(1); | ||
} | ||
|
||
let config; | ||
try { | ||
const fileContents = fs.readFileSync(configPath, "utf8"); | ||
config = yaml.load(fileContents); | ||
} catch (e) { | ||
console.error(`Error reading or parsing "${configFileName}": ${e.message}`); | ||
process.exit(1); | ||
} | ||
|
||
createProjectStructure(config); | ||
|
||
console.log( | ||
chalk.blue("\n------ Happy Coding with Universal-Box 🚀 ------\n") | ||
); | ||
} | ||
|
||
function createProjectStructure(config) { | ||
const { models, controllers, views, routes } = config; | ||
console.log(chalk.blue("Creating project structure...")); | ||
if (models) { | ||
utils | ||
.makeFolders(["models"]) | ||
.then(() => { | ||
return Promise.all( | ||
models.map((model) => { | ||
return fs.promises.writeFile( | ||
`models/${model}.js`, | ||
` | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
const ${model}Schema = new Schema({}); // Write your schema here | ||
const ${model} = mongoose.model('${model}', ${model}Schema); | ||
module.exports = ${model}; | ||
` | ||
); | ||
}) | ||
); | ||
}) | ||
.then(() => | ||
console.log(chalk.yellow("\x1b[32m", "✅ Models generated")) | ||
) | ||
.catch((err) => console.error(err)); | ||
} | ||
|
||
if (views) { | ||
utils | ||
.makeFolders(["views"]) | ||
.then(() => { | ||
return Promise.all( | ||
views.map((view) => { | ||
return fs.promises.writeFile( | ||
`views/${view}.html`, | ||
` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>${view}</title> | ||
</head> | ||
<body> | ||
</body> | ||
</html> | ||
` | ||
); | ||
}) | ||
); | ||
}) | ||
.then(() => | ||
console.log(chalk.yellow("\x1b[32m", "✅ Views generated")) | ||
) | ||
.catch((err) => console.error(err)); | ||
} | ||
|
||
if (controllers) { | ||
utils | ||
.makeFolders(["controllers"]) | ||
.then(() => { | ||
return Promise.all( | ||
Object.entries(controllers).map(([fileName, methods]) => { | ||
const content = ` | ||
module.exports = { | ||
${methods | ||
.map((method) => `${method}: function() {}, // Add function logic here`) | ||
.join("\n")} | ||
}; | ||
`; | ||
return fs.promises.writeFile(`controllers/${fileName}.js`, content); | ||
}) | ||
); | ||
}) | ||
.then(() => | ||
console.log( | ||
chalk.yellow("\x1b[32m", "✅ Controllers generated") | ||
) | ||
) | ||
.catch((err) => console.error(err)); | ||
} | ||
|
||
if (routes) { | ||
utils | ||
.makeFolders(["routes"]) | ||
.then(() => { | ||
return Promise.all( | ||
Object.entries(routes).map(([fileName, methods]) => { | ||
const content = ` | ||
const router = require('express').Router(); | ||
${Object.entries(methods) | ||
.map(([methodType, paths]) => | ||
paths | ||
.map( | ||
(path) => | ||
`router.${methodType}('${path}', (req, res) => {}); // Add your route logic here` | ||
) | ||
.join("\n") | ||
) | ||
.join("\n")} | ||
module.exports = router; | ||
`; | ||
return fs.promises.writeFile(`routes/${fileName}.js`, content); | ||
}) | ||
); | ||
}) | ||
.then(() => | ||
console.log(chalk.yellow("\x1b[32m", "✅ Routes generated")) | ||
) | ||
.catch((err) => console.error(err)); | ||
} | ||
} | ||
|
||
module.exports = { generateFromConfig }; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const fs = require("fs"); | ||
var exec = require("child_process").exec; | ||
const chalk = require("chalk"); | ||
|
||
module.exports = { | ||
makeFolders: function (array_list) { | ||
const supportedFolders = ["models", "views", "controllers", "routes"]; | ||
|
||
return new Promise((resolve, reject) => { | ||
array_list.forEach((folder) => { | ||
if (supportedFolders.includes(folder)) { | ||
if (!fs.existsSync(`./${folder}`)) { | ||
fs.mkdirSync(`./${folder}`); | ||
console.log(chalk.yellow(`Directory Created -> ./${folder}`)); | ||
} | ||
} else { | ||
console.log("\x1b[31m", `NO SUPPORT FOR DIRECTORY ./${folder}`); | ||
reject({ | ||
success: false, | ||
message: "No support for entered directory!", | ||
}); | ||
} | ||
}); | ||
|
||
resolve({ success: true, message: "Created folders successfully" }); | ||
}); | ||
}, | ||
}; |
Oops, something went wrong.