Skip to content

Commit

Permalink
Change main DownloadBuilder, from Frontend to Main
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rxaviers committed Feb 15, 2013
1 parent 57b33c2 commit 46534cc
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 55 deletions.
2 changes: 1 addition & 1 deletion grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function cloneOrFetch( callback ) {
}

function buildAll( callback ) {
var config = require( "./lib/config" );
var config = require( "./lib/config" )();

async.forEachSeries( config.jqueryUi, function( jqueryUi, callback ) {
async.series([
Expand Down
93 changes: 52 additions & 41 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,58 @@
var config, jqueryUi,
var Config, jqueryUi,
fs = require( "fs" ),
path = require( "path" );

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

// Validate jqueryUi, eg:
// "jqueryUi": {
// "stable": { // required
// "version": "1.10.0"
// "dependsOn": "jQuery 1.7+"
// },
// "legacy": { // optional
// "version": "1.9.1"
// "dependsOn": "jQuery 1.6+"
// }
// }
if ( config.jqueryUi == null ) {
throw new Error( "Missing jqueryUi branch/tag in config.json" );
}
if ( typeof config.jqueryUi !== "object" ) {
throw new Error( "Invalid jqueryUi branch/tag in config.json" );
}
if ( config.jqueryUi.stable == null ) {
throw new Error( "Missing \"stable\" jqueryUi branch/tag in config.json" );
}
// Normalizing jqueryUi object into an Array
jqueryUi = [ config.jqueryUi.stable ];
jqueryUi[ 0 ].stable = true;
if ( config.jqueryUi.legacy ) {
jqueryUi.push( config.jqueryUi.legacy );
}
config.jqueryUi = jqueryUi.map(function( entry ) {
if ( typeof entry !== "object" || !entry.version ) {
throw new Error( "Invalid jqueryUi entry " + JSON.stringify( entry ) + " in config.json" );
// Validate jqueryUi, eg:
// "jqueryUi": {
// "stable": { // required
// "version": "1.10.0"
// "dependsOn": "jQuery 1.7+"
// },
// "legacy": { // optional
// "version": "1.9.1"
// "dependsOn": "jQuery 1.6+"
// }
// }
if ( config.jqueryUi == null ) {
throw new Error( "Missing jqueryUi branch/tag in config.json" );
}
entry.ref = entry.version;
return entry;
});
if ( typeof config.jqueryUi !== "object" ) {
throw new Error( "Invalid jqueryUi branch/tag in config.json" );
}
if ( config.jqueryUi.stable == null ) {
throw new Error( "Missing \"stable\" jqueryUi branch/tag in config.json" );
}
// Normalizing jqueryUi object into an Array
jqueryUi = [ config.jqueryUi.stable ];
jqueryUi[ 0 ].stable = true;
if ( config.jqueryUi.legacy ) {
jqueryUi.push( config.jqueryUi.legacy );
}
config.jqueryUi = jqueryUi.map(function( entry ) {
if ( entry.path && !entry.version ) {
entry.version = "skip";
}
if ( typeof entry !== "object" || !entry.version ) {
throw new Error( "Invalid jqueryUi entry " + JSON.stringify( entry ) + " in config.json" );
}
entry.ref = entry.version;
return entry;
});

// Validate jquery
if ( config.jquery == null ) {
throw new Error( "Missing jquery version in config.json" );
}
if ( typeof config.jquery !== "string" ) {
throw new Error( "Invalid jquery version in config.json" );
}

return config;
};

// Validate jquery
if ( config.jquery == null ) {
throw new Error( "Missing jquery version in config.json" );
}
if ( typeof config.jquery !== "string" ) {
throw new Error( "Invalid jquery version in config.json" );
}
Config.get = function() {
return JSON.parse( fs.readFileSync( path.join( __dirname, "../config.json" ) ) );
};
12 changes: 5 additions & 7 deletions lib/release.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var commonFiles, componentFiles,
_ = require( "underscore" ),
config = require( "./config" ),
config = require( "./config" )(),
fs = require( "fs" ),
glob = require( "glob-whatev" ).glob,
logger = require( "simple-log" ).init( "download.jqueryui.com" ),
Expand Down Expand Up @@ -124,16 +124,14 @@ function Release( path, options ) {

Release.all = function() {
if ( !Release._all ) {
if ( !fs.existsSync( __dirname + "/../release" ) ) {
throw new Error( "Missing ./release folder. Run `grunt prepare` first." );
}
Release._all = config.jqueryUi.map(function( jqueryUi ) {
var path = __dirname + "/../release/" + jqueryUi.ref + "/";
// 1: Allow config to override path, used by jQuery UI release process to generate themes.
var path = jqueryUi.path /* 1 */ || __dirname + "/../release/" + jqueryUi.ref + "/";
if ( !fs.existsSync( path ) ) {
throw new Error( "Missing ./release/" + jqueryUi.ref + " folder. Run `grunt prepare` first." );
throw new Error( "Missing ./" + require( "path").relative( __dirname, path ) + " folder. Run `grunt prepare` first, or fix your config file." );
}
if ( !fs.existsSync( path + "package.json" ) ) {
throw new Error( "Invalid ./release/" + jqueryUi.ref + " folder. Run `grunt prepare` first." );
throw new Error( "Invalid ./" + require( "path").relative( __dirname, path ) + " folder. Run `grunt prepare` first, or fix your config file." );
}
return new Release( path, jqueryUi );
});
Expand Down
104 changes: 104 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
var Main,
Config = require( "./lib/config" );

/**
* 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`.
*
*/
module.exports = function( options ) {
return new Main( options );
};

Main = function( options ) {
options = options || {};
if ( options.config && typeof options.config === "object" ) {
Config.get = function() {
return options.config;
};
}
this.frontend = new ( require( "./frontend" ) )( options.host );
};

Main.prototype = {

/**
* Main#buildThemesBundle( callback )
* - callback( err, bundleFiles ): bundleFiles is an Array of { path:<path>, data:<data> }'s.
*
* Generates the theme bundle with base and all themes from themegallery.
*/
buildThemesBundle: function( callback ) {
var allComponents, release, success,
async = require( "async" ),
Builder = require( "./lib/builder" ),
bundleFiles = [],
Release = require( "./lib/release" ),
themeGallery = require( "./lib/themeroller.themegallery" );

release = Release.getStable();
allComponents = release.components().map(function( component ) {
return component.name;
});

async.mapSeries( themeGallery, function( theme, callback ) {
var builder = new Builder( release, allComponents, theme ),
folderName = theme.folderName();
builder.build(function( err, files ) {
if ( err ) {
return callback( err );
}
// Add theme files.
files
// Pick only theme files we need on the bundle.
.filter(function( file ) {
var themeCssOnlyRe = new RegExp( "development-bundle/themes/" + folderName + "/jquery.ui.theme.css" ),
themeDirRe = new RegExp( "css/" + folderName );
if ( themeCssOnlyRe.test( file.path ) || themeDirRe.test( file.path ) ) {
return true;
}
return false;
})
// Convert paths the way bundle needs and add it into bundleFiles.
.forEach(function( file ) {
// 1: Remove initial package name eg. "jquery-ui-1.10.0.custom".
// 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
file.path = file.path
.split( "/" ).slice( 1 ).join( "/" ) /* 1 */
.replace( /development-bundle\/themes/, "css" )
.replace( /css/, "themes" )
.replace( /jquery-ui-.*?(\.min)*\.css/, "jquery-ui$1.css" ); /* 2 */
bundleFiles.push( file );
});

callback( null, files );
});
}, function( err, builds ) {
if ( !err ) {
// Add base.
builds[0]
// Pick only the base files we need on the bundle.
.filter(function( file ) {
return (/development-bundle\/themes\/base/).test( file.path );
})
// Convert paths the way bundle needs and add it into bundleFiles.
.forEach(function( file ) {
// 1: Remove initial package name eg. "jquery-ui-1.10.0.custom".
file.path = file.path
.split( "/" ).slice( 1 ).join( "/" ) /* 1 */
.replace( /development-bundle\/themes/, "themes" );
bundleFiles.push( file );
});
}
callback( err, bundleFiles );
});
}
};

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"devDependencies": {
"nodeunit": "~0.7.4"
},
"main": "frontend.js",
"main": "main.js",
"engine": {
"node": ">=0.8.x"
},
Expand Down
5 changes: 0 additions & 5 deletions themeroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ Frontend.prototype = {
return theme.css();
},

// used by jQuery UI release process to generate themes
gallery: function() {
return themeGallery;
},

icon: function( filename, response, error ) {
renderImage( filename, response, function( err ) {
if ( err ) {
Expand Down

0 comments on commit 46534cc

Please sign in to comment.