Skip to content
Permalink
Browse files

feat(build options): add ability to skip build option(s) when running…

… a build via --skip-options optX
  • Loading branch information
jyounce committed Jun 28, 2016
1 parent baf5734 commit f0804d20d59469cb5ae1839672ca6fab0d899459
Showing with 154 additions and 76 deletions.
  1. +28 −76 bin/build.js
  2. +1 −0 package.json
  3. +11 −0 src/cli/add-colors.coffee
  4. +93 −0 src/cli/get-build.coffee
  5. +21 −0 src/cli/get-cli-opts.coffee
@@ -1,92 +1,44 @@
#! /usr/bin/env node
// process.env.RB_LIB = 'src'
// process.env.RB_LIB = 'src';

/* Requires
***********/
var path = require('path'),
CSON = require('cson'),
merge = require('deepmerge');
var path = require('path');

/* Constants
************/
const APP_PATH = process.cwd();
const BUILD_PATH = path.join(__dirname, '..');
const BUILD_PKG = require(path.join(BUILD_PATH, 'package.json'));
const APP_OPTS = path.join(APP_PATH, BUILD_PKG.name);
const FOR_LIB = process.env.RB_LIB !== 'src'; // 'src' for development
const LIB_PATH = FOR_LIB ? path.join(BUILD_PATH, 'lib') : path.join(BUILD_PATH, 'src');
if (!FOR_LIB) require('coffee-script/register');

/* Helpers
**********/
var help = {
_throw: e => {
throw e
},
isEmptyObj: obj => {
return Object.keys(obj).length === 0 && obj.constructor === Object;
},
isError: obj => {
return obj instanceof Error
},
getBuildType: type => {
if (typeof type !== 'string') return 'default';
type = type.trim().toLowerCase();
if (/^(test|dev|prod)/i.test(type) === false) return 'default';
return type;
},
getBuildTypeOpts: (type, extraOpts) => {
var types = ['common'],
bType = type.split(':')[0], // base type
isDev = /^(default|test|dev)/i.test(bType),
start = bType == 'default' && extraOpts[2] != 'default' ? 2 : 3;
/* Config
*********/
var config = {};
config.app = {};
config.build = {};
config.app.path = APP_PATH;
config.build.path = BUILD_PATH;
config.build.pkg = require(path.join(config.build.path, 'package.json'));
config.app.build = {};
config.app.build.opts = path.join(config.app.path, config.build.pkg.name);
config.build.cli = {};
config.build.cli.path = path.join(LIB_PATH, 'cli');
config.build.cli.opts = require(path.join(config.build.cli.path, 'get-cli-opts'))(config);

extraOpts = extraOpts.slice(start);
if (isDev) types.push('dev');
if (bType == 'test') types.push(bType);
if (type.indexOf(':test') != -1) types.push('test');
if (bType == 'prod') types.push(bType);
types = types.concat(extraOpts);
return types;
},
getBuildOpts: () => {
try {
var opts = CSON.requireCSONFile(`${APP_OPTS}.cson`);
return help.isError(opts) ? help._throw(opts.message) : opts;
}
catch(e) {
try { return require(`${APP_OPTS}.json`); }
catch(e) {
try { return require(`${APP_OPTS}.js`); }
catch(e) { return {}; }
}
}
},
mergeBuildOpts: (opts, typeOpts) => {
if (help.isEmptyObj(opts)) return {};
var mergedOpts = {},
optKeys = Object.keys(opts);
for (var type of typeOpts) {
if (optKeys.indexOf(type) == -1) continue;
mergedOpts = merge(mergedOpts, opts[type]);
}
return mergedOpts;
}
}

/* Setup
********/
var build = {};
build.buildType = help.getBuildType(process.argv[2]);
build.buildTypeOpts = help.getBuildTypeOpts(build.buildType, process.argv);
build.buildOpts = help.getBuildOpts();
build.buildOpts = help.mergeBuildOpts(build.buildOpts, build.buildTypeOpts);
build.execute = require(BUILD_PATH)(build.buildOpts);
/* Bootstrap
************/
require(path.join(config.build.cli.path, 'add-colors'))();
var build = require(path.join(config.build.cli.path, 'get-build'))(config);

