-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathpostinstall.js
101 lines (85 loc) · 3.32 KB
/
postinstall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const fs = require("fs");
const path = require("path");
const foregroundServicePermTemplate = `
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<!-- <uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/> declare permission like this according to your use case https://developer.android.com/about/versions/14/changes/fgs-types-required -->
`;
const metadataTemplate = `
<meta-data
android:name="com.supersami.foregroundservice.notification_channel_name"
android:value="Sticky Title"
/>
<meta-data
android:name="com.supersami.foregroundservice.notification_channel_description"
android:value="Sticky Description."
/>
<meta-data
android:name="com.supersami.foregroundservice.notification_color"
android:resource="@color/blue"
/>
<service android:name="com.supersami.foregroundservice.ForegroundService"></service> // also define android:foregroundServiceType="" according to your use case
<service android:name="com.supersami.foregroundservice.ForegroundServiceTask"></service> // also define android:foregroundServiceType="" according to your use case
`;
const androidManifestPath = `${process.cwd()}/android/app/src/main/AndroidManifest.xml`;
fs.readFile(androidManifestPath, "utf8", function (err, data) {
if (err) {
return console.log(err);
}
if (!data.includes(foregroundServicePermTemplate)) {
const reg = /<manifest[^>]*>/;
const content = reg.exec(data)[0];
const result = data.replace(
reg,
`${content}\n${foregroundServicePermTemplate}`
);
fs.writeFile(androidManifestPath, result, "utf8", function (err) {
if (err) return console.log(err);
});
}
if (!data.includes(metadataTemplate)) {
const reg = /<application[^>]*>/;
const content = reg.exec(data)[0];
const result = data.replace(reg, `${content}${metadataTemplate}`);
console.log({ result });
fs.writeFile(androidManifestPath, result, "utf8", function (err) {
if (err) return console.log(err);
});
}
});
const colorTemplate = `
<item name="blue" type="color">#00C4D1</item>
<integer-array name="androidcolors">
<item>@color/blue</item>
</integer-array>
`;
const colorFilePath = `${process.cwd()}/android/app/src/main/res/values/colors.xml`;
// Ensure the directory exists
const dirPath = path.dirname(colorFilePath);
fs.mkdirSync(dirPath, { recursive: true });
// Check if the file exists
if (!fs.existsSync(colorFilePath)) {
// Create the file with initial content if it doesn't exist
fs.writeFileSync(colorFilePath, `<resources>${colorTemplate}</resources>`, "utf8");
console.log(`Successfully created color file at ${colorFilePath}`);
} else {
// Read and update the file if it exists
fs.readFile(colorFilePath, "utf8", function (err, data) {
if (err) {
return console.error(`Error reading file: ${err}`);
}
const reg = /<resources[^>]*>/;
const content = reg.exec(data)?.[0];
let result;
if (!content) {
result = `<resources>${colorTemplate}</resources>`;
} else {
result = data.replace(reg, `${content}${colorTemplate}`);
}
fs.writeFile(colorFilePath, result, "utf8", function (err) {
if (err) {
return console.error(`Error writing file: ${err}`);
}
console.log(`Successfully updated color file at ${colorFilePath}`);
});
});
}