xpress-mvc
Version:
An MVC framework based on Express.js
121 lines (95 loc) • 4.13 kB
JavaScript
const path = require("path");
const { spawn } = require("child_process");
const { renameDirectory,mergeDirectory, readJson,removeDirectory,removeDependency } = require('./functions');
const { askORMChoice, askDatabaseChoice } = require("./questions");
const fs = require("fs-extra");
const config = require("./config");
const repoUrl = "https://github.com/Anxiety471/expressjs-api-scaffholding.git"
const secondStep = async (projectName) => {
const repoName = path.basename(repoUrl, ".git");
const gitClone = spawn("git", ["clone",'--branch','base' ,repoUrl,'--single-branch'], { stdio: "inherit" });
gitClone.on("close", async (code) => {
if (code === 0) {
console.log(`✅ Successfully cloned ${repoName}`);
await renameDirectory(repoName, projectName);
await thirdStep(projectName)
} else {
console.log(`❌ Cloning failed with code ${code}`);
}
});
};
const thirdStep = async (projectName) => {
const branch = await askORMChoice()
await askDatabaseChoice()
const repoName = path.basename(repoUrl, ".git");
const gitClone = spawn("git", ["clone",'--branch',branch ,repoUrl,'--single-branch'], { stdio: "inherit" });
gitClone.on("close", async (code) => {
if (code === 0) {
const result = await mergeDirectory(repoName, projectName);
if(result) await fourthStep(projectName)
} else {
console.log(`❌ Cloning failed with code ${code}`);
}
});
}
const fourthStep = async (projectName) => {
return new Promise( async (resolve,reject) => {
try {
process.chdir(projectName);
const package = await readJson()
package.name = projectName
await fs.writeJSON('package.json',package,{ spaces : 2 })
if (config.ormName === 'ObjectionKnex'){
process.chdir('Database')
if(config.database === 'sqlite') await fs.writeFile(path.join(process.cwd(),'database.sqlite3'),'',{flag: 'w'})
let configData = await fs.readFile(path.join(process.cwd(),'config.js'),'utf-8')
const regex = /development:\s*\{([\s\S]*?)\n\s*\},/;
configData = configData.replace(regex,config.development)
await fs.writeFile(path.join(process.cwd(),'config.js'),configData)
process.chdir('..')
}
if(config.ormName === 'sequelize'){
process.chdir('Database')
if(config.database === 'sqlite') await fs.writeFile(path.join(process.cwd(),'database.sqlite3'),'',{flag: 'w'})
let configData = await fs.readFile(path.join(process.cwd(),'config.js'),'utf-8')
const regex = /development:\s*\{([\s\S]*?)\n\s*\},/;
configData = configData.replace(regex,config.development)
await fs.writeFile(path.join(process.cwd(),'config.js'),configData)
process.chdir('..')
}
await lastStep()
resolve(true)
} catch (error) {
reject(new Error('Error on fourth step: ' + error))
}
})
}
const lastStep = async() => {
return new Promise( async (resolve,reject) => {
const dependency = spawn("npm", ["install",...config.orm,config.databaseDriver], {
stdio: "inherit",
shell: true,
})
dependency.on('close',async (code) => {
if (code !== 0) {
console.log('Error on installing dependency: ' + code)
}
const finalInstall = spawn("npm", ["install"], {stdio: "inherit",shell: true,})
await removeDirectory('.git')
finalInstall.on('close', async () => {
process.chdir('..')
await removeDependency()
console.log(`cd ${config.name}`)
console.log('run npx nodemon to start the server')
process.exit(0)
})
resolve(true)
})
dependency.on('error', async (error) => {
console.log('Spawn Error: ' + error.message)
})
})
}
module.exports = {
secondStep,thirdStep,fourthStep,lastStep
}