diff --git a/lib/index.ts b/lib/index.ts index 065c338..7b53aad 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -223,7 +223,26 @@ export namespace getProcessTree { }); } +/** + * Returns a list of all processes on the system + * @param callback The callback to use with the returned set of processes + * @param flags The flags for what process data should be included + */ +export function getAllProcesses(callback: (processList: IProcessInfo[]) => void, flags?: ProcessDataFlag): void { + if (process.platform !== 'win32') { + throw new Error('getAllProcesses is only implemented on Windows'); + } + getRawProcessList(callback, flags); +} + +export namespace getAllProcesses { + // tslint:disable-next-line:variable-name + export const __promisify__ = (flags?: ProcessDataFlag): Promise => new Promise((resolve, reject) => { + getAllProcesses((processList) => resolve(processList), flags); + }); +} + // Since symbol properties can't be declared via namespace merging, we just define __promisify__ that way and // and manually set the "modern" promisify symbol: https://github.com/microsoft/TypeScript/issues/36813 -[getProcessTree, getProcessList, getProcessCpuUsage].forEach(func => +[getProcessTree, getProcessList, getProcessCpuUsage, getAllProcesses].forEach(func => Object.defineProperty(func, promisify.custom, { enumerable: false, value: func.__promisify__ })); diff --git a/lib/promises.ts b/lib/promises.ts index 93e7093..c60cf3b 100644 --- a/lib/promises.ts +++ b/lib/promises.ts @@ -12,3 +12,4 @@ export { ProcessDataFlag } from './index'; export const getProcessTree = promisify(wpc.getProcessTree); export const getProcessList = promisify(wpc.getProcessList); export const getProcessCpuUsage = promisify(wpc.getProcessCpuUsage); +export const getAllProcesses = promisify(wpc.getAllProcesses); diff --git a/lib/test.ts b/lib/test.ts index f502765..dddf6a8 100644 --- a/lib/test.ts +++ b/lib/test.ts @@ -27,6 +27,39 @@ function pollUntil(makePromise: () => Promise, cb: () => void, interval if (isWindows) { const native = require('../build/Release/windows_process_tree.node'); + describe('getAllProcesses', () => { + it('should return a list containing this process via native API', (done) => { + native.getProcessList((list) => { + assert.notStrictEqual(list?.find(p => p.pid === process.pid), undefined); + done(); + }, 0); + }); + + it('should work via API', (done) => { + const { getAllProcesses } = require('./index'); + getAllProcesses((list) => { + assert.ok(Array.isArray(list)); + assert.notStrictEqual(list.find(p => p.pid === process.pid), undefined); + done(); + }); + }); + + it('should work promisified', async () => { + const list = await promises.getAllProcesses(); + assert.ok(Array.isArray(list)); + assert.notStrictEqual(list.find(p => p.pid === process.pid), undefined); + }); + + it('should return memory information only when the flag is set', async () => { + const list = await promises.getAllProcesses(ProcessDataFlag.Memory); + assert.ok(list.some(p => typeof p.memory === 'number')); + }); + + it('should return command line information only when the flag is set', async () => { + const list = await promises.getAllProcesses(ProcessDataFlag.CommandLine); + assert.ok(list.every(p => typeof p.commandLine === 'string')); + }); + }); describe('getRawProcessList', () => { it('should throw if arguments are not provided', (done) => { assert.throws(() => native.getProcessList()); diff --git a/typings/windows-process-tree.d.ts b/typings/windows-process-tree.d.ts index 996868e..70e242b 100644 --- a/typings/windows-process-tree.d.ts +++ b/typings/windows-process-tree.d.ts @@ -72,4 +72,15 @@ declare module '@vscode/windows-process-tree' { namespace getProcessCpuUsage { function __promisify__(processList: IProcessInfo[]): Promise; } + + /** + * Returns a list of all processes on the system. + * @param callback - The callback to use with the returned list of processes. + * @param flags - The flags for what process data should be included. + */ + export function getAllProcesses(callback: (processList: IProcessInfo[]) => void, flags?: ProcessDataFlag): void; + + namespace getAllProcesses { + function __promisify__(flags?: ProcessDataFlag): Promise; + } } diff --git a/typings/windows-process-tree/promises.d.ts b/typings/windows-process-tree/promises.d.ts index 25d8422..ce1cbb5 100644 --- a/typings/windows-process-tree/promises.d.ts +++ b/typings/windows-process-tree/promises.d.ts @@ -37,4 +37,10 @@ declare module '@vscode/windows-process-tree/promises' { * @param processList - The list of processes. */ export function getProcessCpuUsage(processList: IProcessInfo[]): Promise; + + /** + * Returns a list of all processes on the system. + * @param flags - The flags for what process data should be included. + */ + export function getAllProcesses(flags?: ProcessDataFlag): Promise; }