Skip to content

Commit

Permalink
feat: external editor with aos
Browse files Browse the repository at this point in the history
  • Loading branch information
twilson63 committed Sep 15, 2024
1 parent 13c763b commit 4205fe2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
29 changes: 26 additions & 3 deletions src/commands/pad.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import os from 'node:os'
import path from 'node:path'
import { spawn } from 'node:child_process'
import fs from 'node:fs'
import crypto from 'node:crypto'

export function pad(pid, callback) {
const tempFilePath = path.join(os.homedir(), `.pad-${pid}.lua`);
let hash = null
try {
hash = getFileHash(tempFilePath)
} catch (e) {
hash = ""
}

const editor = process.env.EDITOR || (process.platform === 'win32' ? 'notepad' : 'vi');
const child = spawn(editor, [tempFilePath], {
Expand All @@ -13,9 +20,25 @@ export function pad(pid, callback) {
});

child.on('exit', (exitCode) => {
console.log('Exit Code', exitCode)
const editedContent = fs.readFileSync(tempFilePath, 'utf8');
callback(null, editedContent);
//console.log('Exit Code: ', exitCode)
if (exitCode == 0) {
const editedContent = fs.readFileSync(tempFilePath, 'utf8');
if (getFileHash(tempFilePath) !== hash) {
callback(null, editedContent);
} else {
callback(new Error('no changes'))
}
} else {
callback(new Error('exited'))
}
});
}


function getFileHash(filePath) {
const fileBuffer = fs.readFileSync(filePath);
const hashSum = crypto.createHash('sha256');
hashSum.update(fileBuffer);
return hashSum.digest('hex');
}

6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ if (!argv['watch']) {
pad(id, async (err, content) => {
if (!err) {
// console.log(content)
await doEvaluate(content, id, jwk, spinner, rl)
await doEvaluate(content, id, jwk, spinner, rl, loadedModules)
}
rl.resume();
rl.prompt(true);
Expand All @@ -383,7 +383,7 @@ if (!argv['watch']) {
if (process.env.DEBUG) console.time(chalk.gray('Elapsed'))
printLive()

await doEvaluate(line, id, jwk, spinner, rl)
await doEvaluate(line, id, jwk, spinner, rl, loadedModules)

if (process.env.DEBUG) {
console.timeEnd(chalk.gray('Elapsed'))
Expand Down Expand Up @@ -482,7 +482,7 @@ async function handleLoadArgs(jwk, id) {
}
}

async function doEvaluate(line, id, jwk, spinner, rl) {
async function doEvaluate(line, id, jwk, spinner, rl, loadedModules) {
spinner.start();
spinner.suffixText = chalk.gray("[Dispatching message...]")

Expand Down

0 comments on commit 4205fe2

Please sign in to comment.