Skip to content

Instantly share code, notes, and snippets.

@amark
Created October 1, 2016 21:33
Show Gist options
  • Select an option

  • Save amark/6bd085ec7222e0294f7bc6162a61644c to your computer and use it in GitHub Desktop.

Select an option

Save amark/6bd085ec7222e0294f7bc6162a61644c to your computer and use it in GitHub Desktop.
for Paul. Thoughts? I can't get indenting to work though
/**
* Created by Paul on 10/1/2016.
*/
var fs = require('fs');
var nodePath = require('path');
var banner = "//console.log(\"!!!!!!!!!!!!!!!! WARNING THIS IS GUN 0.5 !!!!!!!!!!!!!!!!!!!!!!\");\n";
var polyCover = "\n\t/* UNBUILD */\n";
var immediateStart = "\n;(function(){\n";
var reqStart = "\n;require(function(module){\n";
var immediateEnd = "\n}());\n";
var requireStart = "\n\trequire.register('$filename', function(exports, require, module){\n";
var requireEnd = "\n\t});//module $filename\n";
function indent(text, i) {
var replacer = '\n' + Array((i || 1) + 1).join('\t');
return text.replace(/\n/ig, replacer);
}
function buildByPath(path) {
var buffers = [];
var files = fs.readdirSync(path);
for(var i = 0, len = files.length; i < len; i++) {
var filePath = nodePath.join(path, files[i]);
if(fs.statSync(filePath).isFile()) {
var content = fs.readFileSync(filePath, 'utf8');
buffers.push(
requireStart.replace('$filename', path.replace(/\.\/src/ig, '.') + '/' + files[i].slice(0, -3)),
indent(content, 2),
requireEnd.replace('$filename', path.replace(/\.\/src/ig, '.') + '/' + files[i].slice(0, -3))
);
}
}
return buffers;
}
;(function(){
var rel = __dirname + '/../src/';
var polyfill = fs.readFileSync(__dirname + '/../src/polyfill/unbuild.js', 'utf8');
// var requirePolyfill = readModuleFile('commonjs-require-definition/require')
var r = [
banner,
immediateStart,
indent(polyCover),
indent(polyfill),
indent(polyCover),
];
var order = [
'type',
'on',
'onify',
'schedule',
'HAM',
'val',
'node',
'state',
'graph',
'gun',
'index',
'api',
'adapters/localStorage',
'polyfill/request',
'polyfill/peer',
'adapters/wsp'
];
order.forEach(function (dir) {
var buf = fs.readFileSync(rel + dir + '.js', 'utf8');
r = r.concat(reqStart, indent(buf), "})(require, './" + dir + "');\n");
});
r.push(immediateEnd);
return fs.writeFileSync('./result.js', r.join(''));
}());
@devel-pa
Copy link

devel-pa commented Oct 1, 2016

Thought about it, don't like it as requires certain script ordering, but a step further to more maintainable code base. So, let's go to another step. Big thumbs up!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment