1/* 2 * Copyright (c) 2024 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 */ 15import path from 'path'; 16 17import { 18 OHOS_PLUGIN, 19 NATIVE_MODULE, 20 SYSTEM_PLUGIN 21} from '../../../lib/pre_define'; 22import { 23 preprocessExtend, 24} from '../../../lib/validate_ui_syntax'; 25 26function normalizeFileContent(content: string): string { 27 // Replace all types of line endings with a single newline character 28 const normalizedLineEndings = content.replace(/\r\n|\r/g, '\n'); 29 30 // Remove leading and trailing whitespace from each line 31 const normalizedWhitespace = normalizedLineEndings.split('\n').map(line => line.trim()).join('\n'); 32 33 // Remove empty lines 34 const normalizedEmptyLines = normalizedWhitespace.split('\n').filter(line => line !== '').join('\n'); 35 36 return normalizedEmptyLines; 37} 38 39function processSystemApi(content: string): string { 40 const REG_SYSTEM = 41 /import\s+(.+)\s+from\s+['"]@(system|ohos)\.(\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)/g; 42 const REG_LIB_SO = 43 /import\s+(.+)\s+from\s+['"]lib(\S+)\.so['"]|import\s+(.+)\s*=\s*require\(\s*['"]lib(\S+)\.so['"]\s*\)/g; 44 const newContent = content.replace(REG_LIB_SO, (_, item1, item2, item3, item4) => { 45 const libSoValue = item1 || item3; 46 const libSoKey = item2 || item4; 47 return `var ${libSoValue} = globalThis.requireNapi("${libSoKey}", true);`; 48 }).replace(REG_SYSTEM, (item, item1, item2, item3, item4, item5, item6, item7) => { 49 let moduleType = item2 || item5; 50 let systemKey = item3 || item6; 51 let systemValue = item1 || item4; 52 if (NATIVE_MODULE.has(`${moduleType}.${systemKey}`)) { 53 item = `var ${systemValue} = globalThis.requireNativeModule('${moduleType}.${systemKey}')`; 54 } else if (moduleType === SYSTEM_PLUGIN) { 55 item = `var ${systemValue} = isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ? ` + 56 `globalThis.systemplugin.${systemKey} : globalThis.requireNapi('${systemKey}')`; 57 } else if (moduleType === OHOS_PLUGIN) { 58 item = `var ${systemValue} = globalThis.requireNapi('${systemKey}') || ` + 59 `(isSystemplugin('${systemKey}', '${OHOS_PLUGIN}') ? ` + 60 `globalThis.ohosplugin.${systemKey} : isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ` + 61 `? globalThis.systemplugin.${systemKey} : undefined)`; 62 } 63 return item; 64 }); 65 return newContent; 66} 67 68export function cleanCopyRight(str: string): string { 69 const copyrightBlockRegex = /(?:\/\*.*Copyright \([c|C]\) [- \d]+ [\w ]+\., Ltd\..*\*\/)/gs; 70 71 return str.replace(copyrightBlockRegex, ''); 72} 73 74export function processExecInStr(str: string): string { 75 const regex = /\${(.*?)}/g; 76 return str.replace(regex, (match, p1) => { 77 return eval(p1); 78 }); 79} 80 81export function parseFileNameFromPath(filePath: string): string { 82 return path.basename(filePath, path.extname(filePath)); 83} 84 85export function parseLog(log: string): string { 86 try { 87 const regexPattern = /(:\d+:\d+)?\n(.*)/gm; 88 const matchResult = regexPattern.exec(log); 89 90 const logInfo: string = matchResult[2].trim(); 91 return logInfo; 92 } catch { 93 return log; 94 } 95} 96 97export function parseCode(code: string): string { 98 return normalizeFileContent(cleanCopyRight(code)); 99} 100 101export function sourceReplace(source: string): string { 102 let content: string = source; 103 content = preprocessExtend(content); 104 content = processSystemApi(content); 105 return content; 106} 107