├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.js ├── license ├── package.json ├── readme.md ├── test ├── fixture └── test.js └── test_win.bat /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions/setup-node@v1 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import os from 'os'; 2 | import fs from 'fs'; 3 | import path from 'path'; 4 | import childProcess from 'child_process'; 5 | 6 | export function parseShellHistory(string) { 7 | const reBashHistory = /^: \d+:0;/; 8 | 9 | return string.trim().split('\n').map(line => { 10 | if (reBashHistory.test(line)) { 11 | return line.split(';').slice(1).join(';'); 12 | } 13 | 14 | // ZSH just places one command on each line 15 | return line; 16 | }); 17 | } 18 | 19 | export function shellHistoryPath({extraPaths = []} = {}) { 20 | if (process.env.HISTFILE) { 21 | return process.env.HISTFILE; 22 | } 23 | 24 | const homeDir = os.homedir(); 25 | 26 | const paths = new Set([ 27 | path.join(homeDir, '.bash_history'), 28 | path.join(homeDir, '.zsh_history'), 29 | path.join(homeDir, '.history') 30 | ]); 31 | 32 | for (const path of extraPaths) { 33 | paths.add(path); 34 | } 35 | 36 | const filterdHistoryPath = () => { 37 | let largestFile; 38 | let size = 0; 39 | 40 | for (const path of paths) { 41 | if (!fs.existsSync(path)) { 42 | continue; 43 | } 44 | 45 | if (fs.statSync(path).size > size) { 46 | size = fs.statSync(path).size; 47 | largestFile = path; 48 | } 49 | } 50 | 51 | return largestFile; 52 | }; 53 | 54 | return filterdHistoryPath(); 55 | } 56 | 57 | export function shellHistory(options = {}) { 58 | if (process.platform === 'win32') { 59 | const historyPath = shellHistoryPath(options); 60 | if (historyPath) { 61 | return parseShellHistory(fs.readFileSync(historyPath, 'utf8')); 62 | } 63 | 64 | const {stdout} = childProcess.spawnSync('doskey', ['/history'], {encoding: 'utf8'}); 65 | return stdout.trim().split('\r\n'); 66 | } 67 | 68 | return parseShellHistory(fs.readFileSync(shellHistoryPath(options), 'utf8')); 69 | } 70 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shell-history", 3 | "version": "2.0.0", 4 | "description": "Get the command history of the user's shell", 5 | "license": "MIT", 6 | "repository": "sindresorhus/shell-history", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=12" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava" 20 | }, 21 | "files": [ 22 | "index.js" 23 | ], 24 | "keywords": [ 25 | "shell", 26 | "sh", 27 | "zsh", 28 | "bash", 29 | "cli", 30 | "command-line", 31 | "terminal", 32 | "term", 33 | "console", 34 | "history", 35 | "histfile", 36 | "command", 37 | "cmd", 38 | "parse", 39 | "path" 40 | ], 41 | "devDependencies": { 42 | "ava": "3.15.0", 43 | "xo": "^0.38.2" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # shell-history 2 | 3 | > Get the command history of the user's [shell](https://en.wikipedia.org/wiki/Shell_(computing)) 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install shell-history 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import {shellHistory, shellHistoryPath} from 'shell-history'; 15 | 16 | console.log(shellHistory()); 17 | //=> ['ava', 'echo unicorn', 'node', 'npm test', …] 18 | 19 | console.log(shellHistoryPath()); 20 | //=> '/Users/sindresorhus/.history' 21 | ``` 22 | 23 | ## API 24 | 25 | ### shellHistory() 26 | 27 | Get an array of commands. 28 | 29 | On Windows, unless the `HISTFILE` environment variable is set, this will only return commands from the current session. 30 | 31 | ### shellHistoryPath() 32 | 33 | Get the path of the file containing the shell history. 34 | 35 | On Windows, this will return either the `HISTFILE` environment variable or `undefined`. 36 | 37 | ### parseShellHistory(string) 38 | 39 | Parse a shell history string into an array of commands. 40 | 41 | ## Related 42 | 43 | - [shell-path](https://github.com/sindresorhus/shell-path) - Get the $PATH from the shell 44 | - [shell-env](https://github.com/sindresorhus/shell-env) - Get environment variables from the shell 45 | -------------------------------------------------------------------------------- /test/fixture: -------------------------------------------------------------------------------- 1 | foo 2 | : 1453941685:0;gt 3 | bar 4 | : 1453941901:0;nt 5 | : 1453942304:0;o 6 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import {shellHistory} from '../index.js'; 3 | 4 | test('main', t => { 5 | t.true(shellHistory({extraPaths: ['./test/fixture']}).length > 0); 6 | }); 7 | -------------------------------------------------------------------------------- /test_win.bat: -------------------------------------------------------------------------------- 1 | :: Confirms that the batch file works correctly in Windows Terminal / Command Prompt. 2 | :: This batch file is used to visually test to see if `shell-history` works, rather 3 | :: than to act as a real, responsive "test case" (as such, `ava` is not used here). 4 | 5 | @echo off 6 | 7 | echo Print entire history 8 | node --print "import shellHistory from './index.js'; shellHistory()" 9 | pause 10 | echo. 11 | echo Print last command (which will be "test_win.bat") 12 | node --print "import shellHistory from './index.js'; const history = shellHistory(); history[history.length - 1]" 13 | pause 14 | --------------------------------------------------------------------------------