13af6ab5fSopenharmony_ci#!/usr/bin/env node
23af6ab5fSopenharmony_ci
33af6ab5fSopenharmony_ci/*
43af6ab5fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
53af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
63af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
73af6ab5fSopenharmony_ci * You may obtain a copy of the License at
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
103af6ab5fSopenharmony_ci *
113af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
123af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
133af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
143af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
153af6ab5fSopenharmony_ci * limitations under the License.
163af6ab5fSopenharmony_ci */
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ciimport {program} from 'commander';
193af6ab5fSopenharmony_ciimport * as path from 'path';
203af6ab5fSopenharmony_ci
213af6ab5fSopenharmony_ciimport { ArkObfuscatorForTest } from '../ArkObfuscatorForTest';
223af6ab5fSopenharmony_ciimport { performancePrinter } from '../ArkObfuscator';
233af6ab5fSopenharmony_ciimport { EventList } from '../utils/PrinterUtils';
243af6ab5fSopenharmony_ciimport { UnitTestUtil } from '../utils/UnitTestUtil';
253af6ab5fSopenharmony_ciimport { FileUtils } from '../utils/FileUtils';
263af6ab5fSopenharmony_ci
273af6ab5fSopenharmony_ciimport type { IOptions } from '../configs/IOptions';
283af6ab5fSopenharmony_ci
293af6ab5fSopenharmony_ci/**
303af6ab5fSopenharmony_ci * Main Entry of Obfuscation in
313af6ab5fSopenharmony_ci */
323af6ab5fSopenharmony_ciconst minParametersNum: number = 3;
333af6ab5fSopenharmony_ci(function (): void {
343af6ab5fSopenharmony_ci  if (process.argv.length < minParametersNum) {
353af6ab5fSopenharmony_ci    console.error('Too few input parameters.');
363af6ab5fSopenharmony_ci    console.error('Usage: SecHarmony [input files] [options]');
373af6ab5fSopenharmony_ci    return;
383af6ab5fSopenharmony_ci  }
393af6ab5fSopenharmony_ci
403af6ab5fSopenharmony_ci  initOptionsSetting();
413af6ab5fSopenharmony_ci  let configPath: string = program.opts()?.configPath;
423af6ab5fSopenharmony_ci  configPath = path.resolve(configPath);
433af6ab5fSopenharmony_ci  let fileList: Array<string> = [];
443af6ab5fSopenharmony_ci  program.args.forEach((value) => {
453af6ab5fSopenharmony_ci    const resolved: string = path.resolve(value);
463af6ab5fSopenharmony_ci    fileList.push(resolved);
473af6ab5fSopenharmony_ci  });
483af6ab5fSopenharmony_ci
493af6ab5fSopenharmony_ci  let obfuscator: ArkObfuscatorForTest = new ArkObfuscatorForTest(fileList, configPath);
503af6ab5fSopenharmony_ci  performancePrinter?.iniPrinter?.startEvent(EventList.OBFUSCATION_INITIALIZATION);
513af6ab5fSopenharmony_ci  const config: IOptions = FileUtils.readFileAsJson(configPath);
523af6ab5fSopenharmony_ci  const initSuccess: boolean = obfuscator.init(config);
533af6ab5fSopenharmony_ci  let inplace: boolean = program.opts()?.inplace;
543af6ab5fSopenharmony_ci  if (inplace) {
553af6ab5fSopenharmony_ci    obfuscator.setWriteOriginalFile(true);
563af6ab5fSopenharmony_ci  }
573af6ab5fSopenharmony_ci
583af6ab5fSopenharmony_ci  let testType: string | undefined = program.opts()?.testType;
593af6ab5fSopenharmony_ci  obfuscator.setTestType(testType);
603af6ab5fSopenharmony_ci
613af6ab5fSopenharmony_ci  UnitTestUtil.initKeepPathConfig(obfuscator.customProfiles, obfuscator.configPath);
623af6ab5fSopenharmony_ci  performancePrinter?.iniPrinter?.endEvent(EventList.OBFUSCATION_INITIALIZATION);
633af6ab5fSopenharmony_ci  if (!initSuccess) {
643af6ab5fSopenharmony_ci    console.error('init from config file error.');
653af6ab5fSopenharmony_ci    return;
663af6ab5fSopenharmony_ci  }
673af6ab5fSopenharmony_ci  obfuscator.obfuscateFiles();
683af6ab5fSopenharmony_ci})();
693af6ab5fSopenharmony_ci
703af6ab5fSopenharmony_cifunction initOptionsSetting(): void {
713af6ab5fSopenharmony_ci  program.name('SecHarmony')
723af6ab5fSopenharmony_ci    .version('1.0.0')
733af6ab5fSopenharmony_ci    .description('A tool to obfuscate open harmony application written by Javascript or Typescript.')
743af6ab5fSopenharmony_ci    .usage('Usage: SecHarmony [input files] [options]')
753af6ab5fSopenharmony_ci    .option('-v, --version', 'show version information.')
763af6ab5fSopenharmony_ci    .option('--inplace', 'write the ofuscated content to the original file')
773af6ab5fSopenharmony_ci    .option('--test-type <type>', 'Indicates the type of test case, must be either "grammar" or "combinations"')
783af6ab5fSopenharmony_ci    .option('-cp, --config-path <dir>', 'obfuscation configuration for open harmony application.')
793af6ab5fSopenharmony_ci    .parse();
803af6ab5fSopenharmony_ci}
81