-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
23db0c8
Add IcedCoffeeScript support, add lazy loading for CoffeeScript and I…
internalfx 508d6c1
Add sails.config.moduleloader, and build regexes from global config
internalfx 6c309ac
Merge remote-tracking branch 'upstream/master' into icy_coffee
internalfx 1f93490
fix typo
internalfx e768c64
Merge remote-tracking branch 'upstream/master' into icy_coffee
internalfx c40d9fa
Merge remote-tracking branch 'upstream/master' into icy_coffee
internalfx afb346f
Merge branch 'master' of github.com:balderdashy/sails into icy_coffee
internalfx 3d390b7
Merge branch 'master' of github.com:balderdashy/sails into icy_coffee
internalfx 6e57b7d
Merge branch 'master' of https://github.com/balderdashy/sails into ic…
internalfx 36dde17
Add livescript support
internalfx 5685d67
Merge branch 'master' of https://github.com/balderdashy/sails into ic…
internalfx 4d5fbf6
Remove TODO's since I did them :smile:
internalfx 138eefb
cleanup, make code DRY'er, allow easy addition of new languages
internalfx 7d8d61a
add comments on the supported languages array
internalfx 5193ba7
fix failing test
internalfx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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(), | ||
|
@@ -109,6 +80,71 @@ module.exports = function(sails) { | |
layout: path.resolve(config.appPath, 'views/layout.ejs'), | ||
} | ||
}; | ||
|
||
var hasCoffee = false, hasIced = false, hasLive = false; | ||
|
||
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); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}, | ||
|
||
|
||
|
@@ -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(); | ||
}, | ||
|
||
|
@@ -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); | ||
|
@@ -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); | ||
}, | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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/ | ||
|
@@ -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)); | ||
}, | ||
|
@@ -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) { | ||
|
@@ -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)); | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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); | ||
}, | ||
|
@@ -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)); | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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