Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 10, 2021
1 parent 5258f2d commit 67fd834
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
indent_size = 2
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto eol=lf
* text=auto eol=lf
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
yarn.lock
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
57 changes: 25 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
'use strict';
const os = require('os');
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
import os from 'os';
import fs from 'fs';
import path from 'path';
import childProcess from 'child_process';

function parse(string) {
export function parseShellHistory(string) {
const reBashHistory = /^: \d+:0;/;

return string.trim().split('\n').map(x => {
if (reBashHistory.test(x)) {
return x.split(';').slice(1).join(';');
return string.trim().split('\n').map(line => {
if (reBashHistory.test(line)) {
return line.split(';').slice(1).join(';');
}

// ZSH just places one command on each line
return x;
return line;
});
}

function getPath(options) {
options = options || {};

export function shellHistoryPath({extraPaths = []} = {}) {
if (process.env.HISTFILE) {
return process.env.HISTFILE;
}
Expand All @@ -32,21 +29,22 @@ function getPath(options) {
path.join(homeDir, '.history')
]);

if (options.extraPaths) {
for (const path of options.extraPaths) {
paths.add(path);
}
for (const path of extraPaths) {
paths.add(path);
}

const filterdHistoryPath = () => {
let largestFile;
let size = 0;

for (const path of paths) {
if (fs.existsSync(path)) {
if (fs.statSync(path).size > size) {
size = fs.statSync(path).size;
largestFile = path;
}
if (!fs.existsSync(path)) {
continue;
}

if (fs.statSync(path).size > size) {
size = fs.statSync(path).size;
largestFile = path;
}
}

Expand All @@ -56,21 +54,16 @@ function getPath(options) {
return filterdHistoryPath();
}

module.exports = options => {
options = options || {};

export function shellHistory(options = {}) {
if (process.platform === 'win32') {
const historyPath = getPath(options);
const historyPath = shellHistoryPath(options);
if (historyPath) {
return parse(fs.readFileSync(historyPath, 'utf8'));
return parseShellHistory(fs.readFileSync(historyPath, 'utf8'));
}

const {stdout} = childProcess.spawnSync('doskey', ['/history'], {encoding: 'utf8'});
return stdout.trim().split('\r\n');
}

return parse(fs.readFileSync(getPath(options), 'utf8'));
};

module.exports.path = getPath;
module.exports.parse = parse;
return parseShellHistory(fs.readFileSync(shellHistoryPath(options), 'utf8'));
}
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
83 changes: 43 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
{
"name": "shell-history",
"version": "1.1.0",
"description": "Get the command history of the user's shell",
"license": "MIT",
"repository": "sindresorhus/shell-history",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=10"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"shell",
"sh",
"zsh",
"bash",
"cli",
"command-line",
"terminal",
"term",
"console",
"history",
"histfile",
"command",
"cmd",
"parse",
"path"
],
"devDependencies": {
"ava": "2.4.0",
"xo": "^0.33.1"
}
"name": "shell-history",
"version": "1.1.0",
"description": "Get the command history of the user's shell",
"license": "MIT",
"repository": "sindresorhus/shell-history",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"shell",
"sh",
"zsh",
"bash",
"cli",
"command-line",
"terminal",
"term",
"console",
"history",
"histfile",
"command",
"cmd",
"parse",
"path"
],
"devDependencies": {
"ava": "3.15.0",
"xo": "^0.38.2"
}
}
21 changes: 6 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@

> Get the command history of the user's [shell](https://en.wikipedia.org/wiki/Shell_(computing))

## Install

```
$ npm install --save shell-history
$ npm install shell-history
```


## Usage

```js
const shellHistory = require('shell-history');
import {shellHistory, shellHistoryPath} from 'shell-history';

console.log(shellHistory());
//=> ['ava', 'echo unicorn', 'node', 'npm test', ...]
//=> ['ava', 'echo unicorn', 'node', 'npm test', ]

console.log(shellHistory.path());
console.log(shellHistoryPath());
//=> '/Users/sindresorhus/.history'
```


## API

### shellHistory()
Expand All @@ -31,23 +28,17 @@ Get an array of commands.

On Windows, unless the `HISTFILE` environment variable is set, this will only return commands from the current session.

### shellHistory.path()
### shellHistoryPath()

Get the path of the file containing the shell history.

On Windows, this will return either the `HISTFILE` environment variable or `undefined`.

### shellHistory.parse(string)
### parseShellHistory(string)

Parse a shell history string into an array of commands.


## Related

- [shell-path](https://github.com/sindresorhus/shell-path) - Get the $PATH from the shell
- [shell-env](https://github.com/sindresorhus/shell-env) - Get environment variables from the shell


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
6 changes: 2 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import test from 'ava';
import shellHistory from '..';

process.chdir(__dirname);
import {shellHistory} from '../index.js';

test('main', t => {
t.true(shellHistory({extraPaths: ['./fixture']}).length > 0);
t.true(shellHistory({extraPaths: ['./test/fixture']}).length > 0);
});
4 changes: 2 additions & 2 deletions test_win.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
@echo off

echo Print entire history
node --print "const history = require('.'); history()"
node --print "import shellHistory from './index.js'; shellHistory()"
pause
echo.
echo Print last command (which will be "test_win.bat")
node --print "const history = require('.'); let h = history(); h[h.length - 1]"
node --print "import shellHistory from './index.js'; const history = shellHistory(); history[history.length - 1]"
pause

0 comments on commit 67fd834

Please sign in to comment.