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
16const fs = require('fs');
17const path = require('path');
18const { exec } = require('child_process');
19import { Extension } from '../src/common/type';
20
21function obfuscateDirs(obfConfig, obfDir) {
22  const command = `node --loader=ts-node/esm src/cli/SecHarmony.ts ${obfDir} --config-path ${obfConfig} --test-type grammar`;
23  exec(command, (error, stdout, stderr) => {
24    if (error) {
25      console.error(`Error executing command: ${error.message}`);
26      return;
27    }
28    if (stdout) {
29      console.log('Debug info: ', stdout);
30    }
31  });
32}
33
34function traverseDirs(rootDirPath, configPath) {
35  const currentEntries = fs.readdirSync(rootDirPath);
36  let configFile = 'obfConfig.json';
37  if (currentEntries.includes(configFile)) {
38    configPath = rootDirPath;
39  }
40
41  const hasJsOrTsFiles = currentEntries.some(entry => {
42    return entry.endsWith(Extension.TS) || entry.endsWith(Extension.JS) || entry.endsWith(Extension.DETS);
43  });
44
45  if (hasJsOrTsFiles) {
46    obfuscateDirs(path.join(configPath, configFile), rootDirPath);
47    return;
48  }
49
50  for (const currentEntry of currentEntries) {
51    const currentPath = path.join(rootDirPath, currentEntry);
52    if (fs.statSync(currentPath).isDirectory()) {
53      traverseDirs(currentPath, configPath);
54    }
55  }
56}
57
58function run() {
59  const testCasesRootDir = path.join(__dirname, '../test/grammar');
60  traverseDirs(testCasesRootDir, testCasesRootDir);
61}
62
63function main() {
64  run();
65}
66
67main();