This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli.js
208 lines (171 loc) · 5.35 KB
/
cli.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var fs = require('fs'),
path = require('path');
var FsTools = require('fs-tools');
var minimatch = require('minimatch');
var _ = require("underscore");
var argparse = require('argparse');
var ArgumentParser = argparse.ArgumentParser;
var cli = module.exports = new ArgumentParser({
version: require('./version'),
addHelp: true,
formatterClass: function(options) {
options['maxHelpPosition'] = 40;
return new argparse.HelpFormatter(options);
}
});
cli.addArgument(['paths'], {
help: 'Source files location',
metavar: 'PATH',
action: 'append',
nargs: '+'
});
cli.addArgument(['--excludes'], {
help: 'Glob patterns of filenames to exclude ' +
'(you can use wildcards: ?, *, **).',
dest: 'excludes',
metavar: 'PATTERN',
action: 'append',
defaultValue: []
});
cli.addArgument(['--extension'], {
help: 'The extension of your Markdown files. [.md]',
dest: 'extension',
metavar: 'STRING',
defaultValue: '.md'
});
cli.addArgument(['-o', '--output'], {
help: 'Resulting file(s) location [out]',
dest: 'output',
metavar: 'PATH',
defaultValue: 'out'
});
cli.addArgument(['-oa', '--outputAssets'], {
help: 'Resulting file(s) location for assets [out/assets]',
dest: 'outputAssets',
metavar: 'PATH',
defaultValue: 'out/assets'
});
cli.addArgument(['--keepOutDir'], {
help: 'Does not wipe output directory before building',
dest: 'keepOutDir',
action: 'storeTrue',
defaultValue: false
});
cli.addArgument(['-t', '--title'], {
help: 'Title of the index page [Panda: Default Title Here]',
dest: 'title',
metavar: 'STRING',
defaultValue: 'Panda: Default Title Here'
});
cli.addArgument(['--skin'], {
help: 'The location of your Jade templates [./templates/default]',
dest: 'skin',
metavar: 'PATH',
defaultValue: './templates/default'
});
cli.addArgument(['--assets'], {
help: 'The location of your asset files (CSS, Javascript, e.t.c.) [./templates/default/assets]',
dest: 'assets',
metavar: 'PATH'
});
cli.addArgument(['-e', '--resources'], {
help: 'A resources directory to also copy into the _/out_ directory. This is usually used for accompanying or inline images.',
dest: 'resources',
metavar: 'PATH'
});
cli.addArgument(['-d', '--disableTests'], {
help: 'Disables the test suite that runs at the end of an HTML build. This is NOT recommended.',
dest: 'disableTests',
action: 'storeTrue',
defaultValue: false
});
cli.addArgument(['-nr', '--noRelease'], {
help: 'If set, indicates that you\'re not doing a release',
dest: 'noRelease',
action: 'storeTrue',
defaultValue: false
});
cli.addArgument(['--keepFirstHeader'], {
help: 'If set, keeps the first header (<h1>) detected',
dest: 'keepFirstHeader',
action: 'storeTrue',
defaultValue: false
});
cli.addArgument(['--codeHighlightTheme'], {
help: ' The name of the highlightjs theme to use for code highlighting',
dest: 'codeHighlightTheme',
action: 'storeTrue',
metavar: 'STRING',
defaultValue: 'github'
});
cli.addArgument(['--baseUrl'], {
help: 'Base url of all links [./]',
dest: 'baseUrl',
metavar: 'STRING',
defaultValue: './'
});
cli.addArgument(['--json'], {
help: 'Provide a JSON file alongside the HTML output',
dest: 'json',
action: 'storeTrue',
defaultValue: false
});
cli.addArgument(['--tocId'], {
help: 'The DOM ID of your TOC section (to aid in JSON creation); optional',
dest: 'tocId',
metavar: 'STRING'
});
cli.addArgument(['--contentId'], {
help: 'The DOM ID of your main content (to aid in JSON creation); optional',
dest: 'contentId',
metavar: 'STRING'
});
cli.findFiles = function findFiles(paths, excludes, extension, callback) {
var entries = [];
excludes = ["node_modules"].concat([]);
// prepare matchers matchers
if (excludes.length > 0) {
excludes = _.map(excludes || [], function (p) {
return isFunction(p) ? {test: p} : minimatch.makeRe(p);
});
}
// make a copy consisting valueble paths only
paths = paths.filter(function (p) { return !!p; });
function isFunction(obj) {
return typeof obj === 'function';
}
function process(filename, lstats) {
var include = true;
if (0 < excludes.length) {
include = !_.any(excludes, function (filter) {
return /node_modules/.test(filename) || filter.test(filename, lstats);
});
}
if (include && path.extname(filename) == extension) {
entries.push(filename);
}
}
function walk(err) {
var pathname, lstats;
// get next path
pathname = paths.shift();
// skip empty path or report real error
if (err || !pathname) {
callback(err, entries.sort());
return;
}
// get lstats of the pathname
lstats = fs.lstatSync(pathname);
if (!lstats.isDirectory()) {
// process non-directories directly
process(pathname, lstats);
walk();
return;
}
FsTools.walk(pathname, function (filename, lstats, next) {
process(filename, lstats);
next();
}, walk);
}
walk();
};