Skip to content

Commit

Permalink
Merge pull request #336 from permaweb/jeremiahstockdale-aos-329
Browse files Browse the repository at this point in the history
feat(aos): save input history after exiting a session #329
  • Loading branch information
Jeremiahstockdale authored Sep 2, 2024
2 parents aecfacc + 9f2442e commit abd3c9f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import chalk from 'chalk'
import path from 'path'
import { errors } from './errors.js'
import * as url from 'url'
import process from 'node:process';

import { of, fromPromise, Rejected, Resolved } from 'hyper-async'

Expand Down Expand Up @@ -37,6 +38,7 @@ import { help, replHelp } from './services/help.js'
import { list } from './services/list.js'
import { patch } from './commands/patch.js'
import * as os from './commands/os.js'
import { readHistory, writeHistory } from './services/history-service.js'

const argv = minimist(process.argv.slice(2))

Expand Down Expand Up @@ -83,7 +85,6 @@ if (argv['module'] && argv['module'].length === 43) {
}

let cron = null
let history = []

if (argv['watch'] && argv['watch'].length === 43) {
live(argv['watch'], true).then(res => {
Expand Down Expand Up @@ -128,6 +129,8 @@ if (!argv['watch']) {
let editorData = ""
let editorPrompt = ""

let history = readHistory(id)

if (luaData.length > 0 && argv['load']) {
const spinner = ora({
spinner: 'dots',
Expand Down Expand Up @@ -427,6 +430,15 @@ if (!argv['watch']) {
rl.prompt(true)
return
})

process.on('SIGINT', function () {
// save the input history when the user exits
if (id) {
writeHistory(id, history)
}
process.exit(0)
})

//}

//repl()
Expand Down
26 changes: 26 additions & 0 deletions src/services/history-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from 'fs';
import path from 'path';
import os from 'os';

const historyFilePath = (processId) => {
return path.join(os.homedir(), `.${processId}.history`);
};

export const readHistory = (processId) => {
const filePath = historyFilePath(processId);
if (fs.existsSync(filePath)) {
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
}
return [];
};

export const writeHistory = (processId, history) => {
const filePath = historyFilePath(processId);

try {
const historyToSave = history.slice(-100); // Only save the last 100 commands
fs.writeFileSync(filePath, JSON.stringify(historyToSave, null, 2));
} catch (err) {
console.error('Error writing history file:', err);
}
};

0 comments on commit abd3c9f

Please sign in to comment.