Skip to content
Browse files

Moved around CLI handler into a YUIDoc class so it can be called from…

… options.
  • Loading branch information...
1 parent fccc38f commit fcb494826bc9bdf01c66bdf9cba24c8dec24f0f1 @davglass committed
Showing with 247 additions and 135 deletions.
  1. +60 −0 lib/cli.js
  2. +0 −3 lib/docparser.js
  3. +183 −126 lib/yuidoc.js
  4. +4 −6 package.json
View
60 lib/cli.js
@@ -0,0 +1,60 @@
+#!/usr/bin/env node
+
+var YUI = require("yui3").YUI,
+ fs = require("fs"),
+ sys = require("sys"),
+ path = require("path");
+
+YUI({
+ modules: {
+ docparser: {
+ fullpath: path.join(__dirname, 'docparser.js'),
+ requires: ['base-base', 'json-stringify']
+ },
+ yuidoc: {
+ fullpath: path.join(__dirname, 'yuidoc.js')
+ }
+ },
+ logExclude: {
+ yui: true,
+ get: true,
+ loader: true
+ }
+}).use('docparser', 'yuidoc', function(Y) {
+
+ var args = Y.Array(process.argv, 2),
+ options = {
+ paths: []
+ };
+
+ while (args.length > 0) {
+ var v = args.shift();
+ switch (v) {
+ case "-e":
+ case "--extension":
+ options.extension = args.shift();
+ break;
+ case "-x":
+ case "--exclude":
+ options.extension = args.shift();
+ break;
+ case "-v":
+ case "--version":
+ options.version = args.shift();
+ break;
+ case "-n":
+ case "--norecurse":
+ options.norecurse = true;
+ break;
+ case "-o":
+ case "--outdir":
+ options.outdir = args.shift();
+ break;
+ default:
+ options.paths.push(v);
+ }
+ }
+
+
+ (new Y.YUIDoc(options)).run();
+});
View
3 lib/docparser.js
@@ -22,7 +22,6 @@ YUI.add('docparser', function(Y) {
* during initialization, and will be updated if additional
* digesters are added.
* @property TAGLIST
- * @inner
* @final
*/
TAGLIST = [
@@ -95,7 +94,6 @@ YUI.add('docparser', function(Y) {
/**
* Common errors will get scrubbed instead of being ignored.
* @property CORRECTIONS
- * @inner
* @final
*/
CORRECTIONS = {
@@ -116,7 +114,6 @@ YUI.add('docparser', function(Y) {
* by supplying the string name that points to the
* implementation rather than a function.
* @property DIGESTERS
- * @inner
* @final
*/
DIGESTERS = {
View
309 lib/yuidoc.js
@@ -1,144 +1,201 @@
-#! /usr/bin/env node
-// -*- coding: utf-8 -*-
-// vim: et sw=4 ts=4
-
-var YUI = require("yui3").YUI,
- fs = require("fs"),
+var fs = require("fs"),
sys = require("sys"),
path = require("path");
+YUI.add('yuidoc', function(Y) {
-YUI({
- modules: {
- docparser: {
- fullpath: path.join(__dirname, 'docparser.js'),
- requires: ['base-base', 'json-stringify']
- }
- },
- logExclude: {
- yui: true,
- get: true,
- loader: true
- }
-}).use('docparser', function(Y) {
-
- var options = {
+
+ /**
+ * The default list of configuration options
+ * @property OPTIONS
+ * @final
+ * @for YUIDoc
+ */
+
+ var OPTIONS = {
+ writeJSON: true,
outdir: path.join(process.cwd(), 'out'),
extension: '.js',
exclude: '.DS_Store,.svn,CVS,.git,build_rollup_tmp,build_tmp',
norecurse: false,
- version: '0.1.0'
- },
- args = Y.Array(process.argv, 2),
- paths = [],
- starttime = new Date().getTime(),
- filecount = 0,
- config = function() {
- while (args.length > 0) {
- var v = args.shift();
- switch (v) {
- case "-e":
- case "--extension":
- options.extension = args.shift();
- break;
- case "-x":
- case "--exclude":
- options.extension = args.shift();
- break;
- case "-v":
- case "--version":
- options.version = args.shift();
- break;
- case "-n":
- case "--norecurse":
- options.norecurse = true;
- break;
- case "-o":
- case "--outdir":
- options.outdir = args.shift();
- break;
- default:
- paths.push(v);
- }
- }
- options.extensions = Y.Array.hash(options.extension.split(','));
- options.excludes = Y.Array.hash(options.exclude.split(','));
- },
-
- output = function(parser) {
- // Y.log(Y.JSON.stringify(parser.data, null, 4));
- var file = path.join(options.outdir, 'data.json'), out;
- if (!path.existsSync(options.outdir)) {
- Y.log('Making out dir: ' + options.outdir, 'warn', 'yuidoc');
- fs.mkdirSync(options.outdir, 0777);
- }
- out = fs.createWriteStream(file, {
- flags: "w", encoding: "utf8", mode: 0644
- });
- out.write(Y.JSON.stringify(parser.data, null, 4));
- out.end();
- },
-
- filemap = {},
- dirmap = {},
-
- run = function() {
- output(new Y.DocParser({
- filemap: filemap,
- dirmap: dirmap
- }).parse());
-
- var endtime = new Date().getTime();
-
-Y.log('Parsed ' + filecount + ' files in ' + ((endtime - starttime) / 1000) + ' seconds');
- },
-
- parsefiles = function(dir, files) {
- Y.each(files, function(filename) {
- var ext = path.extname(filename), text, fullpath;
- if (ext) {
- if (ext in options.extensions) {
+ version: '0.1.0',
+ paths: []
+ };
+
+ /**
+ * YUIDoc main class
+ * @uses DocParser
+ * @class YUIDoc
+ * @constructor
+ * @param config The config object
+ */
+ Y.YUIDoc = function(config) {
+ /**
+ * Holds the number of files that we are processing.
+ * @property filecount
+ * @type Boolean
+ * @private
+ */
+ this.filecount = 0;
+ /**
+ * Holder for the list of files we are processing.
+ * @property filemap
+ * @type Object
+ * @private
+ */
+ this.filemap = {};
+ /**
+ * Holder for the list of directories we are processing.
+ * @property dirmap
+ * @type Object
+ * @private
+ */
+ this.dirmap = {};
+
+ /**
+ * Internal holder for configuration options.
+ * @property options
+ * @type Object
+ * @private
+ */
+ this.options = Y.merge(OPTIONS, config);
+
+ };
+
+ Y.YUIDoc.prototype = {
+ /**
+ * Does post process on self.options.
+ * @method _processConfig
+ * @private
+ */
+ _processConfig: function() {
+ this.options.extensions = Y.Array.hash(this.options.extension.split(','));
+ this.options.excludes = Y.Array.hash(this.options.exclude.split(','));
+ },
+ /**
+ * Walks the paths and parses the directory contents
+ * @method walk
+ * @private
+ */
+ walk: function() {
+ Y.each(this.options.paths, function(dir) {
+ this.parsedir(dir);
+ }, this);
+ },
+ /**
+ * Walks the passed directory and grabs all the files recursively.
+ * @method parsedir
+ * @param {String} dir The directory to parse the contents of.
+ * @private
+ */
+ parsedir: function(dir) {
+ var allfiles = fs.readdirSync(dir), stats,
+ files = [], fullpath, self = this;
+
+ Y.each(allfiles, function(filename) {
+ if (!(filename in self.options.excludes)) {
fullpath = path.join(dir, filename);
- if (path.existsSync(fullpath)) {
- filecount++;
- text = fs.readFileSync(fullpath, "utf8");
- filemap[fullpath] = text;
- dirmap[fullpath] = dir;
+
+ stats = fs.statSync(fullpath);
+
+ if (stats.isDirectory() && !self.options.norecurse) {
+ self.parsedir(fullpath);
} else {
- Y.log('File skipped: ' + fullpath, 'warn', 'yuidoc');
+ files.push(filename);
}
}
- }
- });
- },
-
- parsedir = function(dir) {
- var allfiles = fs.readdirSync(dir), stats,
- files = [], fullpath;
- Y.each(allfiles, function(filename) {
- if (!(filename in options.excludes)) {
- fullpath = path.join(dir, filename);
- stats = fs.statSync(fullpath);
- if (stats.isDirectory() && !options.norecurse) {
- parsedir(fullpath);
- } else {
- files.push(filename);
+ });
+
+ this.parsefiles(dir, files);
+ },
+ /**
+ * Gathers all the file data and populates the filemap and dirmap hashes.
+ * @method parsefiles
+ * @param {String} dir The directory to start from.
+ * @param {Array} files List of files to parse.
+ * @private
+ */
+ parsefiles: function(dir, files) {
+ var self = this;
+
+ Y.each(files, function(filename) {
+ var ext = path.extname(filename), text, fullpath;
+
+ if (ext) {
+ if (ext in self.options.extensions) {
+ fullpath = path.join(dir, filename);
+
+ if (path.existsSync(fullpath)) {
+ self.filecount++;
+ text = fs.readFileSync(fullpath, "utf8");
+
+ self.filemap[fullpath] = text;
+ self.dirmap[fullpath] = dir;
+ } else {
+ Y.log('File skipped: ' + fullpath, 'warn', 'yuidoc');
+ }
+ }
}
+ });
+ },
+ /**
+ * Writes the parser JSON data to disk.
+ * @method writeJSON
+ * @param {Object} parser The DocParser instance to use
+ * @private
+ * @return {Object} The JSON data returned from the DocParser
+ */
+ writeJSON: function(parser) {
+ var self = this;
+ if (self.options.writeJSON) {
+ // Y.log(Y.JSON.stringify(parser.data, null, 4));
+ var file = path.join(self.options.outdir, 'data.json'), out;
+ if (!path.existsSync(self.options.outdir)) {
+ Y.log('Making out dir: ' + self.options.outdir, 'warn', 'yuidoc');
+ fs.mkdirSync(self.options.outdir, 0777);
+ }
+ out = fs.createWriteStream(file, {
+ flags: "w", encoding: "utf8", mode: 0644
+ });
+ out.write(Y.JSON.stringify(parser.data, null, 4));
+ out.end();
}
- });
-
- parsefiles(dir, files);
- },
- load = function() {
- Y.each(paths, function(dir) {
- parsedir(dir);
- });
+ return parser.data;
+ },
+ /**
+ * Process the config, walk the file tree and write out the JSON data.
+ * @method run
+ * @return {Object} The JSON data returned from the DocParser
+ */
+ run: function() {
+ /**
+ * Timestamp holder so we know when YUIDoc started the parse process.
+ * @property starttime
+ */
+ Y.log('YUIDoc Started', 'info', 'yuidoc');
+ this.starttime = new Date().getTime();
+
+ var self = this;
+
+ this._processConfig();
+ this.walk();
+
+ var json = this.writeJSON(new Y.DocParser({
+ filemap: self.filemap,
+ dirmap: self.dirmap
+ }).parse());
+
+ /**
+ * Timestamp holder so we know when YUIDoc has finished the parse process.
+ * @property endtime
+ */
+ this.endtime = new Date().getTime();
+
+ Y.log('Parsed ' + this.filecount + ' files in ' + ((this.endtime - this.starttime) / 1000) + ' seconds');
+
+ return json;
+ }
};
- config();
- load();
- run();
-
});
-
View
10 package.json
@@ -9,15 +9,13 @@
{ "name": "Dav Glass", "email": "davglass@gmail.com" }
],
"engines": {
- "node" : ">=0.1.100"
- },
- "directories": {
- "lib" : "lib"
+ "node" : ">=0.4.0"
},
"main": "./lib/yuidoc",
- "bin" : { "yuidoc" : "./lib/yuidoc.js" },
+ "bin" : { "yuidoc" : "./lib/cli.js" },
"dependencies": {
- "yui3-core": ">=3.2.0"
+ "yui3-core": ">=3.3.0",
+ "mustache": "0.3.x"
},
"licenses":[
{

0 comments on commit fcb4948

Please sign in to comment.
Something went wrong with that request. Please try again.