1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const path_1 = __importDefault(require("path"));
|
7 | const fs_extra_1 = __importDefault(require("fs-extra"));
|
8 | const xml2js_1 = require("xml2js");
|
9 | const BASE_STRINGS_XML = `<resources></resources>`;
|
10 | async 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 | }
|
24 | exports.getProjectStringsXMLPathAsync = getProjectStringsXMLPathAsync;
|
25 | async 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 | }
|
31 | exports.readStringsXMLAsync = readStringsXMLAsync;
|
32 | async 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 | }
|
37 | exports.writeStringsXMLAsync = writeStringsXMLAsync;
|
38 | function 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 |
|
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 |
|
52 | stringFileContentsJSON.resources = {};
|
53 | }
|
54 | stringFileContentsJSON.resources.string = itemToAdd;
|
55 | }
|
56 | return stringFileContentsJSON;
|
57 | }
|
58 | exports.setStringItem = setStringItem;
|
59 |
|
\ | No newline at end of file |