try breaking up build-style pipeline into pipeable files

This commit is contained in:
Mike Morris
2014-07-08 18:28:00 -07:00
parent bc30a9b8cd
commit ab36059fda
8 changed files with 106 additions and 121 deletions

View File

@@ -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]));

32
bin/fuzz-style.js Normal file → Executable file
View File

@@ -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);

15
bin/load-style.js Executable file
View File

@@ -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);

102
bin/mkdirp.js Normal file → Executable file
View File

@@ -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]));

View File

@@ -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"
}
}

28
bin/parse.js Executable file
View File

@@ -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);

26
bin/stringify.js Executable file
View File

@@ -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);

6
bin/style.js → bin/style.json Normal file → Executable file
View File

@@ -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 = {
}
}
}]
};
}