@oclif/plugin-autocomplete
Advanced tools
Comparing version
@@ -8,4 +8,4 @@ import { Config } from '@oclif/core'; | ||
constructor(config: Config); | ||
private get coTopics(); | ||
generate(): string; | ||
private get coTopics(); | ||
private genCmdHashtable; | ||
@@ -12,0 +12,0 @@ private genHashtable; |
import * as ejs from 'ejs'; | ||
import { EOL } from 'node:os'; | ||
import * as util from 'node:util'; | ||
import { format } from 'node:util'; | ||
export default class PowerShellComp { | ||
@@ -14,2 +14,16 @@ config; | ||
} | ||
get coTopics() { | ||
if (this._coTopics) | ||
return this._coTopics; | ||
const coTopics = []; | ||
for (const topic of this.topics) { | ||
for (const cmd of this.commands) { | ||
if (topic.name === cmd.id) { | ||
coTopics.push(topic.name); | ||
} | ||
} | ||
} | ||
this._coTopics = coTopics; | ||
return this._coTopics; | ||
} | ||
generate() { | ||
@@ -211,16 +225,2 @@ const genNode = (partialId) => { | ||
} | ||
get coTopics() { | ||
if (this._coTopics) | ||
return this._coTopics; | ||
const coTopics = []; | ||
for (const topic of this.topics) { | ||
for (const cmd of this.commands) { | ||
if (topic.name === cmd.id) { | ||
coTopics.push(topic.name); | ||
} | ||
} | ||
} | ||
this._coTopics = coTopics; | ||
return this._coTopics; | ||
} | ||
genCmdHashtable(cmd) { | ||
@@ -276,7 +276,7 @@ const flaghHashtables = []; | ||
} | ||
childTpl = util.format(childTpl, childNodes.join('\n')); | ||
return util.format(leafTpl, childTpl); | ||
childTpl = format(childTpl, childNodes.join('\n')); | ||
return format(leafTpl, childTpl); | ||
} | ||
// last node | ||
return util.format(leafTpl, childTpl); | ||
return format(leafTpl, childTpl); | ||
} | ||
@@ -289,3 +289,3 @@ const childNodes = []; | ||
throw new Error('no command'); | ||
childNodes.push(util.format('"_command" = %s', this.genCmdHashtable(cmd))); | ||
childNodes.push(format('"_command" = %s', this.genCmdHashtable(cmd))); | ||
} | ||
@@ -296,3 +296,3 @@ else if (node[key][k]._command) { | ||
throw new Error('no command'); | ||
childNodes.push(util.format(`"${k}" = @{\n"_command" = %s\n}`, this.genCmdHashtable(cmd))); | ||
childNodes.push(format(`"${k}" = @{\n"_command" = %s\n}`, this.genCmdHashtable(cmd))); | ||
} | ||
@@ -305,3 +305,3 @@ else { | ||
if (childNodes.length > 0) { | ||
return util.format(leafTpl, childNodes.join('\n')); | ||
return format(leafTpl, childNodes.join('\n')); | ||
} | ||
@@ -308,0 +308,0 @@ return leafTpl; |
@@ -8,4 +8,4 @@ import { Config } from '@oclif/core'; | ||
constructor(config: Config); | ||
private get coTopics(); | ||
generate(): string; | ||
private get coTopics(); | ||
private genZshFlagArgumentsBlock; | ||
@@ -12,0 +12,0 @@ private genZshTopicCompFun; |
import * as ejs from 'ejs'; | ||
import * as util from 'node:util'; | ||
import { format } from 'node:util'; | ||
const argTemplate = ' "%s")\n %s\n ;;\n'; | ||
@@ -14,2 +14,16 @@ export default class ZshCompWithSpaces { | ||
} | ||
get coTopics() { | ||
if (this._coTopics) | ||
return this._coTopics; | ||
const coTopics = []; | ||
for (const topic of this.topics) { | ||
for (const cmd of this.commands) { | ||
if (topic.name === cmd.id) { | ||
coTopics.push(topic.name); | ||
} | ||
} | ||
} | ||
this._coTopics = coTopics; | ||
return this._coTopics; | ||
} | ||
generate() { | ||
@@ -80,16 +94,2 @@ const firstArgs = []; | ||
} | ||
get coTopics() { | ||
if (this._coTopics) | ||
return this._coTopics; | ||
const coTopics = []; | ||
for (const topic of this.topics) { | ||
for (const cmd of this.commands) { | ||
if (topic.name === cmd.id) { | ||
coTopics.push(topic.name); | ||
} | ||
} | ||
} | ||
this._coTopics = coTopics; | ||
return this._coTopics; | ||
} | ||
genZshFlagArgumentsBlock(flags) { | ||
@@ -207,3 +207,3 @@ // if a command doesn't have flags make it only complete files | ||
}); | ||
argsBlock += util.format(argTemplate, subArg, `_${this.config.bin}_${underscoreSepId}_${subArg}`); | ||
argsBlock += format(argTemplate, subArg, `_${this.config.bin}_${underscoreSepId}_${subArg}`); | ||
} | ||
@@ -218,5 +218,5 @@ for (const c of this.commands.filter((c) => c.id.startsWith(id + ':') && c.id.split(':').length === depth + 1)) { | ||
}); | ||
argsBlock += util.format(flagArgsTemplate, subArg, this.genZshFlagArgumentsBlock(c.flags)); | ||
argsBlock += format(flagArgsTemplate, subArg, this.genZshFlagArgumentsBlock(c.flags)); | ||
} | ||
return util.format(coTopicCompFunc, this.genZshValuesBlock(subArgs), argsBlock); | ||
return format(coTopicCompFunc, this.genZshValuesBlock(subArgs), argsBlock); | ||
} | ||
@@ -231,3 +231,3 @@ let argsBlock = ''; | ||
}); | ||
argsBlock += util.format(argTemplate, subArg, `_${this.config.bin}_${underscoreSepId}_${subArg}`); | ||
argsBlock += format(argTemplate, subArg, `_${this.config.bin}_${underscoreSepId}_${subArg}`); | ||
} | ||
@@ -242,3 +242,3 @@ for (const c of this.commands.filter((c) => c.id.startsWith(id + ':') && c.id.split(':').length === depth + 1)) { | ||
}); | ||
argsBlock += util.format(flagArgsTemplate, subArg, this.genZshFlagArgumentsBlock(c.flags)); | ||
argsBlock += format(flagArgsTemplate, subArg, this.genZshFlagArgumentsBlock(c.flags)); | ||
} | ||
@@ -263,3 +263,3 @@ const topicCompFunc = `_${this.config.bin}_${underscoreSepId}() { | ||
`; | ||
return util.format(topicCompFunc, this.genZshValuesBlock(subArgs), argsBlock); | ||
return format(topicCompFunc, this.genZshValuesBlock(subArgs), argsBlock); | ||
} | ||
@@ -266,0 +266,0 @@ genZshValuesBlock(subArgs) { |
import { Command } from '@oclif/core'; | ||
import { mkdirSync, openSync, writeSync } from 'node:fs'; | ||
import * as path from 'node:path'; | ||
import path from 'node:path'; | ||
export class AutocompleteBase extends Command { | ||
@@ -5,0 +5,0 @@ get acLogfilePath() { |
@@ -6,3 +6,2 @@ import { AutocompleteBase } from '../../base.js'; | ||
private _commands?; | ||
run(): Promise<void>; | ||
private get bashCommandsWithFlagsList(); | ||
@@ -15,8 +14,4 @@ private get bashCompletionFunction(); | ||
private get commands(); | ||
private createFiles; | ||
private ensureDirs; | ||
private get genAllCommandsMetaString(); | ||
private get genCaseStatementForFlagsMetaString(); | ||
private genCmdPublicFlags; | ||
private genZshFlagSpecs; | ||
private get pwshCompletionFunctionPath(); | ||
@@ -29,2 +24,7 @@ private get pwshFunctionsDir(); | ||
private get zshSetupScriptPath(); | ||
run(): Promise<void>; | ||
private createFiles; | ||
private ensureDirs; | ||
private genCmdPublicFlags; | ||
private genZshFlagSpecs; | ||
} |
import makeDebug from 'debug'; | ||
import { mkdir, writeFile } from 'node:fs/promises'; | ||
import * as path from 'node:path'; | ||
import path from 'node:path'; | ||
import bashAutocompleteWithSpaces from '../../autocomplete/bash-spaces.js'; | ||
import bashAutocomplete from '../../autocomplete/bash.js'; | ||
import bashAutocompleteWithSpaces from '../../autocomplete/bash-spaces.js'; | ||
import PowerShellComp from '../../autocomplete/powershell.js'; | ||
@@ -23,8 +23,2 @@ import ZshCompWithSpaces from '../../autocomplete/zsh.js'; | ||
_commands; | ||
async run() { | ||
// 1. ensure needed dirs | ||
await this.ensureDirs(); | ||
// 2. save (generated) autocomplete files | ||
await this.createFiles(); | ||
} | ||
get bashCommandsWithFlagsList() { | ||
@@ -103,28 +97,4 @@ return this.commands | ||
} | ||
async createFiles() { | ||
// zsh | ||
const supportSpaces = this.config.topicSeparator === ' '; | ||
await Promise.all([ | ||
writeFile(this.bashSetupScriptPath, this.bashSetupScript), | ||
writeFile(this.bashCompletionFunctionPath, this.bashCompletionFunction), | ||
writeFile(this.zshSetupScriptPath, this.zshSetupScript), | ||
// eslint-disable-next-line unicorn/prefer-spread | ||
].concat(process.env.OCLIF_AUTOCOMPLETE_TOPIC_SEPARATOR === 'colon' || !supportSpaces | ||
? [writeFile(this.zshCompletionFunctionPath, this.zshCompletionFunction)] | ||
: [ | ||
writeFile(this.zshCompletionFunctionPath, new ZshCompWithSpaces(this.config).generate()), | ||
writeFile(this.pwshCompletionFunctionPath, new PowerShellComp(this.config).generate()), | ||
])); | ||
} | ||
async ensureDirs() { | ||
// ensure autocomplete cache dir before doing the children | ||
await mkdir(this.autocompleteCacheDir, { recursive: true }); | ||
await Promise.all([ | ||
mkdir(this.bashFunctionsDir, { recursive: true }), | ||
mkdir(this.zshFunctionsDir, { recursive: true }), | ||
mkdir(this.pwshFunctionsDir, { recursive: true }), | ||
]); | ||
} | ||
/* eslint-disable no-useless-escape */ | ||
get genAllCommandsMetaString() { | ||
// eslint-disable-next-line no-useless-escape | ||
return this.commands.map((c) => `\"${c.id.replaceAll(':', '\\:')}:${c.description}\"`).join('\n'); | ||
@@ -147,24 +117,2 @@ } | ||
} | ||
genCmdPublicFlags(Command) { | ||
const Flags = Command.flags || {}; | ||
return Object.keys(Flags) | ||
.filter((flag) => !Flags[flag].hidden) | ||
.map((flag) => `--${flag}`) | ||
.join(' '); | ||
} | ||
genZshFlagSpecs(Klass) { | ||
return Object.keys(Klass.flags || {}) | ||
.filter((flag) => Klass.flags && !Klass.flags[flag].hidden) | ||
.map((flag) => { | ||
const f = (Klass.flags && Klass.flags[flag]) || { description: '' }; | ||
const isBoolean = f.type === 'boolean'; | ||
const isOption = f.type === 'option'; | ||
const name = isBoolean ? flag : `${flag}=-`; | ||
const multiple = isOption && f.multiple ? '*' : ''; | ||
const valueCmpl = isBoolean ? '' : ':'; | ||
const completion = `${multiple}--${name}[${sanitizeDescription(f.summary || f.description)}]${valueCmpl}`; | ||
return `"${completion}"`; | ||
}) | ||
.join('\n'); | ||
} | ||
get pwshCompletionFunctionPath() { | ||
@@ -178,3 +126,2 @@ // <cachedir>/autocomplete/functions/powershell/<bin>.ps1 | ||
} | ||
/* eslint-enable no-useless-escape */ | ||
get zshCompletionFunction() { | ||
@@ -244,2 +191,54 @@ const { cliBin } = this; | ||
} | ||
async run() { | ||
// 1. ensure needed dirs | ||
await this.ensureDirs(); | ||
// 2. save (generated) autocomplete files | ||
await this.createFiles(); | ||
} | ||
async createFiles() { | ||
// zsh | ||
const supportSpaces = this.config.topicSeparator === ' '; | ||
await Promise.all([ | ||
writeFile(this.bashSetupScriptPath, this.bashSetupScript), | ||
writeFile(this.bashCompletionFunctionPath, this.bashCompletionFunction), | ||
writeFile(this.zshSetupScriptPath, this.zshSetupScript), | ||
// eslint-disable-next-line unicorn/prefer-spread | ||
].concat(process.env.OCLIF_AUTOCOMPLETE_TOPIC_SEPARATOR === 'colon' || !supportSpaces | ||
? [writeFile(this.zshCompletionFunctionPath, this.zshCompletionFunction)] | ||
: [ | ||
writeFile(this.zshCompletionFunctionPath, new ZshCompWithSpaces(this.config).generate()), | ||
writeFile(this.pwshCompletionFunctionPath, new PowerShellComp(this.config).generate()), | ||
])); | ||
} | ||
async ensureDirs() { | ||
// ensure autocomplete cache dir before doing the children | ||
await mkdir(this.autocompleteCacheDir, { recursive: true }); | ||
await Promise.all([ | ||
mkdir(this.bashFunctionsDir, { recursive: true }), | ||
mkdir(this.zshFunctionsDir, { recursive: true }), | ||
mkdir(this.pwshFunctionsDir, { recursive: true }), | ||
]); | ||
} | ||
genCmdPublicFlags(Command) { | ||
const Flags = Command.flags || {}; | ||
return Object.keys(Flags) | ||
.filter((flag) => !Flags[flag].hidden) | ||
.map((flag) => `--${flag}`) | ||
.join(' '); | ||
} | ||
genZshFlagSpecs(Klass) { | ||
return Object.keys(Klass.flags || {}) | ||
.filter((flag) => Klass.flags && !Klass.flags[flag].hidden) | ||
.map((flag) => { | ||
const f = (Klass.flags && Klass.flags[flag]) || { description: '' }; | ||
const isBoolean = f.type === 'boolean'; | ||
const isOption = f.type === 'option'; | ||
const name = isBoolean ? flag : `${flag}=-`; | ||
const multiple = isOption && f.multiple ? '*' : ''; | ||
const valueCmpl = isBoolean ? '' : ':'; | ||
const completion = `${multiple}--${name}[${sanitizeDescription(f.summary || f.description)}]${valueCmpl}`; | ||
return `"${completion}"`; | ||
}) | ||
.join('\n'); | ||
} | ||
} |
@@ -70,14 +70,13 @@ import { Args, Flags, ux } from '@oclif/core'; | ||
} | ||
case 'zsh': { | ||
case 'powershell': { | ||
instructions += ` | ||
1) Run this command in your terminal window: | ||
1) Run these two cmdlets in your PowerShell window in the order shown: | ||
${cyan(`printf "$(${scriptCommand})" >> ~/.zshrc; source ~/.zshrc`)} | ||
${cyan(`New-Item -Type Directory -Path (Split-Path -Parent $PROFILE) -ErrorAction SilentlyContinue | ||
Add-Content -Path $PROFILE -Value (Invoke-Expression -Command "${scriptCommand}"); .$PROFILE`)} | ||
The previous command adds the ${cyan(setupEnvVar)} environment variable to your zsh config file and then sources the file. | ||
2) (Optional) If you want matching completions printed below the command line, run this cmdlet: | ||
2) (Optional) Run this command to ensure that you have no permissions conflicts: | ||
${cyan('Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete')} | ||
${cyan('compaudit -D')} | ||
3) Start using autocomplete: | ||
@@ -90,13 +89,14 @@ | ||
} | ||
case 'powershell': { | ||
case 'zsh': { | ||
instructions += ` | ||
1) Run these two cmdlets in your PowerShell window in the order shown: | ||
1) Run this command in your terminal window: | ||
${cyan(`New-Item -Type Directory -Path (Split-Path -Parent $PROFILE) -ErrorAction SilentlyContinue | ||
Add-Content -Path $PROFILE -Value (Invoke-Expression -Command "${scriptCommand}"); .$PROFILE`)} | ||
${cyan(`printf "$(${scriptCommand})" >> ~/.zshrc; source ~/.zshrc`)} | ||
2) (Optional) If you want matching completions printed below the command line, run this cmdlet: | ||
The previous command adds the ${cyan(setupEnvVar)} environment variable to your zsh config file and then sources the file. | ||
${cyan('Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete')} | ||
2) (Optional) Run this command to ensure that you have no permissions conflicts: | ||
${cyan('compaudit -D')} | ||
3) Start using autocomplete: | ||
@@ -103,0 +103,0 @@ |
@@ -8,5 +8,5 @@ import { AutocompleteBase } from '../../base.js'; | ||
static hidden: boolean; | ||
run(): Promise<void>; | ||
private get prefix(); | ||
private get suffix(); | ||
run(): Promise<void>; | ||
} |
import { Args } from '@oclif/core'; | ||
import * as path from 'node:path'; | ||
import path from 'node:path'; | ||
import { AutocompleteBase } from '../../base.js'; | ||
@@ -14,2 +14,8 @@ export default class Script extends AutocompleteBase { | ||
static hidden = true; | ||
get prefix() { | ||
return '\n'; | ||
} | ||
get suffix() { | ||
return ` # ${this.cliBin} autocomplete setup\n`; | ||
} | ||
async run() { | ||
@@ -26,8 +32,2 @@ const { args } = await this.parse(Script); | ||
} | ||
get prefix() { | ||
return '\n'; | ||
} | ||
get suffix() { | ||
return ` # ${this.cliBin} autocomplete setup\n`; | ||
} | ||
} |
@@ -117,3 +117,3 @@ { | ||
}, | ||
"version": "3.2.18" | ||
"version": "3.2.19" | ||
} |
{ | ||
"name": "@oclif/plugin-autocomplete", | ||
"description": "autocomplete plugin for oclif", | ||
"version": "3.2.18", | ||
"version": "3.2.19", | ||
"author": "Salesforce", | ||
@@ -15,2 +15,3 @@ "bugs": "https://github.com/oclif/plugin-autocomplete/issues", | ||
"@commitlint/config-conventional": "^19", | ||
"@eslint/compat": "^1.2.5", | ||
"@oclif/plugin-help": "^6", | ||
@@ -27,6 +28,5 @@ "@oclif/prettier-config": "^0.2.1", | ||
"commitlint": "^19", | ||
"eslint": "^8.57.1", | ||
"eslint-config-oclif": "^5.2.2", | ||
"eslint-config-oclif-typescript": "^3.1.13", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint": "^9.19.0", | ||
"eslint-config-oclif": "^6.0.0", | ||
"eslint-config-prettier": "^10.0.1", | ||
"husky": "^9.1.7", | ||
@@ -37,3 +37,3 @@ "lint-staged": "^15.4.1", | ||
"nyc": "^15.1.0", | ||
"oclif": "^4.17.10", | ||
"oclif": "^4.17.17", | ||
"prettier": "^3.4.2", | ||
@@ -74,3 +74,3 @@ "shx": "^0.3.3", | ||
"compile": "tsc", | ||
"lint": "eslint . --ext .ts", | ||
"lint": "eslint", | ||
"postpack": "yarn run clean", | ||
@@ -77,0 +77,0 @@ "posttest": "yarn lint", |
@@ -64,3 +64,3 @@ # @oclif/plugin-autocomplete | ||
_See code: [src/commands/autocomplete/index.ts](https://github.com/oclif/plugin-autocomplete/blob/v3.2.18/src/commands/autocomplete/index.ts)_ | ||
_See code: [src/commands/autocomplete/index.ts](https://github.com/oclif/plugin-autocomplete/blob/v3.2.19/src/commands/autocomplete/index.ts)_ | ||
<!-- commandsstop --> | ||
@@ -67,0 +67,0 @@ |
62584
-0.21%1425
-0.07%