Skip to content

Commit 46534cc

Browse files
committed
Change main DownloadBuilder, from Frontend to Main
Previously, main was `frontend.js`, now it is `main.js`. Frontend can now be accessed by `download.frontend`, where `download = require( "download.jqueryui.com" )()`; The new API: Main( options ) -- or require( "download.jqueryui.com" )( options ) - options [ Object ]: key-value pairs detailed below. options - host [ String ]: optional, specify the host where download.jqueryui.com server is running. Default: "" (empty string). - config [ Object ]: optional, if present used instead of the `config.json` file; attributes - frontend [ Object ]: for more details see `frontend.js`. Main#buildThemesBundle( callback ), generates the theme bundle with base and all themes from themegallery. - callback( err, bundleFiles ): bundleFiles is an Array of { path:<path>, data:<data> }'s. On the config, a prepared path can be specified for stable or legacy by using the `path` attribute, instead of passing the version plus letting `grunt prepare` prepare it. Eg. download = require( "download.jqueryui.com" )({ config: { "jqueryUi": { "stable": { "path": "my/stable/path" } } } }); The above prevents jQuery UI of some workarounds on its grunt `release_themes` task.
1 parent 57b33c2 commit 46534cc

File tree

6 files changed

+163
-55
lines changed

6 files changed

+163
-55
lines changed

grunt.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function cloneOrFetch( callback ) {
123123
}
124124

