Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
refactor mkdir to remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong committed Oct 25, 2012
1 parent 06167dc commit daace5e
Showing 1 changed file with 44 additions and 46 deletions.
90 changes: 44 additions & 46 deletions lib/build.js
Expand Up @@ -22,9 +22,21 @@ function _getOutputPath(file, inPath, outPath) {
return _getOutputFile(file, inPath, outPath).replace(/\/[^\/]*$/, '');
}

function _mkdir(file, inPath, outPath, cb) {
var dirname = _getOutputPath(file, inPath, outPath);

mkdirp(dirname, function (err) {
if (err && err.errno !== 47) {
utils.error('Cannot make directory', dirname);
throw err;
process.exit(1);
}
cb();
});
}

function _build(file, inPath, outPath, encoding, engine, stat, cb) {
var dirname = _getOutputPath(file, inPath, outPath),
outFile = _getOutputFile(file, inPath, outPath);
var outFile = _getOutputFile(file, inPath, outPath);

getData(file, encoding, function (err, data) {
data = _.extend({ now: now, stat: stat }, data);
Expand All @@ -34,48 +46,33 @@ function _build(file, inPath, outPath, encoding, engine, stat, cb) {
process.exit(1);
}

mkdirp(dirname, function (err) {
if (err && err.errno !== 47) {
utils.error('Cannot make directory', dirname);
process.exit(1);
fs.writeFile(outFile, html, encoding, function (err) {
if (err) {
utils.error('Cannot write file', outFile);
return;
}

fs.writeFile(outFile, html, encoding, function (err) {
if (err) {
utils.error('Cannot write file', outFile);
return;
}

utils.out('[Built]', outFile);
if (cb) {
cb();
}
utils.out('[Built]', outFile);
if (cb) {
cb();
}
});
});
});
});
}

function _copy(file, inPath, outPath, cb) {
var out = file.replace(inPath, outPath),
dirname = _getOutputPath(file, inPath, outPath);
var out = file.replace(inPath, outPath);

mkdirp(dirname, function (err) {
var is = fs.createReadStream(file),
os = fs.createWriteStream(out, { flags: 'a' });
util.pump(is, os, function (err) {
if (err) {
utils.error('Cannot make directory', dirname);
process.exit(1);
utils.warn('Failed to copy file', file);
} else {
utils.out('[Copied]', out);
}

var is = fs.createReadStream(file),
os = fs.createWriteStream(out, { flags: 'a' });
util.pump(is, os, function (err) {
if (err) {
utils.warn('Failed to copy file', file);
} else {
utils.out('[Copied]', out);
}
cb(err);
});
cb(err);
});
}

Expand Down Expand Up @@ -122,7 +119,6 @@ exports.build = function (argv, cb) {

finder.on('file', function (file, stat) {
var isHTML = (/\.html$/).test(file),
path,
i = _ignore.length;

while (i) {
Expand All @@ -140,23 +136,25 @@ exports.build = function (argv, cb) {

files += 1;

if (!isHTML) {
_copy(file, _in, _out, function () {
_mkdir(file, _in, _out, function () {
if (!isHTML) {
_copy(file, _in, _out, function () {
built += 1;
if (cb && files <= built) {
cb();
}
});
return;
}

var path = file.replace(/\.html$/, '');

_build(file, _in, _out, _encoding, _engine, stat, function () {
built += 1;
if (cb && files <= built) {
cb();
}
});
return;
}

path = file.replace(/\.html$/, '');

_build(file, _in, _out, _encoding, _engine, stat, function () {
built += 1;
if (cb && files <= built) {
cb();
}
});
});

Expand Down

0 comments on commit daace5e

Please sign in to comment.