From 2c26c411b35db66ebdedad1dac443c1eebf200fe Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Sun, 22 Aug 2021 17:19:14 -0600 Subject: [PATCH] feat: add autocomplete --- src/autocomplete.ts | 36 ++++++++++++++++++++++++++++++++++++ src/commands/setup.ts | 2 ++ src/config.ts | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/autocomplete.ts diff --git a/src/autocomplete.ts b/src/autocomplete.ts new file mode 100644 index 00000000..a5375cdd --- /dev/null +++ b/src/autocomplete.ts @@ -0,0 +1,36 @@ +import * as path from 'path'; +import { writeFile } from 'fs/promises'; +import { exec } from 'shelljs'; +import { AsyncCreatable } from '@salesforce/kit'; +import { ConfigFile } from './configFile'; + +const AUTO_COMPLETE_TEMPLATE = ` +#/usr/bin/env bash + +_repo_completions() +{ + local cur + COMPREPLY=() + cur=\${COMP_WORDS[COMP_CWORD]} + code_dir=@CODE_DIRECTORY@ + COMPREPLY=($( compgen -W "$(ls -d $code_dir/*/ | cut -d "/" -f 5)" -- $cur ) ) +} + +complete -F _repo_completions mpm view +complete -F _repo_completions mpm open +`; + +export class AutoComplete extends AsyncCreatable { + public static LOCATION = path.join(ConfigFile.MPM_DIR, 'autocomplete.bash'); + public constructor(private directory: string) { + super(directory); + } + + protected async init(): Promise { + if (process.platform === 'win32') return; + + const contents = AUTO_COMPLETE_TEMPLATE.replace('@CODE_DIRECTORY@', this.directory); + await writeFile(AutoComplete.LOCATION, contents); + exec(`source ${AutoComplete.LOCATION}`, { silent: true }); + } +} diff --git a/src/commands/setup.ts b/src/commands/setup.ts index 1953c869..befb7410 100644 --- a/src/commands/setup.ts +++ b/src/commands/setup.ts @@ -3,6 +3,7 @@ import * as path from 'path'; import { Command, Flags } from '@oclif/core'; import { prompt } from 'inquirer'; import { Config } from '../config'; +import { AutoComplete } from '../autocomplete'; export class Setup extends Command { public static readonly description = 'Setup mpm'; @@ -30,5 +31,6 @@ export class Setup extends Command { await config.write(); this.log(`All repositories will be cloned into ${config.get('directory')}`); + await AutoComplete.create(config.get('directory')); } } diff --git a/src/config.ts b/src/config.ts index 4725f2ed..e6ce165c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,7 +2,7 @@ import * as os from 'os'; import * as path from 'path'; import { ConfigFile, JsonMap } from './configFile'; -export interface Configuration extends JsonMap { +export interface Configuration extends JsonMap { directory: string; }