diff --git a/.editorconfig b/.editorconfig index 3f816aa..1c6314a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,4 +9,4 @@ insert_final_newline = true [*.yml] indent_style = space -indent_size = 2 \ No newline at end of file +indent_size = 2 diff --git a/.gitattributes b/.gitattributes index 94f480d..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -* text=auto eol=lf \ No newline at end of file +* text=auto eol=lf diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c1870cf..41fe626 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,7 +12,6 @@ jobs: node-version: - 14 - 12 - - 10 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 diff --git a/.gitignore b/.gitignore index 97008e5..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ node_modules -yarn.lock \ No newline at end of file +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/index.js b/index.js index ab20740..4661872 100644 --- a/index.js +++ b/index.js @@ -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; } @@ -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; } } @@ -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')); +} diff --git a/license b/license index 39ce9a5..fa7ceba 100644 --- a/license +++ b/license @@ -1,21 +1,9 @@ -The MIT License (MIT) +MIT License Copyright (c) Sindre Sorhus (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. diff --git a/package.json b/package.json index 3e84e5d..a6339c3 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/readme.md b/readme.md index 09b3c05..8135c47 100644 --- a/readme.md +++ b/readme.md @@ -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() @@ -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) diff --git a/test/test.js b/test/test.js index f6db9b1..2ba57b5 100644 --- a/test/test.js +++ b/test/test.js @@ -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); }); diff --git a/test_win.bat b/test_win.bat index c3696a5..3e4ee02 100644 --- a/test_win.bat +++ b/test_win.bat @@ -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