Skip to content

Commit

Permalink
fix: render ejs templates
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiand391 committed May 15, 2023
1 parent 49b87e0 commit b4d2af8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@oclif/plugin-help": "^5",
"@oclif/test": "^2",
"@types/chai": "^4",
"@types/ejs": "^3.1.2",
"@types/fs-extra": "^9.0.13",
"@types/mocha": "^8",
"@types/nock": "^11.1.0",
Expand Down Expand Up @@ -65,4 +66,4 @@
"version": "oclif readme && git add README.md",
"build": "shx rm -rf lib && tsc"
}
}
}
31 changes: 16 additions & 15 deletions src/autocomplete/powershell.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import * as util from 'util'
import {EOL} from 'os'
import {Config, Interfaces, Command} from '@oclif/core'

function sanitizeSummary(description?: string): string {
if (description === undefined) {
// PowerShell:
// [System.Management.Automation.CompletionResult] will error out if will error out if you pass in an empty string for the summary.
return ' '
}
return description
.replace(/"/g, '""') // escape double quotes.
.replace(/`/g, '``') // escape backticks.
.split(EOL)[0] // only use the first line
}
import * as ejs from 'ejs'

type CommandCompletion = {
id: string;
Expand Down Expand Up @@ -78,7 +67,7 @@ export default class PowerShellComp {
// skip hidden flags
if (f.hidden) continue

const flagSummary = sanitizeSummary(f.summary || f.description)
const flagSummary = this.sanitizeSummary(f.summary || f.description)

if (f.type === 'option' && f.multiple) {
flaghHashtables.push(
Expand Down Expand Up @@ -167,6 +156,18 @@ ${flaghHashtables.join('\n')}
return leafTpl
}

private sanitizeSummary(summary?: string): string {
if (summary === undefined) {
// PowerShell:
// [System.Management.Automation.CompletionResult] will error out if will error out if you pass in an empty string for the summary.
return ' '
}
return ejs.render(summary, {config: this.config})
.replace(/"/g, '""') // escape double quotes.
.replace(/`/g, '``') // escape backticks.
.split(EOL)[0] // only use the first line
}

public generate(): string {
const genNode = (partialId: string): Record<string, any> => {
const node: Record<string, any> = {}
Expand Down Expand Up @@ -410,7 +411,7 @@ Register-ArgumentCompleter -Native -CommandName ${this.config.bin} -ScriptBlock
})
.map(t => {
const description = t.description ?
sanitizeSummary(t.description) :
this.sanitizeSummary(t.description) :
`${t.name.replace(/:/g, ' ')} commands`

return {
Expand All @@ -428,7 +429,7 @@ Register-ArgumentCompleter -Native -CommandName ${this.config.bin} -ScriptBlock
this.config.plugins.forEach(p => {
p.commands.forEach(c => {
if (c.hidden) return
const summary = sanitizeSummary(c.summary || c.description)
const summary = this.sanitizeSummary(c.summary || c.description)
const flags = c.flags
cmds.push({
id: c.id,
Expand Down
29 changes: 15 additions & 14 deletions src/autocomplete/zsh.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import * as util from 'util'
import {Config, Interfaces, Command} from '@oclif/core'

function sanitizeSummary(description?: string): string {
if (description === undefined) {
return ''
}
return description
.replace(/([`"])/g, '\\\\\\$1') // backticks and double-quotes require triple-backslashes
// eslint-disable-next-line no-useless-escape
.replace(/([\[\]])/g, '\\\\$1') // square brackets require double-backslashes
.split('\n')[0] // only use the first line
}
import * as ejs from 'ejs'

const argTemplate = ' "%s")\n %s\n ;;\n'

Expand Down Expand Up @@ -44,6 +34,17 @@ export default class ZshCompWithSpaces {
this.commands = this.getCommands()
}

private sanitizeSummary(summary?: string): string {
if (summary === undefined) {
return ''
}
return ejs.render(summary, {config: this.config})
.replace(/([`"])/g, '\\\\\\$1') // backticks and double-quotes require triple-backslashes
// eslint-disable-next-line no-useless-escape
.replace(/([\[\]])/g, '\\\\$1') // square brackets require double-backslashes
.split('\n')[0] // only use the first line
}

public generate(): string {
const firstArgs: {id: string; summary?: string}[] = []

Expand Down Expand Up @@ -133,7 +134,7 @@ _${this.config.bin}
// skip hidden flags
if (f.hidden) continue

const flagSummary = sanitizeSummary(f.summary || f.description)
const flagSummary = this.sanitizeSummary(f.summary || f.description)

let flagSpec = ''

Expand Down Expand Up @@ -369,7 +370,7 @@ _${this.config.bin}
return 0
})
.map(t => {
const description = t.description ? sanitizeSummary(t.description) : `${t.name.replace(/:/g, ' ')} commands`
const description = t.description ? this.sanitizeSummary(t.description) : `${t.name.replace(/:/g, ' ')} commands`

return {
name: t.name,
Expand All @@ -386,7 +387,7 @@ _${this.config.bin}
this.config.plugins.forEach(p => {
p.commands.forEach(c => {
if (c.hidden) return
const summary = sanitizeSummary(c.summary || c.description)
const summary = this.sanitizeSummary(c.summary || c.description)
const flags = c.flags
cmds.push({
id: c.id,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,11 @@
dependencies:
"@types/node" "*"

"@types/ejs@^3.1.2":
version "3.1.2"
resolved "https://registry.yarnpkg.com/@types/ejs/-/ejs-3.1.2.tgz#75d277b030bc11b3be38c807e10071f45ebc78d9"
integrity sha512-ZmiaE3wglXVWBM9fyVC17aGPkLo/UgaOjEiI2FXQfyczrCefORPxIe+2dVmnmk3zkVIbizjrlQzmPGhSYGXG5g==

"@types/eslint-visitor-keys@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
Expand Down

0 comments on commit b4d2af8

Please sign in to comment.