UNPKG

3.08 kBJavaScriptView Raw
1/**
2 * Titanium SDK Library for Node.js
3 * Copyright (c) 2012-2013 by Appcelerator, Inc. All Rights Reserved.
4 * Please see the LICENSE file for information about licensing.
5 */
6'use strict';
7
8const fs = require('fs');
9const path = require('path');
10const DOMParser = require('xmldom').DOMParser;
11
12const appc = require('node-appc');
13const __ = appc.i18n(__dirname).__;
14const xml = appc.xml;
15
16const launchScreensCache = {};
17
18exports.load = load;
19exports.findLaunchScreens = findLaunchScreens;
20
21function load(projectDir, logger, opts) {
22 if (process.argv.indexOf('--i18n-dir') !== -1) {
23 // Enable developers to specify i18n directory location with build flag
24 const customI18n = process.argv[process.argv.indexOf('--i18n-dir') + 1];
25 if (customI18n && fs.existsSync(path.join(path.resolve(projectDir), customI18n))) {
26 projectDir = path.join(projectDir, customI18n);
27 }
28 }
29 const i18nDir = path.join(projectDir, 'i18n'),
30 data = {},
31 ignoreDirs = opts && opts.ignoreDirs,
32 ignoreFiles = opts && opts.ignoreFiles;
33
34 // TODO: Process languages in parallel!
35 if (fs.existsSync(i18nDir)) {
36 logger && logger.debug(__('Compiling localization files'));
37 fs.readdirSync(i18nDir).forEach(function (lang) {
38 const langDir = path.join(i18nDir, lang),
39 isDir = fs.statSync(langDir).isDirectory();
40
41 if (fs.existsSync(langDir) && isDir && (!ignoreDirs || !ignoreDirs.test(lang))) {
42 const s = data[lang] = {};
43
44 fs.readdirSync(langDir).forEach(function (name) {
45 const file = path.join(langDir, name);
46 if (/.+\.xml$/.test(name) && (!ignoreFiles || !ignoreFiles.test(name)) && fs.existsSync(file) && fs.statSync(file).isFile()) {
47 logger && logger.debug(__('Processing i18n file: %s', (lang + '/' + name).cyan));
48
49 const dest = name === 'app.xml' ? 'app' : 'strings',
50 obj = s[dest] = s[dest] || {},
51 dom = new DOMParser().parseFromString(fs.readFileSync(file).toString(), 'text/xml');
52
53 xml.forEachElement(dom.documentElement, function (elem) {
54 if (elem.nodeType == 1 && elem.tagName == 'string') { // eslint-disable-line eqeqeq
55 const name = xml.getAttr(elem, 'name');
56 name && (obj[name] = elem && elem.firstChild && elem.firstChild.data || '');
57 }
58 });
59 }
60 });
61 }
62 });
63 }
64
65 return data;
66}
67
68function findLaunchScreens(projectDir, logger, opts) {
69 if (launchScreensCache[projectDir]) {
70 return launchScreensCache[projectDir];
71 }
72
73 var i18nDir = path.join(projectDir, 'i18n'),
74 data = [];
75
76 opts || (opts = {});
77
78 if (fs.existsSync(i18nDir)) {
79 logger.debug(__('Checking for Splash Screen localization'));
80 fs.readdirSync(i18nDir).forEach(function (lang) {
81 var langDir = path.join(i18nDir, lang);
82 if (fs.existsSync(langDir) && fs.statSync(langDir).isDirectory() && (!opts.ignoreDirs || !opts.ignoreDirs.test(lang))) {
83 fs.readdirSync(langDir).forEach(function (name) {
84 if (/^(Default(-(Landscape|Portrait))?(-[0-9]+h)?(@[2-9]x)?)\.png$/.test(name)) {
85 data.push(path.join(langDir, name));
86 }
87 });
88 }
89 });
90 }
91
92 return launchScreensCache[projectDir] = data;
93}