Skip to content

Commit 555c0d6

Browse files
author
Aaron O'Mullan
committed
Add gitbook init command, fixes GitbookIO#67
1 parent fb05609 commit 555c0d6

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

bin/gitbook.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var prog = require('commander');
77

88
var pkg = require('../package.json');
99
var generators = require("../lib/generate").generators;
10+
var initDir = require("../lib/generate/init");
1011
var fs = require('../lib/generate/fs');
1112

1213
var utils = require('./utils');
@@ -82,6 +83,14 @@ buildCommand(prog)
8283
});
8384
});
8485

86+
prog
87+
.command('init [source_dir]')
88+
.description('Create files and folders based on contents of SUMMARY.md')
89+
.action(function(dir) {
90+
dir = dir || process.cwd();
91+
return initDir(dir);
92+
});
93+
8594
// Parse and fallback to help if no args
8695
if(_.isEmpty(prog.parse(process.argv).args) && process.argv.length === 2) {
8796
prog.help();

lib/generate/fs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ module.exports = {
5151
copy: Q.denodeify(fsExtra.copy),
5252
remove: Q.denodeify(fsExtra.remove),
5353
symlink: Q.denodeify(fsExtra.symlink),
54+
exists: function(path) {
55+
var d = Q.defer();
56+
fs.exists(path, d.resolve);
57+
return d.promise;
58+
},
5459
};

lib/generate/init.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
var Q = require('q');
2+
var _ = require('lodash');
3+
4+
var path = require('path');
5+
6+
var fs = require('./fs');
7+
var parse = require('../parse');
8+
9+
10+
// Extract paths out of a summary
11+
function paths(summary) {
12+
return _.reduce(summary.chapters, function(accu, chapter) {
13+
return accu.concat(
14+
_.filter([chapter.path].concat(_.pluck(chapter.articles, 'path')))
15+
);
16+
}, []);
17+
}
18+
19+
// Get the parent folders out of a group of files
20+
function folders(files) {
21+
return _.chain(files)
22+
.map(function(file) {
23+
return path.dirname(file);
24+
})
25+
.uniq()
26+
.value();
27+
}
28+
29+
function initDir(dir) {
30+
return fs.readFile(path.join(dir, 'SUMMARY.md'), 'utf8')
31+
.then(function(src) {
32+
// Parse summary
33+
return parse.summary(src);
34+
})
35+
.then(function(summary) {
36+
// Extract paths from summary
37+
return paths(summary);
38+
})
39+
.then(function(paths) {
40+
// Convert to absolute paths
41+
return _.map(paths, function(file) {
42+
return path.resolve(file);
43+
});
44+
})
45+
.then(function(files) {
46+
// Create folders
47+
return Q.all(_.map(folders(files), function(folder) {
48+
return fs.mkdirp(folder);
49+
}))
50+
.then(_.constant(files));
51+
})
52+
.then(function(files) {
53+
// Create files that don't exist
54+
return Q.all(_.map(files, function(file) {
55+
return fs.exists(file)
56+
.then(function(exists) {
57+
if(exists) return;
58+
return fs.writeFile(file, '');
59+
});
60+
}));
61+
})
62+
.fail(function(err) {
63+
console.error(err.stack);
64+
});
65+
}
66+
67+
68+
// Exports
69+
module.exports = initDir;

0 commit comments

Comments
 (0)