forked from Tyler-Gordon/docstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
383 lines (356 loc) · 12.3 KB
/
Gruntfile.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"use strict";
/**
* @fileOverview Gruntfile tasks. These tasks are intended to help you when modifying the template. If you are
* just using the template, don't sweat this stuff. To use these tasks, you must install grunt, if you haven't already,
* and install the dependencies. All of this requires node.js, of course.
*
* Install grunt:
*
* npm install -g grunt-cli
*
* Then in the directory where you found this file:
*
* npm install
*
* And you are all set. See the individual tasks for details.
*
* @module Gruntfile
* @requires path
* @requires lodash
* @requires http
* @requires async
* @requires fs
*/
var path = require( "path" );
var _ = require( "lodash" );
var request = require('request');
var async = require( "async" );
var fs = require( "fs" );
// this rather odd arrangement of composing tasks like this to make sure this works on both
// windows and linux correctly. We can't depend on Grunt or Node to normalize
// paths for us because we shell out to make this work. So we gather up
// our relative paths here, normalize them later and then pass them into
// the shell to be run by JSDoc3.
/**
* The definition to run the development test files. This runs the files in `fixtures` with the
* project's `conf.json` file.
* @private
*/
var jsdocTestPages = {
dest : "./testdocs",
tutorials : "./fixtures/tutorials",
template : "./template",
config : "./fixtures/testdocs.conf.json",
options : " --lenient --verbose --recurse"
};
/**
* The definition to run the sample files. This runs the files in `fixtures` with the
* sample's `conf.json` file. No task directly exposes this configuration. The `fixtures` task
* modifies this for each swatch it finds and then run the docs command against it.
* @private
*/
var jsdocExamplePages = {
src : ["./fixtures/", "./README.md"],
dest : "./themes",
tutorials : "./fixtures/tutorials",
template : "./template",
config : "./fixtures/example.conf.json",
options : " --lenient --verbose --recurse"
};
/**
* This definition provides the project's main, published documentation.
* @private
*/
var projectDocs = {
src : ["./Gruntfile.js", "./README.md", "./template/publish.js"],
dest : "./dox",
tutorials : "",
template : "./template",
config : "./template/jsdoc.conf.json",
options : " --lenient --verbose --recurse --private"
};
/**
* Normalizes all paths from a JSDoc task definition and and returns an executable string that can be passed to the shell.
* @param {object} jsdoc A JSDoc definition
* @returns {string}
*/
function jsdocCommand( jsdoc ) {
var cmd = [];
cmd.unshift( jsdoc.options );
if ( jsdoc.tutorials.length > 0 ) {
cmd.push( "-u " + path.resolve( jsdoc.tutorials ) );
}
cmd.push( "-d " + path.resolve( jsdoc.dest ) );
cmd.push( "-t " + path.resolve( jsdoc.template ) );
cmd.push( "-c " + path.resolve( jsdoc.config ) );
_.each( jsdoc.src, function ( src ) {
cmd.push( path.resolve( src ) );
} );
cmd.unshift( path.resolve( "./node_modules/jsdoc/jsdoc" ) );
cmd.unshift( "node" );
return cmd.join( " " );
}
var tasks = {
shell : {
options : {
stdout : true,
stderr : true
},
/**
* TASK: Create the a documentation set for testing changes to the template
* @name shell:testdocs
* @memberOf module:Gruntfile
*/
testdocs : {
command : jsdocCommand( jsdocTestPages )
},
/**
* TASK: Create project documentation
* @name shell:dox
* @memberOf module:Gruntfile
*/
dox : {
command : jsdocCommand( projectDocs )
},
release1 : {
command : [
"touch Gruntfile.js",
"git add .",
'git commit -m "ready for release"',
].join( ";" )
},
release2 : {
command : ["npm version patch",
"git push",
"git push --tags",
"npm publish"
].join( "&&" )
}
},
jsdoc : {
testdocs : {
src : ['fixtures/**.js', "./README.md"],
jsdoc : "./node_modules/jsdoc/jsdoc.js",
options : {
destination : './testdocs',
rescurse : true,
"private" : true,
"template" : "./template",
"configure" : "./template/jsdoc.conf.json"
}
}
},
/**
* TASK: The less task creates the themed css file from main.less. The file is written to the template styles
* directory as site.[name of theme].css. Later the .conf file will look for the theme to apply based
* on this naming convention.
* @name less
* @memberOf module:Gruntfile
*/
less : {
dev : {
files : {
"template/static/styles/site.<%= jsdocConf.templates.theme %>.css" : "styles/main.less"
}
}
},
copy : {
docs : {
files : [
{expand : true, cwd : "dox/", src : ['**'], dest : '../docstrap-dox/'},
{expand : true, cwd : "themes/", src : ['**'], dest : '../docstrap-dox/themes'}
]
}
},
uglify : {
template : {
files : {
'template/static/scripts/docstrap.lib.js' : [
'bower_components/jquery/dist/jquery.min.js',
'bower_components/sunlight/src/sunlight.js',
'bower_components/sunlight/src/lang/sunlight.xml.js',
'bower_components/sunlight/src/**/*.js',
'bower_components/jquery.scrollTo/jquery.scrollTo.min.js',
'bower_components/jquery.localScroll/jquery.localScroll.min.js',
'bower_components/bootstrap/dist/js/bootstrap.min.js'
]
}
}
}
};
module.exports = function ( grunt ) {
tasks.jsdocConf = grunt.file.readJSON( 'template/jsdoc.conf.json' );
grunt.initConfig( tasks );
grunt.loadNpmTasks( 'grunt-contrib-less' );
grunt.loadNpmTasks( 'grunt-shell' );
grunt.loadNpmTasks( 'grunt-contrib-copy' );
grunt.loadNpmTasks( 'grunt-contrib-uglify' );
grunt.registerTask( "default", ["docs"] );
/**
* Builds the project's documentation
* @name docs
* @memberof module:Gruntfile
*/
grunt.registerTask( "docs", "Create the project documentation", ["shell:dox"] );
/**
* Compile the CSS and create the project documentation
* @name dev
* @memberof module:Gruntfile
*/
grunt.registerTask( "dev", "Compile the CSS and create the project documentation", ["less", "shell:dox"] );
/**
* TASK: Builds the main less file and then generates the test documents
* @name testdocs
* @memberof module:Gruntfile
*/
grunt.registerTask( "testdocs", "Builds the main less file and then generates the test documents", ["less:dev", "shell:testdocs"] );
/**
* TASK: Builds the whole shebang. Which means creating testdocs, the bootswatch fixtures and then resetting the
* styles directory.
* @name build
* @memberof module:Gruntfile
*/
grunt.registerTask( "build", "Builds the whole shebang. Which means creating testdocs, the bootswatch samples and then resetting the styles directory", ["uglify:template", "testdocs", "shell:dox", "bootswatch", "examples", "apply", "copy"] );
/**
* TASK: Applies the theme in the conf file and applies it to the styles directory.
* @name apply
* @memberof module:Gruntfile
*/
grunt.registerTask( "apply", "Applies the theme in the conf file and applies it to the styles directory", function () {
var def = {
less : "http://bootswatch.com/" + tasks.jsdocConf.templates.theme + "/bootswatch.less",
lessVariables : "http://bootswatch.com/" + tasks.jsdocConf.templates.theme + "/variables.less"
};
grunt.registerTask( "swatch-apply", _.partial( applyTheme, grunt, def ) );
grunt.task.run( ["swatch-apply"] );
} );
/**
* TASK: Grab all Bootswatch themes and create css from each one based on the main.less in the styles directory. NOTE that this will
* leave the last swatch downloaded in the styles directory, you will want to call "apply" afterwards
* @name bootswatch
* @memberof module:Gruntfile
*/
grunt.registerTask( "bootswatch", "Grab all Bootswatch themes and create css from each one based on the main.less in the styles directory", function () {
var toRun = [];
var done = this.async();
getBootSwatchList( function ( err, list ) {
if ( err ) {return done( err );}
_.each( list.themes, function ( entry ) {
toRun.push( "swatch" + entry.name );
grunt.registerTask( "swatch" + entry.name, _.partial( applyTheme, grunt, entry ) );
var key = "template/static/styles/site." + entry.name.toLowerCase() + ".css";
var def = {};
def[key] = "styles/main.less";
tasks.less["swatch" + entry.name] = {
files : def
};
toRun.push( "less:swatch" + entry.name );
} );
grunt.task.run( toRun );
done();
} );
} );
/**
* TASK:Create fixtures from the themes. The files must have been built first from the bootswatch task.
* @name examples
* @memberof module:Gruntfile
*/
grunt.registerTask( "examples", "Create samples from the themes", function () {
var toRun = [];
var done = this.async();
getBootSwatchList( function ( err, list ) {
if ( err ) {return done( err );}
_.each( list.themes, function ( entry ) {
var conf = grunt.file.readJSON( './fixtures/example.conf.json' );
conf.templates.theme = entry.name.toLowerCase();
grunt.file.write( "tmp/example.conf." + conf.templates.theme + ".json", JSON.stringify( conf, null, 4 ) );
var jsdenv = _.cloneDeep( jsdocExamplePages );
jsdenv.config = "./tmp/example.conf." + conf.templates.theme + ".json";
jsdenv.dest = "./themes/" + conf.templates.theme;
tasks.shell["example" + conf.templates.theme] = {
command : jsdocCommand( jsdenv )
};
toRun.push( "shell:example" + conf.templates.theme );
} );
grunt.registerTask( "cleanup", "", function () {
grunt.file["delete"]( "tmp/" );
} );
toRun.push( "cleanup" );
grunt.task.run( toRun );
done();
} );
} );
grunt.registerTask( "release", "Create the project documentation", ["shell:release1", "shell:release2"] );
};
/**
* Applies one of the Bootswatch themes to the working `styles` directory. When you want to modify a particular theme, this where you
* get the basis for it. The files are written to `./styles/variables.less` and `./styles/bootswatch.less`. The `./styles/main.less`
* file includes them directly, so after you apply the theme, modify `main.less` to your heart's content and then run the `less` task
* as in
*
* grunt less
*
* @param {object} grunt The grunt object reference
* @param {object} definition The swatch definition files
* @param {string} definition.less The url to the `bootswatch.less` file
* @param {string} definition.lessVariables The url to the `variables.less` file
* @private
*/
function applyTheme( grunt, definition ) {
//noinspection JSHint
var webProtocol = tasks.jsdocConf.templates.protocol || "//";
var done = this.async();
async.waterfall( [
function ( cb ) {
getBootSwatchComponent( definition.less, function ( err, swatch ) {
if ( err ) {return cb( err );}
var fullPath = path.join( __dirname, "styles/bootswatch.less" );
fs.writeFile( fullPath, swatch.replace( "http://", webProtocol ), cb );
} );
},
function ( cb ) {
getBootSwatchComponent( definition.lessVariables, function ( err, swatch ) {
if ( err ) {return cb( err );}
var fullPath = path.join( __dirname, "styles/variables.less" );
fs.writeFile( fullPath, swatch.replace( "http://", webProtocol ), cb );
} );
}
], done );
}
/**
* Gets the list of available Bootswatches from, well, Bootswatch.
*
* @see http://news.bootswatch.com/post/22193315172/bootswatch-api
* @param {function(err, responseBody)} done The callback when complete
* @param {?object} done.err If an error occurred, you will find it here.
* @param {object} done.responseBody This is a parsed edition of the bootswatch server's response. It's format it defined
* by the return message from [here](http://api.bootswatch.com/)
* @private
*/
function getBootSwatchList( done ) {
request('http://api.bootswatch.com/3/', function(error, response, body) {
if (error) {
return done(error);
}
done(null, JSON.parse(body));
});
}
/**
* This method will get one of the components from Bootswatch, which is generally a `less` file or a `lessVariables` file.
*
* @see http://news.bootswatch.com/post/22193315172/bootswatch-api
* @param {string} url The url to retreive from
* @param {function(err, responseText)} done The callback when complete
* @param {?object} done.err If an error occurred, you will find it here.
* @param {string} done.responseText The body of whatever was returned
* @private
*/
function getBootSwatchComponent( url, done ) {
var body = "";
var req = request(url, function ( error, response, body ) {
if (error) {
return done(error);
}
done(null, body);
});
}