-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility for loading files that modify the Monarch module
- Loading branch information
1 parent
d8e46f1
commit 1faa86d
Showing
3 changed files
with
51 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,14 @@ | ||
_ = require 'underscore' | ||
Monarch = require './load_core' | ||
|
||
loader = require './util/global_loader' | ||
loader.configure( | ||
dir: __dirname, | ||
globals: { Monarch, _ } | ||
) | ||
|
||
Monarch.Sql = {} | ||
|
||
loader.requireTree('.') | ||
|
||
module.exports = Monarch |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Global Loader - a utility for loading multiple files that collectively define | ||
# a common global module. Each file should export a function that receives a | ||
# single object containing the global modules to be modified. | ||
|
||
_ = require 'underscore' | ||
fs = require 'fs' | ||
glob = require 'glob' | ||
path = require 'path' | ||
|
||
loader = | ||
dir: '.' | ||
globals: {} | ||
|
||
configure: (options) -> | ||
for key in ['globals', 'dir'] | ||
loader[key] = options[key] if options[key] | ||
|
||
require: (filePath) -> | ||
fileExport = require(fullPath(filePath)) | ||
fileExport(loader.globals) if _.isFunction(fileExport) | ||
|
||
requireTree: (dirPath) -> | ||
filePaths = glob.sync("#{dirPath}/**/*", cwd: loader.dir) | ||
filePaths.forEach (filePath) -> | ||
if isFile(fullPath(filePath)) | ||
loader.require(stripExtension(filePath)) | ||
|
||
fullPath = (filePath) -> | ||
path.resolve(loader.dir, filePath) | ||
|
||
isFile = (filePath) -> | ||
fs.statSync(filePath).isFile() | ||
|
||
stripExtension = (filePath) -> | ||
extension = path.extname(filePath) | ||
filePath.replace("#{extension}$", '') | ||
|
||
module.exports = loader |
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