Skip to content

Commit 1e67743

Browse files
rzzfchouchouji
andauthored
feat: support selective deletion instead of deleting all items (#22)
* feat: support selective deletion instead of deleting all items * test: fix test case * refactor: optimize code * refactor: optimize code --------- Co-authored-by: chouchouji <[email protected]>
1 parent bbdcdc7 commit 1e67743

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

l10n/bundle.l10n.zh-cn.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"Please choose a group to add": "请选择一个组添加别名",
1818
"frequency: {frequency}": "运行次数: {frequency}",
1919
"No any group can be added": "没有可以添加的组",
20-
"Are you sure to delete all aliases?": "确认删除所有的别名吗?",
20+
"Please choose the aliases you want to delete": "请选择要删除的别名",
21+
"Are you sure to delete all aliases?": "确定要删除所有别名吗?",
2122
"Confirm": "确认",
2223
"The .zshrc file on {path} doesn't exist. Do you want to create .zshrc file in this path?": "{path} 下.zshrc文件不存在,你想创建此文件吗?",
2324
"Create .zshrc file on {path} successfully": "{path} 下.zshrc文件创建成功",

package.nls.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"addAlias": "Add Alias",
88
"copyAllAlias": "Copy All Alias",
99
"deleteAlias": "Delete Alias",
10-
"deleteAllAlias": "Delete All Alias",
10+
"deleteAllAlias": "Delete Aliases",
1111
"run": "Run",
1212
"setDescription": "Set Description",
1313
"renameAliasName": "Rename Alias Name",

package.nls.zh-cn.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"addAlias": "新增别名",
88
"copyAllAlias": "复制全部别名",
99
"deleteAlias": "删除别名",
10-
"deleteAllAlias": "删除所有别名",
10+
"deleteAllAlias": "删除多个别名",
1111
"run": "运行",
1212
"setDescription": "更新备注",
1313
"renameAliasName": "重命名别名名称",

src/aliases.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { exec } from 'node:child_process'
22
import fs from 'node:fs'
3-
import { isEmpty } from 'rattail'
3+
import { isEmpty, normalizeToArray } from 'rattail'
44
import type { Alias } from './types'
55
import { filterAliases, isSameAlias, resolveAlias } from './utils'
66

@@ -24,25 +24,26 @@ export function appendAliasToStoreFile(path: fs.PathOrFileDescriptor, content: s
2424
reloadStoreFile(path)
2525
}
2626

27-
export function deleteAliases(path: fs.PathOrFileDescriptor, specificAlias?: Alias) {
27+
export function deleteAliases(path: fs.PathOrFileDescriptor, specificAlias: Alias | Alias[] = []) {
2828
const content = getContentFromPath(path)
2929

3030
if (isEmpty(content)) {
3131
return
3232
}
3333

34+
const specificAliases = normalizeToArray(specificAlias)
3435
const data = content
3536
.split('\n')
3637
.map((text) => text.trim())
3738
.filter((text) => {
3839
const alias = resolveAlias(text)
3940

40-
if (!specificAlias) {
41+
if (!specificAliases.length) {
4142
return !alias
4243
}
4344

4445
if (alias) {
45-
return !isSameAlias(alias, specificAlias)
46+
return !specificAliases.some((specificAlias) => isSameAlias(alias, specificAlias))
4647
}
4748

4849
return true

src/extension.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -388,23 +388,43 @@ alias ${alias}`,
388388
return
389389
}
390390

391-
const text = await vscode.window.showInformationMessage(
392-
vscode.l10n.t('Are you sure to delete all aliases?'),
393-
{ modal: true },
394-
vscode.l10n.t('Confirm'),
391+
const selectedAliases = await vscode.window.showQuickPick(
392+
aliases.map((alias) => alias.aliasName),
393+
{
394+
placeHolder: vscode.l10n.t('Please choose the aliases you want to delete'),
395+
canPickMany: true,
396+
},
395397
)
396-
// click cancel button
397-
if (text === undefined) {
398+
399+
// cancel pick group
400+
if (selectedAliases === undefined || !selectedAliases.length) {
398401
return
399402
}
400403

401-
executeCommandInTerminal(formatUnaliasCommand(aliases))
404+
if (selectedAliases.length === aliases.length) {
405+
const text = await vscode.window.showInformationMessage(
406+
vscode.l10n.t('Are you sure to delete all aliases?'),
407+
{ modal: true },
408+
vscode.l10n.t('Confirm'),
409+
)
410+
// click cancel button
411+
if (text === undefined) {
412+
return
413+
}
414+
}
415+
416+
const needDeleteAliases = aliases.filter((alias) => selectedAliases.includes(alias.aliasName))
402417

403-
deleteAliases(storePath.path)
418+
executeCommandInTerminal(formatUnaliasCommand(needDeleteAliases))
404419

405-
// remove all aliases under every groups
420+
deleteAliases(storePath.path, needDeleteAliases)
421+
422+
// remove aliases under every groups
406423
for (const groupName of this.globalState.keys()) {
407-
this.globalState.update(groupName, [])
424+
const aliases = normalizeAliasesToArray<Alias>(this.globalState.get(groupName)).filter((aliasItem) => {
425+
return !needDeleteAliases.some((alias) => isSameAlias(alias, aliasItem))
426+
})
427+
this.globalState.update(groupName, aliases)
408428
}
409429

410430
this.refresh()

tests/aliases.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,15 @@ describe('test delete alias in .zshrc', () => {
8686
expect(fs.readFileSync(ZSHRC, 'utf-8')).toBe(`\nalias pnpm_version='pnpm -v'`)
8787
})
8888

89-
it('delete all alias in .zshrc', () => {
89+
it('delete multiple aliases in .zshrc', () => {
90+
deleteAliases(ZSHRC, [
91+
{ aliasName: 'pv', command: 'pnpm -v' },
92+
{ aliasName: 'pnpm_version', command: 'pnpm -v' },
93+
])
94+
expect(fs.readFileSync(ZSHRC, 'utf-8')).toBe('')
95+
})
96+
97+
it('delete all aliases in .zshrc', () => {
9098
deleteAliases(ZSHRC)
9199
expect(fs.readFileSync(ZSHRC, 'utf-8')).toBe('')
92100
})

0 commit comments

Comments
 (0)