-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (50 loc) · 1.58 KB
/
index.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
const fs = require('fs');
const path = require('path');
const Tiapp = require('./Tiapp');
const U = require('./util');
const xml = require('./xml');
exports.find = function find(dir) {
const cwd = dir || process.cwd();
const parts = cwd.split(path.sep);
// remove empty element
if (parts[0] === '') {
parts.shift();
}
// iterate up through hierarchy to try and find a tiapp.xml
for (let i = 0, len = parts.length; i < len; i++) {
const p = (/^win/.test(process.platform) ? '' : path.sep)
+ path.join.apply(path, parts.slice(0, len - i).concat('tiapp.xml'));
if (fs.existsSync(p) && fs.statSync(p).isFile()) {
return p;
}
}
return null;
};
exports.parse = function parse(str, file) {
// make sure xml is a string
if (!str || !U.isString(str)) {
throw new Error('Bad argument. xml must be a string.');
}
// parse the xml
const doc = xml.parseFromString(str);
// make sure it's actually a tiapp.xml
if (!doc || !doc.documentElement) {
throw new Error('No XML document created', str);
} else if (doc.documentElement.nodeName !== 'ti:app') {
throw new Error('Parsed XML is not a tiapp.xml', str);
}
// create an instance of Tiapp
return new Tiapp(file, doc);
};
exports.load = function load(file) {
// make sure we have a file
if (typeof file !== 'undefined' && !U.isString(file)) {
throw new Error('Bad argument. If defined, file must be a string.');
}
file = file || exports.find();
if (!file || (file && !fs.existsSync(file))) {
throw new Error('tiapp.xml not found');
}
// parse the file
return exports.parse(fs.readFileSync(file, 'utf8'), file);
};