Skip to content

Commit 22f7902

Browse files
committed
fix: update file content after sorting system alias
1 parent 0585a0c commit 22f7902

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

src/aliases.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,30 @@ export function renameAliases(
8686

8787
reloadStoreFile(path)
8888
}
89+
90+
/**
91+
* Replace aliases with new aliases
92+
*/
93+
export function replaceAllAliases(path: fs.PathOrFileDescriptor, aliases: Alias[]) {
94+
const content = getContentFromPath(path)
95+
96+
if (isEmpty(content)) {
97+
return
98+
}
99+
100+
let aliasIndex = 0
101+
const data = []
102+
for (const line of content.split('\n')) {
103+
if (resolveAlias(line.trim())) {
104+
const { aliasName, command } = aliases[aliasIndex] ?? {}
105+
data.push(`alias ${aliasName}='${command}'`)
106+
aliasIndex += 1
107+
} else {
108+
data.push(line)
109+
}
110+
}
111+
112+
fs.writeFileSync(path, data.join('\n'))
113+
114+
reloadStoreFile(path)
115+
}

src/extension.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import os from 'node:os'
33
import path from 'node:path'
44
import { isArray, isNonEmptyArray } from 'rattail'
55
import * as vscode from 'vscode'
6-
import { appendAliasToStoreFile, deleteAliases, getAliases, renameAliases } from './aliases'
6+
import { appendAliasToStoreFile, deleteAliases, getAliases, renameAliases, replaceAllAliases } from './aliases'
77
import { SYSTEM_ALIAS } from './constants'
88
import storePath from './path'
99
import type { Alias } from './types'
@@ -691,7 +691,11 @@ alias ${alias}`,
691691
}
692692

693693
aliases.sort((a, b) => a.aliasName.toLowerCase().localeCompare(b.aliasName.toLowerCase()))
694-
this.globalState.update(alias.group, aliases)
694+
if (alias.group === SYSTEM_ALIAS) {
695+
replaceAllAliases(storePath.path, aliases)
696+
} else {
697+
this.globalState.update(alias.group, aliases)
698+
}
695699

696700
this.refresh()
697701
}
@@ -704,7 +708,11 @@ alias ${alias}`,
704708
}
705709

706710
aliases.sort((a, b) => (a.frequency ?? 0) - (b.frequency ?? 0))
707-
this.globalState.update(alias.group, aliases)
711+
if (alias.group === SYSTEM_ALIAS) {
712+
replaceAllAliases(storePath.path, aliases)
713+
} else {
714+
this.globalState.update(alias.group, aliases)
715+
}
708716

709717
this.refresh()
710718
}

tests/aliases.spec.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import os from 'node:os'
33
import path from 'node:path'
44
import mockFs from 'mock-fs'
55
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
6-
import { appendAliasToStoreFile, deleteAliases, getAliases, getContentFromPath, renameAliases } from '../src/aliases'
6+
import {
7+
appendAliasToStoreFile,
8+
deleteAliases,
9+
getAliases,
10+
getContentFromPath,
11+
renameAliases,
12+
replaceAllAliases,
13+
} from '../src/aliases'
714

815
const ZSHRC = path.join(os.homedir(), '.zshrc')
916

@@ -99,3 +106,36 @@ describe('test delete alias in .zshrc', () => {
99106
expect(fs.readFileSync(ZSHRC, 'utf-8')).toBe('')
100107
})
101108
})
109+
110+
describe('test replace all aliases with new aliases', () => {
111+
it('replace all aliases in .zshrc', () => {
112+
deleteAliases(ZSHRC)
113+
fs.writeFileSync(
114+
ZSHRC,
115+
`# test
116+
test
117+
`,
118+
)
119+
appendAliasToStoreFile(ZSHRC, `alias nv='node -v'\n`)
120+
appendAliasToStoreFile(ZSHRC, `alias pv='pnpm -v'\n`)
121+
122+
fs.appendFileSync(ZSHRC, '# test', 'utf-8')
123+
124+
replaceAllAliases(ZSHRC, [
125+
{
126+
aliasName: 'nv2',
127+
command: 'node -v',
128+
},
129+
{
130+
aliasName: 'pv2',
131+
command: 'pnpm -v',
132+
},
133+
])
134+
135+
expect(fs.readFileSync(ZSHRC, 'utf-8')).toBe(`# test
136+
test
137+
alias nv2='node -v'
138+
alias pv2='pnpm -v'
139+
# test`)
140+
})
141+
})

0 commit comments

Comments
 (0)