Skip to content

Commit

Permalink
continue...
Browse files Browse the repository at this point in the history
  • Loading branch information
xch89820 committed Oct 27, 2014
1 parent 0353d73 commit 4f2d626
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 20 deletions.
78 changes: 63 additions & 15 deletions docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

var fs = require("fs");
var tmp = require("tmp");
var jsdoc = require("jsdoc");
var path = require("path");
var util = require("util");

var exec = require("./exec.js");

Expand All @@ -20,7 +20,7 @@ var isRootPath = function(path){
};

//Default template path
var DEFAULT_TEMPLATE = "./node_modules/ink-docstrap/template";
var DEFAULT_TEMPLATE = "node_modules/fis-command-jsdoc/node_modules/ink-docstrap/template";

exports.name = 'jsdoc';
exports.desc = 'Generate the JSDoc document for F.I.S';
Expand All @@ -30,64 +30,112 @@ exports.register = function(commander){
.option('--dest', 'The destination of JSDoc. The document will generate to the "docs" directory in your project in default.', String)
.option('--conf', 'The configuration of JSDoc. It may be overwrited by the setting in fis-conf.js', String)
.option('--template', 'The JSDoc template path', String)
.option('--verbose', 'Debug option', String)
.action(function(){
var docOptions,
var docOptions = {},
options = arguments[arguments.length - 1],
rootPath = fis.util.realpath();
rootPath = fis.util.realpath(".");
//Loading the fis-conf.js
var fisConf = path.join(rootPath, "./fis-conf.js");
if (fis.util.isFile(fisConf)){
require(fisConf);
}

//First, read configure file if exist
if (options.conf){
if (options.conf ){
var jsDocPath = isRootPath(options.conf) ? path.join(rootPath, options.conf) : options.conf;
if (fis.util.isFile(jsDocPath)){
docOptions = fis.util.readJSON(jsDocPath) || {};
}
}

//Second, read configure from fis-conf.js
docOptions = fis.util.merge(fis.config.get('settings.jsdoc.options'), docOptions);
docOptions = fis.util.merge(fis.config.get('settings.jsdoc.options'), docOptions) || {};

//Merge to arguments of jsdoc
//Add the support for @private tag
var jsDocArgs = {"private" : true};
//Get the source file from the options
var source = options.src;
if (source){
if (fis.util.isDir(source)){
var sources = options.src;
if (sources){
if (fis.util.isDir(sources)){
//When the source is an directory, open the recurse options
jsDocArgs.recurse = true;
}
}else{
//TODO:Get the source files and can support filter like grunt

sources = [];
//Reading the src configuration from fis-conf.js
var optSrc = fis.config.get('settings.jsdoc.src');
var optSrcFilter = fis.config.get('settings.jsdoc.filter');
if (!util.isArray(optSrc)){
optSrc = [optSrc];
}
//Find all source files and filter them if the filter function existed
for (var i=optSrc.length-1; i>=0; i--){
var sreg = optSrc[i];
if (fis.util.is(sreg, "String") && !isRootPath(sreg)){
sreg = path.join(rootPath, sreg);
}
var srcs = fis.util.find(rootPath, sreg);
if (srcs){
if (optSrcFilter){
srcs = srcs.filter(optSrcFilter);
}
sources = sources.concat(srcs);
}
}
}
fis.log.debug("The source of project is [" + sources.join(",") + "]");

//Get the destination from the options or point to the default destination
jsDocArgs.destination = (
options.dest &&
(isRootPath(options.dest) ? path.join(rootPath, options.dest) : options.dest)
) || "docs";
//Check the template.The plugin includes docstrap, as well as the default template provided by jsdoc3.
jsDocArgs.template = options.template || DEFAULT_TEMPLATE;
if (options.template){
jsDocArgs.template = options.template;
}else{
var npmPath = exec.getNpmGlobalPath();
jsDocArgs.template = path.join(npmPath, DEFAULT_TEMPLATE);
}

if (options.verbose){
//Open the debug options:w
jsDocArgs.debug = true;
jsDocArgs.verbose = true;
}
docOptions.opts = jsDocArgs;
docOptions.opts = fis.util.merge(jsDocArgs, docOptions.opts || {});

//Generate the jsdoc conf.json
tmp.setGracefulCleanup();
tmp.file(function (err, path, fd) {
if (err) {
throw err;
}

//Write the jsDoc configuration
fs.writeSync(fd, JSON.stringify(docOptions));
fs.fsyncSync(fd);

exec.swapn(source, {
var execChild = exec.spawn(sources, {
"configure": path
});

//logs
execChild.stdout.on("data", function (data){
fis.log.debug("jsDoc output : " + data);
});
execChild.stderr.on("data", function(data){
console.error("An error occurs in jsDoc process:\n" + data);
});
execChild.on("exit", function(code){
if (code === 0){
console.log("Documentation generated to " + path.resolve(docOptions.opts.destination));
}else{
console.error("Documentation generated failed. Error code: " + code);
}
});
});
});
};
38 changes: 33 additions & 5 deletions exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,43 @@
* The module for execute command for jsDoc
*/

var fis = require("fis");
var util = require("util");
var path = require("path");
var child_process = require('child_process');
var spawnSync = require('child_process').spawnSync || require('spawn-sync');

module.exports = {
var DEFAULT_JSDOC_PATH = "node_modules/fis-command-jsdoc/node_modules/jsdoc/jsdoc";
var execModule = {
/**
* Return the npm Global installs path
* You can see [here]{@link https://www.npmjs.org/doc/files/npm-folders.html} for more information
*/
getNpmGlobalPath: function(callback){
var isWin = process.platform === 'win32';
var result = spawnSync('npm config get prefix');

if (result.status !== 0){
process.stderr.write(result.stderr);
process.exit(result.status);
}else{
return isWin ? result.stdout.toString().trim() : path.join(result.stdout.toString().trim(), "lib");
}
},
/**
* Execute the jsdoc command
*/
spawn : function(sources, options){
var args = [];
isWin = process.platform === 'win32';

var script = isWin ? "cmd /c node -e " : "";
script += "node_modules/jsdoc/jsdoc.js";
var script = isWin ? "cmd /c " : "";
if (spawnSync('jsdoc -v').status === 0){
script += "jsdoc";
}else{
var npmPath = execModule.getNpmGlobalPath();
script += path.join(npmPath, DEFAULT_JSDOC_PATH);
}
fis.log.debug("JsDoc command : " + script);

var cmd = "";
fis.util.map(options, function(key, value) {
Expand Down Expand Up @@ -46,8 +73,9 @@ module.exports = {

fis.log.debug("Running : "+ script + " " + args.join(' '));

return spawn(script, args, {
return child_process.spawn(script, args, {
windowsVerbatimArguments: isWin // documentation PR is pending: https://github.com/joyent/node/pull/4259
});
}
};
module.exports = execModule;

0 comments on commit 4f2d626

Please sign in to comment.