From 1e59ca3bace6e9fbe53b1c9354cecb7604ce285b Mon Sep 17 00:00:00 2001 From: BRAINS YUM <43896676+McFlat@users.noreply.github.com> Date: Sun, 14 Oct 2018 12:48:08 -0500 Subject: add REPL in server/tools/repl.ts (#1248) --- server/tools/repl.ts | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 server/tools/repl.ts (limited to 'server/tools') 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 @@ +import * as repl from 'repl' +import * as path from 'path' +import * as _ from 'lodash' +import * as uuidv1 from 'uuid/v1' +import * as uuidv3 from 'uuid/v3' +import * as uuidv4 from 'uuid/v4' +import * as uuidv5 from 'uuid/v5' +import * as Sequelize from 'sequelize' +import * as YoutubeDL from 'youtube-dl' + +import { initDatabaseModels, sequelizeTypescript } from '../initializers' +import * as cli from '../tools/cli' +import { logger } from '../helpers/logger' +import * as constants from '../initializers/constants' +import * as modelsUtils from '../models/utils' +import * as coreUtils from '../helpers/core-utils' +import * as ffmpegUtils from '../helpers/ffmpeg-utils' +import * as peertubeCryptoUtils from '../helpers/peertube-crypto' +import * as signupUtils from '../helpers/signup' +import * as utils from '../helpers/utils' +import * as YoutubeDLUtils from '../helpers/youtube-dl' + +let versionCommitHash + +const start = async () => { + await initDatabaseModels(true) + + await utils.getVersion().then((data) => { + versionCommitHash = data + }) + + const initContext = (replServer) => { + return (context) => { + const properties = { + context, repl: replServer, env: process.env, + lodash: _, path, + uuidv1, uuidv3, uuidv4, uuidv5, + cli, logger, constants, + Sequelize, sequelizeTypescript, modelsUtils, + models: sequelizeTypescript.models, transaction: sequelizeTypescript.transaction, + query: sequelizeTypescript.query, queryInterface: sequelizeTypescript.getQueryInterface(), + YoutubeDL, + coreUtils, ffmpegUtils, peertubeCryptoUtils, signupUtils, utils, YoutubeDLUtils + } + + for (let prop in properties) { + Object.defineProperty(context, prop, { + configurable: false, + enumerable: true, + value: properties[prop] + }) + } + } + } + + const replServer = repl.start({ + prompt: `PeerTube [${cli.version}] (${versionCommitHash})> ` + }) + + initContext(replServer)(replServer.context) + replServer.on('reset', initContext(replServer)) + + const resetCommand = { + help: 'Reset REPL', + action () { + this.write('.clear\n') + this.displayPrompt() + } + } + replServer.defineCommand('reset', resetCommand) + replServer.defineCommand('r', resetCommand) + +} + +start().then((data) => { + // do nothing +}).catch((err) => { + console.error(err) +}) -- cgit v1.2.3