1 | const fs = require('fs')
|
2 | const log = require('npmlog')
|
3 | const path = require('path')
|
4 |
|
5 | log.heading = 'react-native-branch'
|
6 |
|
7 | function addBranchConfigToAndroidAssetsFolder() {
|
8 | if (fs.existsSync(path.join('.', 'branch.android.json'))) {
|
9 | ensureAndroidAssetsFolder('main')
|
10 | addSymbolicLink(path.join('..', '..', '..', '..', '..', 'branch.android.json'),
|
11 | path.join('.', 'android', 'app', 'src', 'main', 'assets', 'branch.json'))
|
12 | }
|
13 | else if (fs.existsSync(path.join('.', 'branch.json'))) {
|
14 | ensureAndroidAssetsFolder('main')
|
15 | addSymbolicLink(path.join('..', '..', '..', '..', '..', 'branch.json'),
|
16 | path.join('.', 'android', 'app', 'src', 'main', 'assets', 'branch.json'))
|
17 | }
|
18 |
|
19 |
|
20 | if (fs.existsSync(path.join('.', 'branch.android.debug.json'))) {
|
21 | ensureAndroidAssetsFolder('debug')
|
22 | addSymbolicLink(path.join('..', '..', '..', '..', '..', 'branch.android.debug.json'),
|
23 | path.join('.', 'android', 'app', 'src', 'debug', 'assets', 'branch.json'))
|
24 | }
|
25 | else if (fs.existsSync('./branch.debug.json')) {
|
26 | ensureAndroidAssetsFolder('debug')
|
27 | addSymbolicLink(path.join('..', '..', '..', '..', '..', 'branch.debug.json'),
|
28 | path.join('.', 'android', 'app', 'src', 'debug', 'assets', 'branch.json'))
|
29 | }
|
30 |
|
31 | log.info('Added Branch configuration to android project')
|
32 | }
|
33 |
|
34 | function addSymbolicLink(linkPath, path) {
|
35 | try {
|
36 | const stats = fs.lstatSync(path)
|
37 | if (stats && stats.isSymbolicLink()) {
|
38 | const dest = fs.readlinkSync(path)
|
39 | if (dest == linkPath) {
|
40 | log.warn(path + ' already present in Android app')
|
41 | return
|
42 | }
|
43 |
|
44 | log.error(path + ' is a link to ' + linkPath + '.')
|
45 | return
|
46 | }
|
47 | if (stats) {
|
48 | log.error(path + ' exists and is not a symbolic link.')
|
49 | return
|
50 | }
|
51 | }
|
52 | catch (error) {
|
53 | if (error.code != 'ENOENT') throw error
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | fs.symlinkSync(linkPath, path)
|
59 | }
|
60 | }
|
61 |
|
62 | function removeSymbolicLink(path) {
|
63 | try {
|
64 | const stats = fs.lstatSync(path)
|
65 |
|
66 | if (!stats.isSymbolicLink()) {
|
67 | log.warn(path + ' is not a symbolic link. Not removing.')
|
68 | return
|
69 | }
|
70 | }
|
71 | catch (error) {
|
72 | if (error.code != 'ENOENT') throw error
|
73 |
|
74 | return
|
75 | }
|
76 |
|
77 | fs.unlinkSync(path)
|
78 | }
|
79 |
|
80 | function ensureAndroidAssetsFolder(buildType) {
|
81 | ensureDirectory(path.join('.', 'android', 'app', 'src', buildType, 'assets'))
|
82 | }
|
83 |
|
84 | function ensureDirectory(path) {
|
85 | try {
|
86 | const stats = fs.statSync(path)
|
87 |
|
88 | if (!stats.isDirectory()) {
|
89 | throw(srcDir + ' exists and is not a directory.')
|
90 | }
|
91 | }
|
92 | catch (error) {
|
93 | if (error.code != 'ENOENT') throw error
|
94 |
|
95 | const parent = dirname(path)
|
96 | if (parent !== path) ensureDirectory(parent)
|
97 |
|
98 | fs.mkdirSync(path, 0o777)
|
99 | }
|
100 | }
|
101 |
|
102 | function dirname(path) {
|
103 | if (!path.search(path.sep)) return path
|
104 |
|
105 | const components = path.split(path.sep)
|
106 | components.splice(components.count - 1, 1)
|
107 | return components.join(path.sep)
|
108 | }
|
109 |
|
110 | function removeBranchConfigFromAndroidAssetsFolder() {
|
111 | removeSymbolicLink(path.join('.', 'android', 'app', 'src', 'main', 'assets', 'branch.json'))
|
112 | removeSymbolicLink(path.join('.', 'android', 'app', 'src', 'debug', 'assets', 'branch.json'))
|
113 | log.info('Removed Branch configuration from Android project')
|
114 | }
|
115 |
|
116 | function androidPackageName() {
|
117 | const path = path.join('.', 'android', 'app', 'src', 'main', 'AndroidManifest.xml')
|
118 | manifest = fs.readFileSync(path)
|
119 | const regex = /package=["']([A-Za-z\.0-9])["']/
|
120 | if (!manifest.match(regex)) throw 'package name not found in ' + path
|
121 | return regex.exec(manifest)[1]
|
122 | }
|
123 |
|
124 | function androidPackageDir() {
|
125 | return path.join('.', 'android', 'app', 'src', 'main', 'java', androidPackageName().replace(/\./, path.sep))
|
126 | }
|
127 |
|
128 | module.exports = {
|
129 | addBranchConfigToAndroidAssetsFolder: addBranchConfigToAndroidAssetsFolder,
|
130 | removeBranchConfigFromAndroidAssetsFolder: removeBranchConfigFromAndroidAssetsFolder
|
131 | }
|