1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import path from 'path'; 17import fs from 'fs'; 18 19import { 20 TS2ABC, 21 ESMODULE, 22 AOT_FULL, 23 AOT_PARTIAL, 24 AOT_TYPE, 25 AOT_PROFILE_SUFFIX, 26 NODE_MODULES, 27 OH_MODULES, 28 FAIL 29} from './ark_define'; 30import { isDebug } from '../utils'; 31import { 32 isLinux, 33 isMac, 34 isWindows 35} from '../../../utils'; 36import { getArkBuildDir } from '../../../ark_utils'; 37import { checkAotConfig } from '../../../gen_aot'; 38import { projectConfig as mainProjectConfig } from '../../../../main'; 39 40type ArkConfig = { 41 arkRootPath: string; 42 ts2abcPath: string; 43 js2abcPath: string; 44 mergeAbcPath: string; 45 es2abcPath: string; 46 aotCompilerPath: string; 47 nodePath: string; 48 isDebug: boolean; 49}; 50 51let arkConfig: ArkConfig = {}; 52 53export function initArkConfig(projectConfig: any) { 54 let arkRootPath: string = path.join(__dirname, '..', '..', '..', '..', 'bin', 'ark'); 55 if (projectConfig.arkFrontendDir) { 56 arkRootPath = projectConfig.arkFrontendDir; 57 } 58 arkConfig.nodePath = 'node'; 59 if (projectConfig.nodeJs) { 60 arkConfig.nodePath = projectConfig.nodePath; 61 } 62 processPlatformInfo(arkRootPath); 63 processCompatibleVersion(projectConfig, arkRootPath); 64 arkConfig.isDebug = isDebug(projectConfig); 65 arkConfig.arkRootPath = arkRootPath; 66 67 return arkConfig; 68} 69 70export function initArkProjectConfig(share: any) { 71 let projectConfig: any = share.projectConfig; 72 let arkProjectConfig: any = {}; 73 if (projectConfig.aceBuildJson && fs.existsSync(projectConfig.aceBuildJson)) { 74 const buildJsonInfo = JSON.parse(fs.readFileSync(projectConfig.aceBuildJson).toString()); 75 arkProjectConfig.projectRootPath = buildJsonInfo.projectRootPath; 76 arkProjectConfig.modulePathMap = buildJsonInfo.modulePathMap; 77 arkProjectConfig.isOhosTest = buildJsonInfo.isOhosTest; 78 if (buildJsonInfo.patchConfig) { 79 arkProjectConfig.oldMapFilePath = buildJsonInfo.patchConfig.oldMapFilePath; 80 } 81 if (checkAotConfig(projectConfig.compileMode, buildJsonInfo, 82 (error: string) => { share.throwArkTsCompilerError(error) })) { 83 arkProjectConfig.processTs = true; 84 arkProjectConfig.pandaMode = TS2ABC; 85 arkProjectConfig.anBuildOutPut = buildJsonInfo.anBuildOutPut; 86 arkProjectConfig.anBuildMode = buildJsonInfo.anBuildMode; 87 arkProjectConfig.apPath = buildJsonInfo.apPath; 88 } else { 89 arkProjectConfig.processTs = false; 90 arkProjectConfig.pandaMode = buildJsonInfo.pandaMode; 91 } 92 93 if (projectConfig.compileMode === ESMODULE) { 94 arkProjectConfig.nodeModulesPath = buildJsonInfo.nodeModulesPath; 95 arkProjectConfig.harNameOhmMap = buildJsonInfo.harNameOhmMap; 96 projectConfig.packageDir = buildJsonInfo.packageManagerType === 'ohpm' ? OH_MODULES : NODE_MODULES; 97 } 98 } 99 if (projectConfig.aceManifestPath && fs.existsSync(projectConfig.aceManifestPath)) { 100 const manifestJsonInfo = JSON.parse(fs.readFileSync(projectConfig.aceManifestPath).toString()); 101 if (manifestJsonInfo.minPlatformVersion) { 102 arkProjectConfig.minPlatformVersion = manifestJsonInfo.minPlatformVersion; 103 } 104 } 105 if (projectConfig.aceModuleJsonPath && fs.existsSync(projectConfig.aceModuleJsonPath)) { 106 const moduleJsonInfo = JSON.parse(fs.readFileSync(projectConfig.aceModuleJsonPath).toString()); 107 if (moduleJsonInfo.app.minAPIVersion) { 108 arkProjectConfig.minPlatformVersion = moduleJsonInfo.app.minAPIVersion; 109 } 110 if (moduleJsonInfo.module) { 111 arkProjectConfig.moduleName = moduleJsonInfo.module.name; 112 } 113 if (moduleJsonInfo.app) { 114 arkProjectConfig.bundleName = moduleJsonInfo.app.bundleName; 115 } 116 } 117 118 // Hotreload attributes are initialized by arkui in main.js, just copy them. 119 arkProjectConfig.hotReload = mainProjectConfig.hotReload; 120 arkProjectConfig.patchAbcPath = mainProjectConfig.patchAbcPath; 121 arkProjectConfig.changedFileList = mainProjectConfig.changedFileList; 122 123 return arkProjectConfig; 124} 125 126function processPlatformInfo(arkRootPath: string): void { 127 const arkPlatformPath: string = getArkBuildDir(arkRootPath); 128 if (isWindows()) { 129 arkConfig.es2abcPath = path.join(arkPlatformPath, 'bin', 'es2abc.exe'); 130 arkConfig.ts2abcPath = path.join(arkPlatformPath, 'src', 'index.js'); 131 arkConfig.mergeAbcPath = path.join(arkPlatformPath, 'bin', 'merge_abc.exe'); 132 arkConfig.js2abcPath = path.join(arkPlatformPath, 'bin', 'js2abc.exe'); 133 arkConfig.aotCompilerPath = path.join(arkPlatformPath, 'bin', 'ark_aot_compiler.exe'); 134 return; 135 } 136 if (isLinux() || isMac()) { 137 arkConfig.es2abcPath = path.join(arkPlatformPath, 'bin', 'es2abc'); 138 arkConfig.ts2abcPath = path.join(arkPlatformPath, 'src', 'index.js'); 139 arkConfig.mergeAbcPath = path.join(arkPlatformPath, 'bin', 'merge_abc'); 140 arkConfig.js2abcPath = path.join(arkPlatformPath, 'bin', 'js2abc '); 141 arkConfig.aotCompilerPath = path.join(arkPlatformPath, 'bin', 'ark_aot_compiler'); 142 return; 143 } 144} 145 146function processCompatibleVersion(projectConfig: any, arkRootPath: string) { 147 const platformPath: string = getArkBuildDir(arkRootPath); 148 if (projectConfig.minPlatformVersion && projectConfig.minPlatformVersion.toString() === '8') { 149 // use ts2abc to compile apps with 'CompatibleSdkVersion' set to 8 150 arkConfig.ts2abcPath = path.join(platformPath, 'legacy_api8', 'src', 'index.js'); 151 projectConfig.pandaMode = TS2ABC; 152 } 153}