-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTSDoc.js
87 lines (87 loc) · 3.82 KB
/
TSDoc.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
///<reference path="../dec/node.d.ts"/>
var path = require('path');
var TSDdoc = (function () {
function TSDdoc() {
}
TSDdoc.cmd = function () {
var argv = require('optimist').argv;
var fs = require('fs');
var sys = require('util');
var exec = require('child_process').exec;
var configFile = process.cwd() + path.sep + 'tsdoc.json';
var readmeFile = process.cwd() + path.sep + 'readme.md';
var configContents = fs.readFileSync(path.resolve(__dirname, '..' + path.sep + 'template' + path.sep + 'tsdoc.json'), 'utf8');
if (argv.i || argv.install) {
var parentDir = path.resolve(process.cwd(), '..' + path.sep);
var templateDir = path.resolve(__dirname, '..' + path.sep + 'template');
var outConfig = configContents.replace(/\$TSDOC\$/gi, TSDdoc.processPath(templateDir));
outConfig = outConfig.replace(/\$SOURCE\$/gi, TSDdoc.processPath(process.cwd() + path.sep + 'src' + path.sep));
outConfig = outConfig.replace(/\$DESTINATION\$/gi, TSDdoc.processPath(process.cwd() + path.sep + 'docs'));
outConfig = outConfig.replace(/\$USERNAME\$/gi, TSDdoc.getUserName());
outConfig = outConfig.replace(/\$YEAR\$/gi, new Date().getFullYear());
outConfig = outConfig.replace(/\$PROJECTNAME\$/gi, TSDdoc.processPath(process.cwd().substr(process.cwd().lastIndexOf(path.sep) + 1)));
fs.writeFileSync(configFile, outConfig, 'utf8');
console.log('TSDoc tsdoc.json generated.');
return;
}
else if (argv.v || argv.version) {
var nodePackage = require('./../package.json');
console.log('TSDOC v' + nodePackage.version);
}
else {
if (fs.existsSync(configFile)) {
console.log('TSDoc Generating doc...');
var configJson = require(configFile);
var source = configJson.tsdoc.source;
var destination = configJson.tsdoc.destination;
var readme = TSDdoc.getReadmeFile();
var configFileParam = " -c " + configFile;
var destinationParam = " -d " + configJson.tsdoc.destination;
var tutorials = configJson.tsdoc.tutorials == "" ? "" : " -u " + configJson.tsdoc.tutorials;
var jsonParams = [
"jsdoc ",
configJson.tsdoc.source,
configFileParam,
destinationParam,
tutorials,
readme
].join('');
exec(jsonParams, TSDdoc.onJSDoc);
}
else {
console.log('No tsdoc.json found, please use -i to generate one');
}
}
};
TSDdoc.onJSDoc = function (error, stdout, stderr) {
if (stdout != "")
console.log(stdout);
console.log('TSDoc Done.');
if (error !== null) {
console.log('exec error: ' + error);
}
};
TSDdoc.getUserName = function () {
var userName = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE);
return userName.substr(userName.lastIndexOf(path.sep) + 1);
};
TSDdoc.getReadmeFile = function () {
var fs = require('fs');
var files = fs.readdirSync(process.cwd());
var readme = "";
for (var i = 0; i < files.length; i++) {
if (files[i].toLowerCase().indexOf('readme.md') != -1) {
readme = ' ' + files[i];
}
}
return readme;
};
TSDdoc.processPath = function (path) {
path = path.indexOf(':') != -1 ? path.split(':')[1] : path;
path = path.replace(/\\/gi, '/');
return path;
};
return TSDdoc;
}());
TSDdoc.nodePackage = require('./../package.json');
(module).exports = TSDdoc;