-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation Index and subdirectories parsing #65
Changes from 42 commits
ca43acd
ac03949
b4f79fa
3700130
b21a716
61dfe50
5295b3a
5744dab
5802878
7ec9904
9da1bcf
2c75002
51a466c
6e4fc54
67f5b10
051a517
eb76fe4
c052e40
354b645
62f0f11
c7aff49
41055cb
4267cee
e947600
8771342
80a8603
7e3ee01
47639ae
862cf67
8d8a81b
5ead8ab
81c83c9
7250608
0678b7b
b613458
4748f0a
694639b
d07089f
c7723c7
ce8895d
cb1999e
d22022b
8f51ca4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,11 +44,24 @@ var | |
.options('templateDir', { | ||
alias: 't' | ||
}) | ||
.options('index', { | ||
alias: 'i' | ||
}) | ||
.options('recursive',{ | ||
alias: 'r' | ||
}) | ||
.options('respect-recursive', { | ||
alias: 'rr' | ||
}) | ||
.argv, | ||
packageJson = require('./package.json'), | ||
jsdocParser = require('jsdoc3-parser'), | ||
analyze = require('./lib/analyze'), | ||
generateMD = require('./lib/generateMD'); | ||
generateMD = require('./lib/generateMD'), | ||
index= { | ||
classes: [], | ||
functions: [] | ||
}; | ||
|
||
function inspect(text) { | ||
return util.inspect(text, false, 20); | ||
|
@@ -58,13 +71,17 @@ function printHelp(){ | |
console.log('Usage:\tjsdox [options] <file | directory>'); | ||
console.log('\tjsdox --All --output docs folder\n'); | ||
console.log('Options:'); | ||
console.log(' -c, --config \t<file>\t Configuration JSON file.'); | ||
console.log(' -A, --All\t\t Generates documentation for all available elements including internal methods.'); | ||
console.log(' -d, --debug\t\t Prints debugging information to the console.'); | ||
console.log(' -H, --help\t\t Prints this message and quits.'); | ||
console.log(' -v, --version\t\t Prints the current version and quits.'); | ||
console.log(' -o, --output\t\t Output directory.'); | ||
console.log(' -t, --templateDir\t Template directory to use instead of built-in ones.'); | ||
console.log(' -c,\t--config \t<file>\t Configuration JSON file.'); | ||
console.log(' -A,\t--All\t\t\t Generates documentation for all available elements including internal methods.'); | ||
console.log(' -d,\t--debug\t\t\t Prints debugging information to the console.'); | ||
console.log(' -H,\t--help\t\t\t Prints this message and quits.'); | ||
console.log(' -v,\t--version\t\t Prints the current version and quits.'); | ||
console.log(' -o,\t--output\t\t Output directory.'); | ||
console.log(' -t,\t--templateDir\t\t Template directory to use instead of built-in ones.'); | ||
console.log(' -i,\t--index\t\t\t Generates an index with the documentation. A file name can be provided in argument.'); | ||
console.log(' -r,\t--recursive\t\t Generates documentation in all subdirectories of the directory given as argument.'); | ||
console.log(' --rr,\t--respect-recursive\t Will generate subdirectories and copy the original organization of the sources.'); | ||
|
||
process.exit(); | ||
} | ||
|
||
|
@@ -85,8 +102,27 @@ function generateForDir(filename, destination, templateDir, cb, fileCb) { | |
touched = 0, | ||
error = null; | ||
|
||
var readdirSyncRec = function(dir, filelist) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a separate module in |
||
var files = fs.readdirSync(dir); | ||
filelist = filelist || []; | ||
files.forEach(function(file) { | ||
if (fs.statSync(path.join(dir,file)).isDirectory()) { | ||
filelist = readdirSyncRec(path.join(dir,file), filelist); | ||
} | ||
else { | ||
filelist.push(path.join(dir,file)); | ||
} | ||
}); | ||
return filelist; | ||
}; | ||
|
||
function oneFile(directory, file, cb) { | ||
var fullpath = path.join(destination, file); | ||
var fullpath; | ||
if(argv.rr) { | ||
fullpath = path.join(path.join(destination, path.dirname(file)), path.basename(file)); | ||
}else{ | ||
fullpath = path.join(destination, file); | ||
} | ||
fullpath = fullpath.replace(/\.js$/, '.md'); | ||
|
||
if (argv.debug) { | ||
|
@@ -95,7 +131,7 @@ function generateForDir(filename, destination, templateDir, cb, fileCb) { | |
|
||
waiting++; | ||
|
||
jsdocParser(path.join(directory, file), function(err, result) { | ||
jsdocParser(path.join(directory, path.basename(file)), function(err, result) { | ||
if (err) { | ||
console.error('Error generating docs for file', file, err); | ||
waiting--; | ||
|
@@ -114,6 +150,28 @@ function generateForDir(filename, destination, templateDir, cb, fileCb) { | |
var data = analyze(result, argv), | ||
output = generateMD(data, templateDir); | ||
|
||
|
||
if(argv.index) { | ||
for (var i = 0; i < data.functions.length; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would love for all of this logic to be abstracted into a separate module to keep jsdox.js as clean as possible. It also makes it easier to unit test that module in isolation. |
||
if (data.functions[i].className === undefined) { | ||
var toAddFct = data.functions[i]; | ||
toAddFct.file = path.relative(destination,fullpath); | ||
toAddFct.sourcePath = path.relative(destination,path.join(directory,path.basename(file))); | ||
index.functions.push(toAddFct); | ||
} | ||
} | ||
for (var j = 0; j < data.classes.length; j++) { | ||
if (data.functions[j].className === undefined) { | ||
var toAddClass = data.classes[j]; | ||
toAddClass.file = path.relative(destination,fullpath); | ||
toAddClass.sourcePath = path.relative(destination,path.join(directory,path.basename(file))); | ||
index.classes.push(toAddClass); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
if (output) { | ||
fileCb && fileCb(file, data); | ||
fs.writeFile(fullpath, output, function(err) { | ||
|
@@ -140,26 +198,64 @@ function generateForDir(filename, destination, templateDir, cb, fileCb) { | |
oneFile(path.dirname(filename), path.basename(filename), cb); | ||
|
||
} else { | ||
fs.stat(filename, function (err, s) { | ||
if (!err && s.isDirectory()) { | ||
fs.readdir(filename, function(err, files) { | ||
if (err) { | ||
console.error('Error generating docs for files', filename, err); | ||
return cb(err); | ||
} | ||
files.forEach(function(file) { | ||
if (file.match(/\.js$/)) { | ||
oneFile(filename, file, cb), touched++; | ||
if(argv.recursive || argv.rr){ | ||
fs.stat(filename, function (err, s) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separate module with tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want me to fix this and prepare a new pull request ? I plan also to take a look at the line number issue in a short future (depending on my uni obligation) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes please. You're the best! |
||
if (!err && s.isDirectory()) { | ||
var contentList=readdirSyncRec(filename); | ||
contentList.forEach(function(fileFullPath) { | ||
if(argv.rr){ | ||
//create the sub-directories | ||
try{ | ||
fs.mkdirSync(path.join(destination,path.dirname(fileFullPath))); | ||
}catch(err){} //lazy way: if the file already exists, everything is alright. | ||
try { | ||
oneFile(path.dirname(fileFullPath), fileFullPath, cb), touched++; | ||
}catch(err){ | ||
console.error('Error generating docs for files', path.basename(fileFullPath), err); | ||
return cb(err); | ||
} | ||
}else { | ||
try{ | ||
oneFile(path.dirname(fileFullPath), path.basename(fileFullPath), cb), touched++; | ||
}catch(err){ | ||
console.error('Error generating docs for files', path.basename(fileFullPath), err); | ||
return cb(err); | ||
} | ||
} | ||
|
||
|
||
}); | ||
if(!touched) { | ||
cb(); | ||
} | ||
}); | ||
} else { | ||
cb(); | ||
} | ||
}); | ||
|
||
} else { | ||
cb(); | ||
} | ||
}); | ||
}else { | ||
fs.stat(filename, function (err, s) { | ||
if (!err && s.isDirectory()) { | ||
fs.readdir(filename, function (err, files) { | ||
if (err) { | ||
console.error('Error generating docs for files', filename, err); | ||
return cb(err); | ||
} | ||
files.forEach(function (file) { | ||
if (file.match(/\.js$/)) { | ||
oneFile(filename, file, cb), touched++; | ||
} | ||
}); | ||
if (!touched) { | ||
cb(); | ||
} | ||
}); | ||
} else { | ||
cb(); | ||
} | ||
}); | ||
} | ||
|
||
} | ||
} | ||
|
||
|
@@ -214,9 +310,29 @@ function main(){ | |
|
||
return deferred.promise; | ||
})) | ||
.then(function () { | ||
console.log('jsdox completed'); | ||
}); | ||
.then(function(){ | ||
//create index | ||
if(argv.index) { | ||
var fileName; | ||
if(argv.index===true){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces around operators. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed, a bunch of spaces missing. I also like to see spaces after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated my IDE settings to be compliant with spacing rules. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. We'll have tooling soon (JSCS) that will have travis-ci fail your PR for the styling violations. |
||
fileName='index'; | ||
}else{ | ||
fileName=argv.index; | ||
} | ||
if(typeof argv.output === 'string'){ | ||
fileName=path.join(argv.output,fileName); | ||
}else{ | ||
fileName=path.join('output',fileName); | ||
} | ||
fs.writeFileSync(fileName+'.md', generateMD(index, argv.templateDir, true)); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty spacing. |
||
|
||
|
||
}) | ||
.then(function () { | ||
console.log('jsdox completed'); | ||
}); | ||
}); | ||
} else { | ||
console.error('Error missing input file or directory.'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks much better. Thank you.