forked from jquery/jqueryui.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
229 lines (190 loc) · 6.58 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
var exec = require( "child_process" ).exec,
fs = require( "fs" ),
jqueryContent = require( "grunt-jquery-content" );
module.exports = function( grunt ) {
grunt.loadNpmTasks( "grunt-jquery-content" );
grunt.initConfig({
"build-posts": {
page: "page/**"
},
"build-resources": {
all: "resources/**"
},
wordpress: (function() {
var config = require( "./config" );
config.dir = "dist/wordpress";
return config;
})()
});
grunt.registerTask( "build-download", function() {
function writeFiles() {
var frontend = require( "download.jqueryui.com" ).frontend({
host: "http://download.jqueryui.com",
env: "production"
}),
download = frontend.download,
themeroller = frontend.themeroller,
wordpressDir = grunt.config( "wordpress.dir" ),
resourceCount = 0;
grunt.file.write( wordpressDir + "/posts/page/download.html",
"<script>" + JSON.stringify({
title: "Download Builder",
pageTemplate: "page-fullwidth.php"
}) + "</script>\n" + download.index() );
grunt.file.write( wordpressDir + "/posts/page/themeroller.html",
"<script>" + JSON.stringify({
title: "ThemeRoller",
pageTemplate: "page-fullwidth.php"
}) + "</script>\n" + themeroller.index() );
grunt.file.expand( { filter: "isFile" }, dir + "/app/dist/**" ).forEach(function( file ) {
grunt.file.copy( file,
file.replace( dir + "/app/dist", wordpressDir + "/resources" ) );
resourceCount++;
});
grunt.log.writeln( "Wrote download.html, themeroller.html and " +
resourceCount + " resources." );
}
var path = require( "path" ),
dir = path.dirname( require.resolve( "download.jqueryui.com" ) ),
done = this.async();
if ( grunt.option( "noprepare" ) ) {
writeFiles();
return done();
}
// At this point, the download builder repo is available, so let's initialize it
grunt.log.writeln( "Initializing download module, might take a while..." );
exec( "grunt prepare", {
cwd: "node_modules/download.jqueryui.com"
}, function( error, stdout, stderr ) {
if ( error ) {
grunt.log.error( stderr );
return done( error );
}
writeFiles();
done();
});
});
grunt.registerTask( "build-demos", function() {
function sortByTitle( a, b ) {
if ( a.filename === "default" ) {
return -1;
}
if ( b.filename === "default" ) {
return 1;
}
return a.title.toLowerCase() > b.title.toLowerCase() ? 1 : -1;
}
// We hijack the jquery-ui checkout from download.jqueryui.com
this.requires( "build-download" );
var jqueryCore, subdir,
path = require( "path" ),
cheerio = require( "cheerio" ),
downloadBuilder = require( "download.jqueryui.com" ),
stable = downloadBuilder.JqueryUi.getStable(),
repoDir = path.normalize( stable.path ),
demosDir = repoDir + "demos",
externalDir = (repoDir + "external").replace( process.cwd() + "/", "" ),
targetDir = grunt.config( "wordpress.dir" ) + "/resources/demos",
highlightDir = targetDir + "-highlight",
demoList = {};
jqueryCore = stable.files().jqueryCore[ 0 ].data.match( /jQuery JavaScript Library v([0-9.]*)/ )[ 1 ];
// Copy all demos files to /resources/demos
grunt.file.recurse( demosDir, function( abspath, rootdir, subdir, filename ) {
if ( filename === "index.html" ) {
return;
}
var content, $,
dest = targetDir + "/" + subdir + "/" + filename,
highlightDest = highlightDir + "/" + subdir + "/" + filename;
if ( /html$/.test( filename ) ) {
content = replaceResources( grunt.file.read( abspath ) );
if ( !( /(\/)/.test( subdir ) ) ) {
$ = cheerio.load( content );
if ( !demoList[ subdir ] ) {
demoList[ subdir ] = [];
}
demoList[ subdir ].push({
filename: filename.substr( 0, filename.length - 5 ),
title: $( "title" ).text().replace( /[^\-]+\s-\s/, "" ),
description: $( ".demo-description" ).remove().html()
});
// Save modified demo
content = $.html();
grunt.file.write( dest, content );
// Create syntax highlighted version
$ = cheerio.load( "<pre><code data-linenum='true'></code></pre>" );
$( "code" ).text( content );
grunt.file.write( highlightDest, jqueryContent.syntaxHighlight( $.html() ) );
} else {
grunt.file.write( dest, content );
}
} else {
grunt.file.copy( abspath, dest );
}
});
for ( subdir in demoList ) {
demoList[ subdir ].sort( sortByTitle );
}
// Create list of all demos
grunt.file.write( targetDir + "/demo-list.json", JSON.stringify( demoList, null, "\t" ) );
// Copy externals into /resources/demos/external
grunt.file.expand( { filter: "isFile" }, externalDir + "/**" ).forEach(function( filename ) {
grunt.file.copy( filename, targetDir + "/external/" + filename.replace( externalDir, "" ) );
});
function replaceResources( source ) {
// ../../jquery-x.y.z.js -> CDN
source = source.replace(
/<script src="\.\.\/\.\.\/external\/jquery\/jquery\.js">/,
"<script src=\"//code.jquery.com/jquery-" + jqueryCore + ".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=\"//code.jquery.com/ui/" + stable.pkg.version + "/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=\"//code.jquery.com/ui/" + stable.pkg.version + "/themes/smoothness/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( "create-quickdownload", function() {
// We hijack the jquery-ui checkout from download.jqueryui.com
this.requires( "build-download" );
var done = this.async(),
path = require( "path" );
exec( "grunt build-packages:" + path.resolve( "resources/download" ), {
cwd: "node_modules/download.jqueryui.com"
}, function( error, stdout, stderr ) {
if ( error ) {
grunt.log.error( stderr );
return done( error );
}
grunt.log.write( stdout );
done();
});
});
grunt.registerTask( "build", [
"build-posts",
"build-resources",
"build-download",
"build-demos",
"copy-taxonomies"
]);
};