diff --git a/.jshintignore b/.jshintignore index e33d376f7..25e3bab0a 100644 --- a/.jshintignore +++ b/.jshintignore @@ -1,3 +1,3 @@ node_modules -lib/files/routes/* -lib/utils/createFile.js \ No newline at end of file +lib/templates/routes/* +lib/utils/createFile.js diff --git a/lib/commands/codesplit.js b/lib/commands/codesplit.js index bc1494680..a49d5b218 100644 --- a/lib/commands/codesplit.js +++ b/lib/commands/codesplit.js @@ -5,48 +5,39 @@ const chalk = require('chalk'); const shell = require('shelljs'); const { showBanner } = require('../external/banner'); +const { configFileExists } = require('../utils/messages'); exports.asyncRender = (componentName) => { showBanner(); - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } - - let data = fs.readFileSync('./mevn.json', 'utf8'); - let appname = JSON.parse(data); - shell.cd(appname.project_name); - shell.cd('client'); - shell.cd('src'); - shell.cd('router'); - - let componentNotFound = true; - let routerFile = fs.readFileSync('./index.js', 'utf8').toString().split('\n'); - setTimeout(() => { - - for(let index = 0; index < routerFile.length; index++){ - // Checking whether the routes file already has the respective component - if(routerFile[index].includes(`${componentName}`)){ - // Validating whether the respective component is already imported dynamically - if(routerFile[index].includes(`import ${componentName}`)){ - routerFile[index] = `const ${componentName} = () => import('@/components/${componentName}')`; - componentNotFound = false; - break; - } else{ - console.log(chalk.redBright(`\n ${componentName} component is already imported dynamically!`)); - process.exit(1); + configFileExists(); + + shell.cd('client/src/router'); + + let componentNotFound = true; + let routerFile = fs.readFileSync('./index.js', 'utf8').toString().split('\n'); + for (let index = 0; index < routerFile.length; index++) { + // Checking whether the routes file already has the respective component + if(routerFile[index].includes(`${componentName}`)){ + // Validating whether the respective component is already imported dynamically + if(routerFile[index].includes(`import ${componentName}`)){ + routerFile[index] = `const ${componentName} = () => import('@/components/${componentName}')`; + componentNotFound = false; + break; + } else { + console.log(chalk.redBright(`\n ${componentName} component is already imported dynamically!`)); + process.exit(1); } } } - if(!componentNotFound){ - fs.writeFile('./index.js', routerFile.join('\n'), (err) => { - if(err){ - throw err; - } - }); - } else{ + if (!componentNotFound) { + fs.writeFile('./index.js', routerFile.join('\n'), (err) => { + if (err) { + throw err; + } + }); + } else { console.log(chalk.yellowBright('\n Make sure that the component exists!')); process.exit(1); } diff --git a/lib/commands/component.js b/lib/commands/component.js index 2f18588a9..741c6da7f 100644 --- a/lib/commands/component.js +++ b/lib/commands/component.js @@ -6,26 +6,17 @@ const chalk = require('chalk'); const createFile = require('../utils/createFile'); const { showBanner } = require('../external/banner'); +const { configFileExists } = require('../utils/messages'); let componentsFile = fs.readFileSync(__dirname + '/../templates/components/component.vue', 'utf8'); exports.createComponent = (componentName) => { showBanner(); - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } - - let data = fs.readFileSync('./mevn.json', 'utf8'); - let appname = JSON.parse(data); - shell.cd(appname.project_name); - shell.cd('client'); - shell.cd('src'); - shell.cd('components'); - setTimeout(() => { + configFileExists(); + shell.cd('client/src/components'); createFile(componentName + '.vue', componentsFile, { flag: 'wx' }, (err) => { if (err) throw err; console.log(chalk.green('\n File Created...!')); diff --git a/lib/commands/createConfig.js b/lib/commands/createConfig.js index 7add35ddf..7a1d9d706 100644 --- a/lib/commands/createConfig.js +++ b/lib/commands/createConfig.js @@ -1,34 +1,36 @@ 'use strict'; -const fs = require('fs'); const shell = require('shelljs'); const inquirer = require('inquirer'); const chalk = require('chalk'); const createFile = require('../utils/createFile'); +const { showBanner } = require('../external/banner'); +const { configFileExists } = require('../utils/messages'); + exports.generateConfig = () => { - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } + showBanner(); + configFileExists(); - let data = fs.readFileSync('./mevn.json', 'utf8'); - let appname = JSON.parse(data); - shell.cd(appname.project_name); - shell.cd('server'); - shell.cd('config'); + setTimeout(() => { + shell.cd('server/config'); - inquirer.prompt([{ - name: 'db', - type: 'input', - message: 'Enter the url for the database : ', - }]).then((answers) => { + inquirer.prompt([{ + name: 'db', + type: 'input', + message: 'Enter the url for the database : ', + }]).then((answer) => { - const content = 'module.exports = {\n \'url\': "' + answers.db + '"\n}'; + const configFileContent = [ + '{', + ` "url": "${answer.db}"`, + '}' + ]; - createFile('./config.js', content, { flag: 'wx' }, (err) => { - if (err) throw err; - console.log(chalk.yellow('File Created...!')); + createFile('./config.js', configFileContent.join('\n').toString(), { flag: 'wx' }, (err) => { + if (err) throw err; + console.log(chalk.yellow('File Created...!')); + }); }); - }); + }, 200); }; diff --git a/lib/commands/createController.js b/lib/commands/createController.js index f3f086fc1..bda54f914 100644 --- a/lib/commands/createController.js +++ b/lib/commands/createController.js @@ -6,28 +6,26 @@ const os = require('os'); const chalk = require('chalk'); const createFile = require('../utils/createFile'); +const { showBanner } = require('../external/banner'); +const { configFileExists } = require('../utils/messages'); + let controllersFile = fs.readFileSync(__dirname + '/../templates/controllers/user_controller.js', 'utf8'); exports.generateController = () => { - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } + showBanner(); + configFileExists(); - let data = fs.readFileSync('./mevn.json', 'utf8'); - let appname = JSON.parse(data); - shell.cd(appname.project_name); - shell.cd('server'); - shell.cd('controllers'); + setTimeout(() => { + shell.cd('server/controllers'); - if(os.type() === 'Linux' || os.type() === 'darwin'){ - shell.exec('rm default.js', {silent: true}, () => {}); - } else{ - shell.exec('del default.js', {silent: true}, () => {}); - } + let removeCmd = os.type() === 'Windows_NT' ? 'del' : 'rm'; + if (fs.existsSync('./default.js')) { + shell.exec(`${removeCmd} default.js`); + } - createFile('./user_controller.js', controllersFile, { flag: 'wx' }, (err) => { - if (err) throw err; - console.log(chalk.yellow('File Created...!')); - }); + createFile('./user_controller.js', controllersFile, { flag: 'wx' }, (err) => { + if (err) throw err; + console.log(chalk.yellow('File Created...!')); + }); + }, 200); }; diff --git a/lib/commands/createModel.js b/lib/commands/createModel.js index 6eecc4cc1..e2bd7fb7e 100644 --- a/lib/commands/createModel.js +++ b/lib/commands/createModel.js @@ -6,28 +6,26 @@ const chalk = require('chalk'); const os = require('os'); const createFile = require('../utils/createFile'); +const { showBanner } = require('../external/banner'); +const { configFileExists } = require('../utils/messages'); + let userSchema = fs.readFileSync(__dirname + '/../templates/models/user_schema.js', 'utf8'); exports.generateModel = () => { - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } - - let data = fs.readFileSync('./mevn.json', 'utf8'); - let appname = JSON.parse(data); - shell.cd(appname.project_name); - shell.cd('server'); - shell.cd('models'); + showBanner(); + configFileExists(); - if(os.type() === 'Linux' || os.type() === 'darwin'){ - shell.exec('rm default.js', {silent: true}, () => {}); - } else{ - shell.exec('del default.js', {silent: true}, () => {}); - } + setTimeout(() => { + shell.cd('server/models'); - createFile('./user_schema.js', userSchema, { flag: 'wx' }, (err) => { - if (err) throw err; - console.log(chalk.yellow('File Created...!')); - }); + let removeCmd = os.type() === 'Windows_NT' ? 'del' : 'rm'; + if (fs.existsSync('./default.js')) { + shell.exec(`${removeCmd} default.js`); + } + + createFile('./user_schema.js', userSchema, { flag: 'wx' }, (err) => { + if (err) throw err; + console.log(chalk.yellow('File Created...!')); + }); + }, 200); }; diff --git a/lib/commands/createRoute.js b/lib/commands/createRoute.js index 1d6cf7cd3..e9c8ec65c 100644 --- a/lib/commands/createRoute.js +++ b/lib/commands/createRoute.js @@ -5,11 +5,13 @@ const shell = require('shelljs'); const inquirer = require('inquirer'); const chalk = require('chalk'); const createFile = require('../utils/createFile'); -const { showBanner } = require('../external/banner'); const logUpdate = require('log-update'); const elegantSpinner = require('elegant-spinner'); const cmd = require('node-cmd'); +const { showBanner } = require('../external/banner'); +const { configFileExists } = require('../utils/messages'); + let routesPath = '/../templates/routes/'; let routesFile = fs.readFileSync(__dirname + '/../templates/routes/index.js', 'utf8'); let routesFileWithPassPort = fs.readFileSync(__dirname + '/../templates/routes/index_with_passport.js', 'utf8'); @@ -100,17 +102,9 @@ exports.generateRoute = () => { showBanner(); setTimeout(() => { + configFileExists(); console.log('\n'); - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } - - let data = fs.readFileSync('./mevn.json', 'utf8'); - let appname = JSON.parse(data); - shell.cd(appname.project_name); - inquirer.prompt(questions) .then(answer => { if (answer.passportAuth) { diff --git a/lib/commands/init.js b/lib/commands/init.js index 49d5a1336..9734c6fb0 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -18,9 +18,9 @@ let generalCommands = new Table(); let frame = elegantSpinner(); let projectName; +let projectConfig; let showTables = () => { - console.log(chalk.yellow('server commands:-')); serverCommands.push({ @@ -29,10 +29,6 @@ let showTables = () => { 'mevn create:route' : 'To create routes file' }, { 'mevn create:controller': 'To create controllers file' - }, { - 'mevn create:component': 'To create components file' - },{ - 'mevn add:package': 'To add a package to the project' }, { 'mevn create:config': 'To create config file' }); @@ -41,29 +37,29 @@ let showTables = () => { console.log(chalk.yellow('\ncommands to run:-')); generalCommands.push({ - 'mevn version': 'Shows the current version and additional info' + 'mevn version': 'Current CLI version' }, { 'mevn run:server': 'To run server' }, { 'mevn run:client': 'To run client' }, { - 'mevn add:package': 'To add additional packages as required' + 'mevn add:package': 'Add additional packages' }, { - 'mevn create:component ': 'To create new components as required' + 'mevn create:component ': 'Create new components' }, { - 'mevn codesplit ': 'To lazy load components as required' + 'mevn codesplit ': 'Lazy load components' }, { - 'mevn create:git-repo': 'To create a GitHub repository and fire the first commit' + 'mevn create:git-repo': 'Create a GitHub Repo' }, { - 'mevn dockerize': 'To run the client and server in separate docker containers' + 'mevn dockerize': 'Launch within docker containers' }, { - 'mevn deploy': 'To deploy the app to Heroku' + 'mevn deploy': 'Deploy the app to Heroku' }); console.log(generalCommands.toString()); - console.log(chalk.redBright('\n\nwarning:')); + console.log(chalk.cyanBright(`\n\n Make sure that you've done cd ${projectName}`)); + console.log(chalk.redBright('\nwarning:')); console.log('Do not delete mevn.json file'); - }; let fetchTemplate = (template) => { @@ -78,7 +74,6 @@ let fetchTemplate = (template) => { }, 50); setTimeout(() =>{ - console.log('\n'); clearInterval(fetchSpinner); logUpdate.clear(); @@ -86,13 +81,10 @@ let fetchTemplate = (template) => { }, 5000); - if (os.type() === 'Linux'){ - shell.exec(`mv ${templateDir} ` + projectName); - } else if (os.type() === 'Windows_NT'){ - shell.exec(`ren ${templateDir} ` + projectName); - } else if (os.type() === 'Darwin'){ - shell.exec(`mv ${templateDir} ` + projectName); - } + let renameCmd = os.type() === 'Windows_NT' ? 'ren' : 'mv'; + shell.exec(`${renameCmd} ${templateDir} ${projectName}`); + + fs.writeFileSync(`./${projectName}/mevn.json`, projectConfig.join('\n').toString()); if (template === 'nuxt') { setTimeout(() =>{ @@ -107,18 +99,14 @@ let fetchTemplate = (template) => { }]) .then((choice) => { if (choice.mode === 'Universal') { - let configFile = fs.readFileSync(`./${projectName}/nuxt.config.js`, 'utf8').toString().split('\n'); - let index = configFile.indexOf(configFile.find(line => line.includes('mode'))); configFile[index] = ` mode: 'universal',`; - fs.writeFileSync(`./${projectName}/nuxt.config.js`, configFile.join('\n')); + fs.writeFileSync(`./${projectName}/nuxt.config.js`, configFile.join('\n')); } showTables(); }); - - }, 5000); } }; @@ -154,11 +142,6 @@ exports.initializeProject = (appName) => { process.exit(1); } - if (fs.existsSync('./mevn.json')) { - console.log(`${chalk.yellow.bold(`\n mevn.json file already exists in path!`)}`); - process.exit(1); - } - clearInterval(initialSpinner); logUpdate.clear(); projectName = appName; @@ -171,14 +154,13 @@ exports.initializeProject = (appName) => { }]) .then((choice) => { - const projectConfig = [ + projectConfig = [ '{', - `"project_name": "${appName}",`, + `"name": "${appName}",`, `"template": "${choice.template}"`, '}' ]; - fs.writeFileSync('./mevn.json', projectConfig.join('\n').toString()); if (choice.template === 'Nuxt-js') { choice.template = 'nuxt'; } diff --git a/lib/commands/package.js b/lib/commands/package.js index 2f38fb27b..aeb34b677 100644 --- a/lib/commands/package.js +++ b/lib/commands/package.js @@ -7,7 +7,9 @@ const inquirer = require('inquirer'); const chalk = require('chalk'); const logUpdate = require('log-update'); const elegantSpinner = require('elegant-spinner'); + const { showBanner } = require('../external/banner'); +const { configFileExists } = require('../utils/messages'); let storeFile = fs.readFileSync(__dirname + '/../templates/vuex/store.js', 'utf8'); let frame = elegantSpinner(); @@ -15,28 +17,19 @@ let frame = elegantSpinner(); exports.addPackage = () => { showBanner(); - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } - setTimeout(() => { + configFileExists(); + console.log('\n'); + let questions = [ + { + type: 'list', + name: 'packages', + message: 'Which package do you want to install?', + choices: ['vee-validate', 'axios', 'vuex', 'vuetify'] + } + ]; - console.log('\n'); - let questions = [ - { - type: 'list', - name: 'packages', - message: 'Which package do you want to install?', - choices: ['vee-validate', 'axios', 'vuex', 'vuetify'] - } - ]; - - let data = fs.readFileSync('./mevn.json', 'utf8'); - let appname = JSON.parse(data); - shell.cd(appname.project_name); shell.cd('client'); - inquirer.prompt(questions) .then(answers => { diff --git a/lib/deploy/docker.js b/lib/deploy/docker.js index 5dbdb8db2..381fea58c 100644 --- a/lib/deploy/docker.js +++ b/lib/deploy/docker.js @@ -1,27 +1,20 @@ 'use strict'; -const { showBanner } = require('../external/banner'); -const fs = require('fs'); const chalk = require('chalk'); const shell = require('shelljs'); const os = require('os'); +const { showBanner } = require('../external/banner'); +const { configFileExists } = require('../utils/messages'); + exports.dockerize = () => { showBanner(); - - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } - - let data = fs.readFileSync('./mevn.json', 'utf8'); - let appname = JSON.parse(data); - - shell.cd(appname.project_name); setTimeout(() => { + configFileExists(); + // currently works on linux only - if(os.type() + '' === 'Linux') { + if(os.type() === 'Linux') { console.log('\n'); shell.exec('sudo docker-compose up', (err) => { diff --git a/lib/deploy/gitRepo.js b/lib/deploy/gitRepo.js index 7d0349bd3..4507b0cc6 100644 --- a/lib/deploy/gitRepo.js +++ b/lib/deploy/gitRepo.js @@ -1,27 +1,26 @@ 'use strict'; -const fs = require('fs'); const shell = require('shelljs'); const inquirer = require('inquirer'); -const chalk = require('chalk'); const { showBanner } = require('../external/banner'); +const { configFileExists } = require('../utils/messages'); +const { appData } = require('../utils/projectConfig'); + +let projectName; let deleteCommand; // Delete .git based on the platform exports.createRepo = () => { showBanner(); - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } + setTimeout(() => { + configFileExists(); - let data = fs.readFileSync('./mevn.json', 'utf8'); - let appname = JSON.parse(data); - shell.cd(appname.project_name); + appData(). + then((data) => { + projectName = data.projectName; + }); - setTimeout(() => { - console.log('Create repo'); if (!shell.which('git')) { shell.echo('Sorry, git is not installed on your system, Do you want to install git?'); inquirer.prompt([{ @@ -72,7 +71,6 @@ exports.createRepo = () => { }); } else{ - // stage 1 console.log('\ncreating github repository'); inquirer.prompt([{ name: 'username', @@ -81,7 +79,7 @@ exports.createRepo = () => { }]).then((answers) => { - shell.exec('curl -u \'' + answers.username + '\' https://api.github.com/user/repos -d \'{"name":"' + appname.project_name + '"}\'', (err) => { + shell.exec('curl -u \'' + answers.username + '\' https://api.github.com/user/repos -d \'{"name":"' + projectName + '"}\'', (err) => { if(err) { throw err; } @@ -106,12 +104,11 @@ exports.createRepo = () => { if(err) { throw err; } - shell.exec('git remote add origin https://github.com/' + answers.username + '/' + appname.project_name + '.git', (err) => { + shell.exec('git remote add origin https://github.com/' + answers.username + '/' + projectName + '.git', (err) => { if(err) { throw err; } }); - //stage 2 console.log('\npushing local files to your github account'); shell.exec('git push origin master', (err) => { if(err) { diff --git a/lib/deploy/herokuDeploy.js b/lib/deploy/herokuDeploy.js index a8ca9aab2..ae272f873 100644 --- a/lib/deploy/herokuDeploy.js +++ b/lib/deploy/herokuDeploy.js @@ -1,27 +1,19 @@ 'use strict'; -const fs = require('fs'); const shell = require('shelljs'); const inquirer = require('inquirer'); -const chalk = require('chalk'); const { showBanner } = require('../external/banner'); +const { configFileExists } = require('../utils/messages'); exports.deploy = () => { showBanner(); - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } - - let data = fs.readFileSync('./mevn.json', 'utf8'); - let appname = JSON.parse(data); - shell.cd(appname.project_name); - let appdir = process.cwd() + ''; - shell.cd('client'); - setTimeout(() => { + configFileExists(); + + let appDir = process.cwd(); + shell.cd('client'); shell.exec('npm install', (err) => { if(err) { @@ -33,12 +25,12 @@ exports.deploy = () => { throw err; } - shell.exec('sudo cp -a /' + process.cwd() + '/dist /' + appdir + '/server', (err) => { + shell.exec('sudo cp -a /' + process.cwd() + '/dist /' + appDir + '/server', (err) => { if(err) { throw err; } - shell.exec('cd ' + appdir + '/server', (err) => { + shell.exec('cd ' + appDir + '/server', (err) => { if(err) { throw err; } diff --git a/lib/run/client.js b/lib/run/client.js index aa308b00d..9ea6d7f13 100644 --- a/lib/run/client.js +++ b/lib/run/client.js @@ -1,9 +1,6 @@ 'use strict'; -const fs = require('fs'); const shell = require('shelljs'); -const chalk = require('chalk'); -const process = require('process'); const { setupProject } = require('./launch'); const { appData } = require('../utils/projectConfig'); @@ -11,14 +8,9 @@ const { showBanner } = require('../external/banner'); exports.setupClient = () => { showBanner(); - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } appData() .then((data) => { - shell.cd(data.project_name); if (data.template !== 'Nuxt-js') { shell.cd('client'); } diff --git a/lib/run/server.js b/lib/run/server.js index 8b7200295..74d8e3843 100644 --- a/lib/run/server.js +++ b/lib/run/server.js @@ -1,25 +1,15 @@ 'use strict'; -const fs = require('fs'); const shell = require('shelljs'); -const chalk = require('chalk'); -const process = require('process'); const { setupProject } = require('./launch'); -const { appData } = require('../utils/projectConfig'); const { showBanner } = require('../external/banner'); +const { configFileExists } = require('../utils/messages'); exports.setupServer = () => { showBanner(); - if(!fs.existsSync('./mevn.json')){ - console.log(chalk.red.bold('\n Make sure that you are having a valid MEVN stack project in path')); - process.exit(1); - } + configFileExists(); - appData() - .then((data) => { - shell.cd(data.project_name); - shell.cd('server'); - setupProject('npm start'); - }); + shell.cd('server'); + setupProject('npm start'); }; diff --git a/lib/utils/messages.js b/lib/utils/messages.js new file mode 100644 index 000000000..bb9db45e7 --- /dev/null +++ b/lib/utils/messages.js @@ -0,0 +1,13 @@ +'use strict'; + +const chalk = require('chalk'); +const fs = require('fs'); + +exports.configFileExists = () => { + if (!fs.existsSync('./mevn.json')) { + console.log(chalk.cyanBright(`\n\n Make sure that you're within a valid MEVN project + \n${chalk.redBright('Error:')} No mevn.json file found + `)); + process.exit(1); + } +}; diff --git a/lib/utils/projectConfig.js b/lib/utils/projectConfig.js index 4860b6667..4dbc22b72 100644 --- a/lib/utils/projectConfig.js +++ b/lib/utils/projectConfig.js @@ -2,7 +2,10 @@ const fs = require('fs'); +const { configFileExists } = require('../utils/messages'); + exports.appData = () => { + configFileExists(); const data = fs.readFileSync(process.cwd() + '/mevn.json', 'utf8'); return new Promise((resolve) => { resolve(JSON.parse(data)); diff --git a/package.json b/package.json index 13e8df534..7ecc9b40f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mevn-cli", - "version": "1.0.5", + "version": "1.0.8", "description": "A cli tool for MEVN stack.", "main": "index.js", "directories": {