UNPKG

4.28 kBJavaScriptView Raw
1const fs = require("fs");
2const fse = require("fs-extra");
3const path = require("path");
4const utils = require("./index");
5
6
7const projectUtils = {
8
9 /**
10 * 初始化项目设置
11 * @param dir
12 * @param config
13 */
14 initConfig(dir, config) {
15 //Android id
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 //Android 应用名称
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 //iOS id
42 this.changeFile(path.resolve(dir, 'platforms/ios/eeuiApp/eeuiApp.xcodeproj/project.pbxproj'), 'PRODUCT_BUNDLE_IDENTIFIER\\s*=((?!Tests;).)+;', `PRODUCT_BUNDLE_IDENTIFIER = ${config.bundleIdentifier};`);
43 //iOS 应用名称
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 //iOS URLTypes
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 * @param {string} filePath 文件路径.
54 * @param {string} oldText
55 * @param {string} newText
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 * 将点及后面的第一个字母换成大写字母,如:aaa.bbb.ccc换成AaaBbbCcc
69 * @param string
70 * @returns {*}
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
85module.exports = projectUtils;