Skip to content

Commit

Permalink
feat: add autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Aug 22, 2021
1 parent 61c4b00 commit 2c26c41
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/autocomplete.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
public static LOCATION = path.join(ConfigFile.MPM_DIR, 'autocomplete.bash');
public constructor(private directory: string) {
super(directory);
}

protected async init(): Promise<void> {
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 });
}
}
2 changes: 2 additions & 0 deletions src/commands/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'));
}
}
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
directory: string;
}

Expand Down

0 comments on commit 2c26c41

Please sign in to comment.