diff options
| author | Di Peng | 2011-07-09 18:15:40 -0700 | 
|---|---|---|
| committer | Igor Minar | 2011-07-20 17:33:18 -0700 | 
| commit | 8fa066190af2b2267a5e8111a41beb6e8af5c340 (patch) | |
| tree | 221e9b67892b9b09047ae8a8b6b50f5cbc9e2ead /docs/src/writer.js | |
| parent | e90b741c9492a65e159c842afe49383f1868308e (diff) | |
| download | angular.js-8fa066190af2b2267a5e8111a41beb6e8af5c340.tar.bz2 | |
refactor(gen-docs): use q, qq, q-fs (node modules) to write gen-docs
- re-write gendocs.js, reader.js and writer.js
- all calls are asynchronous
Diffstat (limited to 'docs/src/writer.js')
| -rw-r--r-- | docs/src/writer.js | 167 | 
1 files changed, 92 insertions, 75 deletions
| diff --git a/docs/src/writer.js b/docs/src/writer.js index 0655572d..1acf9df7 100644 --- a/docs/src/writer.js +++ b/docs/src/writer.js @@ -3,37 +3,113 @@   * for testability   */  require.paths.push(__dirname); -var fs         = require('fs'); +var qfs = require('q-fs'); +var Q = require('qq');  var OUTPUT_DIR = "build/docs/"; +var fs = require('fs'); -function output(docs, content, callback){ -  callback(); +exports.output = function(file, content){ +  console.log('writing ', file); +  var fullPath = OUTPUT_DIR + file; +  var dir = parent(fullPath); +  return Q.when(exports.makeDir(dir), function(error) { +    qfs.write(fullPath,exports.toString(content)); +  }); +} + +//recursively create directory +exports.makeDir = function (path) { +  var parts = path.split(/\//); +  var path = "."; +  //Sequentially create directories +  var done = Q.defer(); +  (function createPart() { + +    if(!parts.length) { +      done.resolve(); +    } else { +      path += "/" + parts.shift(); +      qfs.isDirectory(path).then(function(isDir) { +        if(!isDir) { +          qfs.makeDirectory(path); +        } +        createPart(); +      }); +    } +  })(); +  return done.promise; +}; + +exports.copyTpl = function(filename) { +  return exports.copy('docs/src/templates/' + filename, OUTPUT_DIR + filename); +}; + +exports.copy = function (from, to, replacementKey, replacement) { +  // Have to use rb (read binary), char 'r' is infered by library. +  return qfs.read(from,'b').then(function(content) { +    if(replacementKey && replacement) { +      content = content.toString().replace(replacementKey, replacement); +    } +    qfs.write(to, content); +  }); +} + +exports.copyDir = function copyDir(dir) { +  return qfs.listDirectoryTree('docs/' + dir).then(function(dirs) { +    var done; +    dirs.forEach(function(dirToMake) { +      done = Q.when(done, function() { +       return exports.makeDir("./build/" + dirToMake); +      }); +    }); +    return done; +  }).then(function() { +    return qfs.listTree('docs/' + dir); +  }).then(function(files) { +    files.forEach( function(file) { +      exports.copy(file,'./build/' + file); +    }); +  }); +}; + +exports.merge = function(srcs, to) { +  return merge(srcs.map(function(src) { return 'docs/src/templates/' + src; }), OUTPUT_DIR + to); +}; + +function merge(srcs, to) { +  var contents = []; +  //Sequentially read file +  var done; +  srcs.forEach(function (src) { +    done = Q.when(done, function(content) { +      if(content) contents.push(content); +      return qfs.read(src); +    }); +  }); + +  // write to file +  return Q.when(done, function(content) { +    contents.push(content); +    qfs.write(to, contents.join('\n')); +  });  } +//----------------------- Synchronous Methods ---------------------------------- +  function parent(file) {    var parts = file.split('/');    parts.pop();    return parts.join('/');  } -exports.output = function(file, content, callback){ -  console.log('write', file); -  exports.makeDir(parent(OUTPUT_DIR + file), callback.waitFor(function(){ -    fs.writeFile( -        OUTPUT_DIR + file, -        exports.toString(content), -        callback); -  })); -}; - -exports.toString = function toString(obj){ +exports.toString = function toString(obj) {    switch (typeof obj) {    case 'string':      return obj;    case 'object':      if (obj instanceof Array) { -      obj.forEach(function (value, key){ +      obj.forEach(function (value, key) {          obj[key] = toString(value);        });        return obj.join(''); @@ -44,64 +120,5 @@ exports.toString = function toString(obj){    return obj;  }; -exports.makeDir = function (path, callback) { -  var parts = path.split(/\//); -  path = '.'; -  (function next(error){ -    if (error && error.code != 'EEXIST') return callback.error(error); -    if (parts.length) { -      path += '/' + parts.shift(); -      fs.mkdir(path, 0777, next); -    } else { -      callback(); -    } -  })(); -}; - -exports.copyTpl = function(filename, callback) { -  exports.copy('docs/src/templates/' + filename, OUTPUT_DIR + filename, callback); -}; -exports.copy = function(from, to, callback, replacementKey, replacement) { -  //console.log('writing', to, '...'); -  fs.readFile(from, function(err, content){ -    if (err) return callback.error(err); -    if(replacementKey && replacement) { -      content = content.toString().replace(replacementKey, replacement); -    } -    fs.writeFile(to, content, callback); -  }); -}; - -exports.copyDir = function copyDir(dir, callback) { -  exports.makeDir(OUTPUT_DIR + '/' + dir, callback.waitFor(function(){ -    fs.readdir('docs/' + dir, callback.waitFor(function(err, files){ -      if (err) return this.error(err); -      files.forEach(function(file){ -        var path = 'docs/' + dir + '/' + file; -        fs.stat(path, callback.waitFor(function(err, stat) { -          if (err) return this.error(err); -          if (stat.isDirectory()) { -            copyDir(dir + '/' + file, callback.waitFor()); -          } else { -            exports.copy(path, OUTPUT_DIR  + '/' + dir + '/' + file, callback.waitFor()); -          } -        })); -      }); -      callback(); -    })); -  })); -}; - - -exports.merge = function(srcs, to, callback){ -  merge(srcs.map(function(src) { return 'docs/src/templates/' + src; }), OUTPUT_DIR + to, callback); -}; - -function merge(srcs, to, callback) { -  var content = []; -  srcs.forEach(function (src) { -    content.push(fs.readFileSync(src)); -  }); -  fs.writeFile(to, content.join('\n'), callback.waitFor()); -} +function noop(){}; | 
