UNPKG

node-express-api-template

Version:

A productive and scalable nodejs api template for developers

50 lines (38 loc) 1.31 kB
#!/usr/bin/env node const { spawnSync } = require('child_process'); const fs = require('fs'); const path = require('path'); const templatePath = path.resolve(__dirname, 'src'); const destinationPath = path.join(process.cwd(), process.argv[2]); const copyFolderRecursive = (source, destination) => { if (!fs.existsSync(destination)) { fs.mkdirSync(destination); } const files = fs.readdirSync(source); files.forEach((file) => { const sourceFile = path.join(source, file); const destFile = path.join(destination, file); if (fs.lstatSync(sourceFile).isDirectory()) { copyFolderRecursive(sourceFile, destFile); } else { fs.copyFileSync(sourceFile, destFile); } }); }; const createProject = () => { try { copyFolderRecursive(templatePath, destinationPath); console.log('Project structure created successfully!'); console.log('Installing dependencies...'); const npmInstall = spawnSync('npm', ['install'], { cwd: destinationPath, stdio: 'inherit' }); if (npmInstall.error) { throw npmInstall.error; } console.log('Dependencies installed successfully!'); console.log('Your project is ready to go.'); } catch (error) { console.error('Error while creating project:', error); process.exit(1); } }; createProject();