Skip to content

Commit

Permalink
fix: fixes an issue where the autocomplete could not be written (#4076)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasholzer committed Jan 18, 2022
1 parent 14ce2eb commit 2b94f41
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/lib/completion/generate-autocompletion.js
Expand Up @@ -2,7 +2,7 @@
const { existsSync, mkdirSync, writeFileSync } = require('fs')
const { dirname } = require('path')

const { sortOptions } = require('../../utils')
const { sortOptions, warn } = require('../../utils')

const { AUTOCOMPLETION_FILE } = require('./constants')

Expand All @@ -12,25 +12,33 @@ const { AUTOCOMPLETION_FILE } = require('./constants')
* @returns {void}
*/
const createAutocompletion = (program) => {
const autocomplete = program.commands.reduce(
(prev, cmd) => ({
...prev,
[cmd.name()]: {
name: cmd.name(),
description: cmd.description().split('\n')[0],
options: cmd.options
.filter((option) => !option.hidden)
.sort(sortOptions)
.map((opt) => ({ name: `--${opt.name()}`, description: opt.description })),
},
}),
{},
)
try {
const autocomplete = program.commands.reduce(
(prev, cmd) => ({
...prev,
[cmd.name()]: {
name: cmd.name(),
description: cmd.description().split('\n')[0],
options: cmd.options
.filter((option) => !option.hidden)
.sort(sortOptions)
.map((opt) => ({ name: `--${opt.name()}`, description: opt.description })),
},
}),
{},
)

if (!existsSync(dirname(AUTOCOMPLETION_FILE))) {
mkdirSync(dirname(AUTOCOMPLETION_FILE), { recursive: true })
if (!existsSync(dirname(AUTOCOMPLETION_FILE))) {
mkdirSync(dirname(AUTOCOMPLETION_FILE), { recursive: true })
}
writeFileSync(AUTOCOMPLETION_FILE, JSON.stringify(autocomplete), 'utf-8')
} catch (error_) {
// Sometimes it can happen that the autocomplete generation in the postinstall script lacks permissions
// to write files to the home directory of the user. Therefore just warn with the error and don't break install.
if (error_ instanceof Error) {
warn(`could not create autocompletion.\n${error_.message}`)
}
}
writeFileSync(AUTOCOMPLETION_FILE, JSON.stringify(autocomplete), 'utf-8')
}

module.exports = { createAutocompletion }

1 comment on commit 2b94f41

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 355 MB

Please sign in to comment.