Skip to content

Commit 2c383de

Browse files
authored
Merge pull request #81 from adzhiljano/fix_missing_regexp_type
fix: add missing regexp type when passing regexp as value
2 parents 0218cd7 + ef13200 commit 2c383de

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function find(type: 'port' | 'pid' | 'name', value: string | number, options?: F
117117
- `type` - The type of search, supports: `'port' | 'pid' | 'name'`
118118
- `value` - The value to search for. Can be RegExp if type is `'name'`
119119
- `options` - Optional configuration object or boolean for strict mode
120-
- `options.strict` - Optional strict mode for exact matching of port, pid, or name (on Windows, `.exe` can be omitted)
120+
- `options.strict` - Optional strict mode for exact matching of name (on Windows, `.exe` can be omitted)
121121
- `options.logLevel` - Set logging level to `'trace' | 'debug' | 'info' | 'warn' | 'error'`. Useful for silencing netstat warnings on Linux
122122
- `options.skipSelf` - Skip the current process when searching by name
123123

src/find.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const findBy = {
2626
skipSelf: true
2727
})
2828
}
29-
} as Record<FindMethod, (value: string | number, config: FindConfig) => Promise<ProcessInfo[]>>
29+
} as Record<FindMethod, (value: string | RegExp | number, config: FindConfig) => Promise<ProcessInfo[]>>
3030

3131
/**
3232
* find process by condition
@@ -47,12 +47,18 @@ const findBy = {
4747
* @param {Boolean|Option}
4848
* @return {Promise}
4949
*/
50-
function find(by: FindMethod, value: string | number, options?: FindConfig | boolean): Promise<ProcessInfo[]> {
50+
function find(by: FindMethod, value: string | RegExp | number, options?: FindConfig | boolean): Promise<ProcessInfo[]> {
5151
const config: FindConfig = Object.assign({
5252
logLevel: 'warn' as const,
5353
strict: typeof options === 'boolean' ? options : false
5454
}, typeof options === 'object' ? options : {})
5555

56+
// strict is applicable only when finding by name and the value is a string,
57+
// in all other cases whatever is passed is overwritten to false
58+
if (by !== 'name' || typeof value !== 'string') {
59+
config.strict = false;
60+
}
61+
5662
if (config.logLevel) {
5763
log.setLevel(config.logLevel)
5864
}
@@ -67,7 +73,7 @@ function find(by: FindMethod, value: string | number, options?: FindConfig | boo
6773
} else if (by === 'port' && !isNumber) {
6874
reject(new Error('port must be a number'))
6975
} else {
70-
findBy[by](value as any, config).then(resolve, reject)
76+
findBy[by](value, config).then(resolve, reject)
7177
}
7278
}
7379
})

src/find_process.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as path from 'path'
22
import utils from './utils'
33
import { ProcessInfo, FindCondition, PlatformFinder } from './types'
44

5-
function matchName(text: string, name: string): boolean {
5+
function matchName(text: string, name: string | RegExp): boolean {
66
if (!name) {
77
return true
88
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface FindConfig {
2525
*/
2626
export interface FindCondition {
2727
pid?: number
28-
name?: string
28+
name?: string | RegExp
2929
config: FindConfig
3030
skipSelf?: boolean
3131
}

test/find.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ describe('Find process test', function () {
7272
});
7373
})
7474

75+
it('should find process list matched given regexp', function (done) {
76+
const file = path.join(__dirname, 'fixtures/child_process.js')
77+
const cps: ChildProcessWithoutNullStreams = cp.spawn(process.execPath, [file, 'AAABBBCCC']);
78+
79+
(find as any)('name', /A{2,3}B{2,3}C{2,3}/gi)
80+
.then(function (list: any[]) {
81+
cps.kill()
82+
83+
assert(list.length === 1)
84+
assert.equal(cps.pid, list[0].pid)
85+
done()
86+
}, function (err: any) {
87+
cps.kill()
88+
89+
done(err)
90+
})
91+
})
92+
7593
it('should resolve empty array when pid not exists', function (done) {
7694
(find as any)('port', 100000)
7795
.then(function (list: any[]) {

0 commit comments

Comments
 (0)