UNPKG

1.73 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Styles_1 = require("./Styles");
4const Colors_1 = require("./Colors");
5const COLOR_PRIMARY_KEY = 'colorPrimary';
6const DEFAULT_PRIMARY_COLOR = '#023c69';
7function getPrimaryColor(config) {
8 var _a;
9 return _a = config.primaryColor, (_a !== null && _a !== void 0 ? _a : DEFAULT_PRIMARY_COLOR);
10}
11exports.getPrimaryColor = getPrimaryColor;
12async function setPrimaryColor(config, projectDirectory) {
13 let hexString = getPrimaryColor(config);
14 const stylesPath = await Styles_1.getProjectStylesXMLPathAsync(projectDirectory);
15 const colorsPath = await Colors_1.getProjectColorsXMLPathAsync(projectDirectory);
16 if (!colorsPath || !stylesPath) {
17 return false;
18 }
19 let stylesJSON = await Styles_1.readStylesXMLAsync(stylesPath);
20 let colorsJSON = await Colors_1.readColorsXMLAsync(colorsPath);
21 let colorItemToAdd = [{ _: '', $: { name: '' } }];
22 let styleItemToAdd = [{ _: '', $: { name: '' } }];
23 colorItemToAdd[0]._ = hexString;
24 colorItemToAdd[0].$.name = COLOR_PRIMARY_KEY;
25 styleItemToAdd[0]._ = `@color/${COLOR_PRIMARY_KEY}`;
26 styleItemToAdd[0].$.name = COLOR_PRIMARY_KEY;
27 colorsJSON = Colors_1.setColorItem(colorItemToAdd, colorsJSON);
28 stylesJSON = Styles_1.setStylesItem(styleItemToAdd, stylesJSON);
29 try {
30 await Colors_1.writeColorsXMLAsync(colorsPath, colorsJSON);
31 await Styles_1.writeStylesXMLAsync(stylesPath, stylesJSON);
32 }
33 catch (e) {
34 throw new Error(`Error setting Android primary color. Cannot write new styles.xml to ${stylesPath}.`);
35 }
36 return true;
37}
38exports.setPrimaryColor = setPrimaryColor;
39//# sourceMappingURL=PrimaryColor.js.map
\No newline at end of file