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
+ }
0 commit comments