wayhigh
Version:
A simple Node.js package that helps you install express and other dependency packages for a simple express api with a defined folder structrure
118 lines (101 loc) • 3.66 kB
JavaScript
const express = require("express");
// import fs from 'fs';
// import path from 'path';
// import { execSync } from 'child_process';
const fs = require("fs");
const path = require("path");
const execSync = require("child_process").execSync;
// Check if the required packages are installed, and install if necessary
const requiredPackages = ["express", "fs", "path"];
const installPackages = () => {
requiredPackages.forEach((pkg) => {
try {
require.resolve(pkg); // Try to resolve the package
} catch (e) {
console.log(`Package '${pkg}' not found, installing...`);
execSync(`npm install ${pkg}`); // If not found, install the package
}
});
};
// Run the package installer
installPackages();
const createDir = (dirPath,dirname) => {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
console.log(`${dirname} folder created`);
} else {
console.log(`${dirname} folder already exists`);
}
}
// Run this at the startup of the app
const utilsPath = path.join(__dirname, "utils");
createDir(utilsPath,'utils');
const servicesPath = path.join(__dirname, "services");
createDir(servicesPath,'services');
const configPath = path.join(__dirname, "config");
createDir(configPath,'config');
// Create the storage folder path
const loggingPath = path.join(__dirname, "storage/logs");
createDir(loggingPath,'storage');
const publicPath = path.join(__dirname, "public");
createDir(publicPath,'public');
const routesPath = path.join(__dirname, "routes/v1");
createDir(routesPath,'routes');
const seedersPath = path.join(__dirname, "database/seeders");
createDir(seedersPath,'seeders');
const migrationsPath = path.join(__dirname, "database/migrations");
createDir(migrationsPath,'migrations');
const controllersPath = path.join(__dirname, "controllers");
createDir( controllersPath,'controllers');
const middlewaresPath = path.join(__dirname, "middlewares");
createDir( middlewaresPath,'middlewares');
const validationsPath = path.join(__dirname, "validations");
createDir( validationsPath,'validations');
const modelsPath = path.join(__dirname, "models");
createDir( modelsPath,'validations');
// Helper function to create files with content
const createFile = (filePath, content) => {
if (!fs.existsSync(filePath)) {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(filePath, content);
console.log(`File created at ${filePath}`);
} else {
console.log(`File already exists at ${filePath}`);
}
};
// Create routes/v1/index.js
const routesIndexPath = path.join(__dirname, 'routes/v1/index.js');
const routesIndexContent = `const express = require('express');
const router = express.Router();
// Define your routes here
router.get('/example', (req, res) => {
res.send('Hello from v1 example route!');
});
module.exports = router;
`;
createFile(routesIndexPath, routesIndexContent);
// Create server.js
const serverPath = path.join(__dirname, 'server.js');
const serverContent = `const express = require('express');
const app = express();
const v1Routes = require('./routes/v1');
// Middleware
app.use(express.json());
// Initialize v1 routes
app.use('/api/v1', v1Routes);
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(\`Server is running on port \${PORT}\`);
});
`;
createFile(serverPath, serverContent);
// Create an Express app
const app = express();
// Define a simple route
app.get("/", (req, res) => {
res.send("Storage folder management in Express with Bootstrap");
});