125125
function buildAll( callback ) {
126-
var config = require( "./lib/config" );
126+
var config = require( "./lib/config" )();
127127

128128
async.forEachSeries( config.jqueryUi, function( jqueryUi, callback ) {
129129
async.series([

lib/config.js

+52-41
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,58 @@
1-
var config, jqueryUi,
1+
var Config, jqueryUi,
22
fs = require( "fs" ),
33
path = require( "path" );
44

5-
module.exports = config = JSON.parse( fs.readFileSync( path.join( __dirname, "../config.json" ) ) );
5+
Config = module.exports = function() {
6+
var config = Config.get();
67

7-
// Validate jqueryUi, eg:
8-
// "jqueryUi": {
9-
// "stable": { // required
10-
// "version": "1.10.0"
11-
// "dependsOn": "jQuery 1.7+"
12-
// },
13-
// "legacy": { // optional
14-
// "version": "1.9.1"
15-
// "dependsOn": "jQuery 1.6+"
16-
// }
17-
// }
18-
if ( config.jqueryUi == null ) {
19-
throw new Error( "Missing jqueryUi branch/tag in config.json" );
20-
}
21-
if ( typeof config.jqueryUi !== "object" ) {
22-
throw new Error( "Invalid jqueryUi branch/tag in config.json" );
23-
}
24-
if ( config.jqueryUi.stable == null ) {
25-
throw new Error( "Missing \"stable\" jqueryUi branch/tag in config.json" );
26-
}
27-
// Normalizing jqueryUi object into an Array
28-
jqueryUi = [ config.jqueryUi.stable ];
29-
jqueryUi[ 0 ].stable = true;
30-
if ( config.jqueryUi.legacy ) {
31-
jqueryUi.push( config.jqueryUi.legacy );
32-
}
33-
config.jqueryUi = jqueryUi.map(function( entry ) {
34-
if ( typeof entry !== "object" || !entry.version ) {
35-
throw new Error( "Invalid jqueryUi entry " + JSON.stringify( entry ) + " in config.json" );
8+
// Validate jqueryUi, eg:
9+
// "jqueryUi": {
10+
// "stable": { // required
11+
// "version": "1.10.0"
12+
// "dependsOn": "jQuery 1.7+"
13+
// },
14+
// "legacy": { // optional
15+
// "version": "1.9.1"
16+
// "dependsOn": "jQuery 1.6+"
17+
// }
18+
// }
19+
if ( config.jqueryUi == null ) {
20+
throw new Error( "Missing jqueryUi branch/tag in config.json" );
3621
}
37-
entry.ref = entry.version;
38-
return entry;
39-
});
22+
if ( typeof config.jqueryUi !== "object" ) {
23+
throw new Error( "Invalid jqueryUi branch/tag in config.json" );
24+
}
25+
if ( config.jqueryUi.stable == null ) {
26+
throw new Error( "Missing \"stable\" jqueryUi branch/tag in config.json" );
27+
}
28+
// Normalizing jqueryUi object into an Array
29+
jqueryUi = [ config.jqueryUi.stable ];
30+
jqueryUi[ 0 ].stable = true;
31+
if ( config.jqueryUi.legacy ) {
32+
jqueryUi.push( config.jqueryUi.legacy );
33+
}
34+
config.jqueryUi = jqueryUi.map(function( entry ) {
35+
if ( entry.path && !entry.version ) {
36+
entry.version = "skip";
37+
}
38+
if ( typeof entry !== "object" || !entry.version ) {
39+
throw new Error( "Invalid jqueryUi entry " + JSON.stringify( entry ) + " in config.json" );
40+
}
41+
entry.ref = entry.version;
42+
return entry;
43+
});
44+
45+
// Validate jquery
46+
if ( config.jquery == null ) {
47+
throw new Error( "Missing jquery version in config.json" );
48+
}
49+
if ( typeof config.jquery !== "string" ) {
50+
throw new Error( "Invalid jquery version in config.json" );
51+
}
52+
53+
return config;
54+
};
4055

41-
// Validate jquery
42-
if ( config.jquery == null ) {
43-
throw new Error( "Missing jquery version in config.json" );
44-
}
45-
if ( typeof config.jquery !== "string" ) {
46-
throw new Error( "Invalid jquery version in config.json" );
47-
}
56+
Config.get = function() {
57+
return JSON.parse( fs.readFileSync( path.join( __dirname, "../config.json" ) ) );
58+
};

lib/release.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var commonFiles, componentFiles,
22
_ = require( "underscore" ),
3-
config = require( "./config" ),
3+
config = require( "./config" )(),
44
fs = require( "fs" ),
55
glob = require( "glob-whatev" ).glob,
66
logger = require( "simple-log" ).init( "download.jqueryui.com" ),
@@ -124,16 +124,14 @@ function Release( path, options ) {
124124

125125
Release.all = function() {
126126
if ( !Release._all ) {
127-
if ( !fs.existsSync( __dirname + "/../release" ) ) {
128-
throw new Error( "Missing ./release folder. Run `grunt prepare` first." );
129-
}
130127
Release._all = config.jqueryUi.map(function( jqueryUi ) {
131-
var path = __dirname + "/../release/" + jqueryUi.ref + "/";
128+
// 1: Allow config to override path, used by jQuery UI release process to generate themes.
129+
var path = jqueryUi.path /* 1 */ || __dirname + "/../release/" + jqueryUi.ref + "/";
132130
if ( !fs.existsSync( path ) ) {
133-
throw new Error( "Missing ./release/" + jqueryUi.ref + " folder. Run `grunt prepare` first." );
131+
throw new Error( "Missing ./" + require( "path").relative( __dirname, path ) + " folder. Run `grunt prepare` first, or fix your config file." );
134132
}
135133
if ( !fs.existsSync( path + "package.json" ) ) {
136-
throw new Error( "Invalid ./release/" + jqueryUi.ref + " folder. Run `grunt prepare` first." );
134+
throw new Error( "Invalid ./" + require( "path").relative( __dirname, path ) + " folder. Run `grunt prepare` first, or fix your config file." );
137135
}
138136
return new Release( path, jqueryUi );
139137
});

main.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
var Main,
2+
Config = require( "./lib/config" );
3+
4+
/**
5+
* Main( options ) -- or require( "download.jqueryui.com" )( options )
6+
* - options [ Object ]: key-value pairs detailed below.
7+
*
8+
* options
9+
* - host [ String ]: optional, specify the host where download.jqueryui.com server is running. Default: "" (empty string).
10+
* - config [ Object ]: optional, if present used instead of the `config.json` file;
11+
*
12+
* attributes
13+
* - frontend [ Object ]: for more details see `frontend.js`.
14+
*
15+
*/
16+
module.exports = function( options ) {
17+
return new Main( options );
18+
};
19+
20+
Main = function( options ) {
21+
options = options || {};
22+
if ( options.config && typeof options.config === "object" ) {
23+
Config.get = function() {
24+
return options.config;
25+
};
26+
}
27+
this.frontend = new ( require( "./frontend" ) )( options.host );
28+
};
29+
30+
Main.prototype = {
31+
32+
/**
33+
* Main#buildThemesBundle( callback )
34+
* - callback( err, bundleFiles ): bundleFiles is an Array of { path:<path>, data:<data> }'s.
35+
*
36+
* Generates the theme bundle with base and all themes from themegallery.
37+
*/
38+
buildThemesBundle: function( callback ) {
39+
var allComponents, release, success,
40+
async = require( "async" ),
41+
Builder = require( "./lib/builder" ),
42+
bundleFiles = [],
43+
Release = require( "./lib/release" ),
44+
themeGallery = require( "./lib/themeroller.themegallery" );
45+
46+
release = Release.getStable();
47+
allComponents = release.components().map(function( component ) {
48+
return component.name;
49+
});
50+
51+
async.mapSeries( themeGallery, function( theme, callback ) {
52+
var builder = new Builder( release, allComponents, theme ),
53+
folderName = theme.folderName();
54+
builder.build(function( err, files ) {
55+
if ( err ) {
56+
return callback( err );
57+
}
58+
// Add theme files.
59+
files
60+
// Pick only theme files we need on the bundle.
61+
.filter(function( file ) {
62+
var themeCssOnlyRe = new RegExp( "development-bundle/themes/" + folderName + "/jquery.ui.theme.css" ),
63+
themeDirRe = new RegExp( "css/" + folderName );
64+
if ( themeCssOnlyRe.test( file.path ) || themeDirRe.test( file.path ) ) {
65+
return true;
66+
}
67+
return false;
68+
})
69+
// Convert paths the way bundle needs and add it into bundleFiles.
70+
.forEach(function( file ) {
71+
// 1: Remove initial package name eg. "jquery-ui-1.10.0.custom".
72+
// 2: Make jquery-ui-1.10.0.custom.css into jquery-ui.css, or jquery-ui-1.10.0.custom.min.css into jquery-ui.min.css
73+
file.path = file.path
74+
.split( "/" ).slice( 1 ).join( "/" ) /* 1 */
75+
.replace( /development-bundle\/themes/, "css" )
76+
.replace( /css/, "themes" )
77+
.replace( /jquery-ui-.*?(\.min)*\.css/, "jquery-ui$1.css" ); /* 2 */
78+
bundleFiles.push( file );
79+
});
80+
81+
callback( null, files );
82+
});
83+
}, function( err, builds ) {
84+
if ( !err ) {
85+
// Add base.
86+
builds[0]
87+
// Pick only the base files we need on the bundle.
88+
.filter(function( file ) {
89+
return (/development-bundle\/themes\/base/).test( file.path );
90+
})
91+
// Convert paths the way bundle needs and add it into bundleFiles.
92+
.forEach(function( file ) {
93+
// 1: Remove initial package name eg. "jquery-ui-1.10.0.custom".
94+
file.path = file.path
95+
.split( "/" ).slice( 1 ).join( "/" ) /* 1 */
96+
.replace( /development-bundle\/themes/, "themes" );
97+
bundleFiles.push( file );
98+
});
99+
}
100+
callback( err, bundleFiles );
101+
});
102+
}
103+
};
104+

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"devDependencies": {
2525
"nodeunit": "~0.7.4"
2626
},
27-
"main": "frontend.js",
27+
"main": "main.js",
2828
"engine": {
2929
"node": ">=0.8.x"
3030
},

themeroller.js

-5
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ Frontend.prototype = {
109109
return theme.css();
110110
},
111111

112-
// used by jQuery UI release process to generate themes
113-
gallery: function() {
114-
return themeGallery;
115-
},
116-
117112
icon: function( filename, response, error ) {
118113
renderImage( filename, response, function( err ) {
119114
if ( err ) {

0 commit comments

Comments
 (0)