@adonisjs/repl 3.1.10 → 4.0.0-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/LICENSE.md +9 -9
  2. package/README.md +21 -201
  3. package/build/commands/adonis_repl.d.ts +9 -0
  4. package/build/commands/adonis_repl.d.ts.map +1 -0
  5. package/build/commands/adonis_repl.js +17 -0
  6. package/build/commands/commands.json +1 -0
  7. package/build/commands/main.js +36 -0
  8. package/build/index.d.ts +3 -0
  9. package/build/index.d.ts.map +1 -0
  10. package/build/index.js +2 -0
  11. package/build/providers/repl_provider.d.ts +8 -0
  12. package/build/providers/repl_provider.d.ts.map +1 -0
  13. package/build/providers/repl_provider.js +40 -0
  14. package/build/src/adonis_bindings.d.ts +4 -0
  15. package/build/src/adonis_bindings.d.ts.map +1 -0
  16. package/build/src/adonis_bindings.js +26 -0
  17. package/build/src/colorizer.d.ts +5 -0
  18. package/build/src/colorizer.d.ts.map +1 -0
  19. package/build/src/colorizer.js +39 -0
  20. package/build/src/repl.d.ts +17 -0
  21. package/build/src/repl.d.ts.map +1 -0
  22. package/build/src/repl.js +214 -0
  23. package/build/src/types/extended.d.ts +7 -0
  24. package/build/src/types/extended.d.ts.map +1 -0
  25. package/build/src/types/extended.js +1 -0
  26. package/build/src/types/main.d.ts +11 -0
  27. package/build/src/types/main.d.ts.map +1 -0
  28. package/build/src/types/main.js +1 -0
  29. package/build/src/utils.d.ts +2 -0
  30. package/build/src/utils.d.ts.map +1 -0
  31. package/build/src/utils.js +11 -0
  32. package/commands/adonis_repl.ts +38 -0
  33. package/{build/adonis-typings/repl.js → index.ts} +5 -1
  34. package/package.json +107 -129
  35. package/providers/repl_provider.ts +61 -0
  36. package/src/adonis_bindings.ts +63 -0
  37. package/src/colorizer.ts +62 -0
  38. package/src/repl.ts +423 -0
  39. package/{build/adonis-typings/container.js → src/types/extended.ts} +9 -1
  40. package/src/types/main.ts +33 -0
  41. package/src/utils.ts +14 -0
  42. package/build/adonis-typings/container.d.ts +0 -6
  43. package/build/adonis-typings/index.d.ts +0 -2
  44. package/build/adonis-typings/index.js +0 -10
  45. package/build/adonis-typings/repl.d.ts +0 -46
  46. package/build/commands/AdonisRepl.d.ts +0 -11
  47. package/build/commands/AdonisRepl.js +0 -33
  48. package/build/commands/index.d.ts +0 -2
  49. package/build/commands/index.js +0 -11
  50. package/build/providers/ReplProvider.d.ts +0 -7
  51. package/build/providers/ReplProvider.js +0 -26
  52. package/build/src/Compiler/index.d.ts +0 -29
  53. package/build/src/Compiler/index.js +0 -68
  54. package/build/src/ImportsParser/index.d.ts +0 -51
  55. package/build/src/ImportsParser/index.js +0 -137
  56. package/build/src/Repl/index.d.ts +0 -91
  57. package/build/src/Repl/index.js +0 -358
  58. package/build/standalone.d.ts +0 -3
  59. package/build/standalone.js +0 -17
