-
Notifications
You must be signed in to change notification settings - Fork 0
/
styleguide.js
132 lines (104 loc) · 4.14 KB
/
styleguide.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Dependancies
var dss = require( 'dss' );
var underscore = require( 'underscore' );
var handlebars = require( 'handlebars' );
// Expose
module.exports = function ( grunt ) {
// Register DSS
grunt.registerMultiTask( 'dss', 'Parse DSS comment blocks', function () {
// Setup async promise
var promise = this.async();
// Merge task-specific and/or target-specific options with defaults
var options = this.options({
template: __dirname + '/../template/',
template_index: 'index.handlebars',
output_index: 'index.html',
include_empty_files: true
});
// Output options if --verbose cl option is passed
grunt.verbose.writeflags( options, 'Options' );
// Describe custom parsers
for ( key in options.parsers ) {
dss.parser( key, options.parsers[ key ] );
}
// Build Documentation
this.files.forEach( function( f ) {
// Filter files based on their existence
var src = f.src.filter( function ( filepath ) {
// Warn on and remove invalid source files (if nonull was set).
if ( !grunt.file.exists( filepath ) ) {
grunt.log.warn( 'Source file "' + filepath + '" not found.' );
return false;
} else {
return true;
}
});
// Setup
var files = src;
var template_dir = options.template;
var output_dir = f.dest;
var length = files.length;
var styleguide = [];
// Parse files
files.map( function ( filename ) {
// Report file
grunt.verbose.writeln( '• ' + grunt.log.wordlist( [ filename ], { color: 'cyan' } ) );
// Parse
dss.parse( grunt.file.read( filename ), { file: filename }, function ( parsed ) {
// Continue only if file contains DSS annotation
if ( options.include_empty_files || parsed.blocks.length ) {
// Add filename
parsed[ 'file' ] = filename;
// Add comment block to styleguide
styleguide.push( parsed );
}
// Check if we're done
if ( length > 1 ) {
length--;
} else {
// Set output template and file
var template_filepath = template_dir + options.template_index;
var output_filepath = output_dir + options.output_index;
if ( !grunt.file.exists( template_filepath ) ) {
grunt.fail( 'Cannot read the template file' );
}
// copy template assets (except index.handlebars)
var map_files = [ '**/*', '!' + options.template_index ];
var map_expanded = grunt.file.expandMapping( map_files, output_dir, { cwd: template_dir } );
map_expanded.forEach( function ( filePair ) {
filePair.src.forEach( function ( src ) {
if ( grunt.file.isDir( src ) ) {
grunt.verbose.writeln( 'Creating ' + filePair.dest.cyan );
grunt.file.mkdir( filePair.dest );
} else {
grunt.verbose.writeln( 'Copying ' + src.cyan + ' -> ' + filePair.dest.cyan );
grunt.file.copy( src, filePair.dest );
}
});
});
// Create HTML ouput
var html = handlebars.compile( grunt.file.read( template_filepath ) ) ( {
project: grunt.file.readJSON( 'package.json' ),
files: styleguide
});
var output_type = 'created';
var output = null;
if ( grunt.file.exists( output_filepath ) ) {
output_type = 'overwrited';
output = grunt.file.read( output_filepath );
}
// Avoid write if there is no change
if ( output !== html ) {
grunt.file.write( output_filepath, html );
grunt.log.writeln( '✓ Styleguide ' + output_type + ' at: ' + grunt.log.wordlist( [ output_dir ], { color: 'cyan' } ) );
} else {
grunt.log.writeln( '‣ Styleguide unchanged' );
}
// Return promise
promise();
}
});
});
});
});
};