Skip to content

Commit 185eb7d

Browse files
committed
optimize(cli/sync): 优化 isEqual 实现方式, 提示性能, 减少代码行数
1 parent 2744c55 commit 185eb7d

File tree

1 file changed

+5
-27
lines changed

1 file changed

+5
-27
lines changed

packages/cli/src/libs/sync.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -183,33 +183,11 @@ const createServerAndOpenPage = async () => {
183183

184184
// 判断本地配置是否与远程配置一致
185185
const remoteUserConfigs = JSON.parse(fs.readFileSync(configPath, "utf8"));
186-
// 逐个元素对比
187-
const isEqual = (
188-
localUserConfigs: UserConfig[],
189-
remoteUserConfigs: UserConfig[]
190-
) => {
191-
let result = true;
192-
if (localUserConfigs.length !== remoteUserConfigs.length) {
193-
result = false;
194-
return result;
195-
}
196-
for (let i = 0; i < localUserConfigs.length; i++) {
197-
const localUserConfig = localUserConfigs[i];
198-
const remoteUserConfig = remoteUserConfigs[i];
199-
if (localUserConfig.alias !== remoteUserConfig.alias) {
200-
result = false;
201-
}
202-
if (localUserConfig.name !== remoteUserConfig.name) {
203-
result = false;
204-
}
205-
if (localUserConfig.email !== remoteUserConfig.email) {
206-
result = false;
207-
}
208-
if (localUserConfig.origin !== remoteUserConfig.origin) {
209-
result = false;
210-
}
211-
}
212-
return result;
186+
// 对比远程和本地配置是否一致
187+
const isEqual = (local: UserConfig[], remote: UserConfig[]) => {
188+
const hash = (arr: UserConfig[]) =>
189+
arr.map(c => `${c.alias}:${c.name}:${c.email}:${c.origin}`).join('|');
190+
return hash(local) === hash(remote);
213191
};
214192
if (!remoteUserConfigs?.length) {
215193
// 远程配置不存在,直接写入本地配置

0 commit comments

Comments
 (0)