This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
/
grunticon.js
101 lines (83 loc) · 3.25 KB
/
grunticon.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
/*
* grunticon
* https://github.com/filamentgroup/grunticon
*
* Copyright (c) 2012 Scott Jehl, Filament Group, Inc
* Licensed under the MIT license.
*/
module.exports = function(grunt ) {
grunt.registerTask( 'grunticon', 'A mystical CSS icon solution.', function() {
// just a quick starting message
grunt.log.write( "Look, it's a grunticon!\n" );
// get the config
var config = grunt.config.get( "grunticon" );
// fail if config or no src or dest config
if( !config || config.src === undefined || config.dest === undefined ){
grunt.fatal( "Oops! Please provide grunticon configuration for src and dest in your grunt.js file" );
return;
}
// make sure src and dest have / at the end
if( !config.src.match( /\/$/ ) ){
config.src += "/";
}
if( !config.dest.match( /\/$/ ) ){
config.dest += "/";
}
var asyncCSS = grunt.task.getFile( "grunticon/static/grunticon.loader.js" );
var asyncCSSBanner = grunt.task.getFile( "grunticon/static/grunticon.loader.banner.js" );
var previewHTMLsrc = grunt.task.getFile( "grunticon/static/preview.html" );
// CSS filenames with optional mixin from config
var datasvgcss = grunt.config.datasvgcss || "icons.data.svg.css";
var datapngcss = grunt.config.datapngcss || "icons.data.png.css";
var urlpngcss = grunt.config.urlpngcss || "icons.fallback.css";
//filename for generated output preview HTML file
var previewhtml = config.previewhtml || "preview.html";
//filename for generated loader HTML snippet file
var loadersnippet = config.loadersnippet || "grunticon.loader.txt";
// css references base path for the loader
var cssbasepath = config.cssbasepath || "/";
// folder name (within the output folder) for generated png files
var pngfolder = config.pngfolder || "png/";
// make sure pngfolder has / at the end
if( !pngfolder.match( /\/$/ ) ){
pngfolder += "/";
}
// css class prefix
var cssprefix = config.cssprefix || "icon";
// create the output directory
grunt.file.mkdir( config.dest );
// create the output icons directory
grunt.file.mkdir( config.dest + pngfolder );
// minify the source of the grunticon loader and write that to the output
grunt.log.write( "\ngrunticon now minifying the stylesheet loader source." );
var asyncsrc = grunt.file.read( asyncCSS );
var banner = grunt.file.read( asyncCSSBanner );
var min = banner + "\n" + grunt.helper('uglify', asyncsrc );
var loaderCodeDest = config.dest + loadersnippet;
grunt.file.write( loaderCodeDest, min );
grunt.log.write( "\ngrunticon loader file created." );
// take it to phantomjs to do the rest
grunt.log.write( "\ngrunticon now spawning phantomjs..." );
grunt.utils.spawn({
cmd: 'phantomjs',
args: [
grunt.task.getFile('grunticon/phantom.js'),
config.src,
config.dest,
loaderCodeDest,
previewHTMLsrc,
datasvgcss,
datapngcss,
urlpngcss,
previewhtml,
pngfolder,
cssprefix,
cssbasepath
],
fallback: ''
}, function(err, result, code) {
// TODO boost this up a bit.
grunt.log.write("\nSomething went wrong with phantomjs...");
});
});
};