UNPKG

2.61 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const path_1 = __importDefault(require("path"));
7const fs_extra_1 = __importDefault(require("fs-extra"));
8const xml2js_1 = require("xml2js");
9const BASE_STRINGS_XML = `<resources></resources>`;
10async function getProjectStringsXMLPathAsync(projectDir) {
11 try {
12 const shellPath = path_1.default.join(projectDir, 'android');
13 if ((await fs_extra_1.default.stat(shellPath)).isDirectory()) {
14 const stringsPath = path_1.default.join(shellPath, 'app/src/main/res/values/strings.xml');
15 await fs_extra_1.default.ensureFile(stringsPath);
16 return stringsPath;
17 }
18 }
19 catch (error) {
20 throw new Error('No android directory found in your project.');
21 }
22 return null;
23}
24exports.getProjectStringsXMLPathAsync = getProjectStringsXMLPathAsync;
25async function readStringsXMLAsync(stringsPath) {
26 const contents = await fs_extra_1.default.readFile(stringsPath, { encoding: 'utf8', flag: 'r' });
27 const parser = new xml2js_1.Parser();
28 const manifest = parser.parseStringPromise(contents || BASE_STRINGS_XML);
29 return manifest;
30}
31exports.readStringsXMLAsync = readStringsXMLAsync;
32async function writeStringsXMLAsync(stringsPath, stringsContent) {
33 const stringsXml = new xml2js_1.Builder().buildObject(stringsContent);
34 await fs_extra_1.default.ensureDir(path_1.default.dirname(stringsPath));
35 await fs_extra_1.default.writeFile(stringsPath, stringsXml);
36}
37exports.writeStringsXMLAsync = writeStringsXMLAsync;
38function setStringItem(itemToAdd, stringFileContentsJSON) {
39 if (stringFileContentsJSON.resources.string) {
40 let stringNameExists = stringFileContentsJSON.resources.string.filter((e) => e['$'].name === itemToAdd[0]['$'].name)[0];
41 if (stringNameExists) {
42 // replace the previous value
43 stringNameExists['_'] = itemToAdd[0]['_'];
44 }
45 else {
46 stringFileContentsJSON.resources.string = stringFileContentsJSON.resources.string.concat(itemToAdd);
47 }
48 }
49 else {
50 if (typeof stringFileContentsJSON.resources === 'string') {
51 // file was empty and JSON is `{resources : ''}`
52 stringFileContentsJSON.resources = {};
53 }
54 stringFileContentsJSON.resources.string = itemToAdd;
55 }
56 return stringFileContentsJSON;
57}
58exports.setStringItem = setStringItem;
59//# sourceMappingURL=Strings.js.map
\No newline at end of file