docpad-plugin-raw
Version:
DocPad plugin that copies /raw directory to /out
86 lines (81 loc) • 3.27 kB
JavaScript
// Generated by CoffeeScript 2.5.1
// Export Plugin
module.exports = function(BasePlugin) {
var RawPlugin;
return RawPlugin = (function() {
// Define Plugin
class RawPlugin extends BasePlugin {
// Writing all files has finished
writeAfter(opts, next) {
var config, docpad, docpadConfig, eachr, ncp, outPath, pathUtil, safeps, srcPath;
// Import
eachr = require('eachr');
ncp = require('ncp');
safeps = require('safeps');
pathUtil = require('path');
// Prepare
docpad = this.docpad;
config = this.getConfig();
docpadConfig = this.docpad.getConfig();
// Set out directory
// the trailing / indicates to cp that the files of this directory should be copied over
// rather than the directory itself
outPath = pathUtil.normalize(docpad.getPath('out'));
srcPath = pathUtil.normalize(docpad.getPath('source'));
// @TODO: why do we need to normalize these?
if (Object.keys(config).length === 0) {
config.default = {};
config.default.src = 'raw';
}
return eachr(config, function(target, key) {
var CYGWIN, WINDOWS, XCOPY, command, options, src;
docpad.log("info", `Copying ${key}`);
// Use command if specified instead of ncp
if (target.command) {
WINDOWS = /win/.test(process.platform);
CYGWIN = /cygwin/.test(process.env.PATH); // Cheap test!
XCOPY = WINDOWS && !CYGWIN;
target.command || (target.command = (XCOPY ? ['xcopy', '/e', 'src\\raw\\*', 'out\\'] : ['cp', '-Rn', 'src/raw/*', 'out/']));
command = target.command.map(function(part) {
return part.replace(/^out/, outPath).replace(/^src/, srcPath);
});
return safeps.spawn(command, {
output: false
}, function(err) {
if (err) {
return next(err);
}
docpad.log('debug', "Copied raw directory");
return next();
});
} else {
// Otherwise use ncp by default
src = pathUtil.join(srcPath, target.src);
// Use ncp settings if specified
options = (target.options != null) && typeof target.options === 'object' ? target.options : {};
if (options.stopOnErr == null) {
options.stopOnErr = true; // Stop on the first error
}
if (options.clobber == null) {
options.clobber = false; // Don't overwrite files that already exist
}
if (options.dereference == null) {
options.dereference = true; // Follow symbolic links
}
docpad.log('debug', `raw plugin info... out: ${outPath}, src: ${src}, options: ${JSON.stringify(options)}`);
return ncp(src, outPath, options, function(err) {
if (err) {
return next(err);
}
docpad.log('debug', `Done copying ${key}`);
return next();
});
}
});
}
};
// Plugin name
RawPlugin.prototype.name = 'raw';
return RawPlugin;
}).call(this);
};