Skip to content

Instantly share code, notes, and snippets.

@amasad
Created August 11, 2011 17:42
Show Gist options
  • Save amasad/1140265 to your computer and use it in GitHub Desktop.
Save amasad/1140265 to your computer and use it in GitHub Desktop.
var util = require('util'),
spawn = require('child_process').spawn,
fs = require('fs'),
vm = require('vm'),
find = spawn('find', ['./lib', '-name', '*.js']),
jsfiles;
find.stderr.on('data', function (data) {
console.log('something went wrong with find ' + data);
process.exit();
});
find.stdout.on('data', function (data) {
jsfiles = data.toString().split('\n');
// remove emptyline
jsfiles.pop();
appendToPythonjs();
});
var appendToPythonjs = function () {
var pythonjs = fs.readFileSync('python.js').toString();
jsfiles.forEach(function (file) {
console.log("appending " + file);
var moduleCode = fs.readFileSync(file).toString(),
moduleName = file.split('/')[-1];
moduleCode = moduleCode.replace('"use strict";', '');
pythonjs += 'Module[\'' + moduleName + '\']=' + moduleCode;
});
minify(pythonjs);
};
var minify = function (js) {
// minify js
var minModules = {},
sandbox = {
console:{},
print: function () {},
};
vm.runInNewContext(js, sandbox);
jsfiles.forEach(function (file) {
var moduleName = file.split('/')[-1],
moduleCode = sandbox.Module[moduleName];
minModules[file] = moduleCode;
});
console.log(util.inspect(minModules));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment