This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathindex.js
115 lines (102 loc) · 2.31 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict'
/*
* adonis-cli
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const fs = require('fs')
const os = require('os')
const path = require('path')
const { Command } = require('../../../lib/ace')
const historyFile = path.join(os.homedir(), '/.adonis_repl_history')
/**
* Start the repl server session
*
* @class Repl
* @constructor
*/
class Repl extends Command {
/**
* The command signature used by ace
*
* @method signature
*
* @return {String}
*/
static get signature () {
return 'repl'
}
/**
* The command description used by ace
*
* @method description
*
* @return {String}
*/
static get description () {
return 'Start a new repl session'
}
/**
* Reads the history file
*
* @param {Object} repl
*
* @private
*/
_readHistoryFile (repl) {
try {
fs.statSync(historyFile)
} catch (error) {
fs.closeSync(fs.openSync(historyFile, 'w'))
}
repl.rli.history = fs.readFileSync(historyFile, 'utf-8').split('\n').reverse()
repl.rli.history.shift()
repl.rli.historyIndex = -1
}
/**
* Save the history to the history file.
*
* @param {Object} repl
*
* @private
*/
_addHistorySaveListener (repl) {
const fd = fs.openSync(historyFile, 'a')
repl.rli.addListener('line', (code) => {
if (code && code !== '.history') {
fs.write(fd, `${code}\n`, (error) => { if (error) console.log(error) })
return
}
repl.rli.historyIndex++
repl.rli.history.pop()
})
process.on('exit', function () {
fs.closeSync(fd)
})
}
/**
* Method executed by ace to start the command line
* repl
*
* @method handle
*
* @return {void}
*/
async handle () {
const awaitOutside = require('adonis-await-outside')
const server = require('repl').start()
if (typeof (global.use) === 'undefined') {
this.info('You are running repl outside of Adonisjs app')
} else {
server.context.use = global.use
server.context.make = global.make
}
this._readHistoryFile(server)
this._addHistorySaveListener(server)
awaitOutside.addAwaitOutsideToReplServer(server)
}
}
module.exports = Repl