1 | const fs = require("fs");
|
2 | const fse = require("fs-extra");
|
3 | const path = require("path");
|
4 | const utils = require("./index");
|
5 |
|
6 |
|
7 | const projectUtils = {
|
8 |
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 | initConfig(dir, config) {
|
15 |
|
16 | this.changeFile(path.resolve(dir, 'platforms/android/eeuiApp/build.gradle'), 'applicationId\\s*=\\s*(["\'])(.+?)\\1', `applicationId = "${config.applicationID}"`);
|
17 | let androidManifestPath = path.resolve(dir, 'platforms/android/eeuiApp/app/src/main/AndroidManifest.xml');
|
18 | if (fs.existsSync(androidManifestPath)) {
|
19 | let originalPackageName = fs.readFileSync(androidManifestPath, 'utf8').match(/package\s*=\s*(["'])(.+?)\1/)[2];
|
20 | let newPackageName = config.applicationID;
|
21 | if (originalPackageName && originalPackageName != newPackageName) {
|
22 | let myAppPath = path.resolve(dir, 'platforms/android/eeuiApp/app/src/main/java', originalPackageName.replace(/\./g, '/'),'MyApplication.java');
|
23 | if (!fs.existsSync(myAppPath)) {
|
24 | originalPackageName = 'app.eeui.playground';
|
25 | myAppPath = path.resolve(dir, 'platforms/android/eeuiApp/app/src/main/java', originalPackageName.replace(/\./g, '/'),'MyApplication.java')
|
26 | }
|
27 | if (fs.existsSync(myAppPath)) {
|
28 | let originalPackagePath = path.resolve(dir, 'platforms/android/eeuiApp/app/src/main/java', originalPackageName.replace(/\./g, '/'));
|
29 | let newPackagePath = path.resolve(dir, 'platforms/android/eeuiApp/app/src/main/java', newPackageName.replace(/\./g, '/'));
|
30 | fse.moveSync(originalPackagePath, newPackagePath, { overwrite: true });
|
31 | utils.fileDirDisplay(newPackagePath).file.forEach((tmpFile) => {
|
32 | this.changeFile(tmpFile, originalPackageName, newPackageName);
|
33 | });
|
34 | utils.moveEmptyDirParent(originalPackagePath);
|
35 | this.changeFile(androidManifestPath, originalPackageName, newPackageName);
|
36 | }
|
37 | }
|
38 | }
|
39 |
|
40 | this.changeFile(path.resolve(dir, 'platforms/android/eeuiApp/app/src/main/res/values/strings.xml'), '<string(.*?)name\\s*=\\s*("|\')app_name\\2(.*?)>(.*?)<\\/string>', `<string name="app_name" translatable="false">${config.appName}</string>`);
|
41 |
|
42 | this.changeFile(path.resolve(dir, 'platforms/ios/eeuiApp/eeuiApp.xcodeproj/project.pbxproj'), 'PRODUCT_BUNDLE_IDENTIFIER\\s*=((?!Tests;).)+;', `PRODUCT_BUNDLE_IDENTIFIER = ${config.bundleIdentifier};`);
|
43 |
|
44 | this.changeFile(path.resolve(dir, 'platforms/ios/eeuiApp/eeuiApp/Info.plist'), '<key>CFBundleDisplayName<\/key>\\s*\\r*\\n\\s*<string>(.*?)<\/string>', `<key>CFBundleDisplayName</key>\n\t<string>${config.appName}</string>`);
|
45 |
|
46 | utils.replaceDictString(path.resolve(dir, 'platforms/ios/eeuiApp/eeuiApp/Info.plist'), 'eeuiAppName', 'eeuiApp' + this.replaceUpperCase(config.bundleIdentifier));
|
47 |
|
48 | fse.writeFileSync(path.resolve(dir, '.eeui.release'), JSON.stringify(config, null, "\t"), 'utf8');
|
49 | },
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | changeFile(filePath, oldText, newText) {
|
58 | if (!fse.existsSync(filePath)) {
|
59 | return;
|
60 | }
|
61 | let result = fse.readFileSync(filePath, 'utf8').replace(new RegExp(oldText, "g"), newText);
|
62 | if (result) {
|
63 | fse.writeFileSync(filePath, result, 'utf8');
|
64 | }
|
65 | },
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 | replaceUpperCase(string) {
|
73 | try {
|
74 | return string.replace(/^[a-z]/g, function ($1) {
|
75 | return $1.toLocaleUpperCase()
|
76 | }).replace(/\.+(\w)/g, function ($1) {
|
77 | return $1.toLocaleUpperCase()
|
78 | }).replace(/\./g, '');
|
79 | } catch (e) {
|
80 | return string;
|
81 | }
|
82 | }
|
83 | };
|
84 |
|
85 | module.exports = projectUtils;
|