Permalink
Browse files

Move code into helpers

  • Loading branch information...
taylortom committed Nov 19, 2015
1 parent 14fd5a1 commit 5c2a27797dd5d0b2a54c0051ffd514fba34e8d4e
Showing with 99 additions and 86 deletions.
  1. +6 −82 Gruntfile.js
  2. +93 −4 grunt/helpers.js
View
@@ -1,94 +1,18 @@
var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var defaults = {
gruntConfigDir: __dirname + '/grunt/config',
gruntTasksDir: __dirname + '/grunt/tasks',
sourcedir: 'src/',
outputdir: 'build/',
theme: '**',
menu: '**',
includes: [
"src/core/",
"templates/templates.js",
"components/components.js",
"extensions/extensions.js",
"menu/menu.js",
"theme/theme.js"
]
};
var MOD_NOT_FOUND = 'MODULE_NOT_FOUND';
module.exports = function(grunt) {
require('time-grunt')(grunt);
var generateConfigData = function() {
var data = {
sourcedir: appendSlash(grunt.option('sourcedir')) || defaults.sourcedir,
outputdir: appendSlash(grunt.option('outputdir')) || defaults.outputdir,
theme: grunt.option('theme') || defaults.theme,
menu: grunt.option('menu') || defaults.menu,
};
// Selectively load the course.json ('outputdir' passed by server-build)
var prefix = grunt.option('outputdir') ? grunt.option('outputdir') : data.sourcedir;
var buildConfigPath = './' + prefix + 'course/config.json';
try {
var buildConfig = require(buildConfigPath).build || {};
}
catch(error) {
console.log(error);
}
var helpers = require('./grunt/helpers')(grunt);
if(buildConfig.includes) data.includes = getIncludes(buildConfig.includes, data);
if(buildConfig.excludes) data.excludes = buildConfig.excludes
};
var appendSlash = function(dir) {
if (dir) {
var lastChar = dir.substring(dir.length - 1, dir.length);
// TODO: check the use of / on windows
if (lastChar !== '/') return dir + '/';
}
};
var getIncludes = function(buildIncludes, configData) {
var pluginTypes = [ 'components', 'extensions', 'menu', 'theme' ];
var dependencies = [];
for(var i = 0, count = pluginTypes.length; i < count; i++) {
var dir = path.join(__dirname, configData.sourcedir, pluginTypes[i]);
var children = fs.readdirSync(dir);
for(var j = 0, count = children.length; j < count; j++) {
try {
var bowerJson = require(path.join(dir, children[j], 'bower.json'));
for (var key in bowerJson.dependencies) {
if(!_.contains(buildIncludes, key)) dependencies.push(key)
}
} catch(error) { // ignore 'dir doesn't exist errors'
if(error.code !== MOD_NOT_FOUND) console.log(error);
}
}
}
return [].concat(defaults.includes, buildIncludes, dependencies);
};
/**
* Main entry point
*/
require('time-grunt')(grunt);
require('load-grunt-config')(grunt, {
data: generateConfigData(),
configPath: defaults.gruntConfigDir,
data: helpers.generateConfigData(),
configPath: __dirname + '/grunt/config',
jitGrunt: {
customTasksDir: defaults.gruntTasksDir,
customTasksDir: __dirname + '/grunt/tasks',
staticMappings: {
bower: 'grunt-bower-requirejs'
}
}
});
grunt.config('helpers', require('./grunt/helpers')(grunt));
grunt.config('helpers', helpers);
grunt.registerTask('default', ['help']);
};
View
@@ -1,6 +1,12 @@
var _ = require('underscore');
var chalk = require('chalk');
var fs = require('fs');
var path = require('path');
module.exports = function(grunt) {
// tasks
// grunt tasks
grunt.registerTask('_log-server', 'Logs out user-defined build variables', function() {
grunt.log.ok('Starting server in "' + grunt.config('outputdir') + '" using port ' + grunt.config('connect.server.options.port'));
});
@@ -31,6 +37,8 @@ module.exports = function(grunt) {
if (grunt.config('menu') !== '**') grunt.log.ok('Using menu "' + grunt.config('menu') + '"');
});
// privates
var generateIncludedRegExp = function() {
var includes = grunt.config('includes') || [];
var re = '';
@@ -51,8 +59,89 @@ module.exports = function(grunt) {
return new RegExp(re);
};
// exported
var exports = {};
exports.defaults = {
sourcedir: process.cwd() + '/src/',
outputdir: process.cwd() + '/build/',
theme: '**',
menu: '**',
includes: [
"src/core/",
"templates/templates.js",
"components/components.js",
"extensions/extensions.js",
"menu/menu.js",
"theme/theme.js"
],
pluginTypes: [
'components',
'extensions',
'menu',
'theme'
]
};
exports.appendSlash = function(dir) {
if (dir) {
var lastChar = dir.substring(dir.length - 1, dir.length);
// TODO: check the use of / on windows
if (lastChar !== '/') return dir + '/';
}
};
exports.getIncludes = function(buildIncludes, configData) {
var dependencies = [];
for(var i = 0, count = exports.defaults.pluginTypes.length; i < count; i++) {
var dir = path.join(configData.sourcedir, exports.defaults.pluginTypes[i]);
var children = fs.readdirSync(dir);
for(var j = 0, count = children.length; j < count; j++) {
try {
var folderPath = path.join(dir, children[j]);
// not a directory, excape!
if(!fs.statSync(folderPath).isDirectory()) continue;
var bowerJson = require(path.join(folderPath, 'bower.json'));
for (var key in bowerJson.dependencies) {
if(!_.contains(buildIncludes, key)) dependencies.push(key)
}
} catch(error) {
console.log(error);
}
}
}
return [].concat(exports.defaults.includes, buildIncludes, dependencies);
};
exports.generateConfigData = function() {
var data = {
sourcedir: exports.appendSlash(grunt.option('sourcedir')) || exports.defaults.sourcedir,
outputdir: exports.appendSlash(grunt.option('outputdir')) || exports.defaults.outputdir,
theme: grunt.option('theme') || exports.defaults.theme,
menu: grunt.option('menu') || exports.defaults.menu,
};
// Selectively load the course.json ('outputdir' passed by server-build)
var prefix = grunt.option('outputdir') ? grunt.option('outputdir') : data.sourcedir;
var buildConfigPath = prefix + 'course/config.json';
try {
var buildConfig = require(buildConfigPath).build;
}
catch(error) {
return console.log(error);
}
if(buildConfig.includes) data.includes = exports.getIncludes(buildConfig.includes, data);
if(buildConfig.excludes) data.excludes = buildConfig.excludes;
return data;
};
/*
* Uses the parent folder name (menu, theme, components, extensions).
* Also caches a list of the installed plugins
@@ -85,12 +174,12 @@ module.exports = function(grunt) {
var isExcluded = excludes && pluginPath.search(exports.excludedRegExp) !== -1;
if (isExcluded || !isIncluded) {
// grunt.log.writeln('Excluded ' + chalk.magenta(pluginPath));
// grunt.log.writeln('Excluded ' + chalk.magenta(pluginPath));
return false;
}
else {
// grunt.log.writeln('Included ' + chalk.green(pluginPath));
return true;
// grunt.log.writeln('Included ' + chalk.green(pluginPath));
return true;
}
};

0 comments on commit 5c2a277

Please sign in to comment.