-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.ts
52 lines (45 loc) · 1.38 KB
/
edit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as tmp from 'tmp'
import * as execa from 'execa'
import * as _ from 'lodash'
const debug = require('debug')('edit-string')
function tmpFile (opts: tmp.Options): Promise<{name: string, fd: number, cleanup: () => void}> {
return new Promise((resolve, reject) => {
tmp.file(opts, (err, name, fd, cleanup) => {
if (err) return reject(err)
resolve({name, fd, cleanup})
})
})
}
async function edit (name: string, editor: string) {
debug(editor, [name])
let msg = `Waiting for ${editor}... `
process.stderr.write(msg)
await execa(editor, [name], {stdio: 'inherit'})
process.stderr.write(`\r${msg.replace(/./g, ' ')}\r`)
}
module.exports = async function (input: string, options = {}): Promise<string> {
const {promisify} = require('util')
const FS = require('fs')
const fs = {
write: promisify(FS.write),
readFile: promisify(FS.readFile)
}
const {name, fd, cleanup} = await tmpFile(options)
await fs.write(fd, input)
const editors = _.compact([process.env.VISUAL || process.env.EDITOR, 'pico', 'nano', 'vi'])
for (let editor of editors) {
try {
await edit(name, editor)
let output = await fs.readFile(name, 'utf8')
cleanup()
return output
} catch (err) {
if (err.code !== 'ENOENT') {
console.error(err)
continue
}
throw err
}
}
throw new Error('No $VISUAL or $EDITOR set')
}