-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.js
410 lines (346 loc) · 12.7 KB
/
core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
'use strict';
/***
* __ _ __ __
* ____ ___ ____ / /_ (_)/ /___ / /_ ___ _____ ____
* / __ `__ \ / __ \ / __ \ / // // _ \ / __ \ / _ \ / ___// __ \
* / / / / / // /_/ // /_/ // // // __// / / // __// / / /_/ /
* /_/ /_/ /_/ \____//_.___//_//_/ \___//_/ /_/ \___//_/ \____/
*
* mobile solutions for everyday heroes
*
* @file core module for {nativeloop}, a framework for building native mobile apps using nodejs style javascript.
* @module nativeloop/core
* @author Brenton House <[email protected]>*
* @version 1.0.0
* @since 1.0.0
* @copyright Copyright (c) 2017 by Superhero Studios Incorporated. All Rights Reserved.
* @license Licensed under the terms of the MIT License (MIT)
*
*/
const _ = require( 'lodash' );
const path = require( 'path' );
const resolve = require( 'resolve' );
const paths = require( 'global-paths' );
const hjson = require( 'hjson' );
const utils = require( './utils' );
// var config;
var _event;
const debug = require( 'debug' );
const log = debug( 'nativeloop' );
const warn = debug( 'nativeloop' );
debug.log = console.info.bind( console );
const alloyParser = require( './alloy-parser' );
const nativeloop_widgets = require( './widgets' );
var CONST;
var CU;
var U;
function handler( params ) {
init( params );
}
module.exports = handler;
/**
* @function init
* @summary Initialize the core handler
* @param {object} params - Parameters to initialize core
* @since 1.0.0
*/
var init = function( params ) {
// console.debug("params: " + JSON.stringify(params, null, 2));
CONST = params.constants;
CU = params.compilerUtils;
U = params.utils;
// var x = U.getWidgetDirectories.toString();
// console.error('x.length: ' + JSON.stringify(x.length, null, 2));
// console.error('U.getWidgetDirectories: ' + U.getWidgetDirectories.toString());
// console.error('U.getWidgetDirectories.findWidgetAsNodeModule: ' + U.getWidgetDirectories.findWidgetAsNodeModule.toString());
params.resolve.sync = _.wrap( params.resolve.sync, function( func, id, opts ) {
console.error( 'you are here → resolve.sync: ' + id );
var response;
try {
response = func( id, opts );
console.error( 'you are here → resolve.sync: after 1st' );
console.error( 'response: ' + JSON.stringify( response, null, 2 ) );
return response;
} catch( error ) {
// var regex = new RegExp( CONST.NPM_WIDGET_PREFIX + '([^\/]*)\/widget' );
var regex = new RegExp( CONST.NPM_WIDGET_PREFIX + '(.*)\/widget' );
var found = regex.exec( id );
console.error( 'found[1]: ' + JSON.stringify( found[ 1 ], null, 2 ) );
if( found && found[ 1 ] ) {
console.error( 'you are here → resolve.sync: before 2nd -- ' + found[ 1 ] );
response = func( found[ 1 ] + '/widget', opts );
console.error( 'you are here → resolve.sync: after 2nd' );
return response;
}
}
} );
params.jsonlint.parse = _.wrap( params.jsonlint.parse, function( func, input, opts ) {
debug.error( 'you are here → jsonlint.parse: ' );
debug.warn( 'input: ' + JSON.stringify( input, null, 2 ) );
var result = func( input );
return require( './fix-config' ).addWidgets( { input: result, logger: debug } );
// return hjson.parse( input, opts );
} );
U.getWidgetDirectories = _.wrap( U.getWidgetDirectories, function( func, options ) {
console.log( "********* getWidgetDirectories **********" );
return func( options );
} );
// var widgetParser = {
// parse: ( node, state ) => {
// if( !node.getAttribute( 'src' ) && state.widgetId ) {
// node.setAttribute( 'src', state.widgetId );
// }
// console.error( 'you are here → inside widgetParser' );
// node.nodeName = 'Require';
// node.setAttribute( 'type', 'widget' );
// let response = params.requireParser.parse( node, state );
// return response;
// }
// }
params.baseParser.parse = alloyParser.parse( {
baseParser: params.baseParser,
defaultParser: params.defaultParser,
widgetParser: params.widgetParser,
// widgetParser: widgetParser,
} );
// params.requireParser.parse = _.wrap( params.requireParser.parse, ( func, node, state ) => {
// console.error('you are here → inside the FAKE require parser');
// let response = func( node, state );
// // console.error( node );
// // console.error( node.attributes);
// // console.error( state );
// // console.error('node.getAttribute("__nativeloop"): ' + JSON.stringify(node.getAttribute('__nativeloop'), null, 2));
// // console.error( 'response: ' + JSON.stringify( response, null, 2 ) );
// if( node.getAttribute( '__nativeloop' ) && response.parent && response.parent.symbol ) {
// console.error( 'you are here → fixing parent.symbol' );
// // response.parent.symbol = response.parent.symbol.replace( /\.getViewEx[^\)]*\)/, '' );
// response.code = response.code.replace( /\.getViewEx[^\)]*\)/g, '' );
// }
// console.error( 'response.code: ' + JSON.stringify( response.code, null, 2 ) );
// console.error( 'response.parent: ' + JSON.stringify( response.parent, null, 2 ) );
// return response;
// } );
U.XML.getAlloyFromFile = handler.getAlloyFromFile;
console.log( "********* WRAPPING uglifyjs.parse **********" );
params.uglifyjs.parse = _.wrap( params.uglifyjs.parse, function( func, code, options ) {
console.log( "********* PRE:PARSE **********" );
var params = {
code: code
};
executeScripts( "preparse", params );
return func( params.code, options );
} );
alloyParser.init( {
CONST: CONST,
CU: CU,
U: U,
nativeloop_widgets: nativeloop_widgets,
} );
params.task( "pre:load", handler.preload );
params.task( "pre:compile", handler.precompile );
params.task( "post:compile", handler.postcompile );
params.task( "compile:app.js", handler.appjs );
}
/**
* @function getAlloyFromFile
* @summary summary
* @param {string} filename - description
* @since 1.0.0
* @returns {object} - description
*/
handler.getAlloyFromFile = function( filename ) {
// console.trace("***** INSIDE getAlloyFromFile()");
var doc = U.XML.parseFromFile( filename );
var docRoot = doc.documentElement;
if( _.toLower( docRoot.nodeName ) === "nativeloop" ) {
docRoot.nodeName = CONST.ROOT_NODE;
docRoot.setAttribute( "module", "/nativeloop" );
} else if( docRoot.nodeName === CONST.ROOT_NODE.toLowerCase() ) {
docRoot.nodeName = CONST.ROOT_NODE;
}
// Make sure the markup has a top-level <Alloy> tag
else if( docRoot.nodeName !== CONST.ROOT_NODE ) {
exports.die( [
'Invalid view file "' + filename + '".',
'All view markup must have a top-level <Alloy> tag'
] );
}
return docRoot;
};
// handler.preparse = function ( func ) {
// console.log( "********* WRAPPING uglifyjs.parse **********" );
// return _.wrap( func, function ( func, code, options ) {
// console.log( "********* PRE:PARSE **********" );
// var params = {
// code: code
// };
// executeScripts( "preparse", params );
// return func( params.code, options );
// } );
// }
// handler.alloyParser = alloyParser.parse;
/**
* @function splitTasks
* @summary summary
* @param {object} tasks - description
* @since 1.0.0
* @returns {object} - description
*/
function splitTasks( tasks ) {
var results = [];
if( !_.isArray( tasks ) ) {
tasks = [ tasks ];
}
// handler.logger.trace("splitting tasks...");
// handler.logger.trace("tasks: " + JSON.stringify(tasks, null, 2));
_.forEach( tasks, function( task ) {
if( _.isArray( task.events ) && !_.isEmpty( task.events ) ) {
// handler.logger.trace("found events to split");
_.forEach( task.events, function( event ) {
var splitTask = _.cloneDeep( task );
splitTask.events = event;
results.push( splitTask );
} );
} else {
results.push( task );
}
} );
return results;
}
var _init = _.once( function() {
loadConfig();
configureTasks();
} );
/**
* @function loadTasks
* @summary summary
* @since 1.0.0
* @returns {object} - description
*/
function loadTasks() {
var tasks = _.cloneDeep( _.get( handler.config, "nativeloop.tasks", [] ).concat( require( "./core_tasks" ) ) );
// handler.logger.debug("loaded Tasks: " + JSON.stringify(tasks, null, 2));
return tasks;
}
/**
* @function configureTasks
* @summary summary
* @param {object} tasks - description
* @since 1.0.0
* @returns {object} - description
*/
function configureTasks( tasks ) {
tasks = tasks || loadTasks();
var configuredTasks = [];
var importedTasks = [];
// handler.logger.trace("tasks coming into configureTasks(): " + JSON.stringify(tasks, null, 2));
_.forEach( tasks, function( task ) {
// handler.logger.trace("task: " + JSON.stringify(task, null, 2));
if( _.isString( task ) ) {
handler.logger.trace( "getting default tasks for module: " + task );
var target = require( resolve.sync( task, {
basedir: handler.event.dir.project
} ) );
importedTasks = importedTasks.concat( target.tasks || [] );
// handler.logger.error("target.tasks: " + JSON.stringify(target.tasks, null, 2));
// handler.logger.warn("imported tasks: " + JSON.stringify(importedTasks, null, 2));
return true;
} else {
// handler.logger.trace("adding task: " + JSON.stringify(task, null, 2));
configuredTasks.push( _.defaults( task, {
weight: 1000,
platforms: [ "ios", "android", "mobileweb", "windows" ]
} ) );
}
} );
if( !_.isEmpty( importedTasks ) ) {
handler.logger.debug( "Configuring importedTasks" )
configuredTasks = configuredTasks.concat( configureTasks( importedTasks ) );
} else {
handler.configuredTasks = splitTasks( configuredTasks );
// handler.logger.trace("handler.configuredTasks: " + JSON.stringify(handler.configuredTasks, null, 2));
}
return configuredTasks;
}
/**
* @function loadConfig
* @summary Load alloy config file and set it to property in handler
* @since 1.0.0
*/
var loadConfig = function() {
handler.logger.debug( "Loading alloy config file" );
handler.config = require( path.join( handler.event.dir.resourcesPlatform, "alloy", "CFG" ) );
// handler.logger.trace(JSON.stringify(handler.event, null, 2));
// handler.logger.trace(JSON.stringify(handler.config, null, 2));
}
Object.defineProperty( handler, "event", {
get: function() {
return _event;
},
set: function( event ) {
_event = event;
event.dir.resourcesPlatform = path.join( event.dir.resources, event.alloyConfig.platform === 'ios' ? 'iphone' : event.alloyConfig.platform );
},
enumerable: true,
configurable: false
} );
/**
* @function executeScripts
* @summary summary
* @param {string} eventName - description
* @param {object} params - description
* @since 1.0.0
*/
function executeScripts( eventName, params ) {
// handler.logger.trace("task to execute: " + JSON.stringify(handler.configuredTasks, null, 2));
// handler.logger.trace("handler.configuredTasks: " + JSON.stringify(handler.configuredTasks, null, 2));
var tasks = _.sortBy( _.filter( handler.configuredTasks || [], ( task ) => {
return task.events === eventName && _.includes( task.platforms, handler.event.alloyConfig.platform );
} ), "weight" );
params = params || {};
_.forEach( tasks, function( task ) {
//TODO: Check to make sure task is an object...
var taskParams = {
event: handler.event,
config: handler.config,
logger: handler.logger,
code: params.code,
};
_.defaults( taskParams, task );
handler.logger.debug( "executing task: " + task.module );
// handler.logger.debug("taskParams: " + JSON.stringify(taskParams, null, 2));
var target = require( resolve.sync( task.module, {
basedir: handler.event.dir.project,
paths: paths(),
} ) );
_.isFunction( target.execute ) && target.execute( taskParams );
if( taskParams.code ) {
// handler.logger.trace(taskParams.code);
params.code = taskParams.code;
}
} );
}
var events = [ "preload", "precompile", "postcompile", "appjs" ];
_.forEach( events, function( eventName ) {
handler[ eventName ] = function( event, logger ) {
// handler.logger = logger;
debug.trace = logger.trace.bind( logger );
debug.debug = logger.debug.bind( logger );
debug.info = logger.info.bind( logger );
debug.warn = logger.warn.bind( logger );
debug.error = logger.error.bind( logger );
handler.logger = debug;
handler.event = event;
_init();
handler.logger.warn( "********************* STARTING EVENT: " + eventName + " ***************************" );
handler.logger = logger;
// debug.trace = logger.trace.bind(logger);
// debug.warn = logger.debug.bind(logger);
// debug.info = logger.info.bind(logger);
// debug.warn = logger.warn.bind(logger);
// debug.error = logger.error.bind(logger);
// handler.logger = debug;
executeScripts( eventName );
handler.logger.warn( "********************* FINISHED EVENT: " + eventName + " ***************************" );
}
} );