default.coffee | |
---|---|
This convention attempts to adhere to a sane and relatively "standard" set of principles that should work for most node modules. Of course, there really isn't anything close to a standard naming & package layout style, so you might want to take a look at the other conventions if the defaults don't do it for you. Or write your own! Any options not specified in any other conventions fall back to those defined in this one. | |
An ExampleGiven this project hierarchy:
The exported module hierarchy will be as follows (assuming that lib/ is autorequired): | class Default |
Naming Conventions | |
Given a directory name and parent path, return the property name that should be used to represent it in the module's hierarchy. The default is to convert to a | directoryToProperty: (directoryName, parentPath) ->
@camelCase directoryName |
Given a file name and parent path, return the property that should be used to represent it in the module's hierarchy. The default is to expose the file's exports as a | fileToProperty: (fileName, parentPath) ->
@camelCase @stripFileExtension fileName |
Autorequired Source Evaluation | |
Gives the convention an opportunity to modify the sandbox of an autorequired source file before it is loaded. Returns the sandbox that was modified (in case you want to replace it). By default, this registers several sets of globals for the module. See [ | modifySandbox: (sandbox, module) ->
module._globalLazyLoads = @globalLazyLoads(module)
sandbox |
Gives the convention an opportunity to modify the source of a file before it is loaded. Returns the modified source. By default, this works in concert with | modifySource: (source, module) ->
"""
for (var key in module._globalLazyLoads) {
try {
Object.defineProperty(global, key, {
enumerable: false, configurable: true, get: module._globalLazyLoads[key]
});
} catch (err) {}
}
delete module._globalLazyLoads;
""" + source |
Gives the convention an opportunity to modify the exports before they are returned. The convention can also peek into the state of the sandbox after evaluation if it wishes to pull out any properties defined globally within the evaluated module. Returns the modified exports. By default, this is a no-op. | modifyExports: (exports, module) ->
exports |
Autorequired Module Globals | |
Modules to globally register for autorequiring. All the core node libraries. Each one will be registered with a name adhering to You probably don't want to override this list. If you wish to add extra modules, define them
via | globalModules: [
'assert', 'buffer', 'child_process', 'constants', 'crypto', 'dgram', 'dns', 'events',
'freelist', 'fs', 'http', 'https', 'net', 'os', 'path', 'querystring', 'readline', 'repl',
'stream', 'string_decoder', 'sys', 'timers', 'tls', 'tty', 'url', 'util', 'vm'
] |
Any additional modules to add to the | extraGlobalModules: [] |
Returns an object of properties that should be lazy loaded as global objects in an autorequired source file. It should be a map of property names to functions (that will be called on first reference, the result is then memoized). This registers several sets of globals for the module: | globalLazyLoads: (module) ->
result = {}
@appendGlobalModules(result, module)
@appendSameLevelModules(result, module)
@appendParentModules(result, module)
result |
| appendGlobalModules: (lazyLoads, module) ->
for mod in @globalModules.concat @extraGlobalModules
do (mod) =>
lazyLoads[@directoryToProperty(mod)] = -> require mod |
| appendSameLevelModules: (lazyLoads, module) ->
for key of module.autorequireParent
unless key == module.id
do (key) ->
lazyLoads[key] = -> module.autorequireParent[key] |
| appendParentModules: (lazyLoads, module) -> |
For example, from within
Every one of these properties is lazy-loaded. | |
Helpers & Utility | |
Strips the extension from a file name. | stripFileExtension: (fileName) ->
/(.+?)(\.[^.]*$|$)/(fileName)[1] |
CamelCaps a path component. | camelCaps: (pathComponent) ->
pathComponent.split(/[-_]+/).map((val) -> val[0].toLocaleUpperCase() + val[1..]).join '' |
camelCase a path component. | camelCase: (pathComponent) ->
result = @camelCaps pathComponent
result[0].toLocaleLowerCase() + result[1..] |
under_score a path component. | underscore: (pathComponent) ->
pathComponent.split(/[-_]+/).join '_'
module.exports = Default
|