Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Types #62

Merged
merged 7 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7,818 changes: 6,962 additions & 856 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"main": "index.js",
"scripts": {
"dev": "nodemon examples/index.js",
"check": "tsc --noEmit true",
"types": "tsc --emitDeclarationOnly true --noEmitOnError false",
"lint": "eslint --ext=.js src test",
"deps": "npm-check -u",
"test": "jest --coverage"
Expand All @@ -31,7 +33,7 @@
},
"dependencies": {
"clui": "^0.3.6",
"commander": "^8.3.0",
"commander": "^9.0.0",
"glob": "^7.1.7",
"kleur": "^4.1.4",
"lodash": "^4.17.21",
Expand Down
4 changes: 2 additions & 2 deletions src/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const _ = require("lodash");
* Returns the list of versioned services
*
* @param {import("moleculer").ServiceBroker} broker
* @returns {Array<Array<String>>}
* @returns {Array<String>}
*/
function versionedServicesAutocomplete(broker) {
let services = broker.registry.getServiceList({
Expand All @@ -25,7 +25,7 @@ function versionedServicesAutocomplete(broker) {
* Returns the list of nodes and their actions in the following format "<nodeID> <actionName>"
*
* @param {import("moleculer").ServiceBroker} broker
* @returns {Array<Array<String>>}
* @returns {Array<String>}
*/
function nodeIdActionNameAutocomplete(broker) {
// Flatten to a single list
Expand Down
27 changes: 25 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ program.showSuggestionAfterError(true);
* Start REPL mode
*
* @param {import("moleculer").ServiceBroker} broker
* @param {Object|Array} opts
* @param {REPLOptions} opts
*/
/* istanbul ignore next */
function REPL(broker, opts) {
Expand Down Expand Up @@ -127,7 +127,7 @@ async function evaluator(cmd, context, filename, callback) {
* Registers user defined commands
* @param {import('moleculer').ServiceBroker} broker
* @param {import("commander").Command} program Commander
* @param {Object} def
* @param {Partial<CustomCommand>} def Command definition
*/
function registerCustomCommands(broker, program, def) {
const cmd = program.command(def.command);
Expand Down Expand Up @@ -196,3 +196,26 @@ function registerCustomCommands(broker, program, def) {
}

module.exports = REPL;

/**
* @typedef CommandOptions
* @property {String} option Command option. More info: https://github.com/tj/commander.js/#options
* @property {String} description Option description
*/

/**
* @typedef CustomCommand Custom command definition
* @property {String} command Command declaration
* @property {String?} description Command description
* @property {Array<String> | String | null} alias Command alias
* @property {Boolean?} allowUnknownOptions Allow unknown command options
* @property {Function?} parse Custom params parser
* @property {Array<CommandOptions>} options Command options
* @property {Function} action Custom command handler
*/

/**
* @typedef REPLOptions REPL Options
* @property {String|null} delimiter REPL delimiter
* @property {Array<CustomCommand>|CustomCommand|null} customCommands Custom commands
*/
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"allowJs": true,
"strict": false,
"checkJs": true,
"declaration": true,
"declarationDir": "types",
"outDir": "dist"
},
"include": ["**.js"],
"exclude": ["node_modules", ".eslintrc.js", "prettier.config.js", "types"]
}
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const _exports: typeof import("./src");
export = _exports;
25 changes: 25 additions & 0 deletions types/src/args-parser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Parses array of args into an object with key/values
*
* Example:
*
* ```js
* // Input
* const args = [ "--a", "5", "--b", "Bob", "--c", "--no-d", "--e.f", "hello", "--traceHash", "0x75", "--hash", "0x895", ];
* // Output
* const result = {
* a: 5,
* b: 'Bob',
* c: true,
* d: false,
* e: { f: 'hello' },
* traceHash: '0x75',
* hash: '0x895'
* }
*
* ```
*
* @param {Array<String>} args args to parse
* @returns {Object} parsed args
*/
export function parser(args: Array<string>): any;
15 changes: 15 additions & 0 deletions types/src/autocomplete.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Given a line from terminal generates a list of suggestions
*
* @param {String} line
* @param {import("moleculer").ServiceBroker} broker
* @param {import('commander').Command} program
* @returns {[String[], String]} List of suggestions. More info: https://nodejs.org/api/readline.html#use-of-the-completer-function
*/
export function autocompleteHandler(line: string, broker: import("moleculer").ServiceBroker, program: import('commander').Command): [string[], string];
/**
* Returns list of available commands
* @param {import('commander').Command} program
* @returns {String[]} Available commands
*/
export function getAvailableCommands(program: import('commander').Command): string[];
2 changes: 2 additions & 0 deletions types/src/commands/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare function _exports(program: import("commander").Command, broker: import("moleculer").ServiceBroker): void;
export = _exports;
12 changes: 12 additions & 0 deletions types/src/flag-processing.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Tries to parse string to integer
* @param {String} value
* @returns {Number}
*/
export function flagParseInt(value: string): number;
/**
* Checks if string is a hexadecimal
* @param {String} x
* @returns {Boolean}
*/
export function isHex(x: string): boolean;
86 changes: 86 additions & 0 deletions types/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/// <reference types="node" />
export = REPL;
/**
* Start REPL mode
*
* @param {import("moleculer").ServiceBroker} broker
* @param {REPLOptions} opts
*/
declare function REPL(broker: import("moleculer").ServiceBroker, opts: REPLOptions): nodeRepl.REPLServer;
declare namespace REPL {
export { CommandOptions, CustomCommand, REPLOptions };
}
/**
* REPL Options
*/
type REPLOptions = {
/**
* REPL delimiter
*/
delimiter: string | null;
/**
* Custom commands
*/
customCommands: Array<CustomCommand> | CustomCommand | null;
};
import nodeRepl = require("repl");
type CommandOptions = {
/**
* Command option. More info: https://github.com/tj/commander.js/#options
*/
option: string;
/**
* Option description
*/
description: string;
};
/**
* Custom command definition
*/
type CustomCommand = {
/**
* Command declaration
*/
command: string;
/**
* Command description
*/
description: string | null;
/**
* Command alias
*/
alias: Array<string> | string | null;
/**
* Allow unknown command options
*/
allowUnknownOptions: boolean | null;
/**
* Custom params parser
*/
parse: Function | null;
/**
* Command options
*/
options: Array<CommandOptions>;
/**
* Custom command handler
*/
action: Function;
};

declare module "moleculer" {
interface ServiceBroker {
repl(opts?: Partial<REPLOptions>): nodeRepl.REPLServer;
}

interface BrokerOptions {
/**
* Custom command definition
*/
replCommands?: Array<CustomCommand>;
/**
* REPL delimiter
*/
replDelimiter?: string;
}
}