-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbin.js
86 lines (75 loc) · 1.99 KB
/
bin.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
const repl = require('repl')
const util = require('util')
const chrome = require('.')
const fs = require('fs')
const path = require('path')
let instance
function toJSON (object) {
return JSON.stringify(object, null, 2)
}
function display (object) {
return util.inspect(object, {
'colors': process.stdout.isTTY,
'depth': null
})
}
const chromeStart = function () {
instance = chrome()
instance.then(function () {
for (let func of Object.getOwnPropertyNames(chrome.Automator.prototype)) {
if (func !== 'constructor' && func.charAt(0) !== '_') {
cdpRepl.context[func] = function () {
const i = chrome()
i[func].apply(i, arguments).pipe((res) => {
if (res !== undefined) {
if (typeof res === 'object') {
res = toJSON(res)
}
console.log('\x1b[2K\x1b[G%s', res)
cdpRepl.displayPrompt(true)
}
})
}
}
}
})
}
const cdpRepl = repl.start({
prompt: '\x1b[32m.\x1b[0m',
ignoreUndefined: true,
writer: display
})
cdpRepl.context.chrome = chromeStart
const homePath = require('os').homedir()
const historyFile = path.join(homePath, '.ca_history')
const historySize = 10000
function loadHistory () {
// attempt to open the history file
let fd
try {
fd = fs.openSync(historyFile, 'r')
} catch (err) {
return // no history file present
}
// populate the REPL history
fs.readFileSync(fd, 'utf8')
.split('\n')
.filter(function (entry) {
return entry.trim()
})
.reverse() // to be compatible with repl.history files
.forEach(function (entry) {
cdpRepl.history.push(entry)
})
}
function saveHistory () {
// only store the last chunk
const entries = cdpRepl.history.slice(0, historySize).reverse().join('\n')
fs.writeFileSync(historyFile, entries + '\n')
}
loadHistory()
// disconnect on exit
cdpRepl.on('exit', function () {
saveHistory()
})