UNPKG

2.67 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const xml2js_1 = require("xml2js");
4// TODO: make it so intent filters aren't written again if you run the command again
5function getIntentFilters(config) {
6 var _a, _b;
7 return _b = (_a = config.android) === null || _a === void 0 ? void 0 : _a.intentFilters, (_b !== null && _b !== void 0 ? _b : []);
8}
9exports.getIntentFilters = getIntentFilters;
10async function setAndroidIntentFilters(config, manifestDocument) {
11 const intentFilters = getIntentFilters(config);
12 if (!intentFilters.length) {
13 return manifestDocument;
14 }
15 let intentFiltersXML = renderIntentFilters(intentFilters).join('');
16 const parser = new xml2js_1.Parser();
17 const intentFiltersJSON = await parser.parseStringPromise(intentFiltersXML);
18 let mainActivity = manifestDocument.manifest.application[0].activity.filter((e) => e['$']['android:name'] === '.MainActivity');
19 mainActivity[0]['intent-filter'] = mainActivity[0]['intent-filter'].concat(intentFiltersJSON['intent-filter']);
20 return manifestDocument;
21}
22exports.setAndroidIntentFilters = setAndroidIntentFilters;
23function renderIntentFilters(intentFilters) {
24 // returns an array of <intent-filter> tags:
25 // [
26 // `<intent-filter>
27 // <data android:scheme="exp"/>
28 // <data android:scheme="exps"/>
29 //
30 // <action android:name="android.intent.action.VIEW"/>
31 //
32 // <category android:name="android.intent.category.DEFAULT"/>
33 // <category android:name="android.intent.category.BROWSABLE"/>
34 // </intent-filter>`,
35 // ...
36 // ]
37 return intentFilters.map((intentFilter) => {
38 const autoVerify = intentFilter.autoVerify ? ' android:autoVerify="true"' : '';
39 return `<intent-filter${autoVerify}>
40 ${renderIntentFilterData(intentFilter.data)}
41 <action android:name="android.intent.action.${intentFilter.action}"/>
42 ${renderIntentFilterCategory(intentFilter.category)}
43 </intent-filter>`;
44 });
45}
46exports.default = renderIntentFilters;
47function renderIntentFilterDatumEntries(datum) {
48 return Object.keys(datum)
49 .map(key => `android:${key}="${datum[key]}"`)
50 .join(' ');
51}
52function renderIntentFilterData(data) {
53 return (Array.isArray(data) ? data : [data])
54 .map(datum => `<data ${renderIntentFilterDatumEntries(datum)}/>`)
55 .join('\n');
56}
57function renderIntentFilterCategory(category) {
58 return (Array.isArray(category) ? category : [category])
59 .map(cat => `<category android:name="android.intent.category.${cat}"/>`)
60 .join('\n');
61}
62//# sourceMappingURL=IntentFilters.js.map
\No newline at end of file