1 |
|
2 | (function() {
|
3 | var arg, argv, bfdocs, destDir, fs, generateIndex, generateManifest, handleManifest, loadManifests, lock, manifests, options, parts, path, startServer, switches, v, _, _i, _len, _ref;
|
4 |
|
5 | bfdocs = require('..');
|
6 |
|
7 | fs = require('fs');
|
8 |
|
9 | path = require('path');
|
10 |
|
11 | _ = require('underscore');
|
12 |
|
13 | switches = {
|
14 | help: ['--help', 'Display the help information'],
|
15 | server: ['--server', 'Create an HTTP server to access the generated documentation'],
|
16 | port: ['--port', 'The port on which the HTTP server shoud listen'],
|
17 | watch: ['--watch', 'Watch files for modifications and reload them automatically'],
|
18 | manifestsOnly: ['--manifests-only', 'Do not treat the last argument as the output dir but also as a manifest'],
|
19 | title: ['--title', 'Title of the index page'],
|
20 | noHeader: ['--no-header', 'Hides the header'],
|
21 | noToc: ['--no-toc', 'Hides the table of content sidebar'],
|
22 | baseUrl: ['--base-url', 'Base url of all links'],
|
23 | indexOnly: ['--index-only', 'Only generate the index file. The last argument should be the filename of the index'],
|
24 | version: ['--version', 'Display the installed version of beautiful-docs'],
|
25 | templatesDir: ['--templates-dir', 'Directory of custom templates']
|
26 | };
|
27 |
|
28 | argv = [];
|
29 |
|
30 | options = {
|
31 | help: false,
|
32 | server: false,
|
33 | port: 8080,
|
34 | watch: false,
|
35 | manifestsOnly: false,
|
36 | title: 'Beautiful Docs',
|
37 | baseUrl: '',
|
38 | noHeader: false,
|
39 | noToc: false,
|
40 | indexOnly: false,
|
41 | version: false
|
42 | };
|
43 |
|
44 | _ref = process.argv.slice(2);
|
45 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
46 | arg = _ref[_i];
|
47 | if (arg.substr(0, 2) === '--') {
|
48 | parts = arg.split('=');
|
49 | v = parts[1] || true;
|
50 | if (v === 'false') {
|
51 | v = false;
|
52 | }
|
53 | _.forEach(switches, function(sw, name) {
|
54 | if (sw[0] === parts[0]) {
|
55 | return options[name] = v;
|
56 | }
|
57 | });
|
58 | } else {
|
59 | argv.push(arg);
|
60 | }
|
61 | }
|
62 |
|
63 | if (options.help) {
|
64 | console.log("Usage: bfdocs [options] [/path/to/manifest.json] [/path/to/output/dir]\n");
|
65 | console.log("Available options:");
|
66 | _.forEach(switches, function(sw) {
|
67 | return console.log(" " + sw[0] + "\t\t\t" + sw[1]);
|
68 | });
|
69 | process.exit(0);
|
70 | }
|
71 |
|
72 | if (options.version) {
|
73 | console.log(bfdocs.version);
|
74 | process.exit(0);
|
75 | }
|
76 |
|
77 | destDir = options.indexOnly ? './index.html' : './out';
|
78 |
|
79 | if (argv.length === 0) {
|
80 | argv.push('.');
|
81 | } else if (argv.length > 1 && !options.manifestsOnly) {
|
82 | destDir = _.last(argv);
|
83 | argv = _.initial(argv);
|
84 | }
|
85 |
|
86 | lock = argv.length;
|
87 |
|
88 | manifests = [];
|
89 |
|
90 | generateManifest = function(manifest, callback) {
|
91 | var dest, opts;
|
92 | if (callback == null) {
|
93 | callback = null;
|
94 | }
|
95 | dest = destDir;
|
96 | opts = _.extend({}, options);
|
97 | if (argv.length > 1) {
|
98 | dest = path.join(destDir, manifest.slug);
|
99 | _.extend(opts, {
|
100 | baseUrl: options.baseUrl + '/' + manifest.slug
|
101 | });
|
102 | }
|
103 | return bfdocs.generate(manifest, dest, opts, callback);
|
104 | };
|
105 |
|
106 | generateIndex = function(filename, callback) {
|
107 | if (callback == null) {
|
108 | callback = null;
|
109 | }
|
110 | return bfdocs.generateIndex(options.title || 'Beautiful docs', manifests, filename, options, callback);
|
111 | };
|
112 |
|
113 | startServer = function(err) {
|
114 | if (options.server) {
|
115 | console.log("Starting server on port " + options.port);
|
116 | return bfdocs.serveStaticDir(destDir, options.port);
|
117 | } else if (options.watch) {
|
118 | console.log("Press Ctrl+C to quit");
|
119 | return setInterval((function() {
|
120 | return 1;
|
121 | }), 100);
|
122 | }
|
123 | };
|
124 |
|
125 | handleManifest = function(err, manifest) {
|
126 | var indexFilename;
|
127 | if (err) {
|
128 | console.log("An error occured while opening a manifest: " + err);
|
129 | return;
|
130 | }
|
131 | manifests.push(manifest);
|
132 | if (options.indexOnly) {
|
133 | if (--lock === 0) {
|
134 | generateIndex(destDir);
|
135 | }
|
136 | return;
|
137 | }
|
138 | indexFilename = path.join(destDir, 'index.html');
|
139 | return generateManifest(manifest, function(err) {
|
140 | if (err) {
|
141 | lock--;
|
142 | console.log("An error occured while generating a manifest: " + err);
|
143 | return;
|
144 | }
|
145 | if (options.watch) {
|
146 | manifest.watch(function() {
|
147 | console.log("Changes detected to '" + manifest.uri + "'");
|
148 | return generateManifest(manifest, function(err) {
|
149 | if (err) {
|
150 | return;
|
151 | }
|
152 | if (argv.length > 1) {
|
153 | return generateIndex(indexFilename);
|
154 | }
|
155 | });
|
156 | });
|
157 | }
|
158 | if (--lock === 0) {
|
159 | if (argv.length > 1) {
|
160 | return generateIndex(indexFilename, startServer);
|
161 | } else {
|
162 | return startServer();
|
163 | }
|
164 | }
|
165 | });
|
166 | };
|
167 |
|
168 | loadManifests = function() {
|
169 | var filename, _j, _len1, _results;
|
170 | _results = [];
|
171 | for (_j = 0, _len1 = argv.length; _j < _len1; _j++) {
|
172 | filename = argv[_j];
|
173 | if (fs.statSync(filename).isDirectory()) {
|
174 | console.log("Generating documentation from directory '" + filename + "'");
|
175 | _results.push(bfdocs.createManifestFromDir(filename, handleManifest));
|
176 | } else {
|
177 | console.log("Generating documentation from manifest '" + filename + "'");
|
178 | _results.push(bfdocs.open(filename, handleManifest));
|
179 | }
|
180 | }
|
181 | return _results;
|
182 | };
|
183 |
|
184 | if (options.indexOnly) {
|
185 | loadManifests();
|
186 | } else {
|
187 | fs.exists(destDir, function(exists) {
|
188 | if (!exists) {
|
189 | return fs.mkdir(destDir, function(err) {
|
190 | if (err) {
|
191 | return console.log("An error occured while creating '" + destDir + "': " + err);
|
192 | }
|
193 | return loadManifests();
|
194 | });
|
195 | } else {
|
196 | return loadManifests();
|
197 | }
|
198 | });
|
199 | }
|
200 |
|
201 | }).call(this);
|