Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IcedCoffeeScript support, add lazy loading for CoffeeScript and Iced... #2599

Merged
merged 15 commits into from
Apr 22, 2015
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 87 additions & 45 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 Down Expand Up @@ -39,37 +40,7 @@ module.exports = function(sails) {
// (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 {
localConfig = {

// The path to the application
appPath: config.appPath ? path.resolve(config.appPath) : process.cwd(),
Expand Down Expand Up @@ -109,6 +80,71 @@ module.exports = function(sails) {
layout: path.resolve(config.appPath, 'views/layout.ejs'),
}
};

var hasCoffee = false, hasIced = false, hasLive = false;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First we find out what languages are being used by walking the source files, and flagging what we find

walkOptions = {
listeners: {
file: function (root, fileStats, next) {
if (fileStats.name.match(/(.+)\.(coffee|litcoffee)$/)) {
hasCoffee = true;
} else if (fileStats.name.match(/(.+)\.(iced|liticed)$/)) {
hasIced = true;
} else if (fileStats.name.match(/(.+)\.(ls)$/)) {
hasLive = true;
}
next();
},
errors: function (root, nodeStatsArray, next) {
next();
}
}
};

walk.walkSync(localConfig.appPath+'/api', walkOptions);
walk.walkSync(localConfig.appPath+'/config', walkOptions);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we only attempt to load the libraries if we found anything....

if (hasIced) {
try {
require('iced-coffee-script/register');
} catch(e0){
try {
require(path.join(localConfig.appPath, 'node_modules/iced-coffee-script/register'));
}
catch (e1) {
sails.log.error('Please run `npm install iced-coffee-script` to use IcedCoffeeScript!');
sails.log.silly('Here\'s the require error(s): ',e0,e1);
}
}
}
if (hasCoffee) {
try {
require('coffee-script/register');
} catch(e0){
try {
require(path.join(localConfig.appPath, 'node_modules/coffee-script/register'));
}
catch (e1) {
sails.log.error('Please run `npm install coffee-script` to use CoffeeScript!');
sails.log.silly('Here\'s the require error(s): ',e0,e1);
}
}
}
if (hasLive) {
try {
require('LiveScript');
} catch(e0){
try {
require(path.join(localConfig.appPath, 'node_modules/LiveScript/index'));
}
catch (e1) {
sails.log.error('Please run `npm install LiveScript` to use LiveScript!');
sails.log.silly('Here\'s the require error(s): ',e0,e1);
}
}
}

return localConfig;
},


Expand All @@ -117,6 +153,11 @@ module.exports = function(sails) {
// Expose self as `sails.modules` (for backwards compatibility)
sails.modules = sails.hooks.moduleloader;

sails.config.moduleloader = {
configExt: ['js','coffee','litcoffee','iced','liticed','ls','json'],
sourceExt: ['js','coffee','litcoffee','iced','liticed','ls']
}

return cb();
},

Expand Down Expand Up @@ -181,7 +222,7 @@ module.exports = function(sails) {
dirname : sails.config.paths.config || sails.config.appPath + '/config',
exclude : ['locales', 'local.js', 'local.json', 'local.coffee', 'local.litcoffee', 'local.ls'],
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 +232,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 +245,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,7 +260,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',
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
Expand Down Expand Up @@ -254,9 +295,10 @@ module.exports = function(sails) {
* @param {Function} cb
*/
loadControllers: function (cb) {
debugger;
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 +316,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 +336,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 +367,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 +401,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 +423,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 +517,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 +533,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": "~2.2.1",
"skipper": "~0.5.5",
"uid-safe": "^1.0.1",
"walk": "~2.3.9",
"waterline": "~0.10.17"
},
"devDependencies": {
Expand Down