UNPKG

2.56 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_STYLES_XML = `<?xml version="1.0" encoding="utf-8"?><resources></resources>`;
10async 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}
24exports.getProjectColorsXMLPathAsync = getProjectColorsXMLPathAsync;
25async 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}
31exports.readColorsXMLAsync = readColorsXMLAsync;
32async 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}
37exports.writeColorsXMLAsync = writeColorsXMLAsync;
38function 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 //file was empty and JSON is `{resources : ''}`
51 colorFileContentsJSON.resources = {};
52 }
53 colorFileContentsJSON.resources.color = itemToAdd;
54 }
55 return colorFileContentsJSON;
56}
57exports.setColorItem = setColorItem;
58//# sourceMappingURL=Colors.js.map
\No newline at end of file