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 2 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
101 changes: 72 additions & 29 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,21 +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);
}
}

return {
localConfig = {

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

var hasCoffee = false, hasIced = 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;
}
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 IcedCoffeScript!');
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 CoffeScript!');

Choose a reason for hiding this comment

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

s/coffescript/coffeescript

sails.log.silly('Here\'s the require error(s): ',e0,e1);
}
}
}

return localConfig;
},


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

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

return cb();
},

Expand Down Expand Up @@ -165,7 +207,7 @@ module.exports = function(sails) {
dirname : sails.config.paths.config || sails.config.appPath + '/config',
exclude : ['locales', 'local.js', 'local.json', 'local.coffee', 'local.litcoffee'],
excludeDirs: /(locales|env)$/,
filter : /(.+)\.(js|json|coffee|litcoffee)$/,
filter : new RegExp("(.+)\.(" + sails.config.moduleloader.configExt.join('|') + ")$"),
flattenDirectories: !(sails.config.dontFlattenConfig),
identity : false
}, cb);
Expand All @@ -175,7 +217,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)$/,
filter : new RegExp("local\.(" + sails.config.moduleloader.configExt.join('|') + ")$"),
identity : false
}, cb);
},
Expand All @@ -188,7 +230,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)$/,
filter : new RegExp("(.+)\.(" + sails.config.moduleloader.configExt.join('|') + ")$"),
optional : true,
flattenDirectories: !(sails.config.dontFlattenConfig),
identity : false
Expand All @@ -203,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',
filter : new RegExp(env + '.(js|json|coffee|litcoffee)$'),
filter : new RegExp(env + ".(" + sails.config.moduleloader.configExt.join('|') + ")$"),
optional : true,
flattenDirectories: !(sails.config.dontFlattenConfig),
identity : false
Expand Down Expand Up @@ -238,9 +280,10 @@ module.exports = function(sails) {
* @param {Function} cb
*/
loadControllers: function (cb) {
debugger;
buildDictionary.optional({
dirname: sails.config.paths.controllers,
filter: /(.+)Controller\.(js|coffee|litcoffee)$/,
filter: new RegExp("(.+)Controller\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
flattenDirectories: true,
keepDirectoryPath: true,
replaceExpr: /Controller/
Expand All @@ -258,9 +301,9 @@ module.exports = function(sails) {
*/
loadAdapters: function (cb) {
buildDictionary.optional({
dirname : sails.config.paths.adapters,
filter : /(.+Adapter)\.(js|coffee|litcoffee)$/,
replaceExpr : /Adapter/,
dirname: sails.config.paths.adapters,
filter: new RegExp("(.+Adapter)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
replaceExpr: /Adapter/,
flattenDirectories: true
}, bindToSails(cb));
},
Expand All @@ -278,7 +321,7 @@ module.exports = function(sails) {
// Get the main model files
buildDictionary.optional({
dirname : sails.config.paths.models,
filter : /^([^.]+)\.(js|coffee|litcoffee)$/,
filter : new RegExp("^([^.]+)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
replaceExpr : /^.*\//,
flattenDirectories: true
}, function(err, models) {
Expand Down Expand Up @@ -309,7 +352,7 @@ module.exports = function(sails) {
loadServices: function (cb) {
buildDictionary.optional({
dirname : sails.config.paths.services,
filter : /(.+)\.(js|coffee|litcoffee)$/,
filter : new RegExp("(.+)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
depth : 1,
caseSensitive : true
}, bindToSails(cb));
Expand Down Expand Up @@ -343,7 +386,7 @@ module.exports = function(sails) {
loadPolicies: function (cb) {
buildDictionary.optional({
dirname: sails.config.paths.policies,
filter: /(.+)\.(js|coffee|litcoffee)$/,
filter: new RegExp("(.+)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
replaceExpr: null,
flattenDirectories: true,
keepDirectoryPath: true
Expand All @@ -365,7 +408,7 @@ module.exports = function(sails) {
hooksFolder: function(cb) {
buildDictionary.optional({
dirname: sails.config.paths.hooks,
filter: /^(.+)\.(js|coffee|litcoffee)$/,
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 @@ -459,7 +502,7 @@ module.exports = function(sails) {
loadBlueprints: function (cb) {
buildDictionary.optional({
dirname: sails.config.paths.blueprints,
filter: /(.+)\.(js|coffee|litcoffee)$/,
filter: new RegExp("(.+)\.(" + sails.config.moduleloader.sourceExt.join('|') + ")$"),
useGlobalIdForKeyName: true
}, cb);
},
Expand All @@ -475,7 +518,7 @@ module.exports = function(sails) {
loadResponses: function (cb) {
buildDictionary.optional({
dirname: sails.config.paths.responses,
filter: /(.+)\.(js|coffee|litcoffee)$/,
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