Skip to content

Commit

Permalink
chore(autocomplete): tslint -> eslint (#1414)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo committed Feb 3, 2020
1 parent bb45760 commit 50c6625
Show file tree
Hide file tree
Showing 20 changed files with 336 additions and 294 deletions.
1 change: 1 addition & 0 deletions packages/autocomplete/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
9 changes: 9 additions & 0 deletions packages/autocomplete/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": [
"oclif",
"oclif-typescript"
],
"rules": {
"unicorn/no-abusive-eslint-disable": "off"
}
}
9 changes: 6 additions & 3 deletions packages/autocomplete/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@
"@oclif/dev-cli": "1.21.3",
"@oclif/plugin-help": "2.1.6",
"@oclif/test": "1.2.4",
"@oclif/tslint": "3.1.1",
"@types/chai": "4.1.7",
"@types/fs-extra": "^5.0.5",
"@types/lodash.flatten": "^4.4.6",
"@types/mocha": "5.2.6",
"@types/nock": "^9.3.1",
"@types/node": "10.12.24",
"chai": "4.2.0",
"eslint": "^6.7.2",
"eslint-config-oclif": "^3.1.0",
"eslint-config-oclif-typescript": "^0.1.0",
"globby": "9.0.0",
"mocha": "5.2.0",
"nock": "^10.0.6",
"nyc": "13.2.0",
"ts-node": "8.0.2",
"tslint": "5.11.0",
"typescript": "3.3.3333"
},
"engines": {
Expand Down Expand Up @@ -64,7 +65,9 @@
"repository": "heroku/cli",
"scripts": {
"postpublish": "rm -f oclif.manifest.json",
"posttest": "tsc -p test --noEmit && tslint -p test -t stylish",
"pretest": "tsc -p test --noEmit",
"posttest": "yarn lint",
"lint": "eslint . --ext .ts --config .eslintrc",
"prepack": "rm -rf lib && tsc && oclif-dev manifest",
"test": "nyc mocha --forbid-only \"test/**/*.test.ts\""
}
Expand Down
5 changes: 3 additions & 2 deletions packages/autocomplete/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export abstract class AutocompleteBase extends Command {

writeLogFile(msg: string) {
const now = new Date()
let entry = `[${now}] ${msg}\n`
let fd = fs.openSync(this.acLogfilePath, 'a')
const entry = `[${now}] ${msg}\n`
const fd = fs.openSync(this.acLogfilePath, 'a')
// eslint-disable-next-line
// @ts-ignore
fs.write(fd, entry)
}
Expand Down
12 changes: 6 additions & 6 deletions packages/autocomplete/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ export async function updateCache(cachePath: string, cache: any) {
await fs.writeJSON(cachePath, cache)
}

function _mtime(f: any): Date {
return fs.statSync(f).mtime
}

function _isStale(cachePath: string, cacheDuration: number): boolean {
const past = new Date()
past.setSeconds(past.getSeconds() - cacheDuration)
return past.getTime() > _mtime(cachePath).getTime()
}

function _mtime(f: any): Date {
return fs.statSync(f).mtime
}

export async function fetchCache(cachePath: string, cacheDuration: number, options: any): Promise<Array<string>> {
let cachePresent = fs.existsSync(cachePath)
const cachePresent = fs.existsSync(cachePath)
if (cachePresent && !_isStale(cachePath, cacheDuration)) {
return fs.readJSON(cachePath)
}
const cache = await options.cacheFn()
// TODO: move this to a fork
// to-do: move this to a fork
await updateCache(cachePath, cache)
return cache
}
91 changes: 48 additions & 43 deletions packages/autocomplete/src/commands/autocomplete/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const debug = require('debug')('autocomplete:create')

export default class Create extends AutocompleteBase {
static hidden = true

static description = 'create autocomplete setup scripts and completion functions'

private _commands?: Command[]
Expand Down Expand Up @@ -62,17 +63,17 @@ export default class Create extends AutocompleteBase {
if (this._commands) return this._commands

const plugins = this.config.plugins
let commands: Command[] = []
const commands: Command[] = []

plugins.map(p => {
p.commands.map(c => {
plugins.forEach(p => {
p.commands.forEach(c => {
if (c.hidden) return
try {
commands.push(c)
} catch (err) {
} catch (error) {
debug(`Error creating completions for command ${c.id}`)
debug(err.message)
this.writeLogFile(err.message)
debug(error.message)
this.writeLogFile(error.message)
}
})
})
Expand All @@ -86,10 +87,10 @@ export default class Create extends AutocompleteBase {
try {
const publicFlags = this.genCmdPublicFlags(c).trim()
return `${c.id} ${publicFlags}`
} catch (err) {
} catch (error) {
debug(`Error creating bash completion for command ${c.id}, moving on...`)
debug(err.message)
this.writeLogFile(err.message)
debug(error.message)
this.writeLogFile(error.message)
return ''
}
}).join('\n')
Expand All @@ -105,10 +106,10 @@ export default class Create extends AutocompleteBase {
const cmdsWithDescriptions = this.commands.map(c => {
try {
return this.genCmdWithDescription(c)
} catch (err) {
} catch (error) {
debug(`Error creating zsh autocomplete for command ${c.id}, moving on...`)
debug(err.message)
this.writeLogFile(err.message)
debug(error.message)
this.writeLogFile(error.message)
return ''
}
})
Expand All @@ -120,27 +121,27 @@ export default class Create extends AutocompleteBase {
return this.commands.map(c => {
try {
return this.genZshCmdFlagsSetter(c)
} catch (err) {
} catch (error) {
debug(`Error creating zsh autocomplete for command ${c.id}, moving on...`)
debug(err.message)
this.writeLogFile(err.message)
debug(error.message)
this.writeLogFile(error.message)
return ''
}
}).join('\n')
}

private genCmdPublicFlags(Command: Command): string {
let Flags = Command.flags || {}
const Flags = Command.flags || {}
return Object.keys(Flags)
.filter(flag => !Flags[flag].hidden)
.map(flag => `--${flag}`)
.join(' ')
.filter(flag => !Flags[flag].hidden)
.map(flag => `--${flag}`)
.join(' ')
}

private genCmdWithDescription(Command: Command): string {
let description = ''
if (Command.description) {
let text = Command.description.split('\n')[0]
const text = Command.description.split('\n')[0]
description = `:"${text}"`
}
return `"${Command.id.replace(/:/g, '\\:')}"${description}`
Expand All @@ -149,20 +150,24 @@ export default class Create extends AutocompleteBase {
private genZshCmdFlagsSetter(Command: Command): string {
const id = Command.id
const flagscompletions = Object.keys(Command.flags || {})
.filter(flag => Command.flags && !Command.flags[flag].hidden)
.map(flag => {
const f = (Command.flags && Command.flags[flag]) || {description: ''}
const isBoolean = f.type === 'boolean'
const hasCompletion = f.hasOwnProperty('completion') || this.findCompletion(id, flag, f.description)
const name = isBoolean ? flag : `${flag}=-`
let cachecompl = ''
if (hasCompletion) { cachecompl = ': :_compadd_flag_options' }
if (this.wantsLocalFiles(flag)) { cachecompl = ': :_files' }
const help = isBoolean ? '(switch) ' : (hasCompletion ? '(autocomplete) ' : '')
const completion = `--${name}[${help}${f.description}]${cachecompl}`
return `"${completion}"`
})
.join('\n')
.filter(flag => Command.flags && !Command.flags[flag].hidden)
.map(flag => {
const f = (Command.flags && Command.flags[flag]) || {description: ''}
const isBoolean = f.type === 'boolean'
const hasCompletion = 'completion' in f || this.findCompletion(id, flag, f.description)
const name = isBoolean ? flag : `${flag}=-`
let cachecompl = ''
if (hasCompletion) {
cachecompl = ': :_compadd_flag_options'
}
if (this.wantsLocalFiles(flag)) {
cachecompl = ': :_files'
}
const help = isBoolean ? '(switch) ' : (hasCompletion ? '(autocomplete) ' : '')
const completion = `--${name}[${help}${f.description}]${cachecompl}`
return `"${completion}"`
})
.join('\n')

if (flagscompletions) {
return `_set_${id.replace(/:/g, '_')}_flags () {
Expand Down Expand Up @@ -200,14 +205,14 @@ ${cmdsWithDesc.join('\n')}
return `${this.envAnalyticsDir}
${this.envCommandsPath}
HEROKU_AC_BASH_COMPFUNC_PATH=${path.join(
__dirname,
'..',
'..',
'..',
'autocomplete',
'bash',
'heroku.bash',
)} && test -f $HEROKU_AC_BASH_COMPFUNC_PATH && source $HEROKU_AC_BASH_COMPFUNC_PATH;
__dirname,
'..',
'..',
'..',
'autocomplete',
'bash',
'heroku.bash',
)} && test -f $HEROKU_AC_BASH_COMPFUNC_PATH && source $HEROKU_AC_BASH_COMPFUNC_PATH;
`
}

Expand Down Expand Up @@ -238,7 +243,7 @@ bindkey "^I" expand-or-complete-with-dots`
private wantsLocalFiles(flag: string): boolean {
return [
'file',
'procfile'
'procfile',
].includes(flag)
}
}
28 changes: 16 additions & 12 deletions packages/autocomplete/src/commands/autocomplete/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {AutocompleteBase} from '../../base'

export default class Doctor extends AutocompleteBase {
static hidden = true

static description = 'autocomplete diagnostic'

static args = [
{name: 'shell', description: 'shell type', required: false},
]

static flags = {
verbose: flags.boolean({description: 'list completable commands'}),
}
Expand All @@ -20,7 +23,7 @@ export default class Doctor extends AutocompleteBase {
const shell = args.shell || this.config.shell
this.errorIfNotSupportedShell(shell)

let data = []
const data = []

// cli version
data.push({name: 'cli version', value: this.config.version})
Expand Down Expand Up @@ -51,8 +54,8 @@ export default class Doctor extends AutocompleteBase {
const appsCache = path.join(this.completionsCacheDir, 'app')
let appsCacheValue
if (fs.existsSync(appsCache)) {
let length = fs.readJSONSync(appsCache).length
appsCacheValue = length ? length : 'empty'
const length = fs.readJSONSync(appsCache).length
appsCacheValue = length || 'empty'
} else {
appsCacheValue = 'missing'
}
Expand All @@ -62,9 +65,9 @@ export default class Doctor extends AutocompleteBase {
ux.table(data, {
printHeader: undefined,
columns: [
{key: 'name'},
{key: 'value'},
]
{key: 'name'},
{key: 'value'},
],
})

if (flags.verbose) this.printList()
Expand All @@ -75,23 +78,24 @@ export default class Doctor extends AutocompleteBase {
const header = 'Completable Commands'
this.log(header)
this.log('='.repeat(header.length))
this.config.plugins.map(p => {
p.commands.map(c => {
this.config.plugins.forEach(p => {
p.commands.forEach(c => {
try {
if (c.hidden) {
this.log(`${c.id} (hidden)`)
} else {
let results = Object.keys(c.flags).map((f: string) => {
const results = Object.keys(c.flags).map((f: string) => {
let out = `--${f}`
let flag = c.flags[f]
const flag = c.flags[f]
if (flag.type === 'option') out += '='
if (flag.hasOwnProperty('completion') || this.findCompletion(c.id, f, flag.description)) {
const hasCompletion = 'completion' in flag || this.findCompletion(c.id, f, flag.description)
if (hasCompletion) {
out += '(c)'
}
if (flag.hidden) out += '(h)'
return out
})
if (results.length) this.log(`${c.id} -> ${results}`)
if (results.length > 0) this.log(`${c.id} -> ${results}`)
}
} catch {
this.log(`Error creating autocomplete for command ${c.id}`)
Expand Down
2 changes: 1 addition & 1 deletion packages/autocomplete/src/commands/autocomplete/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class Index extends AutocompleteBase {
'$ heroku autocomplete',
'$ heroku autocomplete bash',
'$ heroku autocomplete zsh',
'$ heroku autocomplete --refresh-cache'
'$ heroku autocomplete --refresh-cache',
]

async run() {
Expand Down
Loading

0 comments on commit 50c6625

Please sign in to comment.