aboutsummaryrefslogtreecommitdiffstats
path: root/docs/src/writer.js
blob: cf54e1a3aa94ab8dc4a78fb016aef54866581f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
 * All writing related code here. This is so that we can separate the async code from sync code
 * for testability
 */
require.paths.push(__dirname);
var fs         = require('fs');
var OUTPUT_DIR = "build/docs/";

function output(docs, content, callback){
  callback();
}

exports.output = function(file, content, callback){
  //console.log('writing', OUTPUT_DIR + file, '...');
  fs.writeFile(
      OUTPUT_DIR + file,
      exports.toString(content),
      callback);
};


exports.toString = function toString(obj){
  switch (typeof obj) {
  case 'string':
    return obj;
  case 'object':
    if (obj instanceof Array) {
      obj.forEach(function (value, key){
        obj[key] = toString(value);
      });
      return obj.join('');
    } else {
      return JSON.stringify(obj);
    }
  }
  return obj;
};

exports.makeDir = function (path, callback) {
  var parts = path.split(/\//);
  path = '.';
  (function next(){
    if (parts.length) {
      path += '/' + parts.shift();
      fs.mkdir(path, 0777, next);
    } else {
      callback();
    }
  })();
};

exports.copyTpl = function(filename, callback) {
  copy('docs/src/templates/' + filename, OUTPUT_DIR + filename, callback);
};

function copy(from, to, callback) {
  //console.log('writing', to, '...');
  fs.readFile(from, function(err, content){
    if (err) return callback.error(err);
    fs.writeFile(to, content, callback);
  });
}

exports.copyDir = function(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){
        copy('docs/' + dir + '/' + file, OUTPUT_DIR  + '/' + dir + '/' + file, callback.waitFor());
      });
      callback();
    }));
  }));
};
00' href='#n400'>400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465