From a6ff165e6b93a2e668e0d7b079b52cd1fa45087b Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 15 Jan 2015 01:35:58 -0500 Subject: [PATCH] works in iojs and browser :-) --- desirae.js | 13 ++++-- lib/browser-adapters.js | 2 +- lib/node-adapters/fsapi-real.js | 74 +++++++++++++++++++++++++++++++++ lib/node-adapters/index.js | 1 + 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 lib/node-adapters/fsapi-real.js diff --git a/desirae.js b/desirae.js index f6a5d37..ceeb094 100644 --- a/desirae.js +++ b/desirae.js @@ -16,11 +16,12 @@ } if (!exports.window) { - Desi.sha1sum = require('./lib/node-adapters').sha1sum; - Desi.fsapi = require('./lib/node-adapters').fsapi; + Desi.sha1sum = require('./lib/node-adapters').sha1sum; + Desi.fsapi = require('./lib/node-adapters').fsapi; + Desi.realFsapi = require('./lib/node-adapters').realFsapi; // adds helper methods to fsapi - require('./lib/desirae-utils').create(Desi); + require('./lib/utils').create(Desi); // adds Desi.Frontmatter require('./lib/frontmatter').create(Desi); } @@ -245,7 +246,11 @@ }; // read config and such - Desi.init = function (desi) { + Desi.init = function (desi, env) { + if (!exports.window) { + // TODO pull state out of this later + Desi.realFsapi.create(Desi, env); + } // config.yml, data.yml, site.yml, authors return PromiseA.all([Desi.fsapi.getAllConfigFiles()/*, fsapi.getBlogdir()*/]).then(function (plop) { var arr = plop[0] diff --git a/lib/browser-adapters.js b/lib/browser-adapters.js index f60214f..a0e11e6 100644 --- a/lib/browser-adapters.js +++ b/lib/browser-adapters.js @@ -184,7 +184,7 @@ }); }; - Desi.fsapi = fsapi = {}; + Desi.fsapi = fsapi = Desi.fsapi || {}; fsapi.getMeta = function (collections, opts) { console.log('dm sub 0'); opts = opts || {}; diff --git a/lib/node-adapters/fsapi-real.js b/lib/node-adapters/fsapi-real.js new file mode 100644 index 0000000..c58d226 --- /dev/null +++ b/lib/node-adapters/fsapi-real.js @@ -0,0 +1,74 @@ +'use strict'; + +var PromiseA = require('bluebird').Promise + , fs = PromiseA.promisifyAll(require('fs')) + ; + +function create(Desi, options) { + var fsapi = Desi.fsapi + ; + + options.blogdir = options.working_path; + + fsapi.getMeta = function (dirnames, opts) { + opts = opts || {}; + + var extensions = '' + , dotfiles = '' + , contents = '' + , sha1sum = '' + ; + + if (Array.isArray(opts.extensions)) { + extensions = '&extensions=' + opts.extensions.join(','); // md,markdown,jade,htm,html + } + if (opts.dotfiles) { + dotfiles = '&dotfiles=true'; + } + if (opts.contents) { + contents = '&contents=true'; + } + if (false === opts.sha1sum) { + sha1sum = '&sha1sum=false'; + } + + return fsapi.walk.walkDirs(options.blogdir, dirnames, opts); + }; + + fsapi.getContents = function (filepaths) { + + return fsapi.getfs(options.blogdir, filepaths); + }; + + fsapi.getCache = function () { + return fs.readFileAsync(options.blogdir, '/cache.json').catch(function (/*e*/) { + return {}; + }).then(function (obj) { + return obj; + }); + }; + + fsapi.copy = function (files) { + // TODO size + return fsapi.copyfs(options.blogdir, files); + }; + + fsapi.putFiles = function (files) { + files.forEach(function (file) { + if (!file.contents || 'string' === typeof file.contents) { + return; + } + if (/\.json$/i.test(file.path)) { + file.contents = JSON.stringify(file.contents); + } + else if (/\.ya?ml$/i.test(file.path)) { + file.contents = exports.jsyaml.dump(file.contents); + } + }); + + // TODO size + return fsapi.putfs(options.blogdir, files); + }; +} + +exports.create = create; diff --git a/lib/node-adapters/index.js b/lib/node-adapters/index.js index 5f0f878..f430db8 100644 --- a/lib/node-adapters/index.js +++ b/lib/node-adapters/index.js @@ -2,3 +2,4 @@ exports.fsapi = require('./fsapi'); exports.sha1sum = require('./sha1sum').sha1sum; +exports.realFsapi = require('./fsapi-real');