Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
It compiles, but doesn't output the compiled files yet (or serve them
from a built in server)
  • Loading branch information
Forbes Lindesay committed Aug 16, 2012
1 parent 84f010a commit 457af0a
Show file tree
Hide file tree
Showing 10 changed files with 523 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = require('./lib');
61 changes: 61 additions & 0 deletions lib/app.js
@@ -0,0 +1,61 @@
// => means returns a promise for
// -> means returns

var fs = require('fs');
var path = require('path');
var Q = require('q');

//parse('./')
// => {hash: {'name': {file}, ...}, list: [{file}, ...]}
// where {file} is:
// {name(), path(), source(), raw(), update(value), rename(name),
// show(), hide(), hidden()}
var parse = require('./parse');//depends on readDirRec

//compile('./', [filter(dir), filter(dir), filter(dir), ...])
// => [{file}, {file}, {file}]
var compile = require('./compile');

//fileWriter(dir)
// -> writeFile({file})
// => {file}
var fileWriter = require('./fileWriter');

var select = require('./select');

module.exports = app;
function app(src) {
var self = {};
var filters = [];

self.parse = function parseWrapper() {
return parse(src);
};

self.compile = function compileWrapper() {
return compile(src, filters);
};

self.use = function useWrapper(filter) {
filters.push(filter);
return self;
};

self.select = function selectWrapper(selector) {
return select(selector, filters);
};

self.listen = function listenWrapper(port) {

};
self.build = function buildWrapper(dir) {
return self.compile().then(function (files) {
return Q.all(files.map(fileWriter(dir))).then(function () {
return self;
});
});
};

return self;
}

51 changes: 51 additions & 0 deletions lib/compile.js
@@ -0,0 +1,51 @@
var Q = require('q');
var path = require('path');

//parse('./')
// => {hash: {'name': {file}, ...}, list: [{file}, ...]}
// where {file} is:
// {name(), path(), source(), raw(), update(value), rename(name),
// show(), hide(), hidden()}
var parseDir = require('./parse');//depends on readDirRec

//compile('./', [filter(dir), filter(dir), filter(dir), ...])
// => [{file}, {file}, {file}]
module.exports = compile;
function compile(input, filters) {
return parseDir(input).then(function (dir) {
var hash = dir.hash;
var list = dir.list;
return Q.all(serialMap(filters, filterProcessor(dir))).then(function () {
return dir.list.filter(notHiden);
});
});
}

//serialMap([...], =>map(input))
// => [...]
function serialMap(input, map) {
var output = [];
function next(i) {
if (i === input.length) return output;
return map(input[i]).then(function (res) {
output.push(res);
return next(i + 1);
});
}
return next(0);
}

//filterProcessor('./')
//-> processFilter(filter)
// => filter(dir)
function filterProcessor(dir) {
return function processFilter(filter) {
return Q.resolve(filter(dir));
};
}

//notHidden({file})
//-> true/false
function notHiden(file) {
return !file.hidden();
}
6 changes: 6 additions & 0 deletions lib/fileWriter.js
@@ -0,0 +1,6 @@
module.exports = function fileWriter(dir) {
console.log('file writer for: ', dir);
return function writeFile(file) {
console.log(file.name(), file.source());
};
};
141 changes: 141 additions & 0 deletions lib/index.js
@@ -0,0 +1,141 @@
// => means returns a promise for
// -> means returns

var fs = require('fs');
var path = require('path');
var Q = require('q');



exports = module.exports = require('./app');



//parse('./')
// => {hash: {'name': {file}, ...}, list: [{file}, ...]}
// where {file} is:
// {name(), path(), source(), raw(), update(value), rename(name),
// show(), hide(), hidden()}
var parse = exports.parse = require('./parse');//depends on readDirRec

//compile('./', [filter(dir), filter(dir), filter(dir), ...])
// => [{file}, {file}, {file}]
var compile = exports.compile = require('./compile');




