Comparing version 0.0.2 to 0.1.0
49
cmd.js
#!/usr/local/bin/node | ||
var http = require('http'), | ||
compile = require('./jspackage').compile, | ||
var compile = require('./jspackage').compile, | ||
fs = require('fs'), | ||
@@ -10,4 +9,4 @@ optparse = require('optparse'); | ||
['-h', '--help', "shows this help section"], | ||
['-p', '--port NUMBER', "start a server to serve the file on this port"], | ||
['-b', '--bare', "compile without a top-level function wrapper"], | ||
['-w', '--watch', "watch source files and recompile when any change"], | ||
]; | ||
@@ -20,4 +19,2 @@ | ||
console.log(parser.toString()); | ||
console.log('\nIf an output file is not provided, a server will be started at'); | ||
console.log('the port provided by the --port or -p option or 8080 by default.') | ||
}; | ||
@@ -40,8 +37,2 @@ | ||
var port; | ||
parser.on("port", function(name, value) { | ||
port = value; | ||
}); | ||
var options = {}; | ||
parser.on("bare", function() { | ||
@@ -51,2 +42,6 @@ options.bare = true; | ||
parser.on("watch", function() { | ||
options.watch = true; | ||
}); | ||
parser.parse(process.argv.splice(2)); | ||
@@ -61,23 +56,15 @@ | ||
compile(options, function(err, code) { | ||
if (err) throw err; | ||
fs.writeFile(output, code); | ||
if (options.watch) { | ||
var timestamp = (new Date()).toLocaleTimeString() | ||
if (err) { | ||
console.error(timestamp + " - error: " + err); | ||
} else { | ||
console.info(timestamp + " - generated " + output); | ||
fs.writeFile(output, code); | ||
} | ||
} else { | ||
if (err) throw err; | ||
fs.writeFile(output, code); | ||
} | ||
}); | ||
} | ||
if (port || !output) { | ||
port = port || 8080; | ||
http.createServer(function(req, res) { | ||
res.writeHead(200); | ||
compile(options, function(err, code) { | ||
if (err) | ||
res.end('throw unescape("' + escape(err.toString()) + '");'); | ||
else | ||
res.end(code); | ||
}); | ||
}).listen(port); | ||
console.log("Server listening at http://localhost:" + port); | ||
} |
130
jspackage.js
// Generated by CoffeeScript 1.3.3 | ||
(function() { | ||
var async, cached_files, collectDependencies, compile, extensions, fs, options, parseFile, path, resolveDependencyChain, resolvePath, root; | ||
var async, cached_files, collectDependencies, compile, extensions, fs, options, parseFile, path, resolveDependencyChain, resolvePath, root, watchFile, watchFileFallback, watchFiles; | ||
@@ -11,2 +11,8 @@ fs = require('fs'); | ||
cached_files = {}; | ||
root = null; | ||
options = null; | ||
parseFile = function(full_path, cb) { | ||
@@ -27,3 +33,3 @@ var file; | ||
return fs.readFile(full_path, 'utf8', function(err, source) { | ||
var import_string, parser, re, result; | ||
var import_string, parser, re, result, timestamp; | ||
if (err) { | ||
@@ -37,5 +43,9 @@ cb(err); | ||
} catch (err) { | ||
cb("" + full_path + "\n" + err); | ||
cb("" + full_path + "\n" + err, file); | ||
return; | ||
} | ||
if (options.watch) { | ||
timestamp = (new Date()).toLocaleTimeString(); | ||
console.info("" + timestamp + " - compiled " + file.path); | ||
} | ||
re = parser.import_re; | ||
@@ -76,7 +86,9 @@ re.lastIndex = 0; | ||
}), function(results) { | ||
if (results.length > 1) { | ||
if (results.length === 1) { | ||
doneResolvingPath(null, results[0]); | ||
} else if (results.length === 0) { | ||
doneResolvingPath("unable to resolve import: " + import_string); | ||
} else if (results.length > 1) { | ||
doneResolvingPath("ambiguous import: " + import_string); | ||
return; | ||
} | ||
return doneResolvingPath(null, results[0]); | ||
}); | ||
@@ -86,8 +98,2 @@ }); | ||
cached_files = {}; | ||
root = null; | ||
options = null; | ||
resolveDependencyChain = function(root, doneResolvingDependencyChain) { | ||
@@ -138,11 +144,13 @@ var deps, processNode, seen; | ||
return parseFile(canonical_path, function(err, file) { | ||
if (file) { | ||
cached_files[file.path] = file; | ||
if (root == null) { | ||
root = file; | ||
} | ||
} | ||
if (err) { | ||
doneCollectingDependencies(err); | ||
return; | ||
} else { | ||
cb(file); | ||
} | ||
if (root == null) { | ||
root = file; | ||
} | ||
cached_files[file.path] = file; | ||
return cb(file); | ||
}); | ||
@@ -170,8 +178,51 @@ }; | ||
watchFileFallback = function(filename, options, cb) { | ||
options.interval = 701; | ||
fs.watchFile(filename, options, function(curr, prev) { | ||
if (curr.mtime !== prev.mtime) { | ||
return cb("change", filename); | ||
} | ||
}); | ||
return { | ||
close: function() { | ||
return fs.unwatchFile(filename); | ||
} | ||
}; | ||
}; | ||
watchFile = fs.watch || watchFileFallback; | ||
watchFiles = function(files, cb) { | ||
var doCallback, file, watcher, watchers, _i, _len, _results; | ||
console.log("watching", files); | ||
watchers = []; | ||
doCallback = function(event) { | ||
var watcher, _i, _len; | ||
if (event === "change") { | ||
for (_i = 0, _len = watchers.length; _i < _len; _i++) { | ||
watcher = watchers[_i]; | ||
watcher.close(); | ||
} | ||
return cb(); | ||
} | ||
}; | ||
_results = []; | ||
for (_i = 0, _len = files.length; _i < _len; _i++) { | ||
file = files[_i]; | ||
try { | ||
watcher = fs.watch(file, doCallback); | ||
} catch (err) { | ||
watcher = watchFileFallback(file, doCallback); | ||
} | ||
_results.push(watchers.push(watcher)); | ||
} | ||
return _results; | ||
}; | ||
compile = function(_options, cb) { | ||
options = _options; | ||
root = null; | ||
return collectDependencies(options.mainfile, function(err) { | ||
if (err) { | ||
cb(err); | ||
return collectDependencies(options.mainfile, function(collect_err) { | ||
if (collect_err && !(root != null)) { | ||
cb(collect_err); | ||
return; | ||
@@ -181,16 +232,31 @@ } | ||
var dep, output; | ||
if (_options.watch) { | ||
watchFiles((function() { | ||
var _i, _len, _results; | ||
_results = []; | ||
for (_i = 0, _len = dependency_chain.length; _i < _len; _i++) { | ||
dep = dependency_chain[_i]; | ||
_results.push(dep.path); | ||
} | ||
return _results; | ||
})(), function() { | ||
return compile(_options, cb); | ||
}); | ||
} | ||
if (err) { | ||
cb(err); | ||
return; | ||
} else if (collect_err) { | ||
cb(collect_err); | ||
} else { | ||
output = ((function() { | ||
var _i, _len, _results; | ||
_results = []; | ||
for (_i = 0, _len = dependency_chain.length; _i < _len; _i++) { | ||
dep = dependency_chain[_i]; | ||
_results.push(dep.compiled_js); | ||
} | ||
return _results; | ||
})()).join("\n"); | ||
cb(null, output); | ||
} | ||
output = ((function() { | ||
var _i, _len, _results; | ||
_results = []; | ||
for (_i = 0, _len = dependency_chain.length; _i < _len; _i++) { | ||
dep = dependency_chain[_i]; | ||
_results.push(dep.compiled_js); | ||
} | ||
return _results; | ||
})()).join("\n"); | ||
return cb(null, output); | ||
}); | ||
@@ -197,0 +263,0 @@ }); |
{ | ||
"name": "jspackage", | ||
"description": "build tool which adds client-side import syntax", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Andrew Kelley", |
@@ -27,8 +27,9 @@ Client side build tool with dependency management | ||
many times a file is imported. | ||
* If used as a server, only modified files will be recompiled on subsequent | ||
requests. | ||
* Compiling CoffeeScript, JavaScript, Coco, and LiveScript source files are | ||
included out of the box. You can add more to the `compile.extensions` object. | ||
included out of the box. You can add more to the `compile.extensions` | ||
object. | ||
- Or add support to the bottom of `src/jspackage.coffee` and submit a pull | ||
request. | ||
* Includes a --watch mode which automatically recompiles source files when | ||
they change. | ||
@@ -44,8 +45,5 @@ ## Command line usage | ||
Available options: | ||
-h, --help shows this help section | ||
-p, --port NUMBER start a server to serve the file on this port | ||
-b, --bare compile without a top-level function wrapper | ||
If an output file is not provided, a server will be started at | ||
the port provided by the --port or -p option or 8080 by default. | ||
-h, --help shows this help section | ||
-b, --bare compile without a top-level function wrapper | ||
-w, --watch watch source files and recompile when any change | ||
``` | ||
@@ -52,0 +50,0 @@ |
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
27122
460
1
100