@adonisjs/repl
Advanced tools
Comparing version 4.0.0-7 to 4.0.0-8
@@ -1,1 +0,3 @@ | ||
export { Repl } from './src/repl.js'; | ||
export { R as Repl } from './index-be604bdd.js'; | ||
import '@poppinss/colors/types'; | ||
import 'node:repl'; |
@@ -1,9 +0,296 @@ | ||
/* | ||
* @adonisjs/repl | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
export { Repl } from './src/repl.js'; | ||
// src/repl.ts | ||
import stringWidth from "string-width"; | ||
import useColors from "@poppinss/colors"; | ||
import { inspect, promisify as utilPromisify } from "node:util"; | ||
import { Recoverable, start as startRepl } from "node:repl"; | ||
var GLOBAL_NODE_PROPERTIES = [ | ||
"performance", | ||
"global", | ||
"clearInterval", | ||
"clearTimeout", | ||
"setInterval", | ||
"setTimeout", | ||
"queueMicrotask", | ||
"clearImmediate", | ||
"setImmediate", | ||
"structuredClone", | ||
"atob", | ||
"btoa", | ||
"fetch", | ||
"crypto" | ||
]; | ||
var TS_UTILS_HELPERS = [ | ||
"__extends", | ||
"__assign", | ||
"__rest", | ||
"__decorate", | ||
"__param", | ||
"__esDecorate", | ||
"__runInitializers", | ||
"__propKey", | ||
"__setFunctionName", | ||
"__metadata", | ||
"__awaiter", | ||
"__generator", | ||
"__exportStar", | ||
"__createBinding", | ||
"__values", | ||
"__read", | ||
"__spread", | ||
"__spreadArrays", | ||
"__spreadArray", | ||
"__await", | ||
"__asyncGenerator", | ||
"__asyncDelegator", | ||
"__asyncValues", | ||
"__makeTemplateObject", | ||
"__importStar", | ||
"__importDefault", | ||
"__classPrivateFieldGet", | ||
"__classPrivateFieldSet", | ||
"__classPrivateFieldIn" | ||
]; | ||
var Repl = class { | ||
/** | ||
* Length of the longest custom method name. We need to show a | ||
* symmetric view of custom methods and their description | ||
*/ | ||
#longestCustomMethodName = 0; | ||
/** | ||
* Reference to the original `eval` method of the repl server. | ||
* Since we are monkey patching it, we need a reference to it | ||
* to call it after our custom logic | ||
*/ | ||
#originalEval; | ||
/** | ||
* Compiler that will transform the user input just | ||
* before evaluation | ||
*/ | ||
#compiler; | ||
/** | ||
* Path to the history file | ||
*/ | ||
#historyFilePath; | ||
/** | ||
* Set of registered ready callbacks | ||
*/ | ||
#onReadyCallbacks = []; | ||
/** | ||
* A set of registered custom methods | ||
*/ | ||
#customMethods = {}; | ||
/** | ||
* Colors reference | ||
*/ | ||
colors = useColors.ansi(); | ||
/** | ||
* Reference to the repl server. Available after the `start` method | ||
* is invoked | ||
*/ | ||
server; | ||
constructor(options) { | ||
this.#compiler = options?.compiler; | ||
this.#historyFilePath = options?.historyFilePath; | ||
} | ||
/** | ||
* Registering custom methods with the server context by wrapping | ||
* them inside a function and passes the REPL server instance | ||
* to the method | ||
*/ | ||
#registerCustomMethodWithContext(name) { | ||
const customMethod = this.#customMethods[name]; | ||
if (!customMethod) { | ||
return; | ||
} | ||
const handler = (...args) => customMethod.handler(this, ...args); | ||
Object.defineProperty(handler, "name", { value: customMethod.handler.name }); | ||
this.server.context[name] = handler; | ||
} | ||
/** | ||
* Setup context with default globals | ||
*/ | ||
#setupContext() { | ||
this.addMethod( | ||
"clear", | ||
function clear(repl, key) { | ||
if (!key) { | ||
console.log(repl.colors.red("Define a property name to remove from the context")); | ||
} else { | ||
delete repl.server.context[key]; | ||
} | ||
repl.server.displayPrompt(); | ||
}, | ||
{ | ||
description: "Clear a property from the REPL context", | ||
usage: `clear ${this.colors.gray("(propertyName)")}` | ||
} | ||
); | ||
this.addMethod( | ||
"p", | ||
function promisify(_, fn) { | ||
return utilPromisify(fn); | ||
}, | ||
{ | ||
description: 'Promisify a function. Similar to Node.js "util.promisify"', | ||
usage: `p ${this.colors.gray("(function)")}` | ||
} | ||
); | ||
Object.keys(this.#customMethods).forEach((name) => { | ||
this.#registerCustomMethodWithContext(name); | ||
}); | ||
} | ||
/** | ||
* Find if the error is recoverable or not | ||
*/ | ||
#isRecoverableError(error) { | ||
return /^(Unexpected end of input|Unexpected token|' expected)/.test(error.message); | ||
} | ||
/** | ||
* Custom eval method to execute the user code | ||
* | ||
* Basically we are monkey patching the original eval method, because | ||
* we want to: | ||
* - Compile the user code before executing it | ||
* - And also benefit from the original eval method that supports | ||
* cool features like top level await | ||
*/ | ||
#eval(code, context, filename, callback) { | ||
try { | ||
const compiled = this.#compiler ? this.#compiler.compile(code, filename) : code; | ||
return this.#originalEval(compiled, context, filename, callback); | ||
} catch (error) { | ||
if (this.#isRecoverableError(error)) { | ||
callback(new Recoverable(error), null); | ||
return; | ||
} | ||
callback(error, null); | ||
} | ||
} | ||
/** | ||
* Setup history file | ||
*/ | ||
#setupHistory() { | ||
if (!this.#historyFilePath) { | ||
return; | ||
} | ||
this.server.setupHistory(this.#historyFilePath, (error) => { | ||
if (!error) { | ||
return; | ||
} | ||
console.log(this.colors.red("Unable to write to the history file. Exiting")); | ||
console.error(error); | ||
process.exit(1); | ||
}); | ||
} | ||
/** | ||
* Prints the help for the context properties | ||
*/ | ||
#printContextHelp() { | ||
console.log(""); | ||
console.log(this.colors.green("CONTEXT PROPERTIES/METHODS:")); | ||
const context = Object.keys(this.server.context).reduce( | ||
(result, key) => { | ||
if (!this.#customMethods[key] && !GLOBAL_NODE_PROPERTIES.includes(key) && !TS_UTILS_HELPERS.includes(key)) { | ||
result[key] = this.server.context[key]; | ||
} | ||
return result; | ||
}, | ||
{} | ||
); | ||
console.log(inspect(context, false, 1, true)); | ||
} | ||
/** | ||
* Prints the help for the custom methods | ||
*/ | ||
#printCustomMethodsHelp() { | ||
console.log(""); | ||
console.log(this.colors.green("GLOBAL METHODS:")); | ||
Object.keys(this.#customMethods).forEach((method) => { | ||
const { options } = this.#customMethods[method]; | ||
const usage = this.colors.yellow(options.usage || method); | ||
const spaces = " ".repeat(this.#longestCustomMethodName - options.width + 2); | ||
const description = this.colors.dim(options.description || ""); | ||
console.log(`${usage}${spaces}${description}`); | ||
}); | ||
} | ||
/** | ||
* Prints the context to the console | ||
*/ | ||
#ls() { | ||
this.#printCustomMethodsHelp(); | ||
this.#printContextHelp(); | ||
this.server.displayPrompt(); | ||
} | ||
/** | ||
* Notify by writing to the console | ||
*/ | ||
notify(message) { | ||
console.log(this.colors.yellow().italic(message)); | ||
if (this.server) { | ||
this.server.displayPrompt(); | ||
} | ||
} | ||
/** | ||
* Register a callback to be invoked once the server is ready | ||
*/ | ||
ready(callback) { | ||
this.#onReadyCallbacks.push(callback); | ||
return this; | ||
} | ||
/** | ||
* Register a custom loader function to be added to the context | ||
*/ | ||
addMethod(name, handler, options) { | ||
const width = stringWidth(options?.usage || name); | ||
if (width > this.#longestCustomMethodName) { | ||
this.#longestCustomMethodName = width; | ||
} | ||
this.#customMethods[name] = { handler, options: Object.assign({ width }, options) }; | ||
if (this.server) { | ||
this.#registerCustomMethodWithContext(name); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Returns the collection of registered methods | ||
*/ | ||
getMethods() { | ||
return this.#customMethods; | ||
} | ||
/** | ||
* Register a compiler. Make sure register the compiler before | ||
* calling the start method | ||
*/ | ||
useCompiler(compiler) { | ||
this.#compiler = compiler; | ||
return this; | ||
} | ||
/** | ||
* Start the REPL server | ||
*/ | ||
start() { | ||
console.log(""); | ||
this.notify('Type ".ls" to a view list of available context methods/properties'); | ||
this.server = startRepl({ | ||
prompt: `> ${this.#compiler?.supportsTypescript ? "(ts) " : "(js) "}`, | ||
input: process.stdin, | ||
output: process.stdout, | ||
terminal: process.stdout.isTTY && !Number.parseInt(process.env.NODE_NO_READLINE, 10), | ||
useGlobal: true | ||
}); | ||
this.server.defineCommand("ls", { | ||
help: "View list of available context methods/properties", | ||
action: this.#ls.bind(this) | ||
}); | ||
this.#setupContext(); | ||
this.#setupHistory(); | ||
this.#originalEval = this.server.eval; | ||
this.server.eval = this.#eval.bind(this); | ||
this.server.displayPrompt(); | ||
this.#onReadyCallbacks.forEach((callback) => callback(this)); | ||
return this; | ||
} | ||
}; | ||
export { | ||
Repl | ||
}; |
@@ -1,20 +0,3 @@ | ||
import { Repl } from './repl.js'; | ||
/** | ||
* Custom method callback function | ||
*/ | ||
export type MethodCallback = (repl: Repl, ...args: any[]) => any; | ||
/** | ||
* Options that can be set when defining a custom method | ||
*/ | ||
export type MethodOptions = { | ||
description?: string; | ||
usage?: string; | ||
}; | ||
/** | ||
* Shape of the Compiler that must be passed to the | ||
* repl constructor | ||
*/ | ||
export type Compiler = { | ||
compile: (code: string, fileName: string) => string; | ||
supportsTypescript: boolean; | ||
}; | ||
export { C as Compiler, M as MethodCallback, a as MethodOptions } from '../index-be604bdd.js'; | ||
import '@poppinss/colors/types'; | ||
import 'node:repl'; |
{ | ||
"name": "@adonisjs/repl", | ||
"description": "REPL for AdonisJS", | ||
"version": "4.0.0-7", | ||
"version": "4.0.0-8", | ||
"engines": { | ||
@@ -11,5 +11,3 @@ "node": ">=18.16.0" | ||
"files": [ | ||
"build/src", | ||
"build/index.d.ts", | ||
"build/index.js" | ||
"build" | ||
], | ||
@@ -26,5 +24,5 @@ "exports": { | ||
"typecheck": "tsc --noEmit", | ||
"compile": "npm run lint && npm run clean && tsc", | ||
"compile": "npm run lint && npm run clean && tsup-node", | ||
"build": "npm run compile", | ||
"release": "np --message=\"chore(release): %s\"", | ||
"release": "np", | ||
"version": "npm run build", | ||
@@ -40,21 +38,21 @@ "sync-labels": "github-label-sync --labels .github/labels.json adonisjs/repl", | ||
"@adonisjs/tsconfig": "^1.1.8", | ||
"@commitlint/cli": "^17.7.1", | ||
"@commitlint/config-conventional": "^17.7.0", | ||
"@japa/assert": "^2.0.0-1", | ||
"@japa/runner": "^3.0.0-2", | ||
"@swc/core": "^1.3.78", | ||
"@types/node": "^20.5.3", | ||
"@commitlint/cli": "^17.8.0", | ||
"@commitlint/config-conventional": "^17.8.0", | ||
"@japa/assert": "^2.0.0", | ||
"@japa/runner": "^3.0.2", | ||
"@swc/core": "1.3.82", | ||
"@types/node": "^20.8.6", | ||
"c8": "^8.0.1", | ||
"copyfiles": "^2.4.1", | ||
"del-cli": "^5.0.0", | ||
"eslint": "^8.47.0", | ||
"del-cli": "^5.1.0", | ||
"eslint": "^8.51.0", | ||
"github-label-sync": "^2.3.1", | ||
"husky": "^8.0.3", | ||
"np": "^8.0.4", | ||
"prettier": "^3.0.2", | ||
"prettier": "^3.0.3", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.1.3" | ||
"tsup": "^7.2.0", | ||
"typescript": "^5.2.2" | ||
}, | ||
"dependencies": { | ||
"@poppinss/colors": "4.1.0-2", | ||
"@poppinss/colors": "^4.1.0", | ||
"string-width": "^6.1.0" | ||
@@ -104,3 +102,14 @@ }, | ||
] | ||
}, | ||
"tsup": { | ||
"entry": [ | ||
"./index.ts", | ||
"./src/types.ts" | ||
], | ||
"outDir": "./build", | ||
"clean": true, | ||
"format": "esm", | ||
"dts": true, | ||
"target": "esnext" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15292
8
372
1
+ Added@poppinss/colors@4.1.3(transitive)
- Removed@poppinss/colors@4.1.0-2(transitive)
Updated@poppinss/colors@^4.1.0