Skip to content

Commit

Permalink
fix: Wrong typings for compute method (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake authored Nov 14, 2024
1 parent ab4a6eb commit 9070634
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 49 deletions.
17 changes: 9 additions & 8 deletions src/ssh-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,20 @@ export default class SSHConfig extends Array<Line> {
doFinalPass: false,
}

const obj = {}
const setProperty = (name: string, value) => {
const obj: Record<string, string | string[]> = {}
const setProperty = (name: string, value: string | { val: string }[]) => {
const val = Array.isArray(value) ? value.map(({ val }) => val) : value
const val0 = Array.isArray(val) ? val[0] : val
if (MULTIPLE_VALUE_PROPS.includes(name)) {
const list = obj[name] || (obj[name] = [])
list.push(value)
const list = (obj[name] || (obj[name] = [])) as string[]
list.push(...([] as string[]).concat(val))
} else if (obj[name] == null) {
if (name === 'HostName') {
context.params.HostName = value
context.params.HostName = val0
} else if (name === 'User') {
context.params.User = value
context.params.User = val0
}

obj[name] = value
obj[name] = val
}
}

Expand Down
50 changes: 9 additions & 41 deletions test/unit/ssh-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,15 @@ describe('SSHConfig', function() {
'~/.ssh/id_rsa'
],
ProxyCommand: [
{
'quoted': false,
'separator': ' ',
'val': 'ssh',
},
{
'quoted': false,
'separator': ' ',
'val': '-q',
},
{
'quoted': false,
'separator': ' ',
'val': 'gateway',
},
{
'quoted': false,
'separator': ' ',
'val': '-W',
},
{
'quoted': false,
'separator': ' ',
'val': '%h:%p',
},
'ssh',
'-q',
'gateway',
'-W',
'%h:%p',
],
ServerAliveInterval: '80',
User: 'nil',
ForwardAgent: 'true'
ForwardAgent: 'true',
})
})

Expand Down Expand Up @@ -96,21 +76,9 @@ describe('SSHConfig', function() {
for (const host of ['foo', 'foo.bar', 'baz ham']) {
assert.deepEqual(config.compute(host), {
Host: [
{
'quoted': false,
'separator': ' ',
'val': 'foo',
},
{
'quoted': true,
'separator': ' ',
'val': '*.bar',
},
{
'quoted': true,
'separator': ' ',
'val': 'baz ham',
},
'foo',
'*.bar',
'baz ham',
],
HostName: 'example.com',
User: 'robb'
Expand Down

0 comments on commit 9070634

Please sign in to comment.