/
TJSDocCLI.js
executable file
·245 lines (208 loc) · 5.49 KB
/
TJSDocCLI.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env node
import fs from 'fs';
import minimist from 'minimist';
import path from 'path';
import stripJsonComments from 'strip-json-comments';
import TJSDoc from './TJSDoc.js';
/**
* Command Line Interface for TJSDoc.
*
* @example
* let cli = new TJSDocCLI(process.argv);
* cli.exec();
*/
export default class TJSDocCLI
{
/**
* Create instance.
*
* @param {Object} argv - this is node.js argv(``process.argv``)
*/
constructor(argv)
{
/** @type {TJSDocCLIArgv} */
this._argv = minimist(argv.slice(2));
if (this._argv.h || this._argv.help)
{
this._showHelp();
process.exit(0);
}
if (this._argv.v || this._argv.version)
{
this._showVersion();
process.exit(0);
}
}
/**
* create config object from config file.
*
* @param {string} configPath - config file path.
*
* @return {TJSDocConfig} config object.
* @private
*/
_createConfigFromFile(configPath)
{
configPath = path.resolve(configPath);
const ext = path.extname(configPath);
if (ext === '.js')
{
return require(configPath);
}
else
{
const configJSON = fs.readFileSync(configPath, { encode: 'utf8' }).toString();
return JSON.parse(stripJsonComments(configJSON));
}
}
/**
* create config object from package.json.
*
* @return {TJSDocConfig|null} config object.
* @private
*/
_createConfigFromPackageJSON()
{
try
{
const filePath = path.resolve('./package.json');
const packageJSON = fs.readFileSync(filePath, 'utf8').toString();
const packageObj = JSON.parse(packageJSON);
return packageObj.tjsdoc;
}
catch (err) { /* nop */ }
return null;
}
/**
* execute to generate document.
*
* @param {Object} [runtime=TJSDoc] - The TJSDoc runtime to invoke.
*/
async exec(runtime = TJSDoc)
{
let config, configPath;
try
{
configPath = this._findConfigFilePath();
if (configPath)
{
config = this._createConfigFromFile(configPath);
}
else
{
config = this._createConfigFromPackageJSON();
}
}
catch (err)
{
console.error('Config loading error in TJSDocCLI.');
if (configPath) { console.error(`File path: ${path.resolve(configPath)}`); }
console.error(err.stack);
process.exit(1);
}
try
{
if (config)
{
await runtime.generate(config);
}
else
{
this._showHelp();
process.exit(1);
}
}
catch (err)
{
console.error('Uncaught error in TJSDocCLI:');
console.error(err.stack);
process.exit(1);
}
}
/**
* find TJSDoc config file.
*
* @returns {string|null} config file path.
* @private
*/
_findConfigFilePath()
{
if (this._argv.c) { return this._argv.c; }
const testResolve = (configPath) =>
{
const filePath = path.resolve(configPath);
fs.readFileSync(filePath);
return filePath;
};
try { return testResolve('./.tjsdocrc'); }
catch (err) { /* nop */ }
try { return testResolve('./.tjsdocrc.js'); }
catch (err) { /* nop */ }
try { return testResolve('./.tjsdocrc.json'); }
catch (err) { /* nop */ }
try { return testResolve('./.tjsdoc.js'); }
catch (err) { /* nop */ }
try { return testResolve('./.tjsdoc.json'); }
catch (err) { /* nop */ }
return null;
}
/**
* show help of TJSDoc
*
* @private
*/
_showHelp()
{
console.log('Usage: tjsdoc [-c .tjsdocrc]');
console.log('');
console.log('Options:');
console.log(' -c', 'specify config file');
console.log(' -h', 'output usage information');
console.log(' -v', 'output the version number');
console.log('');
console.log('TJSDoc finds configuration by the order:');
console.log(' 1. `-c your-tjsdocrc.json`');
console.log(' 2. `.tjsdocrc` in current directory');
console.log(' 3. `.tjsdocrc.js` in current directory');
console.log(' 4. `.tjsdocrc.json` in current directory');
console.log(' 5. `.tjsdoc.js` in current directory');
console.log(' 6. `.tjsdoc.json` in current directory');
console.log(' 7. `tjsdoc` property in package.json');
}
/**
* show version of TJSDoc
*
* @private
*/
_showVersion()
{
let packageObj = null;
// Find package.json
try
{
const packageFilePath = path.resolve(__dirname, '../package.json');
const json = fs.readFileSync(packageFilePath, { encode: 'utf8' });
packageObj = JSON.parse(json);
}
catch (err)
{ /* nop */ }
if (packageObj)
{
console.log(packageObj.version);
}
else
{
console.log('0.0.0');
}
}
}
// If this file is directory executed, work as CLI. However in WebStorm when profiling for heap dumps the target source
// file is required by a wrapper so if `TJSDOC_FORCE_CLI` environment variable exists also start the CLI.
if (fs.realpathSync(process.argv[1]) === __filename)
{
new TJSDocCLI(process.argv).exec();
}
else if (process.env.TJSDOC_FORCE_CLI)
{
new TJSDocCLI(process.argv).exec();
}