UNPKG

kill-tabs

Version:

Kill all Chrome tabs to improve performance, decrease battery usage, and save memory

38 lines (28 loc) 1.07 kB
import {promisify} from 'node:util'; import childProcess from 'node:child_process'; import execall from 'execall'; const TEN_MEBIBYTE = 1024 * 1024 * 10; export default async function killTabs(options) { const browsers = []; if (options.chromium) { browsers.push('Caption=\'chromium.exe\''); } if (options.chrome) { browsers.push('Caption=\'chrome.exe\''); } if (options.brave) { browsers.push('Caption=\'brave.exe\''); } if (options.edge) { browsers.push('Caption=\'msedge.exe\''); } const arguments_ = ['process', 'where', browsers.join(' or '), 'get', 'CommandLine,ProcessId', '/format:list']; const response = await promisify(childProcess.execFile)('wmic', arguments_, {maxBuffer: TEN_MEBIBYTE}); if (response.stderr && !response.stderr.includes('No Instance(s) Available.')) { throw new Error('Failed to kill tabs.', {cause: new Error(response.stderr)}); } return execall(/CommandLine=(.+)\s+ProcessId=(\d+)/g, response.stdout).map(element => ({ cmd: element.subMatches[0], pid: Number.parseInt(element.subMatches[1], 10), })); }