Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 13, 2018
1 parent 537ec94 commit 50606e1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 59 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- '10'
- '8'
- '6'
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '10'
install:
- ps: Install-Product node $env:nodejs_version
- npm install --global npm@latest
Expand Down
92 changes: 46 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
'use strict';
const util = require('util');
const path = require('path');
const childProcess = require('child_process');
const pify = require('pify');

const TEN_MEGABYTES = 1000 * 1000 * 10;
const execFile = util.promisify(childProcess.execFile);

function win() {
const windows = async () => {
// Source: https://github.com/MarkTiedemann/fastlist
const bin = path.join(__dirname, 'fastlist.exe');

return pify(childProcess.execFile)(bin, {maxBuffer: TEN_MEGABYTES})
.then(stdout =>
stdout.trim()
.split('\r\n')
.map(line => line.split('\t'))
.map(([name, pid, ppid]) => ({
name,
pid: Number.parseInt(pid, 10),
ppid: Number.parseInt(ppid, 10)
}))
);
}

function def(options = {}) {
const ret = {};
const {stdout} = await execFile(bin, {maxBuffer: TEN_MEGABYTES});

return stdout
.trim()
.split('\r\n')
.map(line => line.split('\t'))
.map(([name, pid, ppid]) => ({
name,
pid: Number.parseInt(pid, 10),
ppid: Number.parseInt(ppid, 10)
}));
};

const main = async (options = {}) => {
const flags = (options.all === false ? '' : 'a') + 'wwxo';
const ret = {};

return Promise.all(['comm', 'args', 'ppid', '%cpu', '%mem'].map(cmd => {
return pify(childProcess.execFile)('ps', [flags, `pid,${cmd}`], {maxBuffer: TEN_MEGABYTES}).then(stdout => {
for (let line of stdout.trim().split('\n').slice(1)) {
line = line.trim();
const [pid] = line.split(' ', 1);
const val = line.slice(pid.length + 1).trim();
await Promise.all(['comm', 'args', 'ppid', '%cpu', '%mem'].map(async cmd => {
const {stdout} = await execFile('ps', [flags, `pid,${cmd}`], {maxBuffer: TEN_MEGABYTES});

if (ret[pid] === undefined) {
ret[pid] = {};
}
for (let line of stdout.trim().split('\n').slice(1)) {
line = line.trim();
const [pid] = line.split(' ', 1);
const val = line.slice(pid.length + 1).trim();

ret[pid][cmd] = val;
if (ret[pid] === undefined) {
ret[pid] = {};
}
});
})).then(() => {
// Filter out inconsistencies as there might be race
// issues due to differences in `ps` between the spawns
// TODO: Use `Object.entries` when targeting Node.js 8
return Object.keys(ret).filter(x => ret[x].comm && ret[x].args && ret[x].ppid && ret[x]['%cpu'] && ret[x]['%mem']).map(x => {
return {
pid: Number.parseInt(x, 10),
name: path.basename(ret[x].comm),
cmd: ret[x].args,
ppid: Number.parseInt(ret[x].ppid, 10),
cpu: Number.parseFloat(ret[x]['%cpu']),
memory: Number.parseFloat(ret[x]['%mem'])
};
});
});
}

module.exports = process.platform === 'win32' ? win : def;

ret[pid][cmd] = val;
}
}));

// Filter out inconsistencies as there might be race
// issues due to differences in `ps` between the spawns
return Object.entries(ret)
.filter(([, value]) => value.comm && value.args && value.ppid && value['%cpu'] && value['%mem'])
.map(([key, value]) => ({
pid: Number.parseInt(key, 10),
name: path.basename(value.comm),
cmd: value.args,
ppid: Number.parseInt(value.ppid, 10),
cpu: Number.parseFloat(value['%cpu']),
memory: Number.parseFloat(value['%mem'])
}));
};

module.exports = process.platform === 'win32' ? windows : main;
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
"index.js",
"fastlist.exe"
],
"keywords": [
"ps",
Expand All @@ -27,11 +28,8 @@
"running",
"tasklist"
],
"dependencies": {
"pify": "^4.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^0.25.0",
"xo": "^0.23.0"
}
}
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import test from 'ava';
import m from '.';
import psList from '.';

const isWindows = process.platform === 'win32';

test('main', async t => {
const binName = isWindows ? 'node.exe' : 'ava';
const list = await m();
const list = await psList();

t.true(list.some(x => x.name.includes(binName)));
t.true(
Expand Down

0 comments on commit 50606e1

Please sign in to comment.