From ab36059fdada88e91d8cb88434287d226db3b73e Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Tue, 8 Jul 2014 18:28:00 -0700 Subject: [PATCH] try breaking up build-style pipeline into pipeable files --- bin/build-style.js | 14 ++--- bin/fuzz-style.js | 32 +++++------ bin/load-style.js | 15 ++++++ bin/mkdirp.js | 102 ++++------------------------------- bin/package.json | 4 +- bin/parse.js | 28 ++++++++++ bin/stringify.js | 26 +++++++++ bin/{style.js => style.json} | 6 +-- 8 files changed, 106 insertions(+), 121 deletions(-) mode change 100644 => 100755 bin/fuzz-style.js create mode 100755 bin/load-style.js mode change 100644 => 100755 bin/mkdirp.js create mode 100755 bin/parse.js create mode 100755 bin/stringify.js rename bin/{style.js => style.json} (99%) mode change 100644 => 100755 diff --git a/bin/build-style.js b/bin/build-style.js index 9b931c811..0d8d09ade 100755 --- a/bin/build-style.js +++ b/bin/build-style.js @@ -1,11 +1,11 @@ #!/usr/bin/env node 'use strict'; -var path = require('path'); -var fs = require('fs'); -var mkdirp = require('./mkdirp'); -var data = JSON.stringify(require(path.join(process.cwd(), process.argv[2]))); +var load = require('./load-style.js'); +var JSONStream = require('JSONStream'); +var stringify = require('./stringify'); +var mkdirp = require('./mkdirp.js'); -var out_path = path.join(process.argv[3], 'bin/style.min.js'); -mkdirp.sync(path.dirname(out_path)); -fs.writeFileSync(out_path, data); +load(process.argv[2]) + .pipe(stringify()) + .pipe(mkdirp(process.argv[3])); diff --git a/bin/fuzz-style.js b/bin/fuzz-style.js old mode 100644 new mode 100755 index 8dae68e4a..79fe7e1cc --- a/bin/fuzz-style.js +++ b/bin/fuzz-style.js @@ -1,24 +1,24 @@ #!/usr/bin/env node 'use strict'; +var concat = require('concat-stream'); var fuzzer = require('fuzzer'); -var path = require('path'); -var fs = require('fs'); -var mkdirp = require('./mkdirp'); -var json = require(path.join(process.cwd(), process.argv[2])); - fuzzer.seed(0); -json.constants = Object.keys(json.constants).reduce(function(obj, key, index) { - var value = json.constants[key]; - if (typeof value === 'string') { - obj[key] = fuzzer.mutate.string(value); - } - return obj; -}, {}); +var read = process.stdin; +var write = concat(function(buffer) { + var json = JSON.parse(buffer); -var data = JSON.stringify(json); + json.constants = Object.keys(json.constants).reduce(function(obj, key, index) { + var value = json.constants[key]; + if (typeof value === 'string') { + obj[key] = fuzzer.mutate.string(value); + } + return obj; + }, {}); -var out_path = path.join(process.argv[3], 'bin/style-fuzzed.min.js'); -mkdirp.sync(path.dirname(out_path)); -fs.writeFileSync(out_path, data); + var data = JSON.stringify(json); + process.stdout.write(data); +}); + +read.pipe(write); diff --git a/bin/load-style.js b/bin/load-style.js new file mode 100755 index 000000000..48aa6ecc4 --- /dev/null +++ b/bin/load-style.js @@ -0,0 +1,15 @@ +#!/usr/bin/env node +'use strict'; + +var fs = require('fs'); +var path = require('path'); +var brfs = require('brfs'); + +module.exports = function(file) { + var fromFile = file && file !== '-'; + var rs = fromFile ? fs.createReadStream(file) : process.stdin; + var fpath = fromFile ? file : path.join(process.cwd(), '-'); + return rs.pipe(brfs(fpath)); +}; + +if (!module.parent) module.exports(process.argv[2]).pipe(process.stdout); diff --git a/bin/mkdirp.js b/bin/mkdirp.js old mode 100644 new mode 100755 index a1742b206..25b3234ef --- a/bin/mkdirp.js +++ b/bin/mkdirp.js @@ -1,97 +1,13 @@ +#!/usr/bin/env node +'use strict'; + var path = require('path'); +var mkdirp = require('mkdirp'); var fs = require('fs'); -module.exports = mkdirP.mkdirp = mkdirP.mkdirP = mkdirP; - -function mkdirP (p, opts, f, made) { - if (typeof opts === 'function') { - f = opts; - opts = {}; - } - else if (!opts || typeof opts !== 'object') { - opts = { mode: opts }; - } - - var mode = opts.mode; - var xfs = opts.fs || fs; - - if (mode === undefined) { - mode = 0777 & (~process.umask()); - } - if (!made) made = null; - - var cb = f || function () {}; - p = path.resolve(p); - - xfs.mkdir(p, mode, function (er) { - if (!er) { - made = made || p; - return cb(null, made); - } - switch (er.code) { - case 'ENOENT': - mkdirP(path.dirname(p), opts, function (er, made) { - if (er) cb(er, made); - else mkdirP(p, opts, cb, made); - }); - break; - - // In the case of any other error, just see if there's a dir - // there already. If so, then hooray! If not, then something - // is borked. - default: - xfs.stat(p, function (er2, stat) { - // if the stat fails, then that's super weird. - // let the original error be the failure reason. - if (er2 || !stat.isDirectory()) cb(er, made) - else cb(null, made); - }); - break; - } - }); -} - -mkdirP.sync = function sync (p, opts, made) { - if (!opts || typeof opts !== 'object') { - opts = { mode: opts }; - } - - var mode = opts.mode; - var xfs = opts.fs || fs; - - if (mode === undefined) { - mode = 0777 & (~process.umask()); - } - if (!made) made = null; - - p = path.resolve(p); - - try { - xfs.mkdirSync(p, mode); - made = made || p; - } - catch (err0) { - switch (err0.code) { - case 'ENOENT' : - made = sync(path.dirname(p), opts, made); - sync(p, opts, made); - break; - - // In the case of any other error, just see if there's a dir - // there already. If so, then hooray! If not, then something - // is borked. - default: - var stat; - try { - stat = xfs.statSync(p); - } - catch (err1) { - throw err0; - } - if (!stat.isDirectory()) throw err0; - break; - } - } - - return made; +module.exports = function(out) { + mkdirp.sync(path.dirname(out)); + return fs.createWriteStream(out); }; + +if (!module.parent) process.stdin.pipe(module.exports(process.argv[2])); diff --git a/bin/package.json b/bin/package.json index 0eefafd26..bb8783d99 100644 --- a/bin/package.json +++ b/bin/package.json @@ -2,7 +2,9 @@ "name": "llmr-native", "version": "0.0.1", "dependencies": { + "concat-stream": "^1.4.6", "fuzzer": "^0.2.0", - "glsl-optimizer": "git://github.com/kkaefer/glsl-optimizer.git#amalgamation" + "glsl-optimizer": "git://github.com/kkaefer/glsl-optimizer.git#amalgamation", + "mkdirp": "^0.5.0" } } diff --git a/bin/parse.js b/bin/parse.js new file mode 100755 index 000000000..15aad1016 --- /dev/null +++ b/bin/parse.js @@ -0,0 +1,28 @@ +#!/usr/bin/env node +'use strict'; + +var concat = require('concat-stream'); +var through = require('through2'); +var fuzzer = require('fuzzer'); +fuzzer.seed(0); + +module.exports = function() { + var read = through(); + var write = concat(function(buffer) { + var json = JSON.parse(buffer); + + json.constants = Object.keys(json.constants).reduce(function(obj, key, index) { + var value = json.constants[key]; + if (typeof value === 'string') { + obj[key] = fuzzer.mutate.string(value); + } + return obj; + }, {}); + + var data = JSON.stringify(json); + process.stdout.write(data); + }); + read.pipe(write); +}; + +// if (!module.parent) module.exports().pipe(process.stdout); diff --git a/bin/stringify.js b/bin/stringify.js new file mode 100755 index 000000000..c65a54ed1 --- /dev/null +++ b/bin/stringify.js @@ -0,0 +1,26 @@ +#!/usr/bin/env node +'use strict'; + +var through = require('through2'); +var fuzzer = require('fuzzer'); +fuzzer.seed(0); + +module.exports = function() { + return through.obj(function(chunk, env, callback) { + var json = JSON.parse(chunk.toString()); + + json.constants = Object.keys(json.constants).reduce(function(obj, key, index) { + var value = json.constants[key]; + if (typeof value === 'string') { + obj[key] = fuzzer.mutate.string(value); + } + return obj; + }, {}); + + var data = JSON.stringify(json); + this.push(data); + callback(); + }); +}; + +// if (!module.parent) module.exports().pipe(process.stdout); diff --git a/bin/style.js b/bin/style.json old mode 100644 new mode 100755 similarity index 99% rename from bin/style.js rename to bin/style.json index 21cca2af3..4044e4562 --- a/bin/style.js +++ b/bin/style.json @@ -1,6 +1,4 @@ -"use strict"; - -module.exports = { +{ "version": 3, "sprite": "https://www.mapbox.com/mapbox-gl-styles/sprites/outdoors", "glyphs": "https://mapbox.s3.amazonaws.com/gl-glyphs-256/{{fontstack}}/{{range}}.pbf", @@ -2473,4 +2471,4 @@ module.exports = { } } }] -}; +}