From 2bcabec917d1e41518d153345cb3e53188a2e180 Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Tue, 13 Dec 2011 22:44:21 -0800 Subject: [PATCH] Fixed readme, tweaked how commands are called and how/when the config file is opened --- README.md | 16 ++++----- lib/sew.js | 96 ++++++++++++++++++++++++-------------------------- package.json | 2 +- src/sew.coffee | 69 +++++++++++++++++------------------- 4 files changed, 89 insertions(+), 94 deletions(-) diff --git a/README.md b/README.md index 01f2800..6aeb284 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,15 @@ Sew is a simple build/dev tool for your web based projects using CoffeeScript, L # Installation -npm install -g sew + npm install -g sew # Usage -Commands: - new Create new config file, this is required - build Build your project - watch Wacth and rebuild your project - serve Start a simple HTTP server on port 3000, watch and build your project + Commands: + new Create new config file, this is required + build Build your project + watch Wacth and rebuild your project + serve Start a simple HTTP server on port 3000, watch and build your project -Options: - -p [default: 3000] + Options: + -p [default: 3000] diff --git a/lib/sew.js b/lib/sew.js index 12944e1..e780366 100644 --- a/lib/sew.js +++ b/lib/sew.js @@ -23,69 +23,51 @@ Worker = (function() { - Worker.prototype.configFile = './config.json'; - - Worker.prototype.options = { - public: './public', - jsPath: './app', - cssPath: './app/css/style.less', - outputJs: './public/js/scripts.js', - outputCss: './public/css/styles.css' + Worker.prototype.configFile = 'config.json'; + + Worker.prototype.defaults = { + public: 'public', + scripts: 'app', + scriptsOutput: 'public/js/app.js', + styles: 'css/style.less', + stylesOutput: 'public/css/app.css' }; function Worker() { - if (!this.readConfig() && argv._[0] !== 'new') { - opti.showHelp(); - return 0; - } - this.package = stitch.createPackage({ - paths: [this.options.jsPath] - }); - switch (argv._[0]) { - case 'new': - this["new"](); - break; - case 'build': - this.compile(); - break; - case 'watch': - this.watch(); - break; - case 'serve': - this.serve(); - break; - case 'help': - opti.showHelp(); - break; - default: - opti.showHelp(); + var action; + action = this['_' + argv._[0]]; + if (action) { + action.call(this); + } else { + this._help(); } } - Worker.prototype["new"] = function() { + Worker.prototype._new = function() { if (true && (!fpath.existsSync(this.configFile) || argv.force)) { util.log('Creating config file'); - return fs.writeFileSync(this.configFile, JSON.stringify(this.options, null, 2)); + return fs.writeFileSync(this.configFile, JSON.stringify(this.defaults, null, 2)); } else { return util.log('Config file already exists use --force to override'); } }; - Worker.prototype.compile = function() { - this.compileScriptsAndTemplates(); + Worker.prototype._build = function() { + this.readConfig(); + this.compileScripts(); return this.compileStyles(); }; - Worker.prototype.watch = function() { + Worker.prototype._watch = function() { var _this = this; - this.compile(); + this._build(); return this.walk('./', function(file) { return fs.watchFile(file, function(curr, prev) { if (curr && (curr.nlink === 0 || +curr.mtime !== +prev.mtime)) { switch (fpath.extname(file)) { case '.coffee': case '.eco': - return _this.compileScriptsAndTemplates(); + return _this.compileScripts(); case '.less': return _this.compileStyles(); } @@ -94,9 +76,9 @@ }); }; - Worker.prototype.serve = function() { + Worker.prototype._serve = function() { var app; - this.watch(); + this._watch(); app = new strata.Builder; app.use(strata.commonLogger); app.use(strata.static, this.options.public, ['index.html', 'index.htm']); @@ -105,11 +87,20 @@ }); }; - Worker.prototype.compileScriptsAndTemplates = function() { + Worker.prototype._help = function() { + return opti.showHelp(); + }; + + Worker.prototype.compileScripts = function() { var _this = this; util.log('Building scripts...'); + if (!this.package) { + this.package = stitch.createPackage({ + paths: [this.options.scripts] + }); + } return this.package.compile(function(err, source) { - return fs.writeFile(_this.options.outputJs, source, function(err) { + return fs.writeFile(_this.options.scriptsOutput, source, function(err) { if (err) return util.log(err.message); }); }); @@ -118,12 +109,12 @@ Worker.prototype.compileStyles = function() { var _this = this; util.log('Building styles...'); - if (fpath.existsSync(this.options.cssPath)) { - return less.render(fs.readFileSync(this.options.cssPath, 'utf8'), function(e, css) { + if (fpath.existsSync(this.options.styles)) { + return less.render(fs.readFileSync(this.options.styles, 'utf8'), function(e, css) { if (e) { util.log("LESS - " + e.name + " | " + e.message + " | " + e.extract); } - return fs.writeFile(_this.options.outputCss, css, function(err) { + return fs.writeFile(_this.options.stylesOutput, css, function(err) { if (err) return util.log(err.message); }); }); @@ -131,17 +122,24 @@ }; Worker.prototype.readConfig = function() { - var config, key, value; + var config, key, value, _ref; + if (this.options) return; if (fpath.existsSync(this.configFile)) { config = fs.readFileSync(this.configFile); config = JSON.parse(config); + this.options = {}; + _ref = this.defaults; + for (key in _ref) { + value = _ref[key]; + this.options[key] = value; + } for (key in config) { value = config[key]; this.options[key] = value; } return true; } - return false; + return process.exit(1); }; Worker.prototype.walk = function(path, callback) { diff --git a/package.json b/package.json index 2ed5982..b31e0fd 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "sew", "description": "Test, and build your CoffeeScript, LESS, Eco projects.", "author": "Mike Murray", - "version": "0.1.2", + "version": "0.1.3", "licenses": [ { "type": "MIT", diff --git a/src/sew.coffee b/src/sew.coffee index 71f6ac9..5caa27d 100644 --- a/src/sew.coffee +++ b/src/sew.coffee @@ -25,80 +25,77 @@ argv = opti.usage(''' class Worker - configFile: './config.json' - options: - public: './public' - jsPath: './app' - cssPath: './app/css/style.less' - outputJs: './public/js/scripts.js' - outputCss: './public/css/styles.css' + configFile: 'config.json' + defaults: + public: 'public' + scripts: 'app' + scriptsOutput: 'public/js/app.js' + styles: 'css/style.less' + stylesOutput: 'public/css/app.css' constructor: -> - if not @readConfig() and argv._[0] isnt 'new' - opti.showHelp() - return 0 - - @package = stitch.createPackage { paths: [@options.jsPath] } - - switch argv._[0] - when 'new' then @new() - when 'build' then @compile() - when 'watch' then @watch() - when 'serve' then @serve() - when 'help' then opti.showHelp() - else opti.showHelp() + action = @['_' + argv._[0]] + if action then action.call(@) else @_help() # Actions - new: -> + _new: -> if true and (!fpath.existsSync(@configFile) or argv.force) util.log 'Creating config file' - fs.writeFileSync @configFile, JSON.stringify(@options, null, 2) + fs.writeFileSync @configFile, JSON.stringify(@defaults, null, 2) else util.log 'Config file already exists use --force to override' - compile: -> - @compileScriptsAndTemplates() + _build: -> + @readConfig() + @compileScripts() @compileStyles() - watch: -> - @compile() + _watch: -> + @_build() @walk './', (file) => fs.watchFile file, (curr, prev) => if curr and (curr.nlink is 0 or +curr.mtime isnt +prev.mtime) switch fpath.extname file - when '.coffee', '.eco' then @compileScriptsAndTemplates() + when '.coffee', '.eco' then @compileScripts() when '.less' then @compileStyles() - serve: -> - @watch() + _serve: -> + @_watch() app = new strata.Builder app.use strata.commonLogger app.use strata.static, @options.public, ['index.html', 'index.htm'] strata.run app, { port: argv.p } - + + _help: -> + opti.showHelp() + # Compilers - compileScriptsAndTemplates: -> + compileScripts: -> util.log 'Building scripts...' + @package = stitch.createPackage { paths: [@options.scripts] } if not @package @package.compile (err, source) => - fs.writeFile @options.outputJs, source, (err) -> + fs.writeFile @options.scriptsOutput, source, (err) -> util.log err.message if err compileStyles: -> util.log 'Building styles...' - if fpath.existsSync @options.cssPath - less.render fs.readFileSync(@options.cssPath, 'utf8'), (e, css) => + if fpath.existsSync @options.styles + less.render fs.readFileSync(@options.styles, 'utf8'), (e, css) => util.log "LESS - #{e.name} | #{e.message} | #{e.extract}" if e - fs.writeFile @options.outputCss, css, (err) -> + fs.writeFile @options.stylesOutput, css, (err) -> util.log err.message if err # Utiliity readConfig: -> + return if @options if fpath.existsSync @configFile config = fs.readFileSync @configFile config = JSON.parse config + @options = {} + @options[key] = value for key, value of @defaults @options[key] = value for key, value of config return true - false + process.exit(1) walk: (path, callback) -> for f in fs.readdirSync(path)