-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_utils.ts
More file actions
426 lines (388 loc) · 19.6 KB
/
platform_utils.ts
File metadata and controls
426 lines (388 loc) · 19.6 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import * as fileSystem from 'fs';
export class PlatformUtils {
static achooProjectDir: string = "/Users/adambridges/AppProjects/achoo";
static filesToTranslateDir: string = __dirname+"/filesToTranslate";
static iOSFastlaneDir: string = this.achooProjectDir+"/ios/fastlane";
static iOSFastlaneMetadataDir: string = this.iOSFastlaneDir+"/metadata";
static androidFastlaneDir: string = this.achooProjectDir+"/android/fastlane/metadata/android";
static storeFilesDir: string = this.filesToTranslateDir+"/storeFiles";
static appFilesDir: string = this.filesToTranslateDir+'/appFiles';
static changeLogsDir: string = this.storeFilesDir+"/changelogs";
static privacyPolicyUrl: string = "https://achoo.adambridges.ca/privacy";
static supportUrl: string = "https://achoo.adambridges.ca/support";
static platforms: string[] = [
"android",
"ios",
];
/** Note that iOS separates the title into two files: name.txt and subtitle.txt. */
static androidTitleFileName = "title.txt";
static iosTitleFileName = "name.txt";
static iosSubtitleFileName = "subtitle.txt";
static androidDescriptionFileName = "full_description.txt";
static iosDescriptionFileName = "description.txt";
static androidShortDescriptionFileName = "short_description.txt";
static iosPromotionalTextFileName = "promotional_text.txt";
static iosKeywordsFileName = "keywords.txt";
static androidChangeLogsFileName = this.getAppBuildNumber()+".txt";
/** No special formatting codes are allowed. */
static iosReleaseNotesFileName = "release_notes.txt";
static androidFiles = [
this.androidChangeLogsFileName,
this.androidDescriptionFileName,
this.androidShortDescriptionFileName,
this.androidTitleFileName
];
static iOSFiles = [
this.iosTitleFileName,
this.iosSubtitleFileName,
this.iosDescriptionFileName, // iOS version of full_description
this.iosPromotionalTextFileName, // iOS version of short_description
this.iosReleaseNotesFileName, // iOS version of change logs
this.iosKeywordsFileName, // iOS only
];
static defaultLanguage = "en-CA";
/** fastlane requires that a directory name for the Play Store include the exact hyphenated abbreviations
* as found on the Play Store's list of languages available for an app. */
static languages: string[] = [
this.defaultLanguage,
"de-DE",
"es-ES",
"fr-FR",
"hi-IN",
"ja-JP",
"pt-BR",
"zh-CN",
"ko-KR",
"it-IT",
"sv-SE",
"uk",
"ru-RU",
"ar",
];
// List is incomplete right now:
static appStoreSupportedLanguages: string[] = [
"en-CA",
"de-DE",
"es-ES",
"fr-FR",
"hi",
"ja",
"pt-BR",
"zh-Hans",
"ko",
"it",
"sv",
"uk",
"ru",
"ar-SA",
];
/** As of 2022-12-06: App Store Description and Release Notes do not appear to mention character limits from
* https://developer.apple.com/app-store/product-page/.
* */
static playAndAppStoreCharacterLimits: Map<string, number|undefined> = new Map([
[this.androidTitleFileName, 30],
[this.androidDescriptionFileName, 4000],
[this.androidShortDescriptionFileName, 80],
[this.iosTitleFileName, 30],
[this.iosSubtitleFileName, 30],
[this.iosDescriptionFileName, 4000],
[this.iosPromotionalTextFileName, 170],
[this.iosKeywordsFileName, 100],
[this.iosReleaseNotesFileName, undefined],
[this.androidChangeLogsFileName, undefined],
]);
/** Keep strings lowercase for easier comparisons: */
static stringsNotToTranslate: string[] = [
"achoo",
];
static pathsExceedingCharLimits: string[] = [];
static iphone8Plus = "iphone 8 plus";
static iphone11ProMax = "iphone 11 pro max";
static ipadPro = "ipad pro";
static iosScreenshotDevices: string[] = [
this.ipadPro,
this.iphone11ProMax,
this.iphone8Plus,
];
static getAppBuildNumber(): string {
let buildNumber = "0";
const file: Buffer = fileSystem.readFileSync(this.achooProjectDir+"/pubspec.yaml");
const versionRegex = /^version: .+\+\d+/m;
let build = file.toString().match(versionRegex)![0] ?? "null";
console.log("build = "+build);
buildNumber = build.match(/\+\d+/m)![0].substring(1) ?? "null";
console.log("buildNumber = "+buildNumber);
return buildNumber;
}
/** Returns the app/play store character limit of the file within the path. */
static characterCountCheck(filePath: string): number {
const fileText = fileSystem.readFileSync(filePath).toString();
const fileName = filePath.slice(filePath.lastIndexOf("/")+1);
const characters = fileText.length;
const charLimit = this.playAndAppStoreCharacterLimits.get(fileName) ?? 4000;
if (characters > charLimit) {
console.error("There are too many characters in %s. Characters: %d | Limit: %d | \nPath: %s",
fileName, characters, charLimit, filePath);
if (filePath.includes("/"+this.defaultLanguage+"/")) throw "Shutting down process...";
this.pathsExceedingCharLimits.push(filePath);
}
return characters;
}
static countAllLocalizedStoreFilesCharacters() {
for (var file of this.androidFiles.concat(this.iOSFiles)) {
const filePath = PlatformUtils.getFilePath(file);
if (filePath == undefined) {
console.warn("no path found for file: ", file);
continue;
}
for (let language of this.languages) {
const appStoreLanguageEquivalent: string | undefined = this
.getAppStoreLanguageEquivalent(filePath, language);
if (appStoreLanguageEquivalent == undefined) continue;
language = appStoreLanguageEquivalent;
const languagefilePath = filePath
?.replace("/"+this.defaultLanguage+"/", "/"+language+"/");
const chars = this.characterCountCheck(languagefilePath!);
console.log("file: %s | characters: %d | language: %s",
file, chars, language);
}
}
}
/** Returns language parameter if filePath includes /android/.
* Returns undefined if a relative language can't be found. */
static getAppStoreLanguageEquivalent(filePath: string, language: string): string|undefined {
if (filePath.includes("/android/")) return language;
const appStoreLanguageEquivalent = this.appStoreSupportedLanguages.find(
(appStoreLanguage) => {
const languageFound = appStoreLanguage == language
|| appStoreLanguage.slice(0, 2) == language.slice(0, 2)
&& !this.appStoreSupportedLanguages.includes(language);
return languageFound;
});
if (filePath.includes("/ios/") && appStoreLanguageEquivalent == undefined) {
console.warn(
"App Store does not support this language or it's missing from languages list: ",
language
);
}
return appStoreLanguageEquivalent;
}
static getPlatformFileNameEquivalent(fileName: string): string {
let platformFileName: string = fileName;
switch (fileName) {
case this.iosDescriptionFileName:
platformFileName = this.androidDescriptionFileName;
break;
case this.androidDescriptionFileName:
platformFileName = this.iosDescriptionFileName;
break;
case this.iosReleaseNotesFileName:
platformFileName = this.androidChangeLogsFileName;
break;
case this.androidChangeLogsFileName:
platformFileName = this.iosReleaseNotesFileName;
break;
case this.androidShortDescriptionFileName:
platformFileName = this.iosPromotionalTextFileName;
break;
case this.iosPromotionalTextFileName:
platformFileName = this.androidShortDescriptionFileName;
break;
case this.iosTitleFileName:
platformFileName = this.androidTitleFileName;
break;
case this.iosSubtitleFileName:
platformFileName = this.androidTitleFileName;
break;
case this.androidTitleFileName:
platformFileName = this.iosSubtitleFileName;
break;
}
return platformFileName;
}
/** Searches within app's fastlane directories for the `fileName` and returns its path. */
static getFilePath(fileName: string, dynamicPath?: string|undefined): string|undefined {
let path: string|undefined;
console.log("file: %s | dPath: %s", fileName, dynamicPath);
for (var platform of PlatformUtils.platforms) {
let startingPath = platform == "android"
? PlatformUtils.androidFastlaneDir+"/"+PlatformUtils.defaultLanguage
: PlatformUtils.iOSFastlaneMetadataDir+"/"+PlatformUtils.defaultLanguage;
if (dynamicPath != undefined) startingPath = dynamicPath;
let files = fileSystem.readdirSync(startingPath);
let fileExists: boolean = files.includes(fileName);
// console.log("checking path... %s | files found: %s", startingPath, files);
if (fileExists) {
path = startingPath+"/"+fileName;
break;
} else {
files.forEach((file, index) => {
if (path != undefined && path.includes(fileName)) return;
const fStat: fileSystem.Stats = fileSystem.statSync(startingPath+"/"+file);
if (fStat.isDirectory() && file != "images") {
path = PlatformUtils.getFilePath(fileName, startingPath+"/"+file);
}
});
}
if (dynamicPath != undefined) break;
}
return path;
}
/** Parses for a screenshots directory and copies screenshots to respective
* platform fastlane screenshots directory */
static copyScreenshotsToFastlaneDirectories() {
// ~/fastlane/metadata/android/[language]/images/
// - phoneScreenshots (iphone 8)
// - sevenInchScreenshots (iphone 13)
// - tenInchScreenshots (ipad)
// ~/fastlane/screenshots/[language]/
// - 0_APP_IPAD_PRO_3GEN_129_0.png (ipad)
// - 0_APP_IPAD_PRO_129_0 (ipad)
// - 0_APP_IPHONE_55_0 (iphone 8)
// - 0_APP_IPHONE_65_0 (iphone 13)
const screenshotsDirPath: string = this.achooProjectDir+"/screenshots/ios";
// IMPORTANT: ipad pro screenshots need to include the string ipadPro129
// in their filename for fastlane to recognize them properly.
const screenshotDirs: string[] = fileSystem.readdirSync(screenshotsDirPath);
for (const sDirectory of screenshotDirs) {
console.log("directory: ", sDirectory);
const dirStats = fileSystem.statSync(screenshotsDirPath+"/"+sDirectory);
if (!dirStats.isDirectory()) continue;
const isIpadProScreenshotDir: boolean = this.isDeviceScreenshotDir(this.ipadPro, sDirectory);
const isIphone11ProMaxScreenshotDir: boolean = this.isDeviceScreenshotDir(
this.iphone11ProMax, sDirectory);
const isIphone8PlusScreenshotDir: boolean = this.isDeviceScreenshotDir(
this.iphone8Plus, sDirectory);
const sDirectoryPath = screenshotsDirPath+"/"+sDirectory;
const sDeviceLanguageDirectories = fileSystem.readdirSync(sDirectoryPath);
for (const languageDirectory of sDeviceLanguageDirectories) {
const languageDirectoryPath = sDirectoryPath+"/"+languageDirectory;
const langDirStats = fileSystem.statSync(languageDirectoryPath);
if (!langDirStats.isDirectory()) continue;
const screenshotFiles = fileSystem.readdirSync(languageDirectoryPath);
for (const platform of this.platforms) {
let fastlaneLanguageDir = this.languages.find((language) => {
const isLocalized = languageDirectory == language
|| languageDirectory.slice(0, 2) == language.slice(0, 2)
&& !sDeviceLanguageDirectories.includes(language);
return isLocalized;
});
if (fastlaneLanguageDir == undefined) {
console.warn("fastlane language directory not found for language: %s\n Path: %s",
languageDirectory, languageDirectoryPath);
return;
}
const appStoreLanguageDir = this.getAppStoreLanguageEquivalent("/"+platform+"/",
fastlaneLanguageDir);
if (appStoreLanguageDir == undefined) continue;
fastlaneLanguageDir = appStoreLanguageDir;
if (platform == "android") {
const phoneScreenshotsDir = "phoneScreenshots";
const sevenInchScreenshotsDir = "sevenInchScreenshots";
const tenInchScreenshotsDir = "tenInchScreenshots";
const imagesDirectories = [
phoneScreenshotsDir,
sevenInchScreenshotsDir,
tenInchScreenshotsDir,
];
imagesDirectories.forEach((imageDirectory) => {
const androidFastlaneScreenshotsDir = this.androidFastlaneDir+
"/"+fastlaneLanguageDir+"/images/"+imageDirectory;
const isDirToCopyTo = imageDirectory == phoneScreenshotsDir
&& isIphone8PlusScreenshotDir
|| imageDirectory == sevenInchScreenshotsDir && isIphone11ProMaxScreenshotDir
|| imageDirectory == tenInchScreenshotsDir && isIpadProScreenshotDir;
if (isDirToCopyTo) {
const dirExists = fileSystem.existsSync(androidFastlaneScreenshotsDir);
if (dirExists) fileSystem.rmSync(androidFastlaneScreenshotsDir,
{recursive: true, force: true});
fileSystem.cpSync(languageDirectoryPath, androidFastlaneScreenshotsDir,
{recursive: true});
}
});
} else if (platform == "ios") {
const iosFastlaneScreenshotsDir = this
.iOSFastlaneDir+"/screenshots/"+fastlaneLanguageDir;
const dirExists = fileSystem.existsSync(iosFastlaneScreenshotsDir);
if (dirExists) {
const currentIOSScreenshotFiles = fileSystem.readdirSync(iosFastlaneScreenshotsDir);
currentIOSScreenshotFiles.forEach((fastlaneSFile) => {
const fastlaneSFilePath = iosFastlaneScreenshotsDir+"/"+fastlaneSFile;
const sFileStats = fileSystem.statSync(fastlaneSFilePath);
const currentTimeMs = Date.now();
// This should prevent the files being copied from being removed as long as
// the function completes in less than 10 seconds:
const isOldFileToReplace = sFileStats.ctimeMs + 10000 < currentTimeMs;
if (isOldFileToReplace) fileSystem.rmSync(fastlaneSFilePath,
{recursive: true, force: true});
});
}
fileSystem.cpSync(languageDirectoryPath, iosFastlaneScreenshotsDir, {recursive: true});
screenshotFiles.forEach((sFile, index) => {
const sFileName = sFile.slice(0, sFile.lastIndexOf(".png"));
const destinationFileName = isIpadProScreenshotDir
? sFileName+"_ipadPro129.png"
: isIphone11ProMaxScreenshotDir
? sFileName+"_iphone65.png"
: sFileName+"_iphone55.png";
const sourceFilePath = iosFastlaneScreenshotsDir+"/"+sFile;
const destinationFilePath = iosFastlaneScreenshotsDir+"/"+destinationFileName;
if (isIpadProScreenshotDir) {
fileSystem.copyFileSync(sourceFilePath,
iosFastlaneScreenshotsDir+"/"+sFileName+"_2ndGenIpad.png");
}
fileSystem.renameSync(sourceFilePath, destinationFilePath);
});
}
}
}
}
}
static isDeviceScreenshotDir(device: string, directory: string): boolean {
let isScreenshotDir = false;
// RegExp matches underscores, hyphens, or periods.
// Only periods that aren't adjacent to numbers are removed.
// Example: "iphone_13_pro", "iphone-13-pro", or "ipad.pro.(12.9-inch)"
// The last example above would not remove the period between 12 and 9.
const matchRegex = /_+|-+|(?=\.\D).|.(?<=\D\.)/gmi;
const formattedDirectory = directory.toLowerCase().replace(matchRegex, " ");
isScreenshotDir = formattedDirectory.includes(device);
return isScreenshotDir;
}
/** Currently only applicable for App Store */
static updatePrivacyPolicyUrls() {
const iosPrivacyPolicyFileName: string = "privacy_url.txt";
const filePath = this.getFilePath(iosPrivacyPolicyFileName);
if (filePath == undefined) {
console.warn("path \'%s\' could not be found. Unable to update privacy policy URLs.", filePath);
return;
}
for (var language of this.appStoreSupportedLanguages) {
const languageFilePath = filePath.replace(this.defaultLanguage, language);
try {
fileSystem.writeFileSync(languageFilePath, this.privacyPolicyUrl);
} catch(e) {
console.error("ERROR: file probably doesn't exist at %s. Skipping...\n%s", language, e);
continue;
}
}
}
/** Currently only applicable for App Store */
static updateSupportUrls() {
const iosPrivacyPolicyFileName: string = "support_url.txt";
const filePath = this.getFilePath(iosPrivacyPolicyFileName);
if (filePath == undefined) {
console.warn("path \'%s\' could not be found. Unable to update privacy policy URLs.", filePath);
return;
}
for (var language of this.appStoreSupportedLanguages) {
const languageFilePath = filePath.replace(this.defaultLanguage, language);
try {
fileSystem.writeFileSync(languageFilePath, this.supportUrl);
} catch(e) {
console.error("ERROR: file probably doesn't exist at %s. Skipping...\n%s", language, e);
continue;
}
}
}
}