Skip to content
Open
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
21 changes: 20 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IProcessInfo[]> => 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__ }));
1 change: 1 addition & 0 deletions lib/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
33 changes: 33 additions & 0 deletions lib/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,39 @@ function pollUntil(makePromise: () => Promise<boolean>, 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());
Expand Down
11 changes: 11 additions & 0 deletions typings/windows-process-tree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,15 @@ declare module '@vscode/windows-process-tree' {
namespace getProcessCpuUsage {
function __promisify__(processList: IProcessInfo[]): Promise<IProcessCpuInfo[]>;
}

/**
* 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<IProcessInfo[]>;
}
}
6 changes: 6 additions & 0 deletions typings/windows-process-tree/promises.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ declare module '@vscode/windows-process-tree/promises' {
* @param processList - The list of processes.
*/
export function getProcessCpuUsage(processList: IProcessInfo[]): Promise<IProcessCpuInfo[]>;

/**
* 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<IProcessInfo[]>;
}