Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 12, 2021
1 parent ca5e30a commit 7c0f52f
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 214 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
20 changes: 10 additions & 10 deletions csv-headers.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';

module.exports = {
const csvHeaders = {
default: [
'imageName',
'pid',
'sessionName',
'sessionNumber',
'memUsage'
'memUsage',
],
defaultVerbose: [
'imageName',
Expand All @@ -17,13 +15,13 @@ module.exports = {
'status',
'username',
'cpuTime',
'windowTitle'
'windowTitle',
],
apps: [
'imageName',
'pid',
'memUsage',
'packageName'
'packageName',
],
appsVerbose: [
'imageName',
Expand All @@ -35,16 +33,18 @@ module.exports = {
'username',
'cpuTime',
'windowTitle',
'packageName'
'packageName',
],
modules: [
'imageName',
'pid',
'modules'
'modules',
],
services: [
'imageName',
'pid',
'services'
]
'services',
],
};

export default csvHeaders;
51 changes: 24 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
const childProcess = require('child_process');
const {promisify} = require('util');
const {pipeline} = require('stream');
const csvHeaders = require('./csv-headers');
const transform = require('./transform');
const csv = require('csv');
import childProcess from 'node:child_process';
import {promisify} from 'node:util';
import {pipeline} from 'node:stream';
import process from 'node:process';
import csv from 'csv';
import csvHeaders from './csv-headers.js';
import transform from './transform.js';

const execFile = promisify(childProcess.execFile);
const parse = promisify(csv.parse);
Expand All @@ -23,12 +23,12 @@ function main(options = {}) {
}

// Check if system, username and password is specified together
const remoteParams = [options.system, options.username, options.password];
const remoteParameters = [options.system, options.username, options.password];
let isRemote;
if (remoteParams.every(value => value === undefined)) {
if (remoteParameters.every(value => value === undefined)) {
// All params are undefined
isRemote = false;
} else if (remoteParams.some(value => value === undefined)) {
} else if (remoteParameters.includes(undefined)) {
// Some, but not all of the params are undefined
throw new Error('The System, Username and Password options must be specified together');
} else {
Expand All @@ -37,12 +37,12 @@ function main(options = {}) {

// Check for unsupported filters on remote machines
if (Array.isArray(options.filter) && isRemote) {
options.filter.forEach(filter => {
for (const filter of options.filter) {
const parameter = filter.split(' ')[0].toLowerCase();
if (parameter === 'windowtitle' || parameter === 'status') {
throw new Error('Windowtitle and Status parameters for filtering are not supported when querying remote machines');
}
});
}
}

// Populate args
Expand Down Expand Up @@ -71,7 +71,7 @@ function main(options = {}) {
args.push(
'/s', options.system,
'/u', options.username,
'/p', options.password
'/p', options.password,
);
}

Expand Down Expand Up @@ -101,18 +101,7 @@ function main(options = {}) {
return {args, columns, currentTransform};
}

function streamInterface(options = {}) {
const {args, columns, currentTransform} = main(options);
const checkEmptyStream = new transform.ReportEmpty().getTransform();
const processOutput = childProcess.spawn('tasklist.exe', args).stdout;

// Ignore errors originating from stream end
const resultStream = pipeline(processOutput, checkEmptyStream, csv.parse({columns}), transform.makeTransform(currentTransform), error => error);
resultStream.on('error', error => error);
return resultStream;
}

async function promiseInterface(options = {}) {
export async function tasklist(options = {}) {
const {args, columns, currentTransform} = main(options);
const {stdout} = await execFile('tasklist.exe', args);
if (!stdout.startsWith('"')) {
Expand All @@ -124,5 +113,13 @@ async function promiseInterface(options = {}) {
return records;
}

module.exports = promiseInterface;
module.exports.stream = streamInterface;
export function tasklistStream(options = {}) {
const {args, columns, currentTransform} = main(options);
const checkEmptyStream = new transform.ReportEmpty().getTransform();
const processOutput = childProcess.spawn('tasklist.exe', args).stdout;

// Ignore errors originating from stream end
const resultStream = pipeline(processOutput, checkEmptyStream, csv.parse({columns}), transform.makeTransform(currentTransform), error => error);
resultStream.on('error', error => error);
return resultStream;
}
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -35,14 +37,14 @@
"services"
],
"dependencies": {
"csv": "^5.1.1",
"sec": "^1.0.0"
"csv": "^5.5.0",
"sec": "^2.0.0"
},
"devDependencies": {
"@types/node": "^13.7.6",
"ava": "^2.1.0",
"get-stream": "^5.1.0",
"xo": "^0.27.2"
"@types/node": "^16.6.0",
"ava": "^3.15.0",
"get-stream": "^6.0.1",
"xo": "^0.44.0"
},
"ava": {
"files": [
Expand Down
Loading

0 comments on commit 7c0f52f

Please sign in to comment.