-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.ts
232 lines (187 loc) · 5.77 KB
/
helper.ts
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
import { execSync } from 'node:child_process'
import { resolve } from 'node:dns/promises'
import { existsSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import arg from 'arg'
import merge from 'deepmerge'
import { create } from 'logua'
import semver from 'semver'
import type { NativeOptions, Options, Package } from './types'
export const log = create('numic', 'green')
export const basePath = () => {
// CWD during postinstall is in package, otherwise in project.
const currentWorkingDirectory = process.cwd()
// Required for pnpm as modules are nested deeper.
if (currentWorkingDirectory.includes('node_modules') && process.env.INIT_CWD) {
return process.env.INIT_CWD
}
if (currentWorkingDirectory.includes('node_modules/numic') || currentWorkingDirectory.includes('node_modules\\numic')) {
return join(currentWorkingDirectory, '../..')
}
return currentWorkingDirectory
}
const optionsSpecificationByScript = {
native: {
'--version': String,
'--debug': Boolean,
'--appName': String,
},
lint: {},
patch: {},
apply: {
'--skipEmpty': Boolean,
},
plugin: {},
}
export const cliOptions = (script: string) => {
const result: Record<string, string> = {}
if (script === 'ios' || script === 'android' || script === 'prompt') {
return result
}
const parsed = arg(optionsSpecificationByScript[script as keyof typeof optionsSpecificationByScript], {
permissive: false,
argv: process.argv.slice(3),
})
for (const option of Object.keys(parsed)) {
// @ts-ignore
result[option.replace('--', '')] = parsed[option]
}
return result
}
export const getFolders = () => ({
numic: join(basePath(), '.numic'),
user: {
ios: join(basePath(), 'ios'),
android: join(basePath(), 'android'),
},
plugin: {
ios: join(basePath(), '.numic', 'ios'),
android: join(basePath(), '.numic', 'android'),
},
plugins: join(basePath(), 'plugin'),
})
export const additionalCliArguments = () =>
[...process.argv]
.splice(3)
.map((item) => `"${item}"`)
.join(' ')
export const filterIos = (source: string) => {
if (source.includes('/ios/Pods/') || source.includes('/ios/build/')) {
return false
}
return true
}
export const filterAndroid = (source: string) => {
if (source.includes('/android/build/')) {
return false
}
return true
}
export const isOnline = async () => {
try {
await resolve('www.google.com')
return true
} catch (_) {
return false
}
}
export const checkCommandVersion = (command: string, version: string) => {
let commandOutput: string
try {
commandOutput = execSync(command, { encoding: 'utf8' }).trim()
} catch (_) {
// Ignore, probably missing executable and will fail later.
return true
}
if (semver.valid(commandOutput)) {
if (!semver.gt(commandOutput, version)) {
// Outdated.
return false
}
}
return true
}
const isTypeScript = (pkg: Package) => Boolean(pkg.devDependencies?.typescript || existsSync(join(basePath(), 'tsconfig.json')))
// Default options.
const defaultOptions = (pkg: Package) => ({
pkg,
typescript: isTypeScript(pkg),
})
let cache: Options | undefined
export const resetOptions = () => {
cache = undefined
}
export const options: () => Options = () => {
if (cache) {
return cache
}
let packageContents: Package = { name: '' }
try {
const packageContentsFile = readFileSync(join(basePath(), 'package.json'), 'utf8')
packageContents = JSON.parse(packageContentsFile)
} catch (_error) {
log('Unable to load package.json', 'error')
}
if (typeof packageContents.name !== 'string') {
log('Missing "name" field in package.json', 'error')
}
let result: Options = defaultOptions(packageContents)
try {
result.reactNativeVersion = JSON.parse(readFileSync(join(basePath(), 'node_modules/react-native/package.json'), 'utf8')).version
} catch (_error) {
log('React native installation not found', 'warning')
}
if (typeof packageContents.numic === 'object') {
// Include project specific overrides
result = merge(result, packageContents.numic, { clone: false })
}
if (typeof packageContents.tsconfig === 'object') {
result.tsconfig = packageContents.tsconfig
}
cache = result
return result
}
export const getAppJsonName = () => {
const appJsonPath = join(basePath(), 'app.json')
if (!existsSync(appJsonPath)) {
return null
}
try {
const contents = JSON.parse(readFileSync(appJsonPath, 'utf-8'))
if (typeof contents.name === 'string' && contents.name.length > 0) {
return contents.name
}
} catch (_error) {
// Ignored
}
return null
}
export const getVersion = (nativeOptions: NativeOptions) => {
if (nativeOptions.version) {
return nativeOptions.version
}
const packageVersion = options().reactNativeVersion
if (packageVersion) {
return packageVersion
}
return ''
}
export const defaultNativeOptions = (nativeOptions: NativeOptions) => {
nativeOptions.debug ??= false
nativeOptions.appName ||= getAppJsonName() ?? 'NumicApp'
nativeOptions.version ||= getVersion(nativeOptions)
return nativeOptions
}
export const hasRejectedHunks = () => {
const rejectedHunksPath = join(basePath(), 'patch/rejected-hunks.patch')
if (existsSync(rejectedHunksPath)) {
log(
`Parts of your patch couldn't be applied and are now in patch/rejected-hunks.patch. Apply the rejected hunks manually to the native folders and then run "npx numic patch" to update the patch. After this remove the rejected-hunks.patch file and try this command again`,
'warning',
)
return true
}
return false
}
// Remove lines like "index c9bc539..eaaabea 100644"
export const replaceIndexLinesFromPatch = (input: string) => input.replaceAll(/index\s[a-zA-Z0-9]{7}\.\.[a-zA-Z0-9]{7}\s\d{6}[\n\r]/g, '')