Created
August 11, 2011 17:42
-
-
Save amasad/1140265 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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