forked from apidoc/apidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (146 loc) · 5.14 KB
/
index.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
var _ = require('lodash');
var apidoc = require('apidoc-core');
var fs = require('fs-extra');
var path = require('path');
var winston = require('winston');
var Markdown = require('markdown-it');
var PackageInfo = require('./package_info');
var defaults = {
dest : path.join(__dirname, '../doc/'),
template: path.join(__dirname, '../template/'),
debug : false,
silent : false,
verbose : false,
simulate: false,
parse : false, // Only parse and return the data, no file creation.
colorize: true,
markdown: true,
config : './'
};
var app = {
log : {},
markdownParser: null,
options : {}
};
// Display uncaught Exception.
process.on('uncaughtException', function(err) {
console.error((new Date()).toUTCString() + ' uncaughtException:', err.message);
console.error(err.stack);
process.exit(1);
});
/**
* Create the documentation
*
* @param {Object} options See defaults and apidoc-core defaults for all options / `apidoc --help`
* @returns {Mixed} true = ok, but nothing todo | false = error | Object with parsed data and project-informations.
*/
function createDoc(options) {
var api;
var apidocPath = path.join(__dirname, '../');
var markdownParser;
var packageInfo;
options = _.defaults({}, options, defaults);
// Paths.
options.dest = path.join(options.dest, './');
options.template = path.join(options.template, './');
// Line-Ending.
if (options.lineEnding) {
if (options.lineEnding === 'CRLF')
options.lineEnding = '\r\n'; // win32
else if (options.lineEnding === 'CR')
options.lineEnding = '\r'; // darwin
else
options.lineEnding = '\n'; // linux
}
// Options.
app.options = options;
// Logger.
app.log = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
level : app.options.debug ? 'debug' : app.options.verbose ? 'verbose' : 'info',
silent : app.options.silent,
prettyPrint: true,
colorize : app.options.colorize,
timestamp : false
}),
]
});
// Markdown Parser: enable / disable / use a custom parser.
if(app.options.markdown === true) {
markdownParser = new Markdown({
breaks : false,
html : true,
linkify : false,
typographer: false
});
} else if(app.options.markdown !== false) {
// Include custom Parser @see MARKDOWN.md and test/fixtures/custom_markdown_parser.js
Markdown = require(app.options.markdown); // Overwrite default Markdown.
markdownParser = new Markdown();
}
app.markdownParser = markdownParser;
try {
packageInfo = new PackageInfo(app);
// generator information
var json = JSON.parse( fs.readFileSync(apidocPath + 'package.json', 'utf8') );
apidoc.setGeneratorInfos({
name : json.name,
time : new Date(),
url : json.homepage,
version: json.version
});
apidoc.setLogger(app.log);
apidoc.setMarkdownParser(markdownParser);
apidoc.setPackageInfos(packageInfo.get());
api = apidoc.parse(app.options);
if (api === true) {
app.log.info('Nothing to do.');
return true;
}
if (api === false)
return false;
if (app.options.parse !== true)
createOutputFiles(api);
app.log.info('Done.');
return api;
} catch(e) {
app.log.error(e.message);
if (e.stack)
app.log.debug(e.stack);
return false;
}
}
/**
* Save parsed data to files
*
* @param {Object[]} blocks
* @param {Object} packageInfos
*/
function createOutputFiles(api) {
if (app.options.simulate)
app.log.warn('!!! Simulation !!! No file or dir will be copied or created.');
app.log.verbose('create dir: ' + app.options.dest);
if ( ! app.options.simulate)
fs.mkdirsSync(app.options.dest);
app.log.verbose('copy template ' + app.options.template + ' to: ' + app.options.dest);
if ( ! app.options.simulate)
fs.copySync(app.options.template, app.options.dest);
// Write api_data
app.log.verbose('write json file: ' + app.options.dest + 'api_data.json');
if( ! app.options.simulate)
fs.writeFileSync(app.options.dest + './api_data.json', api.data);
app.log.verbose('write js file: ' + app.options.dest + 'api_data.js');
if( ! app.options.simulate)
fs.writeFileSync(app.options.dest + './api_data.js', 'define({ "api": ' + api.data + ' });');
// Write api_project
app.log.verbose('write json file: ' + app.options.dest + 'api_project.json');
if( ! app.options.simulate)
fs.writeFileSync(app.options.dest + './api_project.json', api.project);
app.log.verbose('write js file: ' + app.options.dest + 'api_project.js');
if( ! app.options.simulate)
fs.writeFileSync(app.options.dest + './api_project.js', 'define(' + api.project + ');');
}
module.exports = {
createDoc: createDoc
};