-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathselfhost-setup.js
More file actions
260 lines (239 loc) · 7.43 KB
/
selfhost-setup.js
File metadata and controls
260 lines (239 loc) · 7.43 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const replace = require("replace-in-file")
const fs = require("fs")
const path = require("path")
const selfhostConfigEnv = require("./selfhost/env.js")
// Get custom self hosted app name, bundle id and apple development team id
const appName = selfhostConfigEnv.APP_NAME
const bundleId = selfhostConfigEnv.BUNDLE_ID
const teamId = selfhostConfigEnv.TEAM_ID
const LOCKER_BUNDLE_ID_SELFHOST = "com.cystack.locker.selfhost"
const LOCKER_TEAM_ID = "W7S57TNBH5"
const LOCKER_APP_NAME = "SHLocker"
// const appDir = path.dirname(require.main.filename);
/**
* Create directory if not exists
* @param {string} dir direactory path
*/
function mkdirIfNotExist(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}
/**
* Recursively moving files and directory from source path to target path
* @param {string} sourceDir
* @param {string} targetDir
* @param {string} mode ('copy' | 'cut') Copy or move
* @returns
*/
function recursivelyMovingFiles(sourceDir, targetDir, mode) {
mkdirIfNotExist(targetDir)
return new Promise((resolve, reject) => {
fs.readdir(sourceDir, (err, files) => {
if (err) {
reject(err)
return
}
files.forEach((file) => {
const oldPath = path.join(sourceDir, file)
const newPath = path.join(targetDir, file)
const stat = fs.lstatSync(oldPath)
if (stat.isDirectory()) {
mkdirIfNotExist(newPath)
recursivelyMovingFiles(oldPath, newPath, mode)
.then(() => {
if (files.indexOf(file) === files.length - 1) {
resolve()
}
})
.catch((err) => reject(err))
} else if (stat.isFile()) {
const callBack = (err) => {
if (err) {
reject(err)
return
}
if (files.indexOf(file) === files.length - 1) {
resolve()
}
}
if (mode === "copy") {
fs.copyFile(oldPath, newPath, 0, callBack)
} else {
fs.rename(oldPath, newPath, callBack)
}
}
})
})
})
}
/**
* Delete directories from paths (exclude package)
* @param {{
* paths: string[]
* oldPackage: string
* package: string
* }} param0
*/
function deleteOldAndroidPackage({ paths, oldPackage, newPackage }) {
try {
const oldPackagePath = oldPackage.split("/")
const packagePath = newPackage.split("/")
// search for directories outside the new package path
let index = 0
while (true) {
if (index === oldPackagePath.length) {
// the new package path is nested within the old package
// do not remove anythings
index = -1
break
}
if (index === packagePath.length) {
break
}
if (oldPackagePath[index] === packagePath[index]) {
// continue check nest path
index += 1
} else {
break
}
}
if (index === -1) {
return
}
// path need to remove
const unusedDir = oldPackagePath.slice(0, index + 1)
paths.forEach((p) => {
const removeDir = path.join(p, ...unusedDir)
fs.rmSync(removeDir, { recursive: true, force: true })
})
} catch (error) {
console.log(error)
throw new Error()
}
}
const replaceDefaultLockerBundleIDandAppName = async () => {
console.log("Replacing App bundle ID, App name, Team ID...")
const replaceBundleIdOptions = {
// find and replace locker with new bundle id
files: [
"./android/build.gradle",
"./android/app/BUCK",
"./android/app/build.gradle",
"./android/app/proguard-rules.pro",
"./android/app/src/main/AndroidManifest.xml",
"./android/app/src/main/res/values/strings.xml",
"./android/app/src/**/**/**/**/**/**/*.java",
"./android/app/src/**/**/**/**/**/**/**/*.java",
"./android/app/src/**/**/**/**/**/**/**/**/*.java",
"./app/components/webviewModal/WebviewModal.tsx",
"./app/config/constants.ts",
"./app/navigators/RootNavigator.tsx",
"./app/screens/auth/menu/autofillService/AutofillServiceScreen.android.tsx",
"./ios/Locker/Info.plist",
"./ios/Locker/*.entitlements",
"./ios/Locker/RNCryptoService/RSAUtils.swift",
"./ios/Locker.xcodeproj/project.pbxproj",
"./ios/LockerAutofill/Config.xcconfig",
"./ios/LockerAutofill/*.entitlements",
"./ios/LockerAutofill/Utils/Utils.swift",
],
// test selfhosted bundle id , teamid
from: [
new RegExp(LOCKER_BUNDLE_ID_SELFHOST, "g"),
new RegExp(LOCKER_TEAM_ID, "g"),
new RegExp(LOCKER_APP_NAME, "g"),
],
to: [bundleId, teamId, appName],
}
try {
const results = await replace(replaceBundleIdOptions)
const changedFiles = results.filter((result) => result.hasChanged).map((result) => result.file)
changedFiles.forEach((element) => {
console.log(element)
})
} catch (error) {
console.error("Replacing App bundle ID, app name error: ", error)
throw new Error()
}
}
const refactoringAndroidPackage = async () => {
console.log("Refactor android package...")
const androidSrcDir = "./android/app/src"
const modes = ["debug", "main", "release"]
const oldPackage = LOCKER_BUNDLE_ID_SELFHOST.replaceAll(".", "/")
const newPackage = bundleId.replaceAll(".", "/")
const mainAndroidPackageDir = path.join(androidSrcDir, "main", "java", newPackage)
if (!fs.existsSync(mainAndroidPackageDir)) {
Promise.all(
modes.map((mode) => {
const sourceDir = path.join(androidSrcDir, mode, "java", oldPackage)
const targetDir = path.join(androidSrcDir, mode, "java", newPackage)
return recursivelyMovingFiles(sourceDir, targetDir, "cut")
}),
)
.then(() => {
console.log(`Refactor package to ${bundleId}`)
deleteOldAndroidPackage({
paths: modes.map((mode) => {
return path.join(androidSrcDir, mode, "java")
}),
oldPackage,
newPackage,
})
})
.catch((err) => {
console.log("Refactor android package error: ", err)
throw new Error()
})
} else {
console.log(`Android package ${bundleId} path exist.`)
}
}
const replaceAppAssets = async () => {
// console.log("Replacing App icon...")
const tasks = [
{
source: "./selfhost/icons/android",
target: "./android/app/src/main/res",
title: "Replacing Android App icon",
},
{
source: "./selfhost/icons/ios/app",
target: "./ios/Locker/Images.xcassets",
title: "Replacing IOS App icon and SplashScreen",
},
{
source: "./selfhost/icons/ios/service",
target: "./ios/LockerAutofill/assets.xcassets",
title: "Replacing IOS Service icon",
},
{
source: "./selfhost/logo",
target: "./assets/logo",
title: "Replacing Logo in app",
},
]
const exeTask = async (task) => {
try {
console.log(task.title, "..")
await recursivelyMovingFiles(task.source, task.target, "copy")
} catch (e) {
console.log(task.title, " error: ", e)
throw new Error()
}
}
await Promise.all(tasks.map((task) => exeTask(task)))
}
const main = async () => {
// validate app bundle id
const regexp = /^[a-z0-9]+(\.[a-z0-9]+)+$/gi
if (!regexp.test(bundleId)) {
throw new Error("Invalid app bundle id.")
}
await replaceDefaultLockerBundleIDandAppName()
await refactoringAndroidPackage()
await replaceAppAssets()
}
// ---------------- MAIN APP-------------
main()