diff options
author | BRAINS YUM <43896676+McFlat@users.noreply.github.com> | 2018-10-14 12:48:08 -0500 |
---|---|---|
committer | Rigel Kent <par@rigelk.eu> | 2018-10-14 19:48:08 +0200 |
commit | 1e59ca3bace6e9fbe53b1c9354cecb7604ce285b (patch) | |
tree | 52ae3907642c39783e7622972ed20c85f8926293 | |
parent | 6f2ae7a1aa1b4a8b5605fc4c57c0c1d1fbe2a16e (diff) | |
download | PeerTube-1e59ca3bace6e9fbe53b1c9354cecb7604ce285b.tar.gz PeerTube-1e59ca3bace6e9fbe53b1c9354cecb7604ce285b.tar.zst PeerTube-1e59ca3bace6e9fbe53b1c9354cecb7604ce285b.zip |
add REPL in server/tools/repl.ts (#1248)
-rw-r--r-- | server/tools/repl.ts | 79 | ||||
-rw-r--r-- | support/doc/tools.md | 131 |
2 files changed, 210 insertions, 0 deletions
diff --git a/server/tools/repl.ts b/server/tools/repl.ts new file mode 100644 index 000000000..6800ff8ab --- /dev/null +++ b/server/tools/repl.ts | |||
@@ -0,0 +1,79 @@ | |||
1 | import * as repl from 'repl' | ||
2 | import * as path from 'path' | ||
3 | import * as _ from 'lodash' | ||
4 | import * as uuidv1 from 'uuid/v1' | ||
5 | import * as uuidv3 from 'uuid/v3' | ||
6 | import * as uuidv4 from 'uuid/v4' | ||
7 | import * as uuidv5 from 'uuid/v5' | ||
8 | import * as Sequelize from 'sequelize' | ||
9 | import * as YoutubeDL from 'youtube-dl' | ||
10 | |||
11 | import { initDatabaseModels, sequelizeTypescript } from '../initializers' | ||
12 | import * as cli from '../tools/cli' | ||
13 | import { logger } from '../helpers/logger' | ||
14 | import * as constants from '../initializers/constants' | ||
15 | import * as modelsUtils from '../models/utils' | ||
16 | import * as coreUtils from '../helpers/core-utils' | ||
17 | import * as ffmpegUtils from '../helpers/ffmpeg-utils' | ||
18 | import * as peertubeCryptoUtils from '../helpers/peertube-crypto' | ||
19 | import * as signupUtils from '../helpers/signup' | ||
20 | import * as utils from '../helpers/utils' | ||
21 | import * as YoutubeDLUtils from '../helpers/youtube-dl' | ||
22 | |||
23 | let versionCommitHash | ||
24 | |||
25 | const start = async () => { | ||
26 | await initDatabaseModels(true) | ||
27 | |||
28 | await utils.getVersion().then((data) => { | ||
29 | versionCommitHash = data | ||
30 | }) | ||
31 | |||
32 | const initContext = (replServer) => { | ||
33 | return (context) => { | ||
34 | const properties = { | ||
35 | context, repl: replServer, env: process.env, | ||
36 | lodash: _, path, | ||
37 | uuidv1, uuidv3, uuidv4, uuidv5, | ||
38 | cli, logger, constants, | ||
39 | Sequelize, sequelizeTypescript, modelsUtils, | ||
40 | models: sequelizeTypescript.models, transaction: sequelizeTypescript.transaction, | ||
41 | query: sequelizeTypescript.query, queryInterface: sequelizeTypescript.getQueryInterface(), | ||
42 | YoutubeDL, | ||
43 | coreUtils, ffmpegUtils, peertubeCryptoUtils, signupUtils, utils, YoutubeDLUtils | ||
44 | } | ||
45 | |||
46 | for (let prop in properties) { | ||
47 | Object.defineProperty(context, prop, { | ||
48 | configurable: false, | ||
49 | enumerable: true, | ||
50 | value: properties[prop] | ||
51 | }) | ||
52 | } | ||
53 | } | ||
54 | } | ||
55 | |||
56 | const replServer = repl.start({ | ||
57 | prompt: `PeerTube [${cli.version}] (${versionCommitHash})> ` | ||
58 | }) | ||
59 | |||
60 | initContext(replServer)(replServer.context) | ||
61 | replServer.on('reset', initContext(replServer)) | ||
62 | |||
63 | const resetCommand = { | ||
64 | help: 'Reset REPL', | ||
65 | action () { | ||
66 | this.write('.clear\n') | ||
67 | this.displayPrompt() | ||
68 | } | ||
69 | } | ||
70 | replServer.defineCommand('reset', resetCommand) | ||
71 | replServer.defineCommand('r', resetCommand) | ||
72 | |||
73 | } | ||
74 | |||
75 | start().then((data) => { | ||
76 | // do nothing | ||
77 | }).catch((err) => { | ||
78 | console.error(err) | ||
79 | }) | ||
diff --git a/support/doc/tools.md b/support/doc/tools.md index 8efb0c13d..0df8c9f6c 100644 --- a/support/doc/tools.md +++ b/support/doc/tools.md | |||
@@ -206,3 +206,134 @@ To fix this, you have to run: | |||
206 | ``` | 206 | ``` |
207 | $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run update-host | 207 | $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run update-host |
208 | ``` | 208 | ``` |
209 | |||
210 | ### REPL ([Read Eval Print Loop](https://nodejs.org/docs/latest-v8.x/api/repl.html)) | ||
211 | |||
212 | If you want to interact with the application libraries and objects, there is a REPL for that. | ||
213 | |||
214 | usage: `node ./dist/server/tools/repl.js` | ||
215 | |||
216 | "The default evaluator will, by default, assign the result of the most recently evaluated expression to the special variable `_` (underscore). Explicitly setting `_` to a value will disable this behavior." | ||
217 | |||
218 | - type `.help` to list commands available in the repl, notice it starts with a dot | ||
219 | - type `.exit` to exit, note that you still have to press CTRL-C to actually exit, or press CTRL-C (3 times) without typing `.exit` to exit | ||
220 | - type `context` to list all available objects and libraries in the context, note: `Promise` is also available but it's not listed in the context, in case you need promises for something | ||
221 | - type `env` to see the loaded environment variables | ||
222 | - type `path` to access path library | ||
223 | - type `lodash` to access lodash library | ||
224 | - type `uuidv1` to access uuid/v1 library | ||
225 | - type `uuidv3` to access uuid/v3 library | ||
226 | - type `uuidv4` to access uuid/v4 library | ||
227 | - type `uuidv5` to access uuid/v5 library | ||
228 | - type `YoutubeDL` to access youtube-dl library | ||
229 | - type `cli` to access the cli helpers object | ||
230 | - type `logger` to access the logger; if you log to it, it will write to stdout and to the peertube.log file | ||
231 | - type `constants` to access the constants loaded by the server | ||
232 | - type `coreUtils` to access the core-utils helpers object | ||
233 | - type `ffmpegUtils` to access the ffmpeg-utils helpers object | ||
234 | - type `peertubeCryptoUtils` to access the peertube-crypto helpers object | ||
235 | - type `signupUtils` to access the signup helpers object | ||
236 | - type `utils` to access the utils helpers object | ||
237 | - type `YoutubeDLUtils` to access the youtube-dl helpers object | ||
238 | - type `sequelizeTypescript` to access sequelizeTypescript | ||
239 | - type `modelsUtils` to access the models/utils | ||
240 | - type `models` to access the shortcut to sequelizeTypescript.models | ||
241 | - type `transaction` to access the shortcut to sequelizeTypescript.transaction | ||
242 | - type `query` to access the shortcut to sequelizeTypescript.query | ||
243 | - type `queryInterface` to access the shortcut to sequelizeTypescript.queryInterface | ||
244 | |||
245 | #### .help | ||
246 | |||
247 | ``` | ||
248 | PeerTube [1.0.0] (b10eb595)> .help | ||
249 | .break Sometimes you get stuck, this gets you out | ||
250 | .clear Break, and also clear the local context | ||
251 | .editor Enter editor mode | ||
252 | .exit Exit the repl | ||
253 | .help Print this help message | ||
254 | .load Load JS from a file into the REPL session | ||
255 | .r Reset REPL | ||
256 | .reset Reset REPL | ||
257 | .save Save all evaluated commands in this REPL session to a file | ||
258 | PeerTube [1.0.0] (b10eb595)> | ||
259 | ``` | ||
260 | |||
261 | #### Lodash example | ||
262 | |||
263 | ``` | ||
264 | PeerTube [1.0.0] (b10eb595)> lodash.keys(context) | ||
265 | [ 'global', | ||
266 | 'console', | ||
267 | 'DTRACE_NET_SERVER_CONNECTION', | ||
268 | 'DTRACE_NET_STREAM_END', | ||
269 | 'DTRACE_HTTP_SERVER_REQUEST', | ||
270 | 'DTRACE_HTTP_SERVER_RESPONSE', | ||
271 | 'DTRACE_HTTP_CLIENT_REQUEST', | ||
272 | 'DTRACE_HTTP_CLIENT_RESPONSE', | ||
273 | 'process', | ||
274 | 'Buffer', | ||
275 | 'clearImmediate', | ||
276 | 'clearInterval', | ||
277 | 'clearTimeout', | ||
278 | 'setImmediate', | ||
279 | 'setInterval', | ||
280 | 'setTimeout', | ||
281 | 'XMLHttpRequest', | ||
282 | 'compact2string', | ||
283 | 'module', | ||
284 | 'require', | ||
285 | 'path', | ||
286 | 'repl', | ||
287 | 'context', | ||
288 | 'env', | ||
289 | 'lodash', | ||
290 | 'uuidv1', | ||
291 | 'uuidv3', | ||
292 | 'uuidv4', | ||
293 | 'uuidv5', | ||
294 | 'cli', | ||
295 | 'logger', | ||
296 | 'constants', | ||
297 | 'Sequelize', | ||
298 | 'sequelizeTypescript', | ||
299 | 'modelsUtils', | ||
300 | 'models', | ||
301 | 'transaction', | ||
302 | 'query', | ||
303 | 'queryInterface', | ||
304 | 'YoutubeDL', | ||
305 | 'coreUtils', | ||
306 | 'ffmpegUtils', | ||
307 | 'peertubeCryptoUtils', | ||
308 | 'signupUtils', | ||
309 | 'utils', | ||
310 | 'YoutubeDLUtils' ] | ||
311 | PeerTube [1.0.0] (b10eb595)> | ||
312 | ``` | ||
313 | |||
314 | #### YoutubeDL example | ||
315 | ``` | ||
316 | YoutubeDL.getInfo('https://www.youtube.com/watch?v=I5ZN289jjDo', function(err, data) {console.log(err, data)}) | ||
317 | ``` | ||
318 | |||
319 | #### Models examples | ||
320 | ``` | ||
321 | PeerTube [1.0.0] (b10eb595)> new models.ActorModel({id: 3}).getVideoChannel().then(function(data){console.log(data.dataValues.name)}) | ||
322 | Promise { | ||
323 | _bitField: 0, | ||
324 | _fulfillmentHandler0: undefined, | ||
325 | _rejectionHandler0: undefined, | ||
326 | _promise0: undefined, | ||
327 | _receiver0: undefined } | ||
328 | PeerTube [1.0.0] (b10eb595)> Main root channel | ||
329 | PeerTube [1.0.0] (b10eb595)> let out; new models.UserModel({id: 1}).getAccount().then(function (data) {out = data.dataValues.id}) | ||
330 | Promise { | ||
331 | _bitField: 0, | ||
332 | _fulfillmentHandler0: undefined, | ||
333 | _rejectionHandler0: undefined, | ||
334 | _promise0: undefined, | ||
335 | _receiver0: undefined } | ||
336 | PeerTube [1.0.0] (b10eb595)> out | ||
337 | 2 | ||
338 | PeerTube [1.0.0] (b10eb595)> | ||
339 | ``` | ||