Skip to content

Commit 6b7bcf0

Browse files
committed
New command line loader and baseDir. Fixed module loading problems with invisible superior level node_modules directory
1 parent de94763 commit 6b7bcf0

File tree

5 files changed

+180
-114
lines changed

5 files changed

+180
-114
lines changed

bin/cli.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2011 Firebase.co and Contributors - http://www.firebase.co
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
/*
22+
* This file bootstraps the Runtime for the Command Line. It's compiled in place of the initial script of the app. Check the file bin/priest
23+
*/
24+
25+
var priest = require('priest')
26+
var path = require('path')
27+
var sys = require('sys')
28+
var fs = require('fs')
29+
30+
module.exports = function() {
31+
var pureArgs = process.argv.slice(2)
32+
var noArgs = pureArgs.length == 0
33+
var mainScriptPath = module.filename
34+
var mainScriptDirName = path.dirname(mainScriptPath)
35+
36+
var expressionName = priest.inferExpressionNameByFileName(path.basename(mainScriptPath))
37+
if(!expressionName) {
38+
throw "The file '" + scriptName+ "' was not recognized as a priest script due file name extension incompatibility"
39+
return
40+
}
41+
42+
require.paths.unshift(path.join(mainScriptDirName,'node_modules'))
43+
44+
var runtime = new priest.Runtime()
45+
runtime.moduleRequire = function(moduleName) {
46+
return require(moduleName)
47+
}
48+
var manifestPath = path.join(mainScriptDirName, priest.DEFAULT_MANIFEST_FILE_NAME)
49+
path.exists(manifestPath, function(manifestFound) {
50+
var self = this
51+
var initializationFinished = function(err) {
52+
if(err) {
53+
console.error(err.toString())
54+
process.exit(1)
55+
}
56+
if(pureArgs.indexOf('--print-expressions') != -1) {
57+
var expressions = []
58+
var expNames = Object.keys(runtime.loadedExpressionsMeta)
59+
for(var i = 0; i < expNames.length; i++) {
60+
var meta = runtime.loadedExpressionsMeta[expNames[i]]
61+
expressions.push({
62+
name: meta.name,
63+
flags: meta.flags
64+
})
65+
}
66+
sys.print(JSON.stringify(expressions))
67+
process.exit(0)
68+
} else
69+
{
70+
var contextBase = {};
71+
contextBase._resultCallback = function(res) {
72+
if(!(res instanceof priest.IgnoreOutput)) {
73+
sys.print(JSON.stringify(res))
74+
}
75+
process.exit(0)
76+
}
77+
contextBase._loopCallback = function() {};
78+
contextBase._inputExpression = function() {};
79+
contextBase._variables = {};
80+
contextBase._errorCallback = function() {};
81+
runtime.runExpressionByName(expressionName, contextBase ,null)
82+
}
83+
}
84+
85+
if(manifestFound) {
86+
runtime.loadFromManifestFile(manifestPath, initializationFinished)
87+
} else {
88+
runtime.setBaseDir(mainScriptDirName)
89+
// Manually load the scripts
90+
runtime.load(initializationFinished)
91+
}
92+
})
93+
94+
}

bin/priest

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,46 @@
11
#!/usr/bin/env node
2+
// Copyright (c) 2011 Firebase.co and Contributors - http://www.firebase.co
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
221

3-
var CommandLine = require('../src/CommandLine.js')
4-
5-
var cmd = new CommandLine()
6-
cmd.run()
22+
var fs = require('fs')
23+
var path = require('path')
24+
var priest = require('priest')
25+
require.extensions['.json'] = function (module, filename) {
26+
return module._compile(fs.readFileSync(path.join(__dirname,"cli.js"), 'utf8'), filename);
27+
};
28+
var pureArgs = process.argv.slice(2)
29+
var noArgs = pureArgs.length == 0
30+
if(noArgs) {
31+
printHelp()
32+
process.exit(0);
33+
} else {
34+
var initialFileName = pureArgs[pureArgs.length -1]
35+
var initialFileName = path.resolve(initialFileName)
36+
require(initialFileName)() // run the cli... check cli.js for more info.
37+
}
38+
39+
function printHelp() {
40+
console.log("Priest Runtime version " + priest.PackageInfo.version)
41+
console.log("usage: priest TheProject.Main.priest.json")
42+
console.log("options:")
43+
console.log(" --print-expressions: print all the expression names and flags loaded by the runtime as a JSON document to stdout")
44+
console.log("")
45+
console.log("Copyright (C) 2011 Firebase and Contributors. http://priest.firebase.co")
46+
}

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
module.exports = require('./src/core.js')
2-
module.exports.CommandLine = require('./src/CommandLine.js')
1+
module.exports = require('./src/core.js')

src/CommandLine.js

Lines changed: 0 additions & 103 deletions
This file was deleted.

src/core.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
// Copyright (c) 2011 Firebase.co and Contributors - http://www.firebase.co
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
121
var vm = require('vm')
222
var util = require('util')
323
var StringBuffer = require('./StringBuffer')
@@ -391,17 +411,17 @@ function Runtime() {
391411
this.registerWellKnownExpressionDir(dirName)
392412
this._paths = new PathCache()
393413
this.environmentName = process.env.NODE_ENV === undefined ? DEFAULT_ENVIRONMENT : process.env.NODE_ENV
414+
this.scriptDirectories = []
394415
this.moduleRequire = function(moduleName) {
395416
return require(moduleName)
396417
}
397418
this.events = new EventEmitter()
398-
this.scriptDirectories = [{
399-
path: path.resolve('.')
400-
}]
419+
401420

402421
this.mergedManifest = {
403422
modules: []
404423
}
424+
this.baseDir = '.'
405425
}
406426

407427
/*
@@ -593,7 +613,17 @@ Runtime.prototype._mergeWithManifestFile = function(manifestFile) {
593613
}
594614
}
595615
}
616+
617+
Runtime.prototype.setBaseDir = function(dir) {
618+
this.baseDir = dir
619+
}
620+
621+
Runtime.prototype.pathFromBaseDir = function(dir) {
622+
return path.join(this.baseDir, dir)
623+
}
624+
596625
Runtime.prototype.loadFromManifestFile = function(manifestFile, initializationCallback) {
626+
this.setBaseDir(path.dirname(manifestFile))
597627
this._mergeWithManifestFile(manifestFile)
598628
this.load(initializationCallback)
599629
return true
@@ -619,7 +649,10 @@ Runtime.prototype.loadManifestModules = function() {
619649
Prepares the Runtime to Run. Since the introduction of initializer expressions, you can provide a callback to know when the initialization finishes. If no callback is provided no initialization will be executed.
620650
*/
621651
Runtime.prototype.load = function(initializationCallback) {
622-
this.registerInitializersDir(path.resolve(constants.INITIALIZERS_DIR_NAME))
652+
this.scriptDirectories.push({
653+
path: this.pathFromBaseDir('.')
654+
})
655+
this.registerInitializersDir(this.pathFromBaseDir(constants.INITIALIZERS_DIR_NAME))
623656
var self = this
624657
this.loadManifestModules()
625658

@@ -878,4 +911,7 @@ module.exports.exportTestOnlyFunctions = function() {
878911
module.exports._testOnly_getHint = getHint
879912

880913
}
881-
914+
module.exports.IgnoreOutput = function() {
915+
916+
}
917+
module.exports.PackageInfo = JSON.parse(fs.readFileSync(path.join(__dirname,"../package.json"), 'utf8'))

0 commit comments

Comments
 (0)