function run(src, out) {
console.log('run');
compile(src, []).then(function (files) {
console.log('built');
console.log(files.map(function (file) {
return [file.name(), file.source()];
}));
}).end();
}
run.compile = compile;
run.select = selectFile;
function selectFile(match) {
var filter = {selection: null, operations: []};
filters.push(filter);
var selection = {};
var matchIn = match;
if (typeof match === 'string') {
match = function (file) {
return file.name() === matchIn;
};
} else if (match instanceof RegExp) {
match = function (file) {
return matchIn.test(file.name());
};
}
filter.selection = match;
selection.compile = function compile(engine) {
if (typeof engine === 'string') {
engine = require('consolidate-build')[engine].render;
}
return selection.update(function (file, done) {
engine(file.source(), { filename: file.path() }, function (err, res) {
if (err) return done(err);
file.update(res);
done();
});
});
};
selection.update = function update(transformation) {
if (typeof transformation !== 'function') {
var value = transformation;
transformation = function (file) {
return value;
};
}
if (transformation.length !== 2) {
var oldTransformation = transformation;
transformation = function (file, done) {
try {
var res = oldTransformation(file);
} catch (ex) {
return done(ex);
}
done(null, res);
};
}
filter.operations.push(function (file, done) {
transformation(file, done);
});
return selection;
};
selection.rename = function rename(to) {
if (typeof to === 'string') {
var value = to;
to = function (file) {
if (matchIn instanceof RegExp && /\$/g.test(value)) {
return file.name().replace(matchIn, value);
} else {
return value;
}
};
}
if (typeof to !== 'function') {
throw new Error('You can only rename to a string or function.' +
'\nIf you rename to a string, and matched on a regular expression, ' +
'then the string will be used as a regular expression replacement.' +
'\nIf you are using a function for your to value, it will be passed ' +
'the file object, and can return a string or call the callback with a string.');
}
if (to.length !== 2) {
var oldTransformation = to;
to = function (file, done) {
try {
var res = oldTransformation(file);
} catch (ex) {
return done(ex);
}
done(null, res);
};
}
filter.operations.push(function (file, done) {
to(file, function (err, value) {
if (err) return done(err);
file.rename(value);
done();
});
});
return selection;
};
selection.output = function output(to) {
selection.rename(to);
};
selection.hide = function hide(condition) {
filter.operations.push(function (file, done) {
file.hide();
done();
});
};
filter.operations.push(function (file, done) {
file.show();
done();
});
return selection;
}
78 changes: 78 additions & 0 deletions lib/parse.js
@@ -0,0 +1,78 @@
// => means returns a promise for
// -> means returns

var path = require('path');

var Q = require('q');

//readDirRec('./')
// => [{name:'full/path/to/file', content:'raw file content'}, ...]
var readDirRec = require('./readDirRec');

//parse('./')
// => {hash: {'name': {file}, ...}, list: [{file}, ...]}
// where {file} is:
// {name(), path(), source(), raw(), update(value), rename(name),
// show(), hide(), hidden()}
module.exports = parse;
function parse(input) {
input = path.resolve(input);
return readDirRec(input)
.then(function (files) {
return files.map(fileParser(input));
}).then(function (files) {
var hash = {};
var list = [];
files.forEach(function (file) {
hash[file.name()] = file;
list.push(file);
});
return { hash: hash, list: list};
});
}

//parseFile({name:'full/path/to/file', content:'raw file content'})
// -> {name(), path(), source(), raw(), update(value), rename(name),
// show(), hide(), hidden()}
function fileParser(root) {
return function parseFile(file) {
var data = file.content;
var path = file.name;
var name = file.name.replace(root, '').replace(/\\/g, '/');
var isHidden = true;
var result = {
name: function () {
return name;
},
path: function () {
return path;
},
source: function () {
return data.toString();
},
raw: function () {
return data;
},
update: function (value) {
data = value;
return this;
},
rename: function (newName) {
name = newName;
return this;
},
show: function () {
isHidden = false;
return this;
},
hide: function () {
isHidden = true;
return this;
},
hidden: function () {
return isHidden;
}
};
return result;
};
}

0 comments on commit 457af0a

Please sign in to comment.