@@ -1,358 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/repl
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- var __importDefault = (this && this.__importDefault) || function (mod) {
11
- return (mod && mod.__esModule) ? mod : { "default": mod };
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.Repl = void 0;
15
- const vm_1 = require("vm");
16
- const string_width_1 = __importDefault(require("string-width"));
17
- const colors_1 = require("@poppinss/colors");
18
- const repl_1 = require("repl");
19
- const util_1 = require("util");
20
- const Compiler_1 = require("../Compiler");
21
- /**
22
- * List of node global properties to remove from the
23
- * ls inspect
24
- */
25
- const GLOBAL_NODE_PROPERTIES = [
26
- 'performance',
27
- 'global',
28
- 'clearInterval',
29
- 'clearTimeout',
30
- 'setInterval',
31
- 'setTimeout',
32
- 'queueMicrotask',
33
- 'clearImmediate',
34
- 'setImmediate',
35
- 'exports',
36
- '__importDefault',
37
- ];
38
- /**
39
- * Properties injected by the ts-utils
40
- * library
41
- */
42
- const TS_UTIL_HELPERS = [
43
- '__extends',
44
- '__assign',
45
- '__rest',
46
- '__decorate',
47
- '__param',
48
- '__metadata',
49
- '__awaiter',
50
- '__generator',
51
- '__exportStar',
52
- '__values',
53
- '__read',
54
- '__spread',
55
- '__spreadArrays',
56
- '__spreadArray',
57
- '__await',
58
- '__asyncGenerator',
59
- '__asyncDelegator',
60
- '__asyncValues',
61
- '__makeTemplateObject',
62
- '__importStar',
63
- '__importDefault',
64
- '__classPrivateFieldGet',
65
- '__classPrivateFieldSet',
66
- '__createBinding',
67
- ];
68
- const icons = process.platform === 'win32' && !process.env.WT_SESSION
69
- ? {
70
- tick: '√',
71
- pointer: '>',
72
- }
73
- : {
74
- tick: '✔',
75
- pointer: '❯',
76
- };
77
- /**
78
- * Exposes the API to work the REPL server
79
- */
80
- class Repl {
81
- constructor(tsCompiler, historyFilePath) {
82
- this.tsCompiler = tsCompiler;
83
- this.historyFilePath = historyFilePath;
84
- /**
85
- * Compiler to compile REPL input
86
- */
87
- this.compiler = new Compiler_1.Compiler(this.tsCompiler);
88
- /**
89
- * Length of the longest custom method name. We need to show a
90
- * symmetric view of custom methods and their description
91
- */
92
- this.longestCustomMethodName = 0;
93
- /**
94
- * Set of registered ready callbacks
95
- */
96
- this.onReadyCallbacks = [];
97
- /**
98
- * A set of registered custom methods
99
- */
100
- this.customMethods = {};
101
- /**
102
- * Reference to the colors to print colorful messages
103
- */
104
- this.colors = (0, colors_1.getBest)(false);
105
- }
106
- /**
107
- * Find if the error is recoverable or not
108
- */
109
- isRecoverableError(error) {
110
- if (error.name === 'SyntaxError') {
111
- return /^(Unexpected end of input|Unexpected token)/.test(error.message);
112
- }
113
- return false;
114
- }
115
- /**
116
- * Custom eval method to execute the user code
117
- */
118
- async eval(cmd, _, filename, callback) {
119
- try {
120
- let response;
121
- const { compiled, awaitPromise } = await this.compiler.compile(cmd, filename);
122
- if (awaitPromise) {
123
- response = await new vm_1.Script(compiled, { filename }).runInThisContext();
124
- }
125
- else {
126
- response = new vm_1.Script(compiled, { filename }).runInThisContext();
127
- }
128
- callback(null, response);
129
- }
130
- catch (error) {
131
- if (this.isRecoverableError(error)) {
132
- callback(new repl_1.Recoverable(error), null);
133
- return;
134
- }
135
- callback(error, null);
136
- }
137
- }
138
- registerCustomMethodWithContext(name) {
139
- const customMethod = this.customMethods[name];
140
- if (!customMethod) {
141
- return;
142
- }
143
- /**
144
- * Wrap handler
145
- */
146
- const handler = (...args) => customMethod.handler(this, ...args);
147
- /**
148
- * Re-define the function name to be more description
149
- */
150
- Object.defineProperty(handler, 'name', { value: customMethod.handler.name });
151
- /**
152
- * Register with the context
153
- */
154
- this.server.context[name] = handler;
155
- }
156
- /**
157
- * Register custom methods with the server context
158
- */
159
- registerCustomMethodsWithContext() {
160
- Object.keys(this.customMethods).forEach((name) => {
161
- this.registerCustomMethodWithContext(name);
162
- });
163
- }
164
- /**
165
- * Setup context with default globals
166
- */
167
- setupContext() {
168
- /**
169
- * Register "clear" method
170
- */
171
- this.addMethod('clear', function clear(repl, key) {
172
- if (!key) {
173
- console.log(repl.colors.red('Define a property name to remove from the context'));
174
- }
175
- else {
176
- delete repl.server.context[key];
177
- }
178
- repl.server.displayPrompt();
179
- }, {
180
- description: 'Clear a property from the REPL context',
181
- usage: `clear ${this.colors.gray('(propertyName)')}`,
182
- });
183
- /**
184
- * Register "p" method
185
- */
186
- this.addMethod('p', function promisify(_, fn) {
187
- return (0, util_1.promisify)(fn);
188
- }, {
189
- description: 'Promisify a function. Similar to Node.js "util.promisify"',
190
- usage: `p ${this.colors.gray('(function)')}`,
191
- });
192
- /**
193
- * Register all custom methods with the context
194
- */
195
- this.registerCustomMethodsWithContext();
196
- }
197
- /**
198
- * Setup history file
199
- */
200
- setupHistory() {
201
- if (!this.historyFilePath) {
202
- return;
203
- }
204
- this.server.setupHistory(this.historyFilePath, (error) => {
205
- if (!error) {
206
- return;
207
- }
208
- console.log(this.colors.red('Unable to write to the history file. Exiting'));
209
- console.error(error);
210
- process.exit(1);
211
- });
212
- }
213
- /**
214
- * Prints the welcome message
215
- */
216
- printWelcomeMessage() {
217
- console.log('');
218
- /**
219
- * Log about typescript support
220
- */
221
- if (this.compiler.compilesTs) {
222
- console.log(`${this.colors.dim(icons.tick)} ${this.colors.dim('typescript compilation supported')}`);
223
- }
224
- /**
225
- * Log about top level imports
226
- */
227
- console.log(`${this.colors.dim(icons.tick)} ${this.colors.dim('allows top level imports')}`);
228
- console.log('');
229
- /**
230
- * Log about help command
231
- */
232
- this.notify('Type ".ls" to a view list of available context methods/properties');
233
- }
234
- /**
235
- * Prints the help for the custom methods
236
- */
237
- printCustomMethodsHelp() {
238
- /**
239
- * Print loader methods
240
- */
241
- console.log('');
242
- console.log(this.colors.green('GLOBAL METHODS:'));
243
- Object.keys(this.customMethods).forEach((method) => {
244
- const { options } = this.customMethods[method];
245
- const spaces = new Array(this.longestCustomMethodName - options.width + 2).join(' ');
246
- console.log(`${this.colors.yellow(options.usage || method)}${spaces}${this.colors.dim(options.description || '')}`);
247
- });
248
- }
249
- /**
250
- * Prints the help for the context properties
251
- */
252
- printContextHelp() {
253
- /**
254
- * Print context properties
255
- */
256
- console.log('');
257
- console.log(this.colors.green('CONTEXT PROPERTIES/METHODS:'));
258
- const context = Object.keys(this.server?.context).reduce((result, key) => {
259
- if (!this.customMethods[key] &&
260
- !GLOBAL_NODE_PROPERTIES.includes(key) &&
261
- !TS_UTIL_HELPERS.includes(key)) {
262
- result[key] = this.server?.context[key];
263
- }
264
- return result;
265
- }, {});
266
- console.log((0, util_1.inspect)(context, false, 1, true));
267
- }
268
- /**
269
- * Prints the context to the console
270
- */
271
- ls() {
272
- this.printCustomMethodsHelp();
273
- this.printContextHelp();
274
- this.server.displayPrompt();
275
- }
276
- /**
277
- * Notify by writing to the console
278
- */
279
- notify(message) {
280
- console.log(this.colors.yellow().italic(message));
281
- if (this.server) {
282
- this.server.displayPrompt();
283
- }
284
- }
285
- /**
286
- * Start the REPL session
287
- */
288
- start() {
289
- this.printWelcomeMessage();
290
- this.server = (0, repl_1.start)({
291
- prompt: '> ',
292
- input: process.stdin,
293
- output: process.stdout,
294
- terminal: process.stdout.isTTY && !parseInt(process.env.NODE_NO_READLINE, 10),
295
- useGlobal: true,
296
- });
297
- /**
298
- * Define ls command
299
- */
300
- this.server.defineCommand('ls', {
301
- help: 'View a list of available context methods/properties',
302
- action: this.ls.bind(this),
303
- });
304
- /**
305
- * Setup context
306
- */
307
- this.setupContext();
308
- /**
309
- * Setup history
310
- */
311
- this.setupHistory();
312
- /**
313
- * Assigning eval function like this has better completion support.
314
- */
315
- // @ts-ignore
316
- this.server['eval'] = this.eval.bind(this);
317
- /**
318
- * Define exports variable when using Typescript
319
- */
320
- if (this.compiler.compilesTs) {
321
- new vm_1.Script('exports = module.exports', { filename: __dirname }).runInThisContext();
322
- }
323
- /**
324
- * Display prompt
325
- */
326
- this.server.displayPrompt();
327
- /**
328
- * Execute onready callbacks
329
- */
330
- this.onReadyCallbacks.forEach((callback) => callback(this));
331
- return this;
332
- }
333
- /**
334
- * Register a callback to be invoked once the server is ready
335
- */
336
- ready(callback) {
337
- this.onReadyCallbacks.push(callback);
338
- return this;
339
- }
340
- /**
341
- * Register a custom loader function to be added to the context
342
- */
343
- addMethod(name, handler, options) {
344
- const width = (0, string_width_1.default)(options?.usage || name);
345
- if (width > this.longestCustomMethodName) {
346
- this.longestCustomMethodName = width;
347
- }
348
- this.customMethods[name] = { handler, options: Object.assign({ width }, options) };
349
- /**
350
- * Register method right away when server has been started
351
- */
352
- if (this.server) {
353
- this.registerCustomMethodWithContext(name);
354
- }
355
- return this;
356
- }
357
- }
358
- exports.Repl = Repl;
@@ -1,3 +0,0 @@
1
- export { Repl } from './src/Repl';
2
- export { Compiler } from './src/Compiler';
3
- export { ImportsParser } from './src/ImportsParser';
@@ -1,17 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/repl
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.ImportsParser = exports.Compiler = exports.Repl = void 0;
12
- var Repl_1 = require("./src/Repl");
13
- Object.defineProperty(exports, "Repl", { enumerable: true, get: function () { return Repl_1.Repl; } });
14
- var Compiler_1 = require("./src/Compiler");
15
- Object.defineProperty(exports, "Compiler", { enumerable: true, get: function () { return Compiler_1.Compiler; } });
16
- var ImportsParser_1 = require("./src/ImportsParser");
17
- Object.defineProperty(exports, "ImportsParser", { enumerable: true, get: function () { return ImportsParser_1.ImportsParser; } });