/**
* Run Build - in the console type one of the following:
* node build
* node build test | test:client | test:server
* node build dev | dev:test | dev:test:client | dev:test:server
* node build prod | prod:test | prod:test:client | prod:test:server | prod:server
* build
* build test | test:client | test:server
* build dev | dev:test | dev:test:client | dev:test:server
* build prod | prod:test | prod:test:client | prod:test:server | prod:server
*******************************************************************************/
build.execute(build.buildType).then(() => {
console.log('Build Complete!')
})
console.log('Build Complete!'.attn);
});
@@ -53,6 +53,7 @@
"browser-sync": "2.13.0",
"coffee-script": "1.10.0",
"colors": "1.1.2",
"commander": "2.9.0",
"cson": "3.0.2",
"deepmerge": "0.2.10",
"del": "2.2.0",
@@ -0,0 +1,11 @@
# terminal colors
# ===============
module.exports = ->
colors = require 'colors'
colors.setTheme
alert: 'yellow'
attn: ['cyan', 'bold']
error: ['red', 'bold']
info: 'cyan'
success: ['green', 'bold']
colors
@@ -0,0 +1,93 @@
module.exports = (config) ->
# requires
# ========
path = require 'path'
CSON = require 'cson'
merge = require 'deepmerge'

# helpers
# =======
help =
_throw: (e) ->
throw e

isEmptyObj: (obj) ->
Object.keys(obj).length is 0 and obj.constructor is Object

isError: (obj) ->
obj instanceof Error

getBuildType: (type) ->
return 'default' if typeof type isnt 'string'
type = type.trim().toLowerCase()
return 'default' if /^(test|dev|prod)/i.test(type) is false
type

getBuildTypeOpts: (type, extraOpts) ->
types = ['common']
bType = type.split(':')[0] # base type
isDev = /^(default|test|dev)/i.test bType
start = if bType is 'default' and extraOpts[2] isnt 'default' then 2 else 3
extraOpts = extraOpts.slice start
types.push 'dev' if isDev
types.push bType if bType is 'test'
types.push 'test' if type.indexOf(':test') isnt -1
types.push bType if bType is 'prod'
types = types.concat extraOpts
types = help.removeOptsFromTypes types
types

getBuildOpts: -> # cson | json | js
appBuildOpts = config.app.build.opts
try
opts = CSON.requireCSONFile "#{appBuildOpts}.cson"
return help._throw opts.message if help.isError opts
return opts
catch e
try
return require "#{appBuildOpts}.json"
catch e
try
return require("#{appBuildOpts}.js")()
catch e
{}

removeOptsFromTypes: (types) ->
optIndex = null # 1st --option
for i, type of types
if type.indexOf('--') isnt -1
optIndex = i
break
return types if optIndex is null
types.slice 0, optIndex

mergeBuildOpts: (opts, typeOpts) ->
return {} if help.isEmptyObj opts
mergedOpts = {}
optKeys = Object.keys opts
devAndProdOpt = 'devAndProd'
hasDevAndProd = optKeys.indexOf(devAndProdOpt) isnt -1
cliOpts = config.build.cli.opts

for type in typeOpts
continue if cliOpts.skipOpts.indexOf(type) isnt -1
if hasDevAndProd and (type is 'dev' or type is 'prod')
continue if cliOpts.skipOpts.indexOf(devAndProdOpt) isnt -1
mergedOpts = merge mergedOpts, opts[devAndProdOpt]
continue if optKeys.indexOf(type) is -1
mergedOpts = merge mergedOpts, opts[type]

mergedOpts

# configure
# =========
build = {}
build.buildType = help.getBuildType process.argv[2]
build.buildTypeOpts = help.getBuildTypeOpts build.buildType, process.argv
build.buildOpts = help.getBuildOpts()
build.buildOpts = help.mergeBuildOpts build.buildOpts, build.buildTypeOpts
build.execute = require(config.build.path) build.buildOpts

# return
# ======
build
@@ -0,0 +1,21 @@
module.exports = (config) ->
# requires
# ========
program = require 'commander'

# helpers
# =======
list = (val) ->
val.split ','

# configure
# =========
program
.version config.build.pkg.version
.option '-s, --skip-options [opts]', 'skip build option(s) ex: dev,prod', list, []
.parse process.argv

# return
# ======
opts =
skipOpts: program.skipOptions # []

0 comments on commit f0804d2

Please sign in to comment.
You can’t perform that action at this time.