-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathutils.ts
201 lines (176 loc) · 5.15 KB
/
utils.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
import semver from 'semver/preload'
import {
RN_DIFF_REPOSITORIES,
DEFAULT_APP_NAME,
DEFAULT_APP_PACKAGE,
PACKAGE_NAMES,
RN_CHANGELOG_URLS,
FIRST_PRE_060_VERSION,
FIRST_PRE_070_VERSION,
} from './constants'
import versions from './releases'
const getRNDiffRepository = ({ packageName }: { packageName: string }) =>
RN_DIFF_REPOSITORIES[packageName]
export const getReleasesFileURL = ({ packageName }: { packageName: string }) =>
`https://raw.githubusercontent.com/${getRNDiffRepository({
packageName,
})}/master/${packageName === PACKAGE_NAMES.RNM ? 'RELEASES_MAC' : 'RELEASES'}`
export const getDiffURL = ({
packageName,
language,
fromVersion,
toVersion,
}: {
packageName: string
language: string
fromVersion: string
toVersion: string
}) => {
const languageDir =
packageName === PACKAGE_NAMES.RNM
? 'mac/'
: packageName === PACKAGE_NAMES.RNW
? `${language}/`
: ''
return `https://raw.githubusercontent.com/${getRNDiffRepository({
packageName,
})}/diffs/diffs/${languageDir}${fromVersion}..${toVersion}.diff`
}
const getBranch = ({
packageName,
language,
version,
}: {
packageName: string
language?: string
version: string
}) =>
packageName === PACKAGE_NAMES.RNM
? `mac/${version}`
: packageName === PACKAGE_NAMES.RNW
? `${language}/${version}`
: version
interface GetBinaryFileURLProps {
packageName: string
language?: string
version: string
path: string
}
// `path` must contain `RnDiffApp` prefix
export const getBinaryFileURL = ({
packageName,
language,
version,
path,
}: GetBinaryFileURLProps) => {
const branch = getBranch({ packageName, language, version })
return `https://raw.githubusercontent.com/${getRNDiffRepository({
packageName,
})}/release/${branch}/${path}`
}
export const removeAppPathPrefix = (path: string, appName = DEFAULT_APP_NAME) =>
path.replace(new RegExp(`${appName}/`), '')
/**
* Replaces DEFAULT_APP_PACKAGE and DEFAULT_APP_NAME in str with custom
* values if provided.
* str could be a path, or content from a text file.
*/
export const replaceAppDetails = (
str: string,
appName?: string,
appPackage?: string
) => {
const appNameOrFallback = appName || DEFAULT_APP_NAME
const appPackageOrFallback =
appPackage || `com.${appNameOrFallback.toLowerCase()}`
return str
.replaceAll(DEFAULT_APP_PACKAGE, appPackageOrFallback)
.replaceAll(
DEFAULT_APP_PACKAGE.replaceAll('.', '/'),
appPackageOrFallback.replaceAll('.', '/')
)
.replaceAll(DEFAULT_APP_NAME, appNameOrFallback)
.replaceAll(DEFAULT_APP_NAME.toLowerCase(), appNameOrFallback.toLowerCase())
}
export const getVersionsContentInDiff = ({
packageName,
fromVersion,
toVersion,
}: {
packageName: string
fromVersion: string
toVersion: string
}) => {
if (!versions[packageName]) {
return []
}
const cleanedToVersion = semver.valid(semver.coerce(toVersion))
return versions[packageName].filter(({ version }) => {
const cleanedVersion = semver.coerce(version)
// `cleanedVersion` can't be newer than `cleanedToVersion` nor older (or equal) than `fromVersion`
return (
semver.compare(cleanedToVersion, cleanedVersion) !== -1 &&
![0, -1].includes(semver.compare(cleanedVersion, fromVersion))
)
})
}
const getOldVersionsChangelogURL = ({
version,
packageName,
}: {
version: string
packageName: string
}) => {
const urlOldVersion = RN_CHANGELOG_URLS[packageName].replace('/CHANGELOG.md', '')
let formattedVersion = version.replaceAll('.', '')
if (version.split('.').length === 2) formattedVersion += '0'
if (version < FIRST_PRE_060_VERSION) {
return `${urlOldVersion}/CHANGELOG-pre-060.md#v${formattedVersion}`
}
if (version < FIRST_PRE_070_VERSION) {
return `${urlOldVersion}/CHANGELOG-pre-070.md#v${formattedVersion}`
}
}
export const getChangelogURL = ({
version,
packageName,
}: {
version: string
packageName: string
}) => {
if (packageName === PACKAGE_NAMES.RNW || packageName === PACKAGE_NAMES.RNM) {
return `${RN_CHANGELOG_URLS[packageName]}v${version}`
}
if (version < FIRST_PRE_070_VERSION) {
return getOldVersionsChangelogURL({ packageName, version })
}
return `${RN_CHANGELOG_URLS[packageName]}#v${version.replaceAll('.', '')}`
}
// If the browser is headless (running puppeteer) then it doesn't have any duration
export const getTransitionDuration = (duration: number) =>
navigator.webdriver ? 0 : duration
// settings constants
export const SHOW_LATEST_RCS = 'Show latest release candidates'
/**
* Returns the file paths to display for each side of the diff. Takes into account
* custom app name and package, and truncates the leading app name to provide
* paths relative to the project directory.
*/
export const getFilePathsToShow = ({
oldPath,
newPath,
appName,
appPackage,
}: {
oldPath: string
newPath: string
appName?: string
appPackage?: string
}) => {
const oldPathSanitized = replaceAppDetails(oldPath, appName, appPackage)
const newPathSanitized = replaceAppDetails(newPath, appName, appPackage)
return {
oldPath: removeAppPathPrefix(oldPathSanitized, appName),
newPath: removeAppPathPrefix(newPathSanitized, appName),
}
}