-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #336 from permaweb/jeremiahstockdale-aos-329
feat(aos): save input history after exiting a session #329
- Loading branch information
Showing
2 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |