UNPKG

5.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ReplContext = void 0;
4const common_1 = require("@nestjs/common");
5const application_config_1 = require("../application-config");
6const injector_1 = require("../injector");
7const internal_core_module_1 = require("../injector/internal-core-module/internal-core-module");
8const native_functions_1 = require("./native-functions");
9class ReplContext {
10 constructor(app, nativeFunctionsClassRefs) {
11 this.app = app;
12 this.logger = new common_1.Logger(ReplContext.name);
13 this.debugRegistry = {};
14 this.globalScope = Object.create(null);
15 this.nativeFunctions = new Map();
16 this.container = app.container; // Using `any` because `app.container` is not public.
17 this.initializeContext();
18 this.initializeNativeFunctions(nativeFunctionsClassRefs || []);
19 }
20 writeToStdout(text) {
21 process.stdout.write(text);
22 }
23 initializeContext() {
24 const modules = this.container.getModules();
25 modules.forEach(moduleRef => {
26 let moduleName = moduleRef.metatype.name;
27 if (moduleName === internal_core_module_1.InternalCoreModule.name) {
28 return;
29 }
30 if (this.globalScope[moduleName]) {
31 moduleName += ` (${moduleRef.token})`;
32 }
33 this.introspectCollection(moduleRef, moduleName, 'providers');
34 this.introspectCollection(moduleRef, moduleName, 'controllers');
35 // For in REPL auto-complete functionality
36 Object.defineProperty(this.globalScope, moduleName, {
37 value: moduleRef.metatype,
38 configurable: false,
39 enumerable: true,
40 });
41 });
42 }
43 introspectCollection(moduleRef, moduleKey, collection) {
44 var _a;
45 const moduleDebugEntry = {};
46 moduleRef[collection].forEach(({ token }) => {
47 const stringifiedToken = this.stringifyToken(token);
48 if (stringifiedToken === application_config_1.ApplicationConfig.name ||
49 stringifiedToken === moduleRef.metatype.name) {
50 return;
51 }
52 if (!this.globalScope[stringifiedToken]) {
53 // For in REPL auto-complete functionality
54 Object.defineProperty(this.globalScope, stringifiedToken, {
55 value: token,
56 configurable: false,
57 enumerable: true,
58 });
59 }
60 if (stringifiedToken === injector_1.ModuleRef.name) {
61 return;
62 }
63 moduleDebugEntry[stringifiedToken] = token;
64 });
65 this.debugRegistry[moduleKey] = Object.assign(Object.assign({}, (_a = this.debugRegistry) === null || _a === void 0 ? void 0 : _a[moduleKey]), { [collection]: moduleDebugEntry });
66 }
67 stringifyToken(token) {
68 return typeof token !== 'string'
69 ? typeof token === 'function'
70 ? token.name
71 : token === null || token === void 0 ? void 0 : token.toString()
72 : `"${token}"`;
73 }
74 addNativeFunction(NativeFunctionRef) {
75 var _a;
76 const nativeFunction = new NativeFunctionRef(this);
77 const nativeFunctions = [nativeFunction];
78 this.nativeFunctions.set(nativeFunction.fnDefinition.name, nativeFunction);
79 (_a = nativeFunction.fnDefinition.aliases) === null || _a === void 0 ? void 0 : _a.forEach(aliaseName => {
80 const aliasNativeFunction = Object.create(nativeFunction);
81 aliasNativeFunction.fnDefinition = {
82 name: aliaseName,
83 description: aliasNativeFunction.fnDefinition.description,
84 signature: aliasNativeFunction.fnDefinition.signature,
85 };
86 this.nativeFunctions.set(aliaseName, aliasNativeFunction);
87 nativeFunctions.push(aliasNativeFunction);
88 });
89 return nativeFunctions;
90 }
91 registerFunctionIntoGlobalScope(nativeFunction) {
92 // Bind the method to REPL's context:
93 this.globalScope[nativeFunction.fnDefinition.name] =
94 nativeFunction.action.bind(nativeFunction);
95 // Load the help trigger as a `help` getter on each native function:
96 const functionBoundRef = this.globalScope[nativeFunction.fnDefinition.name];
97 Object.defineProperty(functionBoundRef, 'help', {
98 enumerable: false,
99 configurable: false,
100 get: () =>
101 // Dynamically builds the help message as will unlikely to be called
102 // several times.
103 this.writeToStdout(nativeFunction.makeHelpMessage()),
104 });
105 }
106 initializeNativeFunctions(nativeFunctionsClassRefs) {
107 const builtInFunctionsClassRefs = [
108 native_functions_1.HelpReplFn,
109 native_functions_1.GetReplFn,
110 native_functions_1.ResolveReplFn,
111 native_functions_1.SelectReplFn,
112 native_functions_1.DebugReplFn,
113 native_functions_1.MethodsReplFn,
114 ];
115 builtInFunctionsClassRefs
116 .concat(nativeFunctionsClassRefs)
117 .forEach(NativeFunction => {
118 const nativeFunctions = this.addNativeFunction(NativeFunction);
119 nativeFunctions.forEach(nativeFunction => {
120 this.registerFunctionIntoGlobalScope(nativeFunction);
121 });
122 });
123 }
124}
125exports.ReplContext = ReplContext;