Skip to content

Commit

Permalink
Merge pull request #2599 from internalfx/icy_coffee
Browse files Browse the repository at this point in the history
Add IcedCoffeeScript support, add lazy loading for CoffeeScript and Iced...
  • Loading branch information
mikermcneil committed Apr 22, 2015
2 parents 0d38417 + 5193ba7 commit 260eb33
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 59 deletions.
164 changes: 105 additions & 59 deletions lib/hooks/moduleloader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = function(sails) {
var async = require('async');
var _ = require('lodash');
var buildDictionary = require('sails-build-dictionary');
var walk = require('walk');


// TODO:
Expand All @@ -29,47 +30,7 @@ module.exports = function(sails) {
// Default configuration
defaults: function (config) {

// TODO:
// lazy-load (i.e. only do this if a `.coffee` file is encountered), e.g.
// if (filepath.match(/\.coffee$/)) {}

// TODO:
// negotiate the error-- if it's NOT a require error, log it directly.
// if it IS, display the error we have currently
// (the only reason it's verbose right now is that if you're NOT using coffeescript
// it's pretty annoying to see the error pop up every time-- see previous todo)

// Enable server-side CoffeeScript support
try {
require('coffee-script/register');
} catch(e0){
try {
var appPath = config.appPath || process.cwd();
require(path.join(appPath, 'node_modules/coffee-script/register'));
}
catch (e1) {
sails.log.verbose('Please run `npm install coffee-script` to use coffescript (skipping for now)');
sails.log.silly('Here\'s the require error(s): ',e0,e1);
}
}

// Enable server-side LiveScript support
try {
require('LiveScript');
} catch(e0){
try {
var appPath = config.appPath || process.cwd();
require(path.join(appPath, 'node_modules/LiveScript/index'));
}
catch (e1) {
sails.log.verbose('Please run `npm install LiveScript` to use LiveScript (skipping for now)');
sails.log.silly('Here\'s the require error(s): ',e0,e1);
}
}



return {
var localConfig = {

// The path to the application
appPath: config.appPath ? path.resolve(config.appPath) : process.cwd(),
Expand Down Expand Up @@ -106,14 +67,99 @@ module.exports = function(sails) {
//
// For `views` hook
views: path.resolve(config.appPath, 'views'),
layout: path.resolve(config.appPath, 'views/layout.ejs'),
layout: path.resolve(config.appPath, 'views/layout.ejs')
},

moduleloader: {
}
};

var conf = localConfig.moduleloader;

// Declare supported languages.
// To add another language use the format below.
// {
// extensions: An array of file extensions supported by this module
// module: the NPM module name
// require: the require statement
// }
var supportedLangs = [
{
extensions: ['iced','liticed'],
module: 'iced-coffee-script',
require: 'iced-coffee-script/register'
},
{
extensions: ['coffee','litcoffee'],
module: 'coffee-script',
require: 'coffee-script/register'
},
{
extensions: ['ls'],
module: 'LiveScript',
require: 'LiveScript/index'
}
];

var detectedLangs = [];
var detectedExtens = [];

// Function to run for every found file when we walk the directory tree
var walkFunction = {
listeners: {
file: function (root, fileStats, next) {
var fileName = fileStats.name;
var extens = path.extname(fileName).substring(1);

// Look for every file extension we support and flag the appropriate language
_.forEach(supportedLangs, function(lang){
// If we have already found a language, skip it.
if (!_.contains(detectedLangs, lang.module)) {
// If we find a new one, add it to the list.
if (_.contains(lang.extensions, extens)) {
detectedLangs.push(lang.module);
}
}
});

next();
},
errors: function (root, nodeStatsArray, next) {
next();
}
}
};

// Walk the /api and /config directories
walk.walkSync(localConfig.appPath+'/api', walkFunction);
walk.walkSync(localConfig.appPath+'/config', walkFunction);

// Check for which languages were found and load the necessary modules to compile them
_.forEach(detectedLangs, function(moduleName){
var lang = _.find(supportedLangs, {module: moduleName});
detectedExtens = detectedExtens.concat(lang.extensions);

try {
require(lang.require);
} catch(e0){
try {
require(path.join(localConfig.appPath, 'node_modules/'+lang.require));
}
catch (e1) {
sails.log.error('Please run `npm install '+lang.module+'` to use '+lang.module+'!');
sails.log.silly('Here\'s the require error(s): ',e0,e1);
}
}
});

conf.configExt = ['js','json'].concat(detectedExtens);
conf.sourceExt = ['js'].concat(detectedExtens);

return localConfig;
},


initialize: function(cb) {

// Expose self as `sails.modules` (for backwards compatibility)
sails.modules = sails.hooks.moduleloader;

Expand Down Expand Up @@ -179,9 +225,9 @@ module.exports = function(sails) {
// console.log('TRYING T LOAD CONFIG AT:',sails.config.paths.config);
buildDictionary.aggregate({
dirname : sails.config.paths.config || sails.config.appPath + '/config',
exclude : ['locales', 'local.js', 'local.json', 'local.coffee', 'local.litcoffee', 'local.ls'],
exclude : ['locales'].concat(_.map(sails.config.moduleloader.configExt, function(item){ return 'local.'+item; })),
excludeDirs: /(locales|env)$/,
filter : /(.+)\.(js|json|coffee|litcoffee|ls)$/,
filter : new RegExp("(.+)\.(" + sails.config.moduleloader.configExt.join('|') + ")$"),
flattenDirectories: !(sails.config.dontFlattenConfig),
identity : false
}, cb);
Expand All @@ -191,7 +237,7 @@ module.exports = function(sails) {
'config/local' : function loadLocalOverrideFile (cb) {
buildDictionary.aggregate({
dirname : sails.config.paths.config || sails.config.appPath + '/config',
filter : /local\.(js|json|coffee|litcoffee|ls)$/,
filter : new RegExp("local\.(" + sails.config.moduleloader.configExt.join('|') + ")$"),
identity : false
}, cb);
},
Expand All @@ -204,7 +250,7 @@ module.exports = function(sails) {
var env = sails.config.environment || async_data['config/local'].environment || 'development';
buildDictionary.aggregate({
dirname : (sails.config.paths.config || sails.config.appPath + '/config') + '/env/' + env,
filter : /(.+)\.(js|json|coffee|litcoffee|ls)$/,
filter : new RegExp("(.+)\.(" + sails.config.moduleloader.configExt.join('|') + ")$"),
optional : true,
flattenDirectories: !(sails.config.dontFlattenConfig),
identity : false
Expand All @@ -219,15 +265,15 @@ module.exports = function(sails) {
var env = sails.config.environment || async_data['config/local'].environment || 'development';
buildDictionary.aggregate({
dirname : (sails.config.paths.config || sails.config.appPath + '/config') + '/env',
filter : new RegExp(env + '.(js|json|coffee|litcoffee|ls)$'),
filter : new RegExp(env + ".(" + sails.config.moduleloader.configExt.join('|') + ")$"),
optional : true,
flattenDirectories: !(sails.config.dontFlattenConfig),
identity : false
}, cb);
}]

}, function (err, async_data) {
if (err) return cb(err);
if (err) { return cb(err); }
// Save the environment override, if any.
var env = sails.config.environment;
// Merge the configs, with env/*.js files taking precedence over others, and local.js
Expand Down Expand Up @@ -256,7 +302,7 @@ module.exports = function(sails) {
loadControllers: function (cb) {
buildDictionary.optional({
dirname: sails.config.paths.controllers,
filter: /(.+)Controller\.(js|coffee|litcoffee|ls)$/,
filter: new RegExp("(.+)Controller\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
flattenDirectories: true,
keepDirectoryPath: true,
replaceExpr: /Controller/
Expand All @@ -274,9 +320,9 @@ module.exports = function(sails) {
*/
loadAdapters: function (cb) {
buildDictionary.optional({
dirname : sails.config.paths.adapters,
filter : /(.+Adapter)\.(js|coffee|litcoffee|ls)$/,
replaceExpr : /Adapter/,
dirname: sails.config.paths.adapters,
filter: new RegExp("(.+Adapter)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
replaceExpr: /Adapter/,
flattenDirectories: true
}, bindToSails(cb));
},
Expand All @@ -294,7 +340,7 @@ module.exports = function(sails) {
// Get the main model files
buildDictionary.optional({
dirname : sails.config.paths.models,
filter : /^([^.]+)\.(js|coffee|litcoffee|ls)$/,
filter : new RegExp("^([^.]+)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
replaceExpr : /^.*\//,
flattenDirectories: true
}, function(err, models) {
Expand Down Expand Up @@ -325,7 +371,7 @@ module.exports = function(sails) {
loadServices: function (cb) {
buildDictionary.optional({
dirname : sails.config.paths.services,
filter : /(.+)\.(js|coffee|litcoffee|ls)$/,
filter : new RegExp("(.+)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
depth : 1,
caseSensitive : true
}, bindToSails(cb));
Expand Down Expand Up @@ -359,7 +405,7 @@ module.exports = function(sails) {
loadPolicies: function (cb) {
buildDictionary.optional({
dirname: sails.config.paths.policies,
filter: /(.+)\.(js|coffee|litcoffee|ls)$/,
filter: new RegExp("(.+)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
replaceExpr: null,
flattenDirectories: true,
keepDirectoryPath: true
Expand All @@ -381,7 +427,7 @@ module.exports = function(sails) {
hooksFolder: function(cb) {
buildDictionary.optional({
dirname: sails.config.paths.hooks,
filter: /^(.+)\.(js|coffee|litcoffee|ls)$/,
filter: new RegExp("^(.+)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),

// Hooks should be defined as either single files as a function
// OR (better yet) a subfolder with an index.js file
Expand Down Expand Up @@ -475,7 +521,7 @@ module.exports = function(sails) {
loadBlueprints: function (cb) {
buildDictionary.optional({
dirname: sails.config.paths.blueprints,
filter: /(.+)\.(js|coffee|litcoffee|ls)$/,
filter: new RegExp("(.+)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
useGlobalIdForKeyName: true
}, cb);
},
Expand All @@ -491,7 +537,7 @@ module.exports = function(sails) {
loadResponses: function (cb) {
buildDictionary.optional({
dirname: sails.config.paths.responses,
filter: /(.+)\.(js|coffee|litcoffee|ls)$/,
filter: new RegExp("(.+)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
useGlobalIdForKeyName: true
}, bindToSails(cb));
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"semver": "~4.3.3",
"skipper": "~0.5.5",
"uid-safe": "^1.0.1",
"walk": "~2.3.9",
"waterline": "~0.10.17"
},
"devDependencies": {
Expand Down

0 comments on commit 260eb33

Please sign in to comment.