@adonisjs/repl 4.0.0-3 → 4.0.0-5
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.
- package/LICENSE.md +9 -9
- package/build/src/repl.d.ts +22 -6
- package/build/src/repl.js +74 -80
- package/build/src/types.d.ts +4 -5
- package/package.json +4 -5
package/LICENSE.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
# The MIT License
|
|
2
|
-
|
|
3
|
-
Copyright 2022 Harminder Virk, contributors
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
-
|
|
7
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
-
|
|
9
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1
|
+
# The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2022 Harminder Virk, contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/build/src/repl.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="@types/node" resolution-mode="require"/>
|
|
2
2
|
import type { Colors } from '@poppinss/colors/types';
|
|
3
3
|
import { REPLServer } from 'node:repl';
|
|
4
|
-
import {
|
|
4
|
+
import type { MethodCallback, MethodOptions, Compiler } from './types.js';
|
|
5
5
|
export declare class Repl {
|
|
6
6
|
#private;
|
|
7
7
|
/**
|
|
@@ -21,10 +21,6 @@ export declare class Repl {
|
|
|
21
21
|
* Notify by writing to the console
|
|
22
22
|
*/
|
|
23
23
|
notify(message: string): void;
|
|
24
|
-
/**
|
|
25
|
-
* Start the REPL server
|
|
26
|
-
*/
|
|
27
|
-
start(): this;
|
|
28
24
|
/**
|
|
29
25
|
* Register a callback to be invoked once the server is ready
|
|
30
26
|
*/
|
|
@@ -32,5 +28,25 @@ export declare class Repl {
|
|
|
32
28
|
/**
|
|
33
29
|
* Register a custom loader function to be added to the context
|
|
34
30
|
*/
|
|
35
|
-
addMethod(name: string, handler:
|
|
31
|
+
addMethod(name: string, handler: MethodCallback, options?: MethodOptions): this;
|
|
32
|
+
/**
|
|
33
|
+
* Returns the collection of registered methods
|
|
34
|
+
*/
|
|
35
|
+
getMethods(): {
|
|
36
|
+
[name: string]: {
|
|
37
|
+
handler: MethodCallback;
|
|
38
|
+
options: MethodOptions & {
|
|
39
|
+
width: number;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Register a compiler. Make sure register the compiler before
|
|
45
|
+
* calling the start method
|
|
46
|
+
*/
|
|
47
|
+
useCompiler(compiler: Compiler): this;
|
|
48
|
+
/**
|
|
49
|
+
* Start the REPL server
|
|
50
|
+
*/
|
|
51
|
+
start(): this;
|
|
36
52
|
}
|
package/build/src/repl.js
CHANGED
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
* For the full copyright and license information, please view the LICENSE
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
|
-
import useColors from '@poppinss/colors';
|
|
10
|
-
import { Recoverable, start as startRepl } from 'node:repl';
|
|
11
9
|
import stringWidth from 'string-width';
|
|
10
|
+
import useColors from '@poppinss/colors';
|
|
12
11
|
import { inspect, promisify as utilPromisify } from 'node:util';
|
|
12
|
+
import { Recoverable, start as startRepl } from 'node:repl';
|
|
13
13
|
/**
|
|
14
14
|
* List of node global properties to remove from the
|
|
15
15
|
* ls inspect
|
|
@@ -61,9 +61,6 @@ const TS_UTILS_HELPERS = [
|
|
|
61
61
|
'__classPrivateFieldSet',
|
|
62
62
|
'__classPrivateFieldIn',
|
|
63
63
|
];
|
|
64
|
-
const icons = process.platform === 'win32' && !process.env.WT_SESSION
|
|
65
|
-
? { tick: '√', pointer: '>' }
|
|
66
|
-
: { tick: '✔', pointer: '❯' };
|
|
67
64
|
export class Repl {
|
|
68
65
|
/**
|
|
69
66
|
* Length of the longest custom method name. We need to show a
|
|
@@ -75,12 +72,16 @@ export class Repl {
|
|
|
75
72
|
* Since we are monkey patching it, we need a reference to it
|
|
76
73
|
* to call it after our custom logic
|
|
77
74
|
*/
|
|
78
|
-
#originalEval
|
|
75
|
+
#originalEval;
|
|
79
76
|
/**
|
|
80
77
|
* Compiler that will transform the user input just
|
|
81
78
|
* before evaluation
|
|
82
79
|
*/
|
|
83
80
|
#compiler;
|
|
81
|
+
/**
|
|
82
|
+
* Path to the history file
|
|
83
|
+
*/
|
|
84
|
+
#historyFilePath;
|
|
84
85
|
/**
|
|
85
86
|
* Set of registered ready callbacks
|
|
86
87
|
*/
|
|
@@ -89,10 +90,6 @@ export class Repl {
|
|
|
89
90
|
* A set of registered custom methods
|
|
90
91
|
*/
|
|
91
92
|
#customMethods = {};
|
|
92
|
-
/**
|
|
93
|
-
* Path to the history file
|
|
94
|
-
*/
|
|
95
|
-
#historyFilePath;
|
|
96
93
|
/**
|
|
97
94
|
* Colors reference
|
|
98
95
|
*/
|
|
@@ -107,21 +104,27 @@ export class Repl {
|
|
|
107
104
|
this.#historyFilePath = options?.historyFilePath;
|
|
108
105
|
}
|
|
109
106
|
/**
|
|
110
|
-
*
|
|
107
|
+
* Registering custom methods with the server context by wrapping
|
|
108
|
+
* them inside a function and passes the REPL server instance
|
|
109
|
+
* to the method
|
|
111
110
|
*/
|
|
112
|
-
#
|
|
113
|
-
|
|
111
|
+
#registerCustomMethodWithContext(name) {
|
|
112
|
+
const customMethod = this.#customMethods[name];
|
|
113
|
+
if (!customMethod) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
114
116
|
/**
|
|
115
|
-
*
|
|
117
|
+
* Wrap handler
|
|
116
118
|
*/
|
|
117
|
-
|
|
118
|
-
console.log(`${this.colors.green(icons.tick)} ${this.colors.dim('typescript compilation supported')}`);
|
|
119
|
-
console.log('');
|
|
120
|
-
}
|
|
119
|
+
const handler = (...args) => customMethod.handler(this, ...args);
|
|
121
120
|
/**
|
|
122
|
-
*
|
|
121
|
+
* Re-define the function name to be more description
|
|
123
122
|
*/
|
|
124
|
-
|
|
123
|
+
Object.defineProperty(handler, 'name', { value: customMethod.handler.name });
|
|
124
|
+
/**
|
|
125
|
+
* Register with the context
|
|
126
|
+
*/
|
|
127
|
+
this.server.context[name] = handler;
|
|
125
128
|
}
|
|
126
129
|
/**
|
|
127
130
|
* Setup context with default globals
|
|
@@ -154,12 +157,6 @@ export class Repl {
|
|
|
154
157
|
/**
|
|
155
158
|
* Register all custom methods with the context
|
|
156
159
|
*/
|
|
157
|
-
this.#registerCustomMethodsWithContext();
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Register custom methods with the server context
|
|
161
|
-
*/
|
|
162
|
-
#registerCustomMethodsWithContext() {
|
|
163
160
|
Object.keys(this.#customMethods).forEach((name) => {
|
|
164
161
|
this.#registerCustomMethodWithContext(name);
|
|
165
162
|
});
|
|
@@ -173,17 +170,15 @@ export class Repl {
|
|
|
173
170
|
/**
|
|
174
171
|
* Custom eval method to execute the user code
|
|
175
172
|
*
|
|
176
|
-
* Basically we are monkey patching the original eval method
|
|
177
|
-
*
|
|
178
|
-
* -
|
|
179
|
-
* -
|
|
180
|
-
*
|
|
173
|
+
* Basically we are monkey patching the original eval method, because
|
|
174
|
+
* we want to:
|
|
175
|
+
* - Compile the user code before executing it
|
|
176
|
+
* - And also benefit from the original eval method that supports
|
|
177
|
+
* cool features like top level await
|
|
181
178
|
*/
|
|
182
179
|
#eval(code, context, filename, callback) {
|
|
183
180
|
try {
|
|
184
|
-
const compiled = this.#compiler.compile(code, filename)
|
|
185
|
-
.replace('export { };', '')
|
|
186
|
-
.replace(/\/\/# sourceMappingURL=(.*)$/, '/** sourceMappingURL=$1 */');
|
|
181
|
+
const compiled = this.#compiler ? this.#compiler.compile(code, filename) : code;
|
|
187
182
|
return this.#originalEval(compiled, context, filename, callback);
|
|
188
183
|
}
|
|
189
184
|
catch (error) {
|
|
@@ -240,28 +235,12 @@ export class Repl {
|
|
|
240
235
|
console.log(this.colors.green('GLOBAL METHODS:'));
|
|
241
236
|
Object.keys(this.#customMethods).forEach((method) => {
|
|
242
237
|
const { options } = this.#customMethods[method];
|
|
243
|
-
const
|
|
244
|
-
|
|
238
|
+
const usage = this.colors.yellow(options.usage || method);
|
|
239
|
+
const spaces = ' '.repeat(this.#longestCustomMethodName - options.width + 2);
|
|
240
|
+
const description = this.colors.dim(options.description || '');
|
|
241
|
+
console.log(`${usage}${spaces}${description}`);
|
|
245
242
|
});
|
|
246
243
|
}
|
|
247
|
-
#registerCustomMethodWithContext(name) {
|
|
248
|
-
const customMethod = this.#customMethods[name];
|
|
249
|
-
if (!customMethod) {
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Wrap handler
|
|
254
|
-
*/
|
|
255
|
-
const handler = (...args) => customMethod.handler(this, ...args);
|
|
256
|
-
/**
|
|
257
|
-
* Re-define the function name to be more description
|
|
258
|
-
*/
|
|
259
|
-
Object.defineProperty(handler, 'name', { value: customMethod.handler.name });
|
|
260
|
-
/**
|
|
261
|
-
* Register with the context
|
|
262
|
-
*/
|
|
263
|
-
this.server.context[name] = handler;
|
|
264
|
-
}
|
|
265
244
|
/**
|
|
266
245
|
* Prints the context to the console
|
|
267
246
|
*/
|
|
@@ -279,13 +258,52 @@ export class Repl {
|
|
|
279
258
|
this.server.displayPrompt();
|
|
280
259
|
}
|
|
281
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Register a callback to be invoked once the server is ready
|
|
263
|
+
*/
|
|
264
|
+
ready(callback) {
|
|
265
|
+
this.#onReadyCallbacks.push(callback);
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Register a custom loader function to be added to the context
|
|
270
|
+
*/
|
|
271
|
+
addMethod(name, handler, options) {
|
|
272
|
+
const width = stringWidth(options?.usage || name);
|
|
273
|
+
if (width > this.#longestCustomMethodName) {
|
|
274
|
+
this.#longestCustomMethodName = width;
|
|
275
|
+
}
|
|
276
|
+
this.#customMethods[name] = { handler, options: Object.assign({ width }, options) };
|
|
277
|
+
/**
|
|
278
|
+
* Register method right away when server has been started
|
|
279
|
+
*/
|
|
280
|
+
if (this.server) {
|
|
281
|
+
this.#registerCustomMethodWithContext(name);
|
|
282
|
+
}
|
|
283
|
+
return this;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Returns the collection of registered methods
|
|
287
|
+
*/
|
|
288
|
+
getMethods() {
|
|
289
|
+
return this.#customMethods;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Register a compiler. Make sure register the compiler before
|
|
293
|
+
* calling the start method
|
|
294
|
+
*/
|
|
295
|
+
useCompiler(compiler) {
|
|
296
|
+
this.#compiler = compiler;
|
|
297
|
+
return this;
|
|
298
|
+
}
|
|
282
299
|
/**
|
|
283
300
|
* Start the REPL server
|
|
284
301
|
*/
|
|
285
302
|
start() {
|
|
286
|
-
|
|
303
|
+
console.log('');
|
|
304
|
+
this.notify('Type ".ls" to a view list of available context methods/properties');
|
|
287
305
|
this.server = startRepl({
|
|
288
|
-
prompt: '
|
|
306
|
+
prompt: `> ${this.#compiler?.supportsTypescript ? '(ts) ' : '(js) '}`,
|
|
289
307
|
input: process.stdin,
|
|
290
308
|
output: process.stdout,
|
|
291
309
|
terminal: process.stdout.isTTY && !Number.parseInt(process.env.NODE_NO_READLINE, 10),
|
|
@@ -319,28 +337,4 @@ export class Repl {
|
|
|
319
337
|
this.#onReadyCallbacks.forEach((callback) => callback(this));
|
|
320
338
|
return this;
|
|
321
339
|
}
|
|
322
|
-
/**
|
|
323
|
-
* Register a callback to be invoked once the server is ready
|
|
324
|
-
*/
|
|
325
|
-
ready(callback) {
|
|
326
|
-
this.#onReadyCallbacks.push(callback);
|
|
327
|
-
return this;
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* Register a custom loader function to be added to the context
|
|
331
|
-
*/
|
|
332
|
-
addMethod(name, handler, options) {
|
|
333
|
-
const width = stringWidth(options?.usage || name);
|
|
334
|
-
if (width > this.#longestCustomMethodName) {
|
|
335
|
-
this.#longestCustomMethodName = width;
|
|
336
|
-
}
|
|
337
|
-
this.#customMethods[name] = { handler, options: Object.assign({ width }, options) };
|
|
338
|
-
/**
|
|
339
|
-
* Register method right away when server has been started
|
|
340
|
-
*/
|
|
341
|
-
if (this.server) {
|
|
342
|
-
this.#registerCustomMethodWithContext(name);
|
|
343
|
-
}
|
|
344
|
-
return this;
|
|
345
|
-
}
|
|
346
340
|
}
|
package/build/src/types.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { Repl } from './repl.js';
|
|
2
2
|
/**
|
|
3
|
-
* Custom method
|
|
3
|
+
* Custom method callback function
|
|
4
4
|
*/
|
|
5
|
-
export type
|
|
5
|
+
export type MethodCallback = (repl: Repl, ...args: any[]) => any;
|
|
6
6
|
/**
|
|
7
|
-
* Options that can be set when defining a
|
|
8
|
-
* method
|
|
7
|
+
* Options that can be set when defining a custom method
|
|
9
8
|
*/
|
|
10
|
-
export type
|
|
9
|
+
export type MethodOptions = {
|
|
11
10
|
description?: string;
|
|
12
11
|
usage?: string;
|
|
13
12
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/repl",
|
|
3
3
|
"description": "REPL for AdonisJS",
|
|
4
|
-
"version": "4.0.0-
|
|
4
|
+
"version": "4.0.0-5",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=18.16.0"
|
|
7
7
|
},
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
"type": "module",
|
|
10
10
|
"files": [
|
|
11
11
|
"build/src",
|
|
12
|
-
"build/factories",
|
|
13
12
|
"build/index.d.ts",
|
|
14
13
|
"build/index.js"
|
|
15
14
|
],
|
|
@@ -33,12 +32,12 @@
|
|
|
33
32
|
"prepublishOnly": "npm run build"
|
|
34
33
|
},
|
|
35
34
|
"devDependencies": {
|
|
36
|
-
"@adonisjs/core": "^6.1.5-4",
|
|
37
35
|
"@adonisjs/eslint-config": "^1.1.7",
|
|
38
36
|
"@adonisjs/prettier-config": "^1.1.7",
|
|
39
37
|
"@adonisjs/tsconfig": "^1.1.7",
|
|
38
|
+
"@commitlint/cli": "^17.6.6",
|
|
39
|
+
"@commitlint/config-conventional": "^17.6.6",
|
|
40
40
|
"@japa/assert": "^2.0.0-1",
|
|
41
|
-
"@japa/file-system": "^2.0.0-1",
|
|
42
41
|
"@japa/runner": "^3.0.0-2",
|
|
43
42
|
"@swc/core": "^1.3.67",
|
|
44
43
|
"@types/node": "^20.3.3",
|
|
@@ -83,7 +82,7 @@
|
|
|
83
82
|
},
|
|
84
83
|
"publishConfig": {
|
|
85
84
|
"access": "public",
|
|
86
|
-
"tag": "
|
|
85
|
+
"tag": "next"
|
|
87
86
|
},
|
|
88
87
|
"np": {
|
|
89
88
|
"message": "chore(release): %s",
|