-
Notifications
You must be signed in to change notification settings - Fork 7
/
jsca.api.parser.js
80 lines (66 loc) · 2.54 KB
/
jsca.api.parser.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
/**
* Parses the jsca.api and returns a mock implementation of the Titanium api.
*
* credits to and inspired by mockti.js (https://github.com/janruehling/mockti)
*/
function jscaApiParser(apiPath = __dirname){
var fs = require('fs');
var data = JSON.parse(fs.readFileSync(apiPath + '/api.jsca'));
var Ti;
Ti = {};
function _pushNamespaces(parentNamespace, namespace, list, functions, properties){
if(!parentNamespace[namespace]){
parentNamespace[namespace] = {};
}
var shift = list.shift();
if(shift != undefined){
_pushNamespaces(parentNamespace[namespace], shift, list, functions, properties);
} else {
functions.forEach(function(func){
parentNamespace[namespace][func.name] = function(){};
});
properties.forEach(function(prop){
if(prop.isClassProperty){
parentNamespace[namespace][prop.name] = prop.name;
}
if(prop.isInstanceProperty){
var funcForPropName = prop.name;
funcForPropName = funcForPropName.replace(/(^[a-z])/,function (p) { return p.toUpperCase(); } );
funcForPropName = 'get' + funcForPropName;
parentNamespace[namespace][funcForPropName] = function(){};
}
});
}
}
data.types.forEach(function (item) {
if (item.name.indexOf('Titanium.') == -1) return;
var name = item.name.slice(9);
var namespaces = name.split('.');
var firstNamespace = namespaces.shift();
if(firstNamespace != undefined){
_pushNamespaces(Ti, firstNamespace, namespaces, item.functions ? item.functions : [], item.properties ? item.properties : []);
}
});
return Ti;
}
/**
* In order to parse your local sdk's api.jsca, you have to:
* - create /spec/support/parser.json
* - add a property 'path' with as value the folder of your SDKs api.jsca
* - add /spec.support/parser.json to .gitignore so that it does not conflict with other developers
*
* .. or you can call #parse and copy your SDKs api.jsca to your /spec folder.
*/
function parseFromConfig(){
var fs = require('fs');
var data = JSON.parse(fs.readFileSync('./spec/support/parser.json'));
if(data.path){
return jscaApiParser(data.path);
} else {
throw "path to local Titanium jsca.json is not correctly defined";
}
}
module.exports = {
parse: jscaApiParser,
parseFromConfig: parseFromConfig
}