Permalink
Browse files

publishing

  • Loading branch information...
1 parent 674398e commit 44835cbf0b2bc863c4c376f921724a259d228e54 @aconbere committed May 18, 2010
Showing with 87 additions and 13 deletions.
  1. +1 −0 .gitignore
  2. +70 −3 lib/_fs.js
  3. +4 −4 lib/collector.js
  4. +4 −6 lib/item.js
  5. +8 −0 spec/_fs_spec.js
View
@@ -0,0 +1 @@
+*.swp
View
@@ -2,7 +2,69 @@ var path = require('path');
var fs = require('fs');
var sys = require('sys');
-fs.walk = function (start, callback) {
+var _fs = {};
+
+_fs.mkdirs = function (_path, mode, callback) {
+ if (_path[0] !== "/") {
+ _path = path.join(process.cwd(), _path)
+ }
+
+ var dirs = _path.split("/");
+ var walker = [dirs.shift()];
+
+ try {
+ dirs.reduce(function (acc, d) {
+ acc.push(d);
+ var dir = acc.join("/");
+
+ fs.stat(dir, function (err, stat) {
+ // stat errs when the file does not exist
+ if (err) {
+ fs.mkdir(dir, mode, function (err) {
+ if (err) {
+ throw ("Failed to make " + dir + "\n" + err.message)
+ }
+ });
+ } else {
+ if (!stat.isDirectory()) {
+ throw ("Failed to mkdir " + dir + ": File exists");
+ }
+ }
+ });
+ return acc;
+ }, walker);
+ } catch (err) {
+ callback(err);
+ }
+};
+
+_fs.mkdirsSync = function (_path, mode) {
+ if (_path[0] !== "/") {
+ _path = path.join(process.cwd(), _path)
+ }
+
+ var dirs = _path.split("/");
+ var walker = [dirs.shift()];
+
+ dirs.reduce(function (acc, d) {
+ acc.push(d);
+ var dir = acc.join("/");
+
+ try {
+ var stat = fs.statSync(dir);
+ if (!stat.isDirectory()) {
+ throw ("Failed to mkdir " + dir + ": File exists");
+ }
+ } catch (err) {
+ sys.p(dir);
+ sys.p(mode);
+ fs.mkdirSync(dir, mode);
+ }
+ return acc;
+ }, walker);
+};
+
+_fs.walk = function (start, callback) {
fs.lstat(start, function (err, stat) {
if (err) { return callback(err) }
if (stat.isDirectory()) {
@@ -29,7 +91,7 @@ fs.walk = function (start, callback) {
});
};
-fs.walkSync = function (start, callback) {
+_fs.walkSync = function (start, callback) {
var stat = fs.statSync(start);
if (stat.isDirectory()) {
@@ -51,7 +113,7 @@ fs.walkSync = function (start, callback) {
coll.dirs.forEach(function (d) {
var abspath = path.join(start, d);
- fs.walkSync(abspath, callback);
+ _fs.walkSync(abspath, callback);
});
} else {
@@ -62,3 +124,8 @@ fs.walkSync = function (start, callback) {
for (k in fs) {
exports[k] = fs[k];
}
+
+exports.walk = _fs.walk;
+exports.walkSync = _fs.walkSync;
+exports.mkdirs = _fs.mkdirs;
+exports.mkdirsSync = _fs.mkdirsSync;
View
@@ -88,15 +88,15 @@ Collector.prototype.findByRelPath = function (relPath) {
};
Collector.prototype.publish = function () {
- //this.items.forEach(function (item) {
- // item.write();
- //});
+ var that = this;
+ this.items.forEach(function (item) {
+ item.write(that.output);
+ });
};
Collector.prototype.run = function () {
this.walk();
this.process();
- //sys.p(this);
this.publish();
};
View
@@ -51,12 +51,10 @@ Item.prototype.context = function () {
return context;
};
-Item.prototype.template = function () {
- return path.join("_templates", this.headers.template);
-};
-
-Item.prototype.write = function () {
- fs.writeFile(path.join(this.output, this.outputPath), this.body);
+Item.prototype.write = function (output) {
+ var outfile = path.join(output, this.outputPath);
+ fs.mkdirsSync(path.dirname(outfile), 0755);
+ fs.writeFile(outfile, this.body);
};
exports.Item = Item;
View
@@ -6,6 +6,14 @@ var fs = require("../lib/_fs");
minitest.setupListeners();
+minitest.context("_fs#mkdirs", function () {
+ this.assertion("it should make all the directories in the tree", function (test) {
+ fs.mkdirs("/home/aconbere/Projects/javascript/boxer/test", 0755, function(err) {
+ });
+ test.finished();
+ });
+});
+
minitest.context("_fs#walk", function () {
this.assertion("it should call \"callback\" for ever file in the tree", function (test) {
fs.walk("/home/aconbere/Projects/javascript", function(start, dirs, names) {

0 comments on commit 44835cb

Please sign in to comment.