Comparing version 0.0.2 to 0.0.3-beta.0
declare function createProgram(): import("./program").Program; | ||
export { Context, NextFunction } from './program'; | ||
export { Context, NextFunction } from './Commands/Layer'; | ||
export { Terminal } from './terminal'; | ||
export { createProgram }; | ||
export default createProgram; |
@@ -1,17 +0,2 @@ | ||
import { Terminal } from './terminal'; | ||
export interface Params { | ||
command: string; | ||
subCommands: string[]; | ||
flags: [string, string | number | boolean][]; | ||
} | ||
export interface Context { | ||
params: Params; | ||
session: { | ||
[key: string]: unknown; | ||
}; | ||
} | ||
export declare type NextFunctionError = (error?: Error) => void; | ||
export declare type NextFunctionSuccess = () => void; | ||
export declare type NextFunction = NextFunctionSuccess | NextFunctionError; | ||
export declare type UseFunction = (context: Context, terminal: Terminal, nextFunction: NextFunction) => void; | ||
import { Layer, CommandFunction, Params } from './Commands/Layer'; | ||
export interface Program { | ||
@@ -22,9 +7,11 @@ session: { | ||
params: Params; | ||
stack: UseFunction[]; | ||
stack: Layer[]; | ||
parseArguments(args: string[]): void; | ||
lazyStack(promises: Promise<unknown>[]): Generator<unknown, void>; | ||
init(): void; | ||
start(): void; | ||
use(...fn: UseFunction[]): void; | ||
use(command: string, ...fn: UseFunction[]): void; | ||
use(commands: string[], ...fn: UseFunction[]): void; | ||
use(...fn: CommandFunction[]): void; | ||
use(command: string, ...fn: CommandFunction[]): void; | ||
use(notInCommands: string[], ...fn: CommandFunction[]): void; | ||
use(command: string, notInCommands: string[], ...fn: CommandFunction[]): void; | ||
} | ||
@@ -31,0 +18,0 @@ declare const program: Program; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __asyncValues = (this && this.__asyncValues) || function (o) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var m = o[Symbol.asyncIterator], i; | ||
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); | ||
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } | ||
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.program = void 0; | ||
const terminal_1 = require("./terminal"); | ||
const Layer_1 = require("./Commands/Layer"); | ||
const program = { params: {} }; | ||
@@ -12,3 +29,3 @@ exports.program = program; | ||
subCommands: [], | ||
flags: [] | ||
flags: new Map() | ||
}; | ||
@@ -19,15 +36,49 @@ this.stack = []; | ||
}; | ||
/** | ||
* TODO: The stack is wrong, need refactor after.ß | ||
*/ | ||
program.start = function start() { | ||
// Start the program | ||
this.stack.forEach(execution => execution({ session: this.session, params: this.params }, terminal_1.terminal, () => console.log('next'))); | ||
var e_1, _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (var _b = __asyncValues(this.stack), _c; _c = yield _b.next(), !_c.done;) { | ||
const execution = _c.value; | ||
if (!execution.match(this.params.command)) { | ||
continue; | ||
} | ||
execution.handle({ session: this.session, params: this.params }, terminal_1.terminal, () => console.log('next')); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
}); | ||
}; | ||
program.use = function use(fn, fns) { | ||
if (typeof fn !== 'function') { | ||
console.log(fns); | ||
this.stack.push(fns); | ||
program.use = function use(firstArgument, secondArgument) { | ||
let offset = 0; | ||
let command = ''; | ||
let notInCommands = []; | ||
if (typeof firstArgument === 'string' && typeof secondArgument === 'object') { | ||
command = firstArgument; | ||
notInCommands = secondArgument; | ||
offset = 2; | ||
} | ||
else { | ||
this.stack.push(fn); | ||
this.stack.push(fns); | ||
else if (typeof firstArgument === 'string') { | ||
command = firstArgument; | ||
offset = 1; | ||
} | ||
else if (typeof firstArgument === 'object') { | ||
notInCommands = firstArgument; | ||
offset = 1; | ||
} | ||
const callbacks = Object.values(arguments).slice(offset); | ||
for (let i = 0; i < callbacks.length; i++) { | ||
const callback = callbacks[i]; | ||
this.stack.push(new Layer_1.Layer(command, notInCommands, {}, callback)); | ||
} | ||
}; | ||
@@ -45,17 +96,16 @@ program.parseArguments = function parseArguments(args) { | ||
else { | ||
this.params.flags.push([accumulator.flagName, arg]); | ||
this.params.flags.set(accumulator.flagName, arg); | ||
} | ||
} | ||
else if (accumulator.flagPending) { | ||
this.params.flags.push([accumulator.flagName, true]); | ||
this.params.flags.set(accumulator.flagName, true); | ||
} | ||
else if (arg.startsWith('--')) { | ||
if (arg.startsWith('--')) { | ||
const [key, value] = arg.split('='); | ||
this.params.flags.push([key, value || true]); | ||
this.params.flags.set(key, value || true); | ||
} | ||
else if (arg.startsWith('-')) { | ||
const [key] = arg.split('='); | ||
return { | ||
flagPending: true, | ||
flagName: key | ||
flagName: arg | ||
}; | ||
@@ -77,5 +127,5 @@ } | ||
if (hasPendingFlag.flagPending) { | ||
this.params.flags.push([hasPendingFlag.flagName, true]); | ||
this.params.flags.set(hasPendingFlag.flagName, true); | ||
} | ||
}; | ||
exports.default = program; |
@@ -11,2 +11,3 @@ import ora from 'ora'; | ||
succeed(textSuccess: string): void; | ||
fail(textFail: string): void; | ||
}; | ||
@@ -13,0 +14,0 @@ end(): void; |
@@ -39,2 +39,5 @@ "use strict"; | ||
spinner.succeed(textSuccess); | ||
}, | ||
fail(textFail) { | ||
spinner.fail(textFail); | ||
} | ||
@@ -41,0 +44,0 @@ }; |
{ | ||
"name": "cowmand", | ||
"version": "0.0.2", | ||
"version": "0.0.3-beta.0", | ||
"description": "Helper to create a cli api", | ||
@@ -23,2 +23,3 @@ "main": "dist/cowmand.js", | ||
"chalk": "4.1.2", | ||
"jsonfile": "^6.1.0", | ||
"merge-descriptors": "^1.0.1", | ||
@@ -28,2 +29,3 @@ "ora": "5.4.1" | ||
"devDependencies": { | ||
"@types/jsonfile": "^6.0.1", | ||
"@types/merge-descriptors": "^1.0.1", | ||
@@ -30,0 +32,0 @@ "@types/node": "^17.0.8", |
Sorry, the diff of this file is not supported yet
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
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
424
19157
4
15
19
+ Addedjsonfile@^6.1.0
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedjsonfile@6.1.0(transitive)
+ Addeduniversalify@2.0.1(transitive)