var nunjucks = require('nunjucks'),
Get = require('./utils/get'),
get = new Get(),
File = require('./utils/file'),
file = new File(),
Extend = require('./utils/extend'),
extend = new Extend();var nunjucks = require('nunjucks'),
Get = require('./utils/get'),
get = new Get(),
File = require('./utils/file'),
file = new File(),
Extend = require('./utils/extend'),
extend = new Extend();var Geno = function() {};Run through the configuration, the parsing and the compiling of the site.
done is a function.Geno.prototype.build = function(done) {
done = (done || function() {});
this.configure(function(config) {
this.template(config, function(err, data) {
if (err) { return done(err); }
this.compile(config, data, function(err) {
if (err) { return done(err); }
return done();
});
}.bind(this));
}.bind(this));
};Return the config by reading the project's package.json file and
the project's geno.json file, if it exists.
callback is a function.Geno.prototype.configure = function(callback) {
var pkgPath = get.path(process.cwd(), 'package.json'),
pkgInfo = require(pkgPath),
genPath = get.path(process.cwd(), 'geno.json');
file.exists(genPath, function(exists) {
if (exists) {
var genInfo = require(genPath),
config = extend.obj(pkgInfo, genInfo);
return callback(config);
} else {
return callback(pkgInfo);
}
});
};Return the template file's data.
config is an object.callback is a function.Geno.prototype.template = function(config, callback) {
var index = (config.template || get.lib('template/index.html'));
file.read(index, function(err, data) {
if (err) { return callback(err); }
return callback(null, data);
});
};Compile the content by running the data and config through nunjucks.
config is an object.data is a string.callback is a function.Geno.prototype.compile = function(config, data, callback) {
var content = nunjucks.renderString(data, config),
output = (config.output || get.path(process.cwd(), 'site')),
style = (config.stylesheet || get.lib('template/style.css')),
script = (config.javascript || false);
this.generate(content, output, style, script, function(err) {
if (err) { return callback(err); }
return callback(null);
});
};Generate the site by creating the output directory and saving
the index.html file.
content is a string.output is a string.style is a string.script is a string.callback is a function.Geno.prototype.generate = function(content, output, style, script, callback) {
var index = get.path(output, 'index.html');
file.mkDir(output, function(err) {
if (err) { return callback(err); }
file.save(index, content, function() {
if (err) { return callback(err); }
this.assets(output, style, script, function(err) {
if (err) { return callback(err); }
return callback(null);
});
}.bind(this));
}.bind(this));
};Copy all of the assets, ie. the style and script files.
output is a string.style is a string.script is a string.callback is a function.Geno.prototype.assets = function(output, style, script, callback) {
var css = get.path(output, 'style.css'),
src = get.name(script),
js = get.path(output, src);
file.copy(style, css, function(err) {
if (err) { return callback(err); }
if (!script) {
return callback(null);
} else {
file.copy(script, js, function(err) {
if (err) { return callback(err); }
return callback(null);
});
}
});
};module.exports = Geno;var path = require('path');var Get = function() {};Return the full path of the filename.
directory is a string.filename is a string.Get.prototype.path = function(directory, filename) {
return path.join(directory, '/', filename);
};Get.prototype.lib = function(filename) {
return path.join(__dirname, '../../', filename);
};Get.prototype.name = function(filename) {
return path.basename(filename);
};module.exports = Get;var Extend = function() {};Return a single object by combining two into one.
target is an object.options is an object.Extend.prototype.obj = function(target, options) {
for (var key in options) {
if (options.hasOwnProperty(key)) {
if (options[key] !== undefined) {
target[key] = options[key];
}
}
}
return target;
};module.exports = Extend;Import the required modules.
fs-extra enables some extra fs goodies, like copy and mkdirp.var fs = require('fs-extra');var File = function() {};Read the file (synchronously) and return the data.
file is a string.callback is a function.File.prototype.read = function(file, callback) {
try {
var data = fs.readFileSync(file, { encoding: 'utf8' });
return callback(null, data);
} catch(err) {
return callback(err);
}
};Save the file and content (synchronously).
file is a string.content is a string.callback is a function.File.prototype.save = function(file, content, callback) {
try {
fs.writeFileSync(file, content, { encoding: 'utf8' });
return callback(null);
} catch(err) {
return callback(err);
}
};Copy the target to the destination (synchronously).
target is a string.destination is a string.callback is a function.File.prototype.copy = function(target, destination, callback) {
try {
fs.copySync(target, destination);
return callback(null);
} catch(err) {
return callback(err);
}
};Check if the path exists (synchronously).
path is a string.callback is a function.File.prototype.exists = function(path, callback) {
var exists = fs.existsSync(path);
return callback(exists);
};File.prototype.mkDir = function(path, callback) {
try {
this.exists(path, function(exists) {
if (!exists) { fs.mkdirpSync(path); }
return callback(null);
});
} catch(err) {
return callback(err);
}
};module.exports = File;