Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add executed commands to shell history #112

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/helpers/shell-history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import fs from 'fs';
import os from 'os';
import path from 'path';

// Function to get the history file based on the shell
function getHistoryFile(): string | null {
const shell = process.env.SHELL || '';
const homeDir = os.homedir();

switch (path.basename(shell)) {
case 'bash':
case 'sh':
return path.join(homeDir, '.bash_history');
case 'zsh':
return path.join(homeDir, '.zsh_history');
case 'fish':
return path.join(homeDir, '.local', 'share', 'fish', 'fish_history');
case 'ksh':
return path.join(homeDir, '.ksh_history');
case 'tcsh':
return path.join(homeDir, '.history');
default:
return null;
}
}

// Function to get the last command from the history file
function getLastCommand(historyFile: string): string | null {
try {
const data = fs.readFileSync(historyFile, 'utf8');
const commands = data.trim().split('\n');
return commands[commands.length - 1];
} catch (err) {
// Ignore any errors
return null;
}
}

// Function to append the command to the history file if it's not the same as the last command
export function appendToShellHistory(command: string): void {
const historyFile = getHistoryFile();
if (historyFile) {
const lastCommand = getLastCommand(historyFile);
if (lastCommand !== command) {
fs.appendFile(historyFile, `${command}\n`, (err) => {
Copy link
Contributor

@steve8708 steve8708 Jul 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the newline come before the new command instead of after?

Suggested change
fs.appendFile(historyFile, `${command}\n`, (err) => {
fs.appendFile(historyFile, `\n${command}`, (err) => {

or is the idea that these files already always end in a newline (and if so are we 100% confident that this is the case all the time for all of the supported shells, or perhaps should we at least check, like

let fileContents = await readFile(historyFile)
if (!fileContents.endsWith('\n')) {
  // ensure we add the newline
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, but I added a newline at the end of the string because that's a POSIX rule. All lines in text files should end with a newline character, including the last line of plain text files. We can expect a newline at the end of all history files.

However, I am not opposed to checking it first.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah i see, works for me!

if (err) {
// Ignore any errors
}
});
}
}
}
14 changes: 9 additions & 5 deletions src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { projectName } from './helpers/constants';
import { KnownError } from './helpers/error';
import clipboardy from 'clipboardy';
import i18n from './helpers/i18n';
import { appendToShellHistory } from './helpers/shell-history';

const init = async () => {
try {
Expand Down Expand Up @@ -38,12 +39,15 @@ const sample = <T>(arr: T[]): T | undefined => {
async function runScript(script: string) {
p.outro(`${i18n.t('Running')}: ${script}`);
console.log('');
await execaCommand(script, {
stdio: 'inherit',
shell: process.env.SHELL || true,
}).catch(() => {
try {
await execaCommand(script, {
stdio: 'inherit',
shell: process.env.SHELL || true,
});
appendToShellHistory(script);
} catch (error) {
// Nothing needed, it'll output to stderr
});
}
}

async function getPrompt(prompt?: string) {
Expand Down
Loading