Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Deno Tests

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.x

- name: Run Deno smoke tests
run: deno task test
36 changes: 36 additions & 0 deletions .github/workflows/nodejs-legacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Node CI

on:
pull_request:
push:
branches:
- main
- master


jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm run test:legacy:coverage

- name: Codecov
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x, 22.x, 24.x]
node-version: [22.x, 24.x]

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
coverage
dist
lib
node_modules
package-lock.json
.nyc_output
Expand Down
7 changes: 4 additions & 3 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"require": ["ts-node/register"],
"require": "ts-node/register",
"extensions": ["ts"],
"spec": ["test/unit/**/*.test.ts"],
"exit": true,
"recursive": true
"recursive": true,
"enableSourceMaps": true,
"timeout": 5000
}
21 changes: 21 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@cyjake/ssh-config",
"version": "5.0.4",
"exports": "./src/ssh-config.ts",
"imports": {
"sinon": "npm:sinon@^21.0.0",
"mocha": "npm:mocha@^10.0.0"
},
"publish": {
"include": [
"src",
"LICENSE",
"Readme.md"
]
},
"tasks": {
"test": "tsc && deno test test/deno_test.ts --allow-env --allow-read --allow-run --no-check --sloppy-imports"
},
"nodeModulesDir": "auto",
"lock": false
}
26 changes: 16 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,41 @@
"url": "git@github.com:cyjake/ssh-config.git"
},
"files": [
"dist/**"
"dist/**",
"lib/**"
],
"devDependencies": {
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.45",
"@types/sinon": "^17.0.3",
"@types/sinon": "^21.0.0",
"@typescript-eslint/eslint-plugin": "^5.48.0",
"@typescript-eslint/parser": "^5.48.0",
"eslint": "^8.31.0",
"heredoc": "^1.3.1",
"mocha": "^8.2.1",
"mocha": "^11.7.5",
"nyc": "^15.1.0",
"sinon": "^17.0.1",
"sinon": "^21.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.4"
},
"scripts": {
"lint": "eslint --ext ts .",
"lint:fix": "eslint --ext ts --fix .",
"build": "tsc",
"build": "tsc && tsc -p tsconfig.cjs.json",
"prepack": "npm run build",
"pretest": "npm run build",
"test": "mocha",
"test:coverage": "nyc mocha && nyc report --reporter=lcov"
"test": "mocha --spec test/unit/**/*.test.ts --node-option=experimental-transform-types",
"test:coverage": "nyc mocha --spec test/unit/**/*.test.ts --node-option=experimental-transform-types && nyc report --reporter=lcov",
"test:legacy": "TS_NODE_PROJECT=./tsconfig.cjs.json mocha --spec test/legacy/**/*.test.ts",
"test:legacy:coverage": "TS_NODE_PROJECT=./tsconfig.cjs.json nyc mocha --spec test/legacy/**/*.test.ts && nyc report --reporter=lcov"
},
"main": "dist/ssh-config.js",
"exports": {
"import": "./lib/ssh-config.js",
"require": "./dist/ssh-config.js"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"engine": {
"node": ">= 14.0.0"
"node": ">= 14.13.1"
},
"license": "MIT"
}
2 changes: 1 addition & 1 deletion src/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function match(pattern: string, text: string) {
* @param {string|string[]} patternList
* @param {string} str
*/
function glob(patternList: string | string[], text: string) {
function glob(patternList: string | string[], text: string): boolean {
const patterns = Array.isArray(patternList) ? patternList : patternList.split(/,/)

// > If a negated entry is matched, then the Host entry is ignored, regardless of whether any other patterns on the line match.
Expand Down
3 changes: 0 additions & 3 deletions src/index.ts

This file was deleted.

27 changes: 14 additions & 13 deletions src/ssh-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import glob from './glob'
import { spawnSync } from 'child_process'
import os from 'os'
import glob from './glob.ts'
import { spawnSync } from 'node:child_process'
import os from 'node:os'

const RE_SPACE = /\s/
const RE_LINE_BREAK = /\r|\n/
Expand Down Expand Up @@ -84,7 +84,8 @@ const REPEATABLE_DIRECTIVES = [
'CertificateFile',
]

function compare(line, opts) {
function compare(line: Directive, opts: FindOptions) {
// @ts-ignore
return opts.hasOwnProperty(line.param) && opts[line.param] === line.value
}

Expand Down Expand Up @@ -283,23 +284,23 @@ export default class SSHConfig extends Array<Line> {
/**
* Find by Host or Match.
*/
public find(opts: FindOptions): Line | undefined;
public override find(opts: FindOptions): Line | undefined;

/**
* Find by search function.
* @param predicate Function to check against each line; should return a truthy value when a
* matching line is given.
*/
public find(predicate: (line: Line, index: number, config: Line[]) => unknown): Line | undefined;
public override find(predicate: (line: Line, index: number, config: Line[]) => unknown): Line | undefined;

public find(opts: ((line: Line, index: number, config: Line[]) => unknown) | FindOptions) {
public override find(opts: ((line: Line, index: number, config: Line[]) => unknown) | FindOptions) {
if (typeof opts === 'function') return super.find(opts)

if (!(opts && ('Host' in opts || 'Match' in opts))) {
throw new Error('Can only find by Host or Match')
}

return super.find(line => compare(line, opts))
return super.find((line: Line) => 'param' in line && compare(line, opts))
}


Expand All @@ -323,13 +324,13 @@ export default class SSHConfig extends Array<Line> {
} else if (!(opts && ('Host' in opts || 'Match' in opts))) {
throw new Error('Can only remove by Host or Match')
} else {
index = super.findIndex(line => compare(line, opts))
index = super.findIndex((line: Line) => 'param' in line && compare(line, opts))
}

if (index >= 0) return this.splice(index, 1)
}

public toString(): string {
public override toString(): string {
return stringify(this)
}

Expand Down Expand Up @@ -704,9 +705,9 @@ export function stringify(config: SSHConfig): string {
return quoted ? `"${value}"` : value
}

function formatDirective(line) {
function formatDirective(line: Directive) {
const quoted = line.quoted
|| (RE_QUOTE_DIRECTIVE.test(line.param) && RE_SPACE.test(line.value))
|| (RE_QUOTE_DIRECTIVE.test(line.param) && typeof line.value === 'string' && RE_SPACE.test(line.value))
const value = formatValue(line.value, quoted)
return `${line.param}${line.separator}${value}`
}
Expand Down Expand Up @@ -739,4 +740,4 @@ export function stringify(config: SSHConfig): string {
return str
}

export { glob }
export { glob, SSHConfig }
7 changes: 7 additions & 0 deletions test/deno_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'https://deno.land/x/deno_mocha/global.ts'

import './unit/stringify.test.ts'
import './unit/parse.test.ts'
import './unit/compute.test.ts'
import './unit/ssh-config.test.ts'
import './unit/glob.test.ts'
17 changes: 17 additions & 0 deletions test/helpers.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require('node:path')
const fs = require('node:fs/promises')

const stripPattern = /^[ \t]*(?=[^\s]+)/mg

exports.heredoc = function heredoc(text) {
const indentLen = text.match(stripPattern).reduce((min, line) => Math.min(min, line.length), Infinity)
const indent = new RegExp('^[ \\t]{' + indentLen + '}', 'mg')
return indentLen > 0
? text.replace(indent, '').trimStart().replace(/ +?$/, '')
: text
}

exports.readFixture = async function readFixture(fname) {
const fpath = path.join(__dirname, 'fixture', fname)
return (await fs.readFile(fpath, 'utf-8')).replace(/\r\n/g, '\n')
}
9 changes: 6 additions & 3 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import fs from 'fs/promises'
import path from 'path'
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const currentDir = path.dirname(fileURLToPath(import.meta.url))

const stripPattern = /^[ \t]*(?=[^\s]+)/mg

Expand All @@ -12,6 +15,6 @@ export function heredoc(text: string) {
}

export async function readFixture(fname: string) {
const fpath = path.join(__dirname, 'fixture', fname)
const fpath = path.join(currentDir, 'fixture', fname)
return (await fs.readFile(fpath, 'utf-8')).replace(/\r\n/g, '\n')
}
Loading