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_STYLES_XML = `<?xml version="1.0" encoding="utf-8"?><resources></resources>`;
|
10 | async function getProjectColorsXMLPathAsync(projectDir) {
|
11 | try {
|
12 | const shellPath = path_1.default.join(projectDir, 'android');
|
13 | if ((await fs_extra_1.default.stat(shellPath)).isDirectory()) {
|
14 | const colorsPath = path_1.default.join(shellPath, 'app/src/main/res/values/colors.xml');
|
15 | await fs_extra_1.default.ensureFile(colorsPath);
|
16 | return colorsPath;
|
17 | }
|
18 | }
|
19 | catch (error) {
|
20 | throw new Error('No android directory found in your project.');
|
21 | }
|
22 | return null;
|
23 | }
|
24 | exports.getProjectColorsXMLPathAsync = getProjectColorsXMLPathAsync;
|
25 | async function readColorsXMLAsync(colorsPath) {
|
26 | const contents = await fs_extra_1.default.readFile(colorsPath, { encoding: 'utf8', flag: 'r' });
|
27 | const parser = new xml2js_1.Parser();
|
28 | const manifest = parser.parseStringPromise(contents || BASE_STYLES_XML);
|
29 | return manifest;
|
30 | }
|
31 | exports.readColorsXMLAsync = readColorsXMLAsync;
|
32 | async function writeColorsXMLAsync(colorsPath, colorsContent) {
|
33 | const colorsXml = new xml2js_1.Builder().buildObject(colorsContent);
|
34 | await fs_extra_1.default.ensureDir(path_1.default.dirname(colorsPath));
|
35 | await fs_extra_1.default.writeFile(colorsPath, colorsXml);
|
36 | }
|
37 | exports.writeColorsXMLAsync = writeColorsXMLAsync;
|
38 | function setColorItem(itemToAdd, colorFileContentsJSON) {
|
39 | if (colorFileContentsJSON.resources.color) {
|
40 | let colorNameExists = colorFileContentsJSON.resources.color.filter((e) => e['$'].name === itemToAdd[0]['$'].name)[0];
|
41 | if (colorNameExists) {
|
42 | colorNameExists['_'] = itemToAdd[0]['_'];
|
43 | }
|
44 | else {
|
45 | colorFileContentsJSON.resources.color = colorFileContentsJSON.resources.color.concat(itemToAdd);
|
46 | }
|
47 | }
|
48 | else {
|
49 | if (typeof colorFileContentsJSON.resources === 'string') {
|
50 |
|
51 | colorFileContentsJSON.resources = {};
|
52 | }
|
53 | colorFileContentsJSON.resources.color = itemToAdd;
|
54 | }
|
55 | return colorFileContentsJSON;
|
56 | }
|
57 | exports.setColorItem = setColorItem;
|
58 |
|
\ | No newline at end of file |