From 4f2d6265ba5c0e883f8942f0b7495b1018a15813 Mon Sep 17 00:00:00 2001 From: Jone Casper Date: Mon, 27 Oct 2014 12:59:09 -0400 Subject: [PATCH] continue... --- docs.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++----------- exec.js | 38 ++++++++++++++++++++++++---- 2 files changed, 96 insertions(+), 20 deletions(-) diff --git a/docs.js b/docs.js index 0d5ba1d..cfc69be 100644 --- a/docs.js +++ b/docs.js @@ -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"); @@ -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'; @@ -30,13 +30,19 @@ 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) || {}; @@ -44,22 +50,42 @@ exports.register = function(commander){ } //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 = ( @@ -67,27 +93,49 @@ exports.register = function(commander){ (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); + } + }); }); }); }; diff --git a/exec.js b/exec.js index 3161aaf..1a3f3ef 100644 --- a/exec.js +++ b/exec.js @@ -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) { @@ -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;