1010///////////////// CONFIGURATION /////////////////
1111
1212var disableLogger = true ;
13- if ( process . env . AST_PARSER_DISABLE_LOGGING && process . env . AST_PARSER_DISABLE_LOGGING . trim ( ) === "true" ) {
13+ if ( process . env . AST_PARSER_DISABLE_LOGGING && process . env . AST_PARSER_DISABLE_LOGGING . trim ( ) === "true" ) {
1414 disableLogger = true ;
1515}
1616
1717loggingSettings = {
18- "logPath" : require ( "path" ) . dirname ( require . main . filename ) + "/logs/i.txt" ,
19- "strategy" : "console" ,
20- "APP_NAME" : "ast_parser" ,
18+ "logPath" : require ( "path" ) . dirname ( require . main . filename ) + "/logs/i.txt" ,
19+ "strategy" : "console" ,
20+ "APP_NAME" : "ast_parser" ,
2121 "disable" : disableLogger
2222} ;
2323
2424var fs = require ( "fs" ) ,
2525 babelParser = require ( "babylon" ) ,
2626 traverse = require ( "babel-traverse" ) ,
2727 logger = require ( './helpers/logger' ) ( loggingSettings ) ,
28- fileHelpers = require ( "./helpers/file_helpers" ) ( { logger : logger } ) ,
28+ fileHelpers = require ( "./helpers/file_helpers" ) ( { logger : logger } ) ,
2929 path = require ( "path" ) ,
3030 stringify = require ( "./helpers/json_extension" ) ,
3131 es5_visitors = require ( "./visitors/es5-visitors" ) ,
3232 t = require ( "babel-types" ) ,
33- filewalker = require ( 'filewalker' ) ,
3433 lazy = require ( "lazy" ) ,
3534 eol = require ( 'os' ) . EOL ,
3635
@@ -42,28 +41,29 @@ var fs = require("fs"),
4241 interfacesNamesFilePath = "../interfaces-names.txt" , //default interace_names file path
4342 interfaceNames = [ ] ;
4443
44+
4545//env variables
46- if ( process . env . AST_PARSER_OUT_FILE ) {
46+ if ( process . env . AST_PARSER_OUT_FILE ) {
4747 outFile = process . env . AST_PARSER_OUT_FILE . trim ( ) ;
4848}
49- if ( process . env . AST_PARSER_INPUT_DIR ) {
49+ if ( process . env . AST_PARSER_INPUT_DIR ) {
5050 inputDir = process . env . AST_PARSER_INPUT_DIR . trim ( ) ;
5151}
52- if ( process . env . AST_PARSER_INTERFACE_FILE_PATH ) {
52+ if ( process . env . AST_PARSER_INTERFACE_FILE_PATH ) {
5353 interfacesNamesFilePath = process . env . AST_PARSER_INTERFACE_FILE_PATH . trim ( ) ;
5454}
5555
5656
5757//console variables have priority
58- if ( arguments && arguments . length >= 3 ) {
58+ if ( arguments && arguments . length >= 3 ) {
5959 inputDir = arguments [ 2 ]
6060 console . log ( "inputDir: " + inputDir )
6161}
62- if ( arguments && arguments . length >= 4 ) {
62+ if ( arguments && arguments . length >= 4 ) {
6363 outFile = arguments [ 3 ]
6464 console . log ( "outFile: " + outFile )
6565}
66- if ( arguments && arguments . length >= 5 ) {
66+ if ( arguments && arguments . length >= 5 ) {
6767 interfacesNamesFilePath = arguments [ 4 ]
6868 console . log ( "interface names path: " + interfacesNamesFilePath )
6969}
@@ -77,29 +77,58 @@ fileHelpers.createFile(outFile)
7777* Traverses a given input directory and attempts to visit every ".js" file.
7878* It passes each found file down the line.
7979*/
80- var traverseFilesDir = function ( filesDir ) {
81-
82- if ( ! fs . existsSync ( filesDir ) ) {
80+ var traverseAndAnalyseFilesDir = function ( filesDir ) {
81+ if ( ! fs . existsSync ( filesDir ) ) {
8382 throw "The input dir: " + filesDir + " does not exist!" ;
8483 }
8584
86- filewalker ( filesDir )
87- . on ( "file" , function ( file , info ) {
88- if ( file . substring ( file . length - 3 , file . length ) === '.js' ) {
89- var currentFileName = path . join ( filesDir , file ) ;
85+ traverseDirectory ( filesDir ) ;
86+ }
87+
88+ function traverseDirectory ( dir ) {
89+ // list all files in directory
90+ fs . readdir ( dir , function ( err , files ) {
91+ var pJsonFile ;
92+
93+ for ( var i = 0 ; i < files . length ; i ++ ) {
94+ if ( files [ i ] === "package.json" ) {
95+ pJsonFile = true ;
96+ break ;
97+ }
98+ }
99+
100+ if ( pJsonFile ) {
101+ var fullPJsonPath = path . join ( dir , "package.json" ) ;
102+ var pjson = require ( fullPJsonPath ) ;
103+ if ( ! pjson . nativescript ) {
104+ // if (pjson.nativescript.sbgShouldNotVisit && pjson.nativescript.platforms) {
105+ // return;
106+ // }
107+ return ;
108+ }
109+ }
110+
111+ for ( var i = 0 ; i < files . length ; i += 1 ) {
112+ var file = path . join ( dir , files [ i ] ) ;
113+
114+ if ( file . substring ( file . length - 3 , file . length ) === '.js' ) {
115+ console . log ( file ) ;
90116
91- readFile ( currentFileName )
117+ readFile ( file )
92118 . then ( astFromFileContent )
93119 // .then(writeToFile)
94120 . then ( visitAst )
95121 . then ( writeToFile )
96122 . catch ( exceptionHandler )
97123 }
98- } )
99- . on ( 'error' , function ( err ) {
100- reject ( err ) ;
101- } )
102- . walk ( ) ;
124+
125+ var isDir = fs . statSync ( file ) . isDirectory ( ) ;
126+
127+ if ( isDir ) {
128+ traverseDirectory ( file ) ;
129+ }
130+ }
131+ } ) ;
103132}
104133
105134// ENTRY POINT!
@@ -108,21 +137,21 @@ var traverseFilesDir = function(filesDir) {
108137* After reading interface names runs the visiting api
109138*/
110139function readInterfaceNames ( ) {
111- return new Promise ( function ( resolve , reject ) {
140+ return new Promise ( function ( resolve , reject ) {
112141 new lazy ( fs . createReadStream ( interfacesNamesFilePath ) )
113- . lines
114- . forEach ( function ( line ) {
115- interfaceNames . push ( line . toString ( ) ) ;
116- // console.log(line.toString());
117- } ) . on ( 'pipe' , function ( err ) {
118- if ( err ) {
119- reject ( false ) ;
120- }
121- resolve ( inputDir ) ;
122- } ) ;
142+ . lines
143+ . forEach ( function ( line ) {
144+ interfaceNames . push ( line . toString ( ) ) ;
145+ // console.log(line.toString());
146+ } ) . on ( 'pipe' , function ( err ) {
147+ if ( err ) {
148+ reject ( false ) ;
149+ }
150+ resolve ( inputDir ) ;
151+ } ) ;
123152 } )
124153}
125- readInterfaceNames ( ) . then ( traverseFilesDir )
154+ readInterfaceNames ( ) . then ( traverseAndAnalyseFilesDir )
126155
127156
128157/*
@@ -133,7 +162,7 @@ var readFile = function (filePath, err) {
133162
134163 fs . readFile ( filePath , function ( err , data ) {
135164
136- if ( err ) {
165+ if ( err ) {
137166 logger . warn ( "+DIDN'T get content of file!" ) ;
138167 return reject ( err ) ;
139168 }
@@ -154,17 +183,17 @@ var readFile = function (filePath, err) {
154183var astFromFileContent = function ( data , err ) {
155184 return new Promise ( function ( resolve , reject ) {
156185
157- if ( err ) {
186+ if ( err ) {
158187 logger . warn ( "+DIDN'T parse ast from file!" ) ;
159188 return reject ( err ) ;
160189 }
161-
190+
162191 logger . info ( "+parsing ast from file!" ) ;
163192 // console.log("data: " + data.data);
164193 var ast = babelParser . parse ( data . data , {
165- minify : false ,
166- plugins : [ "decorators" ]
167- } ) ;
194+ minify : false ,
195+ plugins : [ "decorators" ]
196+ } ) ;
168197 data . ast = ast ;
169198 return resolve ( data ) ;
170199 } ) ;
@@ -180,8 +209,8 @@ function onlyUnique(value, index, self) {
180209* Passes the extracted bindings data down the line.
181210*/
182211var visitAst = function ( data , err ) {
183- return new Promise ( function ( resolve , reject ) {
184- if ( err ) {
212+ return new Promise ( function ( resolve , reject ) {
213+ if ( err ) {
185214 logger . warn ( "+DIDN'T visit ast!" ) ;
186215 return reject ( err ) ;
187216 }
@@ -211,26 +240,26 @@ var visitAst = function (data, err) {
211240 } ) ;
212241}
213242
214- var writeToFile = function ( data , err ) {
243+ var writeToFile = function ( data , err ) {
215244
216- return new Promise ( function ( resolve , reject ) {
245+ return new Promise ( function ( resolve , reject ) {
217246
218- if ( data . trim ( ) != "" ) {
247+ if ( data . trim ( ) != "" ) {
219248
220249 // fs.appendFile(outFile, stringify(data), function (writeFileError) {
221250 fs . appendFile ( outFile , data + eol , function ( writeFileError ) {
222- if ( err ) {
251+ if ( err ) {
223252 logger . warn ( "Error from writeToFile: " + err ) ;
224253 return reject ( err ) ;
225254 }
226- if ( writeFileError ) {
255+ if ( writeFileError ) {
227256 logger . warn ( "Error writing file: " + writeFileError ) ;
228257 return reject ( writeFileError ) ;
229258 }
230259
231260 logger . info ( "+appended '" + data + "' to file: " + outFile ) ;
232261 return resolve ( data ) ;
233-
262+
234263 } ) ;
235264 }
236265 } ) ;
@@ -241,7 +270,7 @@ var writeToFile = function(data, err) {
241270* If the error is criticalthe process is exited.
242271*/
243272var exceptionHandler = function ( reason ) {
244- if ( reason . errCode && reason . errCode === 1 ) {
273+ if ( reason . errCode && reason . errCode === 1 ) {
245274 logger . error ( "(*)(*)(*)Error: Exception Handler Caught: " + reason . message ) ;
246275 logger . error ( "PROCESS EXITING..." ) ;
247276 process . stderr . write ( reason . message ) ;
0 commit comments