1#!/usr/bin/env node 2 3/* 4 * Copyright (c) 2023 Huawei Device Co., Ltd. 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18import {program} from 'commander'; 19import * as path from 'path'; 20 21import { ArkObfuscatorForTest } from '../ArkObfuscatorForTest'; 22import { performancePrinter } from '../ArkObfuscator'; 23import { EventList } from '../utils/PrinterUtils'; 24import { UnitTestUtil } from '../utils/UnitTestUtil'; 25import { FileUtils } from '../utils/FileUtils'; 26 27import type { IOptions } from '../configs/IOptions'; 28 29/** 30 * Main Entry of Obfuscation in 31 */ 32const minParametersNum: number = 3; 33(function (): void { 34 if (process.argv.length < minParametersNum) { 35 console.error('Too few input parameters.'); 36 console.error('Usage: SecHarmony [input files] [options]'); 37 return; 38 } 39 40 initOptionsSetting(); 41 42 let configPath: string = program.opts()?.configPath; 43 configPath = path.resolve(configPath); 44 let fileList: Array<string> = []; 45 program.args.forEach((value) => { 46 const resolved: string = path.resolve(value); 47 fileList.push(resolved); 48 }); 49 50 let obfuscator: ArkObfuscatorForTest = new ArkObfuscatorForTest(fileList, configPath); 51 performancePrinter?.iniPrinter?.startEvent(EventList.OBFUSCATION_INITIALIZATION); 52 const config: IOptions = FileUtils.readFileAsJson(configPath); 53 const initSuccess: boolean = obfuscator.init(config); 54 let inplace: boolean = program.opts()?.inplace; 55 if (inplace) { 56 obfuscator.setWriteOriginalFile(true); 57 } 58 59 UnitTestUtil.initKeepPathConfig(obfuscator.customProfiles, obfuscator.configPath); 60 performancePrinter?.iniPrinter?.endEvent(EventList.OBFUSCATION_INITIALIZATION); 61 if (!initSuccess) { 62 console.error('init from config file error.'); 63 return; 64 } 65 obfuscator.obfuscateFiles(); 66})(); 67 68function initOptionsSetting(): void { 69 program.name('SecHarmony') 70 .version('1.0.0') 71 .description('A tool to obfuscate open harmony application written by Javascript or Typescript.') 72 .usage('Usage: SecHarmony [input files] [options]') 73 .option('-v, --version', 'show version information.') 74 .option('--inplace', 'write the ofuscated content to the original file') 75 .option('-cp, --config-path <dir>', 'obfuscation configuration for open harmony application.') 76 .parse(); 77} 78