forked from jquery/jqueryui.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grunt.js
213 lines (185 loc) · 6.46 KB
/
grunt.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
module.exports = function( grunt ) {
grunt.loadNpmTasks( "grunt-clean" );
grunt.loadNpmTasks( "grunt-html" );
grunt.loadNpmTasks( "grunt-wordpress" );
grunt.loadNpmTasks( "grunt-jquery-content" );
grunt.initConfig({
clean: {
folder: "dist/"
},
htmllint: {
page: "page/**.html"
},
jshint: {
options: {
undef: true,
node: true
}
},
lint: {
grunt: "grunt.js"
},
watch: {
pages: {
files: "page/**.html",
tasks: "deploy"
}
},
"build-pages": {
all: grunt.file.expandFiles( "page/**" )
},
"build-resources": {
all: grunt.file.expandFiles( "resources/**" )
},
wordpress: grunt.utils._.extend({
dir: "dist/wordpress"
}, grunt.file.readJSON( "config.json" ) )
});
grunt.registerTask( "build-download", function() {
function writeFiles() {
var frontend = new ( require( "download.jqueryui.com" ) )( "http://download.jqueryui.com" ),
resources = grunt.file.expandFiles( dir + "/app/**" ),
download = frontend.download,
themeroller = frontend.themeroller;
grunt.file.write( grunt.config( "wordpress.dir" ) + "/posts/page/download.html",
"<script>" + JSON.stringify({
title: "Download Builder",
pageTemplate: "nosidebar-page.php"
}) + "</script>\n" + download.index() );
grunt.file.write( grunt.config( "wordpress.dir" ) + "/posts/page/themeroller.html",
"<script>" + JSON.stringify({
title: "ThemeRoller",
pageTemplate: "nosidebar-page.php"
}) + "</script>\n" + themeroller.index() );
resources.forEach(function( file ) {
grunt.file.copy( file, file.replace( dir + "/app", grunt.config( "wordpress.dir" ) ) );
});
grunt.log.write( "Wrote download.html, themeroller.html and " + resources.length + " resources." );
}
var path = require( "path" ),
dir = path.dirname( require.resolve( "download.jqueryui.com" ) ),
done = this.async();
if ( grunt.option( "noprepare" ) ) {
writeFiles();
done();
return;
}
// at this point, the download builder repo is available, so let's initialize it
grunt.log.writeln( "Initializing download module, might take a while..." );
grunt.utils.spawn({
cmd: "grunt",
args: [ "prepare" ],
opts: {
cwd: "node_modules/download.jqueryui.com"
}
}, function( error, result, stringResult ) {
if ( error ) {
grunt.log.error( error, stringResult );
done( false );
return;
}
writeFiles();
done();
});
});
grunt.registerTask( "build-demos", function() {
// We hijack the jquery-ui checkout from download.jqueryui.com
this.requires( "build-download" );
var path = require( "path" ),
jsdom = require( "jsdom" ).jsdom,
downloadModulePath = path.dirname( require.resolve( "download.jqueryui.com" ) ),
repoDir = downloadModulePath + "/tmp/jquery-ui",
versions = grunt.file.readJSON( downloadModulePath + "/config.json" ),
demosDir = repoDir + "/demos",
distDir = repoDir + "/dist",
targetDir = grunt.config( "wordpress.dir" ) + "/resources/demos",
highlightDir = targetDir + "-highlight",
demoList = {};
// Copy all demos files to /resources/demos
grunt.file.recurse( demosDir, function( abspath, rootdir, subdir, filename ) {
if ( filename === "index.html" ) {
return;
}
var content, document, description, title,
dest = targetDir + "/" + subdir + "/" + filename,
highlightDest = highlightDir + "/" + subdir + "/" + filename;
if ( /html$/.test( filename ) ) {
content = replaceResources( grunt.file.read( abspath ) );
if ( !( /(\/)/.test( subdir ) ) ) {
document = jsdom( content, null, {
features: {
FetchExternalResources: [],
ProcessExternalResources: false
}
});
description = document.getElementsByClassName( "demo-description" )[0];
if ( description ) {
description.parentNode.removeChild( description );
}
title = document.getElementsByTagName( "title" )[0];
if ( !demoList[ subdir ] ) {
demoList[ subdir ] = {};
}
demoList[ subdir ][ filename.substr( 0, filename.length - 5 ) ] = {
title: title.innerHTML.replace( /[^\-]+\s-\s/, '' ),
description: description ? description.innerHTML : ""
};
// Save modified demo
content = "<!doctype html>\n" + document.innerHTML;
grunt.file.write( dest, content );
// Create syntax highlighted version
document.innerHTML = "<pre><code data-linenum='true'></code></pre>";
document.getElementsByTagName( "code" )[0].appendChild(
document.createTextNode( content ) );
grunt.file.write( highlightDest,
grunt.helper( "syntax-highlight", { content: document.innerHTML } ) );
} else {
grunt.file.write( dest, content );
}
} else {
grunt.file.copy( abspath, dest );
}
});
// Create list of all demos
grunt.file.write( targetDir + "/demo-list.json", JSON.stringify( demoList ) );
// Copy externals into /resources/demos/external
grunt.file.expandFiles( repoDir + "/external/**" ).forEach(function( filename ) {
grunt.file.copy( filename, targetDir + "/external/" + path.basename( filename ) );
});
function replaceResources( source ) {
// ../../jquery-x.y.z.js -> CDN
source = source.replace(
/<script src="\.\.\/\.\.\/jquery-\d+\.\d+(\.\d+)?\.js">/,
"<script src=\"http://code.jquery.com/jquery-" + versions.jquery + ".js\">" );
// ../../ui/* -> CDN
// Only the first script is replaced, all subsequent scripts are dropped,
// including the full line
source = source.replace(
/<script src="\.\.\/\.\.\/ui\/[^>]+/,
"<script src=\"http://code.jquery.com/ui/" + versions.jqueryUi + "/jquery-ui.js\">" );
source = source.replace(
/^.*<script src="\.\.\/\.\.\/ui\/[^>]+><\/script>\n/gm,
"" );
// ../../external/* -> /resources/demos/external/*
source = source.replace(
/<script src="\.\.\/\.\.\/external\//g,
"<script src=\"/resources/demos/external/" );
// ../../ui/themes/* -> CDN
source = source.replace(
/<link rel="stylesheet" href="\.\.\/\.\.\/themes[^>]+>/,
"<link rel=\"stylesheet\" href=\"http://code.jquery.com/ui/" + versions.jqueryUi + "/themes/base/jquery-ui.css\">" );
// ../demos.css -> /resources/demos/style.css
source = source.replace(
/<link rel="stylesheet" href="\.\.\/demos.css\">/,
"<link rel=\"stylesheet\" href=\"/resources/demos/style.css\">" );
return source;
}
});
grunt.registerTask( "copy-taxonomies", function() {
grunt.file.copy( "taxonomies.json",
grunt.config( "wordpress.dir" ) + "/taxonomies.json" );
});
grunt.registerTask( "default", "lint" );
grunt.registerTask( "build", "build-pages build-resources build-download build-demos copy-taxonomies" );
grunt.registerTask( "build-wordpress", "clean lint build" );
};