Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@spinajs/cli

Package Overview
Dependencies
Maintainers
1
Versions
234
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@spinajs/cli - npm Package Compare versions

Comparing version 1.0.5 to 2.0.38

lib/cli.d.ts

74

lib/decorators.d.ts

@@ -1,52 +0,38 @@

export declare const CLI_DESCRIPTOR_SYMBOL: unique symbol;
import { CommandOptions } from 'commander';
export declare const META_COMMAND = "cli:command";
export declare const META_ARGUMENT = "cli:argument";
export declare const META_OPTION = "cli:options";
/**
* decorator used to mark class as cli command.
* Cli command, wrapper for lib commander. For more docs on command options & args check
* https://github.com/tj/commander.js
*
* @example usage
* ```javascript
* @Cli("spine:dosmth","Something to do")
* export class SmthToDoCommand implements CliCommand{
* //.....
* }
* ```
* Then invoke command from cli like this: spine spine:dosmth -option1 -option2 .....
* It allows to use all features that spinajs provides eg. DI, logging, intl support etc.
* inside command functions without hassle.
*
* @param name - name of command, prefered name is eg. spine:dosmthg
* @param description - command help, displayed when using --help option
* @param nameAndArgs - name of command with optional args defined
* @param description - short description
* @param opts - additional options, see https://github.com/tj/commander.js
*/
export declare function Command(nameAndArgs: string, description: string, opts?: CommandOptions): (target: object) => void;
/**
* Options provided for command. Option contain name and argument
* eg. test-command -p 80
* @param flags - short flag (single character) and a long name, separated by a comma or space or vertical bar ('|').
* @param required - if options is required for command
* @param description - short description for option
* @param defaultValue - default value if none provided
* @param parser - callback function for parsing value
*
*
*/
export declare function Cli(name: string, description: string): (target: any) => void;
export declare function Option(flags: string, required?: boolean, description?: string, defaultValue?: any, parser?: (opt: string) => unknown): (target: object) => void;
/**
* Decorator used to add command options ( arguments passed to `execute` command member function ).
* Can be added multiple times to command.
* Command argument
* eg. \@Argument('<first>', 'integer argument')
*
* @param params - param name with options
* @param description - description used in help
* @see commander params definition examples
*
* @example params example
* ```
* -s, --string // for normal value eg. string
* -i, --integer <n> // for integers
* -f, --float <n> // for floats
* -r, --range <a>..<b> // for range
* -l, --list <items> // for list
* -o, --optional [value] // for optional value
* ```
*
* @example usage
* ```javascript
* @Cli("spine:dosmth","Something to do")
* @Option("-o, --option1","Some option")
* @Option("-o2, --option2 [value]","Some optional value")
* export class SmthToDoCommand implements ICliCommand{
* //.....
*
* execute(option1, option2){
* ....
* }
* }
* ```
* @param name - short description argument name
* @param description - short arg description
* @param defaultValue - default value
* @param parser - callback function for parsing value
* @returns
*/
export declare function Option(params: string, description: string, required?: boolean): (target: any) => void;
export declare function Argument(name: string, description?: string, defaultValue?: any, parser?: (opt: string) => unknown): (target: object) => void;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Option = exports.Cli = exports.CLI_DESCRIPTOR_SYMBOL = void 0;
const interfaces_1 = require("./interfaces");
exports.CLI_DESCRIPTOR_SYMBOL = Symbol.for('CLI_DESCRIPTOR');
function _cli(target) {
if (target[exports.CLI_DESCRIPTOR_SYMBOL] === undefined) {
target[exports.CLI_DESCRIPTOR_SYMBOL] = new interfaces_1.CliDescriptor();
}
}
exports.Argument = exports.Option = exports.Command = exports.META_OPTION = exports.META_ARGUMENT = exports.META_COMMAND = void 0;
exports.META_COMMAND = 'cli:command';
exports.META_ARGUMENT = 'cli:argument';
exports.META_OPTION = 'cli:options';
/**
* decorator used to mark class as cli command.
* Cli command, wrapper for lib commander. For more docs on command options & args check
* https://github.com/tj/commander.js
*
* @example usage
* ```javascript
* @Cli("spine:dosmth","Something to do")
* export class SmthToDoCommand implements CliCommand{
* //.....
* }
* ```
* Then invoke command from cli like this: spine spine:dosmth -option1 -option2 .....
* It allows to use all features that spinajs provides eg. DI, logging, intl support etc.
* inside command functions without hassle.
*
* @param name - name of command, prefered name is eg. spine:dosmthg
* @param description - command help, displayed when using --help option
* @param nameAndArgs - name of command with optional args defined
* @param description - short description
* @param opts - additional options, see https://github.com/tj/commander.js
*/
function Command(nameAndArgs, description, opts) {
return function (target) {
const arg = {
nameAndArgs,
description,
opts,
};
Reflect.defineMetadata(exports.META_COMMAND, arg, target);
};
}
exports.Command = Command;
/**
* Options provided for command. Option contain name and argument
* eg. test-command -p 80
* @param flags - short flag (single character) and a long name, separated by a comma or space or vertical bar ('|').
* @param required - if options is required for command
* @param description - short description for option
* @param defaultValue - default value if none provided
* @param parser - callback function for parsing value
*
*
*/
function Cli(name, description) {
return (target) => {
_cli(target);
target[exports.CLI_DESCRIPTOR_SYMBOL].name = name;
target[exports.CLI_DESCRIPTOR_SYMBOL].description = description;
function Option(flags, required, description, defaultValue, parser) {
return function (target) {
const arg = {
flags,
description,
defaultValue,
required,
parser,
};
let args = [];
if (Reflect.hasMetadata(exports.META_OPTION, target)) {
args = Reflect.getMetadata(exports.META_OPTION, target);
}
args.push(arg);
Reflect.defineMetadata(exports.META_OPTION, args, target);
};
}
exports.Cli = Cli;
exports.Option = Option;
/**
* Decorator used to add command options ( arguments passed to `execute` command member function ).
* Can be added multiple times to command.
* Command argument
* eg. \@Argument('<first>', 'integer argument')
*
* @param params - param name with options
* @param description - description used in help
* @see commander params definition examples
*
* @example params example
* ```
* -s, --string // for normal value eg. string
* -i, --integer <n> // for integers
* -f, --float <n> // for floats
* -r, --range <a>..<b> // for range
* -l, --list <items> // for list
* -o, --optional [value] // for optional value
* ```
*
* @example usage
* ```javascript
* @Cli("spine:dosmth","Something to do")
* @Option("-o, --option1","Some option")
* @Option("-o2, --option2 [value]","Some optional value")
* export class SmthToDoCommand implements ICliCommand{
* //.....
*
* execute(option1, option2){
* ....
* }
* }
* ```
* @param name - short description argument name
* @param description - short arg description
* @param defaultValue - default value
* @param parser - callback function for parsing value
* @returns
*/
function Option(params, description, required = false) {
return (target) => {
_cli(target);
const descriptor = target[exports.CLI_DESCRIPTOR_SYMBOL];
descriptor.options.push({
Description: description,
Params: params,
Required: required,
});
function Argument(name, description, defaultValue, parser) {
return function (target) {
const arg = {
name,
description,
defaultValue,
parser,
};
let args = [];
if (Reflect.hasMetadata(exports.META_ARGUMENT, target)) {
args = Reflect.getMetadata(exports.META_ARGUMENT, target);
}
args.push(arg);
Reflect.defineMetadata(exports.META_ARGUMENT, args, target);
};
}
exports.Option = Option;
exports.Argument = Argument;
//# sourceMappingURL=decorators.js.map

@@ -0,3 +1,11 @@

import { CliCommand } from './interfaces';
import { AsyncModule } from '@spinajs/di';
import { ILog } from '@spinajs/log';
import { ClassInfo } from '@spinajs/reflection';
export * from './interfaces';
export * from './decorators';
export * from './module';
export declare class Cli extends AsyncModule {
protected Log: ILog;
Commands: Promise<Array<ClassInfo<CliCommand>>>;
resolveAsync(): Promise<void>;
}
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {

@@ -9,9 +13,64 @@ if (k2 === undefined) k2 = k;

}));
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cli = void 0;
const decorators_1 = require("./decorators");
const di_1 = require("@spinajs/di");
const log_1 = require("@spinajs/log");
const reflection_1 = require("@spinajs/reflection");
const commander_1 = require("commander");
__exportStar(require("./interfaces"), exports);
__exportStar(require("./decorators"), exports);
__exportStar(require("./module"), exports);
di_1.DI.register(() => {
return process.argv;
}).as('__cli_argv_provider__');
class Cli extends di_1.AsyncModule {
async resolveAsync() {
for (const command of await this.Commands) {
this.Log.trace(`Found command ${command.name} in file ${command.file}`);
const cMeta = Reflect.getMetadata(decorators_1.META_COMMAND, command.type);
const oMeta = Reflect.getMetadata(decorators_1.META_OPTION, command.type);
const aMeta = Reflect.getMetadata(decorators_1.META_ARGUMENT, command.type);
const c = commander_1.program.command(cMeta.nameAndArgs, cMeta.description, cMeta.opts);
oMeta.forEach((o) => {
if (o.required) {
c.requiredOption(o.flags, o.description, o.parser, o.defaultValue);
}
else {
c.option(o.flags, o.description, o.parser, o.defaultValue);
}
});
aMeta.forEach((a) => {
c.argument(a.name, a.description, a.parser, a.defaultValue);
});
command.instance.onCreation(c);
c.action(async (...args) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await command.instance.execute(...args);
});
}
const argv = di_1.DI.resolve('__cli_argv_provider__');
commander_1.program.parse(argv);
}
}
__decorate([
(0, log_1.Logger)('CLI'),
__metadata("design:type", Object)
], Cli.prototype, "Log", void 0);
__decorate([
(0, reflection_1.ResolveFromFiles)('/**/!(*.d).{ts,js}', 'system.dirs.cli'),
__metadata("design:type", Promise)
], Cli.prototype, "Commands", void 0);
exports.Cli = Cli;
//# sourceMappingURL=index.js.map

@@ -1,72 +0,47 @@

/**
* Cli option description. Its passed as command arguments
*/
export interface ICliOption {
/**
* Param definition.
* @see commander params definition examples
*
* @example
* ```
* -s, --string // for normal value eg. string
* -i, --integer <n> // for integers
* -f, --float <n> // for floats
* -r, --range <a>..<b> // for range
* -l, --list <items> // for list
* -o, --optional [value] // for optional value
* ```
*/
Params: string;
/**
* Description for option, used in option help.
*/
Description: string;
/**
* Is option required
*/
Required: boolean;
import { AsyncModule } from '@spinajs/di';
import { Command, CommandOptions } from 'commander';
export interface ICommand {
nameAndArgs: string;
opts?: CommandOptions;
description: string;
}
/**
* Cli argument description
*/
export interface ICliArgument {
/**
* Cli option eg. -s, --string
*/
Option: string;
/**
* Argument description used in help
*/
Description: string;
export interface IOption {
flags: string;
description?: string;
defaultValue?: string | boolean | string[];
required?: boolean;
parser?: (opt: string) => unknown;
}
/**
* Cli command interface declaration.
*/
export interface ICliCommand {
/**
* Command name
*/
Name: string;
/**
* This function is executed by cli. Do command stuff here.
*/
execute: (...args: any[]) => Promise<number>;
export interface IArgument {
name: string;
description?: string;
defaultValue?: unknown;
required?: boolean;
parser?: (opt: string) => unknown;
}
/**
* Internall cli command descriptor. Describes command options, name & help
* Wrapper class for commander https://github.com/tj/commander.js
* To declare opions & arguments use decorators. This class is resolved
* when cli module is created.
*
* It allows to use all features that spinajs provides eg. DI, logging, intl support etc.
* inside command functions without hassle.
*/
export declare class CliDescriptor {
export declare abstract class CliCommand extends AsyncModule {
/**
* Name of command eg. test:cli
* Function executed when command is running
*
* @param args - args passed from cli
*/
name: string;
abstract execute(...args: any[]): Promise<void>;
/**
* Command general description, used when displaying help
* Executed on command creation, used when you need to
* use advanced stuff from commander lib
*
* Decorators provide only basic command args & options declarations,
* and are used only for simle use cases
*
* @param c - commander command object
*/
description: string;
/**
* Cli commands options
* @see CliOption
*/
options: ICliOption[];
onCreation(_command: Command): void;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliDescriptor = void 0;
exports.CliCommand = void 0;
const di_1 = require("@spinajs/di");
/**
* Internall cli command descriptor. Describes command options, name & help
* Wrapper class for commander https://github.com/tj/commander.js
* To declare opions & arguments use decorators. This class is resolved
* when cli module is created.
*
* It allows to use all features that spinajs provides eg. DI, logging, intl support etc.
* inside command functions without hassle.
*/
class CliDescriptor {
constructor() {
/**
* Name of command eg. test:cli
*/
this.name = '';
/**
* Command general description, used when displaying help
*/
this.description = '';
/**
* Cli commands options
* @see CliOption
*/
this.options = [];
}
class CliCommand extends di_1.AsyncModule {
/**
* Executed on command creation, used when you need to
* use advanced stuff from commander lib
*
* Decorators provide only basic command args & options declarations,
* and are used only for simle use cases
*
* @param c - commander command object
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
onCreation(_command) { }
}
exports.CliDescriptor = CliDescriptor;
exports.CliCommand = CliCommand;
//# sourceMappingURL=interfaces.js.map
{
"name": "@spinajs/cli",
"version": "1.0.5",
"description": "framework cli module",
"version": "2.0.38",
"description": "SpinaJS command line module",
"main": "lib/index.js",
"private": false,
"bin": {
"spinajs": "node lib/bin/index.js"
},
"scripts": {

@@ -14,6 +11,8 @@ "test": "ts-mocha -p tsconfig.json test/**/*.test.ts",

"build-docs": "rimraf docs && typedoc --options typedoc.json src/",
"build": "tsc",
"build": "npm run clean && npm run compile",
"compile": "tsc -p tsconfig.build.json",
"clean": "",
"prepare": "npm run build",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"lint": "tslint -p tsconfig.json",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "eslint -c .eslintrc.js --ext .ts src",
"prepublishOnly": "npm test && npm run lint",

@@ -27,51 +26,24 @@ "preversion": "npm run lint",

],
"repository": {
"type": "git",
"url": "git+https://github.com/spinajs/cli.git"
"bin": {
"spinajs": "./lib/cli.js"
},
"keywords": [
"di",
"container",
"spinajs",
"di"
],
"author": "SpinaJS <spinajs@coderush.pl> (https://github.com/spinajs/core)",
"license": "MIT",
"bugs": {
"url": "https://github.com/spinajs/cli/issues"
"url": "https://github.com/spinajs/reflection/issues"
},
"homepage": "https://github.com/spinajs/cli#readme",
"homepage": "https://github.com/spinajs/reflection#readme",
"dependencies": {
"@spinajs/configuration": "^1.0.12",
"@spinajs/di": "^1.0.18",
"@spinajs/exceptions": "^1.0.3",
"@spinajs/log": "^1.0.7",
"@spinajs/reflection": "^1.0.7",
"commander": "^5.1.0",
"glob": "^7.1.4",
"lodash": "^4.17.14"
"@spinajs/configuration": "^2.0.38",
"@spinajs/di": "^2.0.38",
"@spinajs/exceptions": "^2.0.12",
"@spinajs/log": "^2.0.38",
"@spinajs/reflection": "^2.0.38",
"commander": "9.4.0",
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
"@types/bunyan": "^1.8.6",
"@types/chai": "^4.1.7",
"@types/chai-as-promised": "^7.1.0",
"@types/lodash": "^4.14.136",
"@types/mocha": "^5.2.7",
"@types/sinon": "^7.0.13",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"mocha": "^6.1.4",
"nyc": "^14.1.1",
"prettier": "^1.18.2",
"sinon": "^7.3.2",
"ts-mocha": "^6.0.0",
"ts-node": "^8.3.0",
"tslint": "^5.20.1",
"tslint-circular-dependencies": "^0.1.0",
"tslint-config-prettier": "^1.18.0",
"tslint-config-standard": "^8.0.1",
"tslint-no-unused-expression-chai": "^0.1.4",
"typedoc": "^0.14.2",
"typescript": "^3.7.3"
}
"@types/node": "^16.11.11"
},
"gitHead": "5ea5440ee9db49595f531592ebdbc6d69f457082"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc