Comparing version 2.1.3 to 2.1.4
@@ -40,2 +40,3 @@ /// <reference types="node" /> | ||
runExit(input: Command<Context> | string[], context: Context): Promise<void>; | ||
suggestFor(input: string[]): Promise<Set<unknown>>; | ||
definitions(): { | ||
@@ -42,0 +43,0 @@ path: string; |
@@ -96,2 +96,6 @@ "use strict"; | ||
} | ||
async suggestFor(input) { | ||
const { contexts, process, suggest } = this.builder.compile(); | ||
return suggest(input); | ||
} | ||
definitions() { | ||
@@ -98,0 +102,0 @@ const data = []; |
@@ -37,3 +37,6 @@ export declare const NODE_INITIAL = 0; | ||
}): void; | ||
export declare function runMachine(machine: StateMachine, input: string[]): RunState; | ||
export declare function runMachineInternal(machine: StateMachine, input: string[]): { | ||
node: number; | ||
state: RunState; | ||
}[]; | ||
export declare function trimSmallerBranches(branches: { | ||
@@ -74,2 +77,3 @@ node: number; | ||
export declare function execute<T extends string>(store: CallbackStore<T>, callback: Callback<T>, state: RunState, segment: string): any; | ||
export declare function suggest(callback: Callback<keyof typeof tests>, state: RunState): string[] | null; | ||
export declare const tests: { | ||
@@ -338,2 +342,3 @@ always: () => boolean; | ||
process: (input: string[]) => RunState; | ||
suggest: (input: string[]) => Set<unknown>; | ||
}; | ||
@@ -348,4 +353,5 @@ constructor({ binaryName }?: Partial<CliOptions>); | ||
process: (input: string[]) => RunState; | ||
suggest: (input: string[]) => Set<unknown>; | ||
}; | ||
} | ||
export {}; |
@@ -104,3 +104,3 @@ "use strict"; | ||
exports.debugMachine = debugMachine; | ||
function runMachine(machine, input) { | ||
function runMachineInternal(machine, input) { | ||
debug(`Running a vm on ${JSON.stringify(input)}`); | ||
@@ -117,3 +117,3 @@ let branches = [{ node: exports.NODE_INITIAL, state: { | ||
debugMachine(machine, { prefix: ` ` }); | ||
for (const segment of [exports.START_OF_INPUT, ...input, exports.END_OF_INPUT]) { | ||
for (const segment of [exports.START_OF_INPUT, ...input]) { | ||
debug(` Processing ${JSON.stringify(segment)}`); | ||
@@ -171,2 +171,28 @@ const nextBranches = []; | ||
} | ||
return branches; | ||
} | ||
exports.runMachineInternal = runMachineInternal; | ||
function suggestMachine(machine, input) { | ||
const branches = runMachineInternal(machine, input); | ||
const suggestions = new Set(); | ||
for (const { node, state } of branches) { | ||
const nodeDef = machine.nodes[node]; | ||
for (const candidate of Object.keys(nodeDef.statics)) | ||
suggestions.add(candidate); | ||
for (const [test, { to }] of nodeDef.dynamics) { | ||
if (to === exports.NODE_ERRORED) | ||
continue; | ||
const tokens = suggest(test, state); | ||
if (tokens === null) | ||
continue; | ||
for (const token of tokens) { | ||
suggestions.add(token); | ||
} | ||
} | ||
} | ||
suggestions.delete(exports.END_OF_INPUT); | ||
return suggestions; | ||
} | ||
function runMachine(machine, input) { | ||
const branches = runMachineInternal(machine, [...input, exports.END_OF_INPUT]); | ||
return selectBestState(input, branches.map(({ state }) => { | ||
@@ -176,3 +202,2 @@ return state; | ||
} | ||
exports.runMachine = runMachine; | ||
function trimSmallerBranches(branches) { | ||
@@ -302,2 +327,16 @@ let maxPathSize = 0; | ||
exports.execute = execute; | ||
function suggest(callback, state) { | ||
const fn = Array.isArray(callback) | ||
? exports.tests[callback[0]] | ||
: exports.tests[callback]; | ||
// @ts-ignore | ||
if (typeof fn.suggest === `undefined`) | ||
return null; | ||
const args = Array.isArray(callback) | ||
? callback.slice(1) | ||
: []; | ||
// @ts-ignore | ||
return fn.suggest(state, ...args); | ||
} | ||
exports.suggest = suggest; | ||
exports.tests = { | ||
@@ -329,2 +368,6 @@ always: () => { | ||
}; | ||
// @ts-ignore | ||
exports.tests.isOption.suggest = (state, name) => { | ||
return [name]; | ||
}; | ||
exports.reducers = { | ||
@@ -489,4 +532,4 @@ setCandidateUsage: (state, segment, usage) => { | ||
registerStatic(machine, helpNode, exports.END_OF_INPUT, exports.NODE_SUCCESS, [`setSelectedIndex`, exports.HELP_COMMAND_INDEX]); | ||
this.registerOptions(machine, lastPathNode); | ||
} | ||
this.registerOptions(machine, lastPathNode); | ||
if (this.arity.leading.length > 0) | ||
@@ -497,3 +540,4 @@ registerStatic(machine, lastPathNode, exports.END_OF_INPUT, exports.NODE_ERRORED, [`setError`, `Not enough positional arguments`]); | ||
const nextLeadingNode = injectNode(machine, makeNode()); | ||
this.registerOptions(machine, nextLeadingNode); | ||
if (!this.arity.proxy) | ||
this.registerOptions(machine, nextLeadingNode); | ||
if (this.arity.trailing.length > 0 || t + 1 !== this.arity.leading.length) | ||
@@ -510,3 +554,4 @@ registerStatic(machine, nextLeadingNode, exports.END_OF_INPUT, exports.NODE_ERRORED, [`setError`, `Not enough positional arguments`]); | ||
const extraNode = injectNode(machine, makeNode()); | ||
this.registerOptions(machine, extraNode); | ||
if (!this.arity.proxy) | ||
this.registerOptions(machine, extraNode); | ||
registerDynamic(machine, lastLeadingNode, positionalArgument, extraNode, `pushExtra`); | ||
@@ -519,3 +564,4 @@ registerDynamic(machine, extraNode, positionalArgument, extraNode, `pushExtra`); | ||
const nextExtraNode = injectNode(machine, makeNode()); | ||
this.registerOptions(machine, nextExtraNode); | ||
if (!this.arity.proxy) | ||
this.registerOptions(machine, nextExtraNode); | ||
registerDynamic(machine, lastExtraNode, positionalArgument, nextExtraNode, `pushExtra`); | ||
@@ -533,3 +579,4 @@ registerShortcut(machine, nextExtraNode, extraShortcutNode); | ||
const nextTrailingNode = injectNode(machine, makeNode()); | ||
this.registerOptions(machine, nextTrailingNode); | ||
if (!this.arity.proxy) | ||
this.registerOptions(machine, nextTrailingNode); | ||
if (t + 1 < this.arity.trailing.length) | ||
@@ -609,7 +656,14 @@ registerStatic(machine, nextTrailingNode, exports.END_OF_INPUT, exports.NODE_ERRORED, [`setError`, `Not enough positional arguments`]); | ||
simplifyMachine(machine); | ||
return { machine, contexts, process: (input) => { | ||
return { | ||
machine, | ||
contexts, | ||
process: (input) => { | ||
return runMachine(machine, input); | ||
} }; | ||
}, | ||
suggest: (input) => { | ||
return suggestMachine(machine, input); | ||
}, | ||
}; | ||
} | ||
} | ||
exports.CliBuilder = CliBuilder; |
{ | ||
"name": "clipanion", | ||
"version": "2.1.3", | ||
"version": "2.1.4", | ||
"main": "lib/advanced", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
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
76010
1816