• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { ApiDiffer } = require('./api_diff');
17const { StatusCode } = require('./reporter');
18const { JSONReporter, ExcelReporter, ChangelogReporter, MarkDownReporter } = require('./api_writer');
19const { applyChangeLogs } = require('./format_data');
20
21exports.ApiDiffPlugin = {
22  pluginOptions: {
23    name: 'diff',
24    version: '0.1.0',
25    description: 'Compare the differences between the two SDKS',
26    commands: [
27      {
28        isRequiredOption: false,
29        options: ['--old <string>', 'old sdk path'],
30      },
31      {
32        isRequiredOption: false,
33        options: ['--new <string>', 'new sdk path'],
34      },
35      {
36        isRequiredOption: true,
37        options: ['--oldVersion <string>', 'old sdk version'],
38      },
39      {
40        isRequiredOption: true,
41        options: ['--newVersion <string>', 'new sdk version'],
42      },
43      {
44        isRequiredOption: true,
45        options: ['--output <string>', 'output file path'],
46      },
47      {
48        isRequiredOption: false,
49        options: ['--format <json, excel, changelog>', 'output file format'],
50      },
51      {
52        isRequiredOption: false,
53        options: ['--newPath <string>', 'new interface_sdk-js path'],
54      },
55      {
56        isRequiredOption: false,
57        options: ['--oldPath <string>', 'old interface_sdk-js path'],
58      },
59      {
60        isRequiredOption: false,
61        options: ['--changelogUrl <string>', 'changelog url'],
62      }
63    ],
64  },
65
66  start: async function (argv) {
67    const oldSdk = argv.old ? argv.old : argv.oldPath;
68    const newSdk = argv.new ? argv.new : argv.newPath;
69    const oldVersion = argv.oldVersion;
70    const newVersion = argv.newVersion;
71    const startTime = Date.now();
72    const diffInfoMap = await ApiDiffer.compareSdks(oldSdk, newSdk);
73    const changelogUrl = argv.changelogUrl;
74    let diffMap = new Map();
75    if (changelogUrl) {
76      diffMap = await applyChangeLogs(diffInfoMap, oldVersion, newVersion, changelogUrl);
77    } else {
78      diffMap = diffInfoMap;
79    }
80    const diffs = this.coverToArray(diffMap);
81    if (!argv.format || argv.format === 'json') {
82      JSONReporter.write(diffs, argv.output, oldVersion, newVersion);
83    } else if (argv.format === 'excel') {
84      ExcelReporter.write(diffs, argv.output, oldVersion, newVersion);
85      MarkDownReporter.write(diffs, argv.output);
86    } else if (argv.format === 'changelog') {
87      const changeLogData = [];
88      this.formatToChangelog(diffs, changeLogData);
89      ChangelogReporter.write(changeLogData, argv.output);
90    }
91    console.log(`elapsed time: ${Date.now() - startTime}`);
92  },
93
94  formatToChangelog(diffs, changeLogData) {
95    diffs.forEach(diffData => {
96      if (diffData.statusCode === StatusCode.NEW_API) {
97        changeLogData.push(this.formatDiffApi('N/A', diffData.rawText, diffData.className, diffData.className,
98          diffData.dtsName, diffData.dtsName));
99        return;
100      } else if (diffData.statusCode === StatusCode.DELETE) {
101        changeLogData.push(this.formatDiffApi(diffData.rawText, 'N/A', diffData.className, diffData.className,
102          diffData.dtsName, diffData.dtsName));
103        return;
104      } else if (diffData.statusCode === StatusCode.DELETE_CLASS) {
105        changeLogData.push(this.formatDiffApi('N/A', 'N/A', 'N/A', 'N/A', diffData.dtsName, 'N/A'));
106        return;
107      } else if (diffData.statusCode === StatusCode.FUNCTION_CHANGES) {
108        changeLogData.push(this.formatDiffApi(diffData.oldMessage, diffData.newMessage, diffData.className,
109          diffData.className, diffData.dtsName, diffData.dtsName));
110        return;
111      } else {
112        changeLogData.push(this.formatDiffApi(diffData.rawText, diffData.rawText, diffData.className,
113          diffData.className, diffData.dtsName, diffData.dtsName));
114      }
115    });
116  },
117
118  formatDiffApi(oldApi, newApi, oldType, newType, oldDtsName, newDtsName) {
119    return {
120      'oldApi': oldApi,
121      'newApi': newApi,
122      'oldType': oldType,
123      'newType': newType,
124      'oldDtsName': oldDtsName,
125      'newDtsName': newDtsName,
126      'changelog': '',
127      'compatible': '',
128    };
129  },
130
131  coverToArray(diffMap) {
132    const values = diffMap.values();
133    const diffs = [];
134    const diffSet = new Set();
135    for (const data of values) {
136      data.forEach(diffApi => {
137        diffApi.changelogs = [...diffApi.changelogs];
138        diffSet.add(JSON.stringify(diffApi));
139      });
140    }
141
142    diffSet.forEach(data => {
143      diffs.push(JSON.parse(data));
144    });
145    return diffs;
146  },
147
148  stop: function () { },
149};