Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added the ability to specify multiple scripts, js libs and styles to …

…be built together
  • Loading branch information...
commit e3686607aeacc3580af34395223a7e7ba5f5c86a 1 parent 54bb1b7
@mikemurray authored
Showing with 81 additions and 42 deletions.
  1. +47 −22 lib/sew.js
  2. +34 −20 src/sew.coffee
View
69 lib/sew.js
@@ -27,9 +27,10 @@
Worker.prototype.defaults = {
public: 'public',
- scripts: 'app',
+ libs: [],
+ scripts: ['app'],
scriptsOutput: 'public/js/app.js',
- styles: 'css/style.less',
+ styles: ['css/style.less'],
stylesOutput: 'public/css/app.css'
};
@@ -67,8 +68,10 @@
switch (fpath.extname(file)) {
case '.coffee':
case '.eco':
+ case 'js':
return _this.compileScripts();
case '.less':
+ case '.css':
return _this.compileStyles();
}
}
@@ -92,52 +95,73 @@
};
Worker.prototype.compileScripts = function() {
+ var lib, libSources, _i, _len, _ref;
var _this = this;
util.log('Building scripts...');
+ libSources = [];
+ _ref = this.options.libs;
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ lib = _ref[_i];
+ if (fpath.existsSync(lib)) libSources.push(fs.readFileSync(lib));
+ }
if (!this.package) {
this.package = stitch.createPackage({
- paths: [this.options.scripts]
+ paths: this.options.scripts
});
}
return this.package.compile(function(err, source) {
- return fs.writeFile(_this.options.scriptsOutput, source, function(err) {
- if (err) return util.log(err.message);
- });
+ if (!_this.options.compineScripts) {
+ source = libSources.join('\n') + source;
+ return fs.writeFile(_this.options.scriptsOutput, source, function(err) {
+ if (err) return util.log(err.message);
+ });
+ }
});
};
Worker.prototype.compileStyles = function() {
+ var style, styleSources, _i, _len, _ref, _results;
var _this = this;
util.log('Building styles...');
- if (fpath.existsSync(this.options.styles)) {
- return less.render(fs.readFileSync(this.options.styles, 'utf8'), function(e, css) {
+ styleSources = [];
+ _ref = this.options.styles;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ style = _ref[_i];
+ if (fpath.existsSync(style)) styleSources.push(fs.readFileSync(style));
+ _results.push(less.render(styleSources.join('\n'), function(e, css) {
if (e) {
util.log("LESS - " + e.name + " | " + e.message + " | " + e.extract);
}
return fs.writeFile(_this.options.stylesOutput, css, function(err) {
if (err) return util.log(err.message);
});
- });
+ }));
}
+ return _results;
};
Worker.prototype.readConfig = function() {
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;
+ try {
+ 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 true;
+ } catch (e) {
+ util.log('Error reading config file, check syntax and try again');
}
opti.showHelp();
return process.exit(1);
@@ -168,6 +192,7 @@
switch (fpath.extname(file)) {
case '.js':
case '.coffee':
+ case '.css':
case '.less':
case '.eco':
return true;
View
54 src/sew.coffee
@@ -27,10 +27,11 @@ class Worker
configFile: 'config.json'
defaults:
- public: 'public'
- scripts: 'app'
+ public: 'public' # Static Files
+ libs: [] # JS to concat and append before app scripts
+ scripts: [ 'app' ]
scriptsOutput: 'public/js/app.js'
- styles: 'css/style.less'
+ styles: [ 'css/style.less' ]
stylesOutput: 'public/css/app.css'
constructor: ->
@@ -56,8 +57,8 @@ class Worker
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 @compileScripts()
- when '.less' then @compileStyles()
+ when '.coffee', '.eco', 'js' then @compileScripts()
+ when '.less', '.css' then @compileStyles()
_serve: ->
@_watch()
@@ -72,15 +73,25 @@ class Worker
# Compilers
compileScripts: ->
util.log 'Building scripts...'
- @package = stitch.createPackage { paths: [@options.scripts] } if not @package
+
+ libSources = []
+ for lib in @options.libs
+ libSources.push(fs.readFileSync lib) if fpath.existsSync lib
+
+ @package = stitch.createPackage { paths: @options.scripts } if not @package
@package.compile (err, source) =>
- fs.writeFile @options.scriptsOutput, source, (err) ->
- util.log err.message if err
-
+ if not @options.compineScripts
+ source = libSources.join('\n') + source
+ fs.writeFile @options.scriptsOutput, source, (err) ->
+ util.log err.message if err
+
+
compileStyles: ->
util.log 'Building styles...'
- if fpath.existsSync @options.styles
- less.render fs.readFileSync(@options.styles, 'utf8'), (e, css) =>
+ styleSources = []
+ for style in @options.styles
+ styleSources.push(fs.readFileSync style) if fpath.existsSync style
+ less.render styleSources.join('\n'), (e, css) =>
util.log "LESS - #{e.name} | #{e.message} | #{e.extract}" if e
fs.writeFile @options.stylesOutput, css, (err) ->
util.log err.message if err
@@ -88,14 +99,17 @@ class Worker
# 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
-
+ try
+ 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
+ catch e
+ util.log 'Error reading config file, check syntax and try again'
+
opti.showHelp()
process.exit(1)
@@ -110,7 +124,7 @@ class Worker
isWatchable: (file) ->
switch fpath.extname file
- when '.js', '.coffee', '.less', '.eco' then return true
+ when '.js', '.coffee', '.css', '.less', '.eco' then return true
false
new Worker()
Please sign in to comment.
Something went wrong with that request. Please try again.