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  let configPath: string = program.opts()?.configPath;
42  configPath = path.resolve(configPath);
43  let fileList: Array<string> = [];
44  program.args.forEach((value) => {
45    const resolved: string = path.resolve(value);
46    fileList.push(resolved);
47  });
48
49  let obfuscator: ArkObfuscatorForTest = new ArkObfuscatorForTest(fileList, configPath);
50  performancePrinter?.iniPrinter?.startEvent(EventList.OBFUSCATION_INITIALIZATION);
51  const config: IOptions = FileUtils.readFileAsJson(configPath);
52  const initSuccess: boolean = obfuscator.init(config);
53  let inplace: boolean = program.opts()?.inplace;
54  if (inplace) {
55    obfuscator.setWriteOriginalFile(true);
56  }
57
58  let testType: string | undefined = program.opts()?.testType;
59  obfuscator.setTestType(testType);
60
61  UnitTestUtil.initKeepPathConfig(obfuscator.customProfiles, obfuscator.configPath);
62  performancePrinter?.iniPrinter?.endEvent(EventList.OBFUSCATION_INITIALIZATION);
63  if (!initSuccess) {
64    console.error('init from config file error.');
65    return;
66  }
67  obfuscator.obfuscateFiles();
68})();
69
70function initOptionsSetting(): void {
71  program.name('SecHarmony')
72    .version('1.0.0')
73    .description('A tool to obfuscate open harmony application written by Javascript or Typescript.')
74    .usage('Usage: SecHarmony [input files] [options]')
75    .option('-v, --version', 'show version information.')
76    .option('--inplace', 'write the ofuscated content to the original file')
77    .option('--test-type <type>', 'Indicates the type of test case, must be either "grammar" or "combinations"')
78    .option('-cp, --config-path <dir>', 'obfuscation configuration for open harmony application.')
79    .parse();
80}
81