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
16import { CONTAINS_PATH, SIGN_FULL_PATH, SIGN_PUBLIC_PATH } from 'config.dev';
17import { checkProjectIfInFull } from "./checkProjectIfInFull"
18const fs = require('fs');
19
20interface ContainsType {
21    SIGN_HAP_PATH?: string;
22    FA_MODAL_AND_LOWER_CASE_LIST?: string;
23    INSTALL_LIST_CAPABILITY?: string;
24    SAVE_XML_PATH?: string;
25    COMBIN_CONFIG?: string;
26    SPECIAL_LIST?: string;
27    SPECIAL_HAP?: string;
28    TARGET_PATH?: string;
29};
30
31function editLinuxContains(targetPath: string) {
32    try {
33        const config: ContainsType = {};
34        const configContent = fs.readFileSync(CONTAINS_PATH, 'utf8');
35        configContent.trim().split('\n').forEach((line: string) => {
36            if (line.trim() !== '') {
37                const [key = '', value = ''] = line.split('=');
38                config[key.trim()] = value.trim();
39            };
40        });
41        if (checkProjectIfInFull(targetPath)) {
42            config.SIGN_HAP_PATH = `r'${SIGN_FULL_PATH}'`;
43        } else {
44            config.SIGN_HAP_PATH = `r'${SIGN_PUBLIC_PATH}'`;
45        };
46        const target = targetPath.split('/').join('_');
47        config.TARGET_PATH = `r'${target}'`;
48        console.log(Object.entries(config).length);
49        const updatedConfigContent = Object.entries(config).map(([key, value]) => `${key} = ${value}`).join('\n');
50        fs.writeFileSync(CONTAINS_PATH, updatedConfigContent);
51        return {
52            status: true,
53            msg: ''
54        };
55    } catch (e) {
56        return {
57            status: false,
58            msg: JSON.stringify(e)
59        };
60    };
61}
62
63export { editLinuxContains };