This repository was archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (42 loc) · 1.39 KB
/
index.js
File metadata and controls
50 lines (42 loc) · 1.39 KB
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
const child_process = require('child_process')
const os = require('os')
const through = require('through')
const debounce = require('lodash.debounce')
const { platform } = require('./utils/platform')
const { open, defaultConfig } = require('./utils/config')
const args = process.argv
const openTab = (cmd, option = {}, cbOrConfig = defaultConfig) => {
let child
if (typeof cbOrConfig === 'object' && cbOrConfig !== null) {
cbOrConfig = Object.assign({}, defaultConfig, cbOrConfig)
} else if (typeof cbOrConfig === 'function') {
cbOrConfig = Object.assign({}, defaultConfig, {
onStdout: cbOrConfig
});
}
child = child_process.exec(open[platform](cmd), option, (error, stdout, stderr) => {
if (error) {
cbOrConfig.onError(error)
return
}
cbOrConfig.onStdout(stdout)
cbOrConfig.onStderr(stderr)
});
child.on('close', (code, signal) => {
// The 'close' event is emitted when the stdio streams of a child process have been closed. This is distinct from the 'exit' event, since multiple processes might share the same stdio streams.
cbOrConfig.onExit(code, signal)
})
}
process.stdin.setEncoding('utf8')
process.stdin.pipe(through(buf => {
openTab(buf.toString())
process.exit(0)
}, () => {})
);
if (args.length > 2) {
openTab(args.slice(2).join(' '))
process.exit(0)
}
module.exports = {
open: debounce(openTab, 100, {leading: true})
}