Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Commit

Permalink
Allow Lottefile to log messages, require additional files, etc.
Browse files Browse the repository at this point in the history
Export useful objects and functions which we think may be used within a
Lottefile. This will be helpful when dealing with plug-ins.
  • Loading branch information
StanAngeloff committed Dec 10, 2011
1 parent 1896d39 commit c43d3a1
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions bin/cli.js
Expand Up @@ -86,30 +86,51 @@ defaults.path = path.resolve(defaults.path || process.env['LOTTE_PATH'] || proce
var lotteFile = path.join(defaults.path, defaults.lottefile); var lotteFile = path.join(defaults.path, defaults.lottefile);
path.exists(lotteFile, function(exists) { path.exists(lotteFile, function(exists) {
if (exists) { if (exists) {
var fs = require('fs'); load(defaults, lotteFile, function(e, options) {
fs.readFile(lotteFile, 'utf8', function(e, code) {
if (e) { if (e) {
console.error.exception(e); console.error.exception(e);
process.exit(1 << 1); process.exit(1 << 1);
} }
var vm = require('vm'), main(options);
previous = defaults.path;
try {
vm.runInNewContext(code, defaults, lotteFile);
if (defaults.path !== previous) {
defaults.path = path.resolve(previous, defaults.path);
}
} catch (e) {
console.error.exception(e);
process.exit(1 << 2);
}
main(defaults);
}); });
} else { } else {
main(defaults); main(defaults);
} }
}); });


function load(options, file, block) {
require('fs').readFile(file, 'utf8', function(e, code) {
if (e) {
return block(e);
}
var context = {};
symbols = ['Buffer', 'console', 'process', 'require', 'setTimeout', 'clearTimeout', 'setInterval', 'clearInterval'];
for (var i = 0; i < symbols.length; i ++) {
context[symbols[i]] = global[symbols[i]];
}
for (var key in options) {
if (Object.prototype.hasOwnProperty.call(options, key)) {
context[key] = options[key];
}
}
try {
require('vm').runInNewContext(code, context, file);
if (context.path !== options.path) {
context.path = path.resolve(options.path, context.path);
}
} catch (e) {
console.error.exception(e);
process.exit(1 << 2);
}
for (var key in options) {
if (Object.prototype.hasOwnProperty.call(options, key)) {
options[key] = context[key];
}
}
block(null, options);
});
};

function main(options) { function main(options) {
options || (options = {}); options || (options = {});
verifyPhantomBinary(options, function() { verifyPhantomBinary(options, function() {
Expand Down

0 comments on commit c43d3a1

Please sign in to comment.