diff --git a/bin/fozy.js b/bin/fozy.js index 423b32e..2dece42 100644 --- a/bin/fozy.js +++ b/bin/fozy.js @@ -3,76 +3,104 @@ 'use strict'; const path = require('path'); -if(!!!global.fozy) { - let root = path.join(process.cwd()); - global.fozy = { - __root: root, - __dev: {}, - } - try{ - global.fozy.__config = require(path.join(root, 'fozy.config')); - } catch(e) {} -} - -const pack = require('../package.json'); const Cli = require('../lib/cli'); - let cli = new Cli(); +let isReady2RunServer; -cli.on(['-v', '--version'], () => { - console.log(`v${pack.version}`); -}); +setFozyObj(); -cli.on(['-h', '--help'], () => { - console.log( -`Usage: fozy [options] +cli.on(['-v', '--version'], printVersion); +cli.on(['-h', '--help'], printHelp); +cli.on(['-w', '--watch'], runInWatchMode); +cli.on(['-p', '--proxy'], runInProxyMode); +cli.on(['--init'], initFozyConfigJSFile); +cli.on(['--nei'], runNeiSetup); -Options: - -v, --version\t\tprint version - -h, --help\t\tprint help - -w, --watch\t\tstart live-reload with browser-sync - -p, --proxy [proxyName], using proxy api, {proxyName} should be contained in de proxyMap - --init\t\tinitialize the project, so far, create fozy.config.js +cli.normal = function(){ + readyToRunServer(); + doRunServer(); +}; -Please visit Github repository https://github.com/elcarim5efil/fozy for more information.`); -}); +cli.end = function runServer(){ + doRunServer(); +}; -let runServer; +function doRunServer(){ + if(isReady2RunServer) { + var app = require('../index'); + app.run({}); + } +} -cli.on(['-w', '--watch'], function(){ +function setFozyObj(){ + if(!!!global.fozy) { + let root = path.join(process.cwd()); + global.fozy = { + __root: root, + __dev: {}, + } + } +} + +function runInWatchMode(){ + readyToRunServer(); global.fozy.__dev.watch = true; - runServer = true; -}); - -cli.on(['-p', '--proxy'], (arg) => { - console.log(`using proxy config: ${arg}`); - let proxy = global.fozy.__config.mock.proxyMap[arg]; - if(proxy){ - console.log('proxy: ', arg); - global.fozy.__config.mock.proxy = proxy; +} + +function runInProxyMode(arg){ + readyToRunServer(); + if(isReady2RunServer) { + console.log(`using proxy config: ${arg}`); + let proxy = global.fozy.__config.mock.proxyMap[arg]; + if(proxy){ + console.log('proxy: ', arg); + global.fozy.__config.mock.proxy = proxy; + } } - runServer = true; -}); +} -cli.on(['--init'], function(){ +function initFozyConfigJSFile(){ var init = require('../lib/init'); + isReady2RunServer = false; init.run(); -}); +} -cli.on(['--nei'], function(arg){ +function runNeiSetup(arg){ let nei = require('../lib/nei'); + isReady2RunServer = false; nei.build(arg); -}); +} -cli.normal = function(){ - runServer = true; -}; +function printVersion(){ + const pack = require('../package.json'); + console.log(`v${pack.version}`); +} -cli.end = function(){ - if(runServer) { - var app = require('../index'); - app.run({}); +function printHelp(){ +console.log( +`Usage: fozy [options] + +Options: + -v, --version\t\t\tprint version + -h, --help\t\t\tprint help + -w, --watch\t\t\tstart live-reload with browser-sync + -p, --proxy {proxyName}, \tusing proxy api, {proxyName} should be contained in de proxyMap + --init\t\t\tinitialize the project, so far, create fozy.config.js + +Please visit Github repository https://github.com/elcarim5efil/fozy for more information.`); +} + +function readyToRunServer(){ + if(!!!global.fozy.__config){ + try{ + global.fozy.__config = require(path.join(global.fozy.__root, 'fozy.config')); + isReady2RunServer = true; + } catch(e) { + console.log('cannot find fozy.config.js, please make sure you make this file.'); + return false; + } } -}; + return true; +} cli.parse(process.argv.slice(2)); diff --git a/package.json b/package.json index baf1f85..3d0eba4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fozy", - "version": "0.2.0", + "version": "0.2.1", "description": "", "main": "index.js", "bin": {