@gkalpak/cli-utils
Advanced tools
Comparing version 0.1.5 to 0.1.6
/** | ||
* A configuration object, specifying the behavior of {@link commandUtils#expandCmd expandCmd()}, | ||
* {@link commandUtils#run run()}, {@link commandUtils#spawnAsPromised spawnAsPromised()}. | ||
* A configuration object, specifying the behavior of {@link CommandUtils#expandCmd expandCmd()}, | ||
* {@link CommandUtils#run run()}, {@link CommandUtils#spawnAsPromised spawnAsPromised()}. | ||
* | ||
@@ -133,2 +133,3 @@ * For cli commands that accept configuration options, names must be prefixed with `--gkcu-` (but letter casing should | ||
private debugMessage; | ||
private getLastLines; | ||
private insertAfter; | ||
@@ -135,0 +136,0 @@ private insertAt; |
@@ -50,5 +50,5 @@ "use strict"; | ||
const re = /(\s{0,1})\\?\$(?:(\*)|([1-9]+)\*|(\d+)|{(?:(?:(\*)|([1-9]+)\*|(\d+))(?::([^}]*))?)})/g; | ||
const cmdPromises = new Map(); | ||
const subCommands = new Map(); | ||
let expandedCmd = cmd.replace(re, (_, g1, g2, g3, g4, g5, g6, g7, g8) => { | ||
const valToReplacement = (val) => !val ? '' : `${g1}${val}`; | ||
const transformValue = (val) => !val ? '' : `${g1}${val}`; | ||
// Value based on the supplied arguments. | ||
@@ -73,19 +73,27 @@ const startIdx = (g2 || g5) ? 0 : (g3 || g6 || g4 || g7) - 1; | ||
const placeholder = `${Math.random()}`; | ||
if (!cmdPromises.has(subCmd)) { | ||
const runConfig = Object.assign({}, config, { returnOutput }); | ||
const cmdPromise = this.run(subCmd, runtimeArgs, runConfig). | ||
then(result => this.trimOutput(result)). | ||
then(result => valToReplacement(runConfig.dryrun ? `{{${result.replace(/\s/g, '_')}}}` : result)); | ||
cmdPromises.set(subCmd, cmdPromise); | ||
if (!subCommands.has(subCmd)) { | ||
subCommands.set(subCmd, []); | ||
} | ||
cmdPromises.set(subCmd, cmdPromises.get(subCmd).then(repl => { | ||
expandedCmd = expandedCmd.replace(placeholder, repl); | ||
return repl; | ||
})); | ||
subCommands.get(subCmd).push({ placeholder, returnOutput, transformValue }); | ||
return placeholder; | ||
} | ||
} | ||
return valToReplacement(value); | ||
return transformValue(value); | ||
}); | ||
yield Promise.all(cmdPromises.values()); | ||
const subCommandPromises = Array.from(subCommands.entries()).map(([subCmd, infoList]) => { | ||
const hasNumericReturnOutput = infoList.some(info => typeof info.returnOutput === 'number'); | ||
const returnOutput = hasNumericReturnOutput ? Infinity : true; | ||
const runConfig = Object.assign({}, config, { returnOutput }); | ||
const subCmdPromise = this.run(subCmd, runtimeArgs, runConfig).then(result => this.trimOutput(result)); | ||
const replPromises = infoList.map(info => subCmdPromise.then(result => { | ||
// Retrieve the part of the output that this sub-command cares about. | ||
const value = (typeof info.returnOutput === 'number') ? this.getLastLines(result, info.returnOutput) : result; | ||
// Construct the replacement for this sub-command (e.g. leading whitespace may vary). | ||
const repl = info.transformValue(config.dryrun ? `{{${value.replace(/\s/g, '_')}}}` : value); | ||
// Replace in `expandedCmd`. | ||
expandedCmd = expandedCmd.replace(info.placeholder, repl); | ||
})); | ||
return Promise.all(replPromises); | ||
}); | ||
yield Promise.all(subCommandPromises); | ||
return expandedCmd; | ||
@@ -192,3 +200,3 @@ }); | ||
() => data : | ||
() => data.trim().split('\n').slice(-returnOutput).join('\n'); | ||
() => this.getLastLines(data.trim(), returnOutput); | ||
const pipedCmdSpecs = this.parseRawCmd(rawCmd, sapVersion, dryrun); | ||
@@ -244,2 +252,5 @@ const lastStdout = pipedCmdSpecs.reduce((prevStdout, cmdSpec, idx, arr) => { | ||
} | ||
getLastLines(input, lineCount) { | ||
return input.split('\n').slice(-lineCount).join('\n').trim(); | ||
} | ||
insertAfter(items, newItem, afterItem) { | ||
@@ -246,0 +257,0 @@ for (let i = 0; i < items.length; ++i) { |
{ | ||
"name": "@gkalpak/cli-utils", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "A private collection of utilities for developing cli tools.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -8,4 +8,4 @@ import {spawn, SpawnOptions} from 'child_process'; | ||
/** | ||
* A configuration object, specifying the behavior of {@link commandUtils#expandCmd expandCmd()}, | ||
* {@link commandUtils#run run()}, {@link commandUtils#spawnAsPromised spawnAsPromised()}. | ||
* A configuration object, specifying the behavior of {@link CommandUtils#expandCmd expandCmd()}, | ||
* {@link CommandUtils#run run()}, {@link CommandUtils#spawnAsPromised spawnAsPromised()}. | ||
* | ||
@@ -56,2 +56,8 @@ * For cli commands that accept configuration options, names must be prefixed with `--gkcu-` (but letter casing should | ||
interface ISubCommandInfo { | ||
placeholder: string; | ||
returnOutput: boolean | number; | ||
transformValue: (value: string) => string; | ||
} | ||
export class CommandUtils { | ||
@@ -98,6 +104,6 @@ /** | ||
const re = /(\s{0,1})\\?\$(?:(\*)|([1-9]+)\*|(\d+)|{(?:(?:(\*)|([1-9]+)\*|(\d+))(?::([^}]*))?)})/g; | ||
const cmdPromises = new Map<string, Promise<string>>(); | ||
const subCommands = new Map<string, ISubCommandInfo[]>(); | ||
let expandedCmd = cmd.replace(re, (_, g1, g2, g3, g4, g5, g6, g7, g8) => { | ||
const valToReplacement = (val: string) => !val ? '' : `${g1}${val}`; | ||
const transformValue = (val: string) => !val ? '' : `${g1}${val}`; | ||
@@ -125,15 +131,7 @@ // Value based on the supplied arguments. | ||
if (!cmdPromises.has(subCmd)) { | ||
const runConfig: IRunConfig = Object.assign({}, config, {returnOutput}); | ||
const cmdPromise = this.run(subCmd, runtimeArgs, runConfig). | ||
then(result => this.trimOutput(result)). | ||
then(result => valToReplacement(runConfig.dryrun ? `{{${result.replace(/\s/g, '_')}}}` : result)); | ||
cmdPromises.set(subCmd, cmdPromise); | ||
if (!subCommands.has(subCmd)) { | ||
subCommands.set(subCmd, []); | ||
} | ||
cmdPromises.set(subCmd, cmdPromises.get(subCmd)!.then(repl => { | ||
expandedCmd = expandedCmd.replace(placeholder, repl); | ||
return repl; | ||
})); | ||
subCommands.get(subCmd)!.push({placeholder, returnOutput, transformValue}); | ||
@@ -144,7 +142,27 @@ return placeholder; | ||
return valToReplacement(value); | ||
return transformValue(value); | ||
}); | ||
await Promise.all(cmdPromises.values()); | ||
const subCommandPromises = Array.from(subCommands.entries()).map(([subCmd, infoList]) => { | ||
const hasNumericReturnOutput = infoList.some(info => typeof info.returnOutput === 'number'); | ||
const returnOutput = hasNumericReturnOutput ? Infinity : true; | ||
const runConfig: IRunConfig = Object.assign({}, config, {returnOutput}); | ||
const subCmdPromise = this.run(subCmd, runtimeArgs, runConfig).then(result => this.trimOutput(result)); | ||
const replPromises = infoList.map(info => subCmdPromise.then(result => { | ||
// Retrieve the part of the output that this sub-command cares about. | ||
const value = (typeof info.returnOutput === 'number') ? this.getLastLines(result, info.returnOutput) : result; | ||
// Construct the replacement for this sub-command (e.g. leading whitespace may vary). | ||
const repl = info.transformValue(config.dryrun ? `{{${value.replace(/\s/g, '_')}}}` : value); | ||
// Replace in `expandedCmd`. | ||
expandedCmd = expandedCmd.replace(info.placeholder, repl); | ||
})); | ||
return Promise.all(replPromises); | ||
}); | ||
await Promise.all(subCommandPromises); | ||
return expandedCmd; | ||
@@ -265,3 +283,3 @@ } | ||
() => data : | ||
() => data.trim().split('\n').slice(-(returnOutput as number)).join('\n'); | ||
() => this.getLastLines(data.trim(), returnOutput as number); | ||
@@ -328,2 +346,6 @@ const pipedCmdSpecs = this.parseRawCmd(rawCmd, sapVersion, dryrun); | ||
private getLastLines(input: string, lineCount: number) { | ||
return input.split('\n').slice(-lineCount).join('\n').trim(); | ||
} | ||
private insertAfter(items: string[], newItem: string, afterItem: string): void { | ||
@@ -330,0 +352,0 @@ for (let i = 0; i < items.length; ++i) { |
Sorry, the diff of this file is not supported yet
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
102673
1571