UNPKG

2.84 kBPlain TextView Raw
1import {
2 assign
3} from 'lodash';
4import * as repl from 'repl';
5import rewrap from '../lib/utils/rewrap';
6import * as chalk from 'chalk';
7import { ui, Command, Project, unwrap } from 'denali-cli';
8
9/**
10 * Launch a REPL with your application loaded
11 *
12 * @package commands
13 */
14export default class ConsoleCommand extends Command {
15
16 /* tslint:disable:completed-docs typedef */
17 static commandName = 'console';
18 static description = 'Launch a REPL with your application loaded';
19 static longDescription = unwrap`
20 Starts a REPL (Read-Eval-Print Loop) with your application initialized and
21 loaded into memory. Type \`.help\` in the REPL environment for more details.`;
22
23 static flags = {
24 environment: {
25 description: 'The target environment to build for.',
26 default: process.env.NODE_ENV || 'development',
27 type: <any>'string'
28 },
29 printSlowTrees: {
30 description: 'Print out an analysis of the build process, showing the slowest nodes.',
31 default: false,
32 type: <any>'string'
33 }
34 };
35
36 static runsInApp = true;
37
38 async run(argv: any) {
39 ui.info(`Loading ${ argv.environment } environment. Type '.help' for details`);
40 let project = new Project({
41 environment: argv.environment,
42 printSlowTrees: argv.printSlowTrees,
43 buildDummy: true
44 });
45 let application = await project.createApplication();
46 if (application.environment === 'production') {
47 ui.warn(rewrap`WARNING: Your console is running in production environment, meaning your
48 production configuration is being used. This means your app is likely connecting to live,
49 production database. Use caution!`);
50 }
51
52 await application.runInitializers();
53
54 let consoleRepl = repl.start({
55 prompt: 'denali> ',
56 useGlobal: true
57 });
58
59 let context = {
60 application,
61 container: application.container,
62 modelFor(type: string) {
63 return application.container.lookup(`model:${ type }`);
64 }
65 };
66 assign(global, context);
67
68 consoleRepl.defineCommand('help', {
69 help: '',
70 action() {
71 // tslint:disable-next-line:no-console
72 console.log(rewrap`
73 Welcome to the Denali console!
74
75 This is a fully interactive REPL for your Denali app. That means normal JavaScript works
76 here. Your application is loaded (but not started) in the background, allowing you to
77 inspect the runtime state of your app.
78
79 The following variables are availabe:
80
81 * ${ chalk.underline('application') } - an instance of your Application class
82 * ${ chalk.underline('container') } - shortcut to application.container. Use this to
83 lookup the various classes associated with your app (i.e. actions, models, etc)
84 `);
85 this.displayPrompt();
86 }
87 });
88 }
89
90}