Skip to content
Permalink
Browse files

feat(cli option): add cli option --cache-clean to clean the build's i…

…nternal application(s) cache
  • Loading branch information
jyounce committed Jul 25, 2016
1 parent e19eec0 commit 96ed51e896b17467ed4c6c7e8a234640c23d9cc7
Showing with 66 additions and 2 deletions.
  1. +3 −0 bin/build.js
  2. +52 −0 src/cli/cache-clean.coffee
  3. +11 −2 src/cli/get-cli-opts.coffee
@@ -31,11 +31,14 @@ config.build.cli.templates.path = path.join(config.build.cli.path, 'templates'
config.build.cli.templates.client = path.join(config.build.cli.templates.path, 'client');
config.build.cli.templates.root = path.join(config.build.cli.templates.path, 'root');
config.build.cli.templates.server = path.join(config.build.cli.templates.path, 'server');
config.build.generated = {};
config.build.generated.path = path.join(BUILD_PATH, 'generated');

/* Bootstrap
************/
require(path.join(config.build.cli.path, 'add-colors'))();
if (config.build.cli.opts.quickStart.length) return require(path.join(config.build.cli.path, 'quick-start'))(config);
if (!!config.build.cli.opts.cacheClean) return require(path.join(config.build.cli.path, 'cache-clean'))(config);
var build = require(path.join(config.build.cli.path, 'get-build'))(config);

/**
@@ -0,0 +1,52 @@
# Cleans dirs in the build's generated directory.
# config.build.cli.opts.cacheClean:
# true (clean app's cache)
# * (clean all app's cache)
# ===============================================
module.exports = (config) ->
cacheClean = config.build.cli.opts.cacheClean
return unless cacheClean

# requires
# ========
del = require 'del'
fs = require 'fs'
fse = require 'fs-extra'
path = require 'path'

# helpers (gen = generated)
# =========================
addGenPathToDirs = (dirs, _path) ->
return [] if not dirs or not dirs.length
dirs = (path.join _path, dir for dir in dirs)

getGenDirs = (_path) ->
try
dirs = fs.readdirSync(_path).filter (file) ->
fs.statSync(path.join(_path, file)).isDirectory()
dirs = addGenPathToDirs dirs, _path
catch e

getGenDir = (_path) ->
appPkg = require path.join config.app.path, 'package.json'
path.join _path, appPkg.name

# vars
# ====
genPath = config.build.generated.path
cleanAll = cacheClean is '*'
cleanDirs = if cleanAll then getGenDirs genPath else getGenDir genPath

# task
# ====
cleanTask = (dirs) ->
promise = del dirs, force: true
promise.then (paths) ->
msg = if cleanAll then 'all apps' else 'app'
msg = "Cleaned #{config.build.pkg.name}'s internal cache for #{msg}."
console.log msg.attn
promise

# return
# ======
cleanTask cleanDirs
@@ -16,16 +16,25 @@ module.exports = (config) ->
return [] if defaults.indexOf(val) is -1
[val]

getCacheClean = (val) ->
return false unless val
return false if val is 'false'
return true if val is true or val is 'true'
return '*' if ['*','all'].indexOf(val) isnt -1
false

# configure
# =========
program
.version config.build.pkg.version
.option '-s, --skip-options [opts]', 'skip build option(s) ex: dev,prod', list, []
.option '--cache-clean [value]', "cleans #{config.build.pkg.name}'s internal cache for an app, optionally provide * to clean internal cache for all apps"
.option '--quick-start [location]', 'creates a simple application structure with a couple files. optional location: client or server. defaults to both'
.parse process.argv

# return
# ======
opts =
skipOpts: program.skipOptions # []
quickStart: getQuickStart program.quickStart # []
cacheClean: getCacheClean program.cacheClean # bool | '*'
quickStart: getQuickStart program.quickStart # []
skipOpts: program.skipOptions # []

0 comments on commit 96ed51e

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