#! /usr/bin/env node 'use strict' const a = process.argv[2] const command = { postinstall, initMin, init, initMongo } if (!a || Object.keys(command).indexOf(a) < 0) return console.error('Need to supply an argument, either of:',Object.keys(command)) /** * Dependencies & variables */ const path = require('path') require('shelljs/global') const templatePath = path.resolve(__dirname,'../template') +'/' /** The command */ command[a]() function postinstall () { //path.resolve('') -> the node_modules/mwb because this script run within mwb try { var tmp = require(path.resolve('../../package.json')) } catch (err) { console.error(err) return } if (!tmp.scripts) tmp.scripts = {} if (tmp.scripts.mwb) return //has been installed already tmp.scripts.mwb = 'mwb' tmp.private = true //your app shouldn't be published in npm public :) tmp.license = 'UNLICENSED' JSON.stringify(tmp,null,2).to('../../package.json') setTimeout(()=>{ require('./ascii_cat.js').cat1() console.log('To start with an express js server, type: npm run mwb init\n') console.log('Without an express js server, type: npm run mwb initMin\n') console.log('To add mongodb, type: npm run mwb initMongo\n') console.log() }) } function initMin (){ console.log(' * Creating src folder\n') mkdir('-p','src/client','src/shared','src/server') //server cp(templatePath + 'serverEntry.js','src/server/entry.js') ''.to('src/client/entry.js') //client, just an empty file ''.to('src/server/app.js') //config ''.to('src/alias.json') '[]'.to('src/loaders.json') '[]'.to('src/plugins.json') //add scripts to the existing package.json file //make a copy console.log(' * Updating package.json file\n') let oldName = 'package.'+Date.now()+'.json' cp('package.json',oldName) let tmp = require(path.resolve('package.json')) if (!tmp.scripts) tmp.scripts = {} tmp.scripts.dev = 'npm run clean && node node_modules/mwb/tool/devServerHot' tmp.scripts.serve = 'node build/server/serverBundle' tmp.scripts.bundle = 'npm run clean && node node_modules/mwb/tool/bundle' tmp.scripts.clean = 'rm -rf build' JSON.stringify(tmp,null,2).to('package.json') console.log(' * Boilerplate created, package.json file has been updated') console.log(' * The old package.json file has been renamed to',oldName,'\n') } function initMongo() { console.log('Installing the latest version of mongoDB from "node-mongodb-native"') // exec('npm i mongodb -S') mkdir('-p','db') let tmp = require(path.resolve('package.json')) //change the start script tmp.scripts.start = process.platform === 'win32' ? 'start /B mongod --dbpath db --bind_ip 127.0.0.1 && npm run clean && node node_modules/mwb/tool/devServerHot' //to stop this, has to use ctrl + break, NOT ctrl + c due to the use of start /B : 'npm run clean && node node_modules/mwb/tool/devServerHot' let oldName = 'package.'+Date.now()+'.json' cp('package.json',oldName) JSON.stringify(tmp,null,2).to('package.json') cp(templatePath + 'mongo.js','src/server/mongo.js') console.log('\nA folder named db has been created, and package.json has been udpated.') console.log('The old package.json file has been renamed to',oldName,'\n') console.log('The connection file mongo.js has been added to the src/server folder') } function init (){ initMin() cp('-f',templatePath + 'expressApp.js','src/server/app.js') cp(templatePath + 'main.js','src/server/main.js') console.log('Installing the latest version of express') exec('npm i express -S') console.log('Installing the latest version of compression') exec('npm i compression -S') console.log() console.log('Initiation completed, to start developing, type: npm run dev') }