66 * Licensed under the MIT license.
77 */
88'use strict' ;
9+ var path = require ( 'path' ) ,
10+ chalk = require ( 'chalk' ) ,
11+ documentation = require ( 'documentation' ) ,
12+ formats = require ( 'documentation' ) . formats ;
913
10- module . exports = function ( grunt ) {
11- var path = require ( 'path' ) ,
12- chalk = require ( 'chalk' ) ,
13- documentation = require ( 'documentation' ) . build || require ( 'documentation' ) ,
14- formats = require ( 'documentation' ) . formats ;
15-
14+ function gruntDocumentation ( grunt ) {
1615 grunt . registerMultiTask (
1716 'documentation' ,
1817 'Use Grunt with documentation to generate great documentation for your JavaScript projects.' ,
@@ -25,6 +24,12 @@ module.exports = function(grunt) {
2524 order : [ ]
2625 } ) ;
2726
27+ var done = this . async ( ) ;
28+
29+ var inputs = this . files . map ( function ( file ) {
30+ return file . src [ 0 ] ;
31+ } ) ;
32+
2833 var formatter = formats [ options . format ] ;
2934 if ( ! formatter ) {
3035 throw new Error (
@@ -43,107 +48,58 @@ module.exports = function(grunt) {
4348 version : options . version
4449 } ;
4550
46- var done = this . async ( ) , waiting = 0 ;
47-
48- function wait ( c ) {
49- c = c || 1 ;
50- waiting += c ;
51- }
52-
53- function release ( ) {
54- waiting -- ;
55- if ( waiting <= 0 ) {
56- done ( ) ;
57- grunt . log . writeln (
58- chalk . green ( 'Done, created documentation at ' ) +
59- path . join ( process . cwd ( ) , options . destination )
60- ) ;
61- }
62- }
63-
64- function generateDocs ( files ) {
65- wait ( ) ;
66- documentation ( files , docOptions , function ( err , comments ) {
67- if ( err ) {
68- grunt . log . error ( err . toString ( ) ) ;
69- if ( err . codeFrame ) {
70- grunt . log . error ( err . codeFrame ) ;
71- }
72- done ( false ) ;
73- } else {
74- wait ( ) ;
75- formatter ( comments , docOptions , function ( err , output ) {
76- if ( err ) {
77- grunt . log . error ( err . toString ( ) ) ;
78- if ( err . codeFrame ) {
79- grunt . log . error ( err . codeFrame ) ;
80- }
81- done ( false ) ;
82- } else {
83- if ( options . format === 'json' || options . format === 'md' ) {
84- var dest = path . join (
85- process . cwd ( ) ,
86- options . destination ,
87- options . filename || 'API.' + options . format
51+ documentation
52+ . build ( inputs , docOptions )
53+ . then ( function ( comments ) {
54+ return formatter ( comments , docOptions ) ;
55+ } )
56+ . then ( function ( output ) {
57+ switch ( options . format ) {
58+ case 'json' :
59+ case 'md' :
60+ var dest = path . join (
61+ process . cwd ( ) ,
62+ options . destination ,
63+ options . filename || 'API.' + options . format
64+ ) ;
65+ grunt . file . write ( dest , output ) ;
66+ grunt . log . writeln (
67+ 'File ' +
68+ chalk . cyan ( options . filename || 'API.' + options . format ) +
69+ ' created.'
70+ ) ;
71+ break ;
72+ case 'html' :
73+ output . forEach ( function ( file ) {
74+ var dest = path . join (
75+ process . cwd ( ) ,
76+ options . destination ,
77+ file . relative
78+ ) ;
79+ if ( file . isDirectory ( ) || grunt . file . isDir ( file . path ) ) {
80+ grunt . file . mkdir ( dest ) ;
81+ grunt . verbose . writeln (
82+ 'Directory ' + chalk . cyan ( file . relative ) + ' created.'
8883 ) ;
89- grunt . file . write ( dest , output ) ;
90- grunt . log . writeln (
91- 'File ' +
92- chalk . cyan ( options . filename || 'API.' + options . format ) +
93- ' created.'
84+ } else {
85+ grunt . file . write ( dest , file . contents ) ;
86+ grunt . verbose . writeln (
87+ 'File ' + chalk . cyan ( file . relative ) + ' created.'
9488 ) ;
95- } else if ( options . format === 'html' ) {
96- wait ( output . length ) ;
97- output . forEach ( function ( file ) {
98- var dest = path . join (
99- process . cwd ( ) ,
100- options . destination ,
101- file . relative
102- ) ;
103- if ( file . isDirectory ( ) || grunt . file . isDir ( file . path ) ) {
104- grunt . file . mkdir ( dest ) ;
105- grunt . verbose . writeln (
106- 'Directory ' + chalk . cyan ( file . relative ) + ' created.'
107- ) ;
108- } else {
109- grunt . file . write ( dest , file . contents ) ;
110- grunt . verbose . writeln (
111- 'File ' + chalk . cyan ( file . relative ) + ' created.'
112- ) ;
113- }
114- release ( ) ;
115- } ) ;
11689 }
117- }
118- release ( ) ;
119- } ) ;
90+ } ) ;
12091 }
121- release ( ) ;
122- } ) ;
123- }
124-
125- var files = [ ] ;
126-
127- var filesToProcess = this . files . length ;
128-
129- this . files . forEach (
130- function ( f ) {
131- var src = f . src . filter ( function ( filepath ) {
132- // Warn on and remove invalid source files (if nonull was set).
133- if ( ! grunt . file . exists ( filepath ) ) {
134- grunt . log . warn ( 'Source file "' + filepath + '" not found.' ) ;
135- return false ;
136- } else {
137- return true ;
138- }
139- } ) ;
140- files = files . concat ( src ) ;
141- filesToProcess -- ;
142- if ( filesToProcess <= 0 ) {
143- generateDocs ( files ) ;
92+ done ( true ) ;
93+ } )
94+ . catch ( function ( err ) {
95+ grunt . log . error ( err . toString ( ) ) ;
96+ if ( err . codeFrame ) {
97+ grunt . log . error ( err . codeFrame ) ;
14498 }
145- } . bind ( this )
146- ) ;
99+ done ( err ) ;
100+ } ) ;
147101 }
148102 ) ;
149- } ;
103+ }
104+
105+ module . exports = gruntDocumentation ;
0 commit comments