generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7db7695
commit 4e974ca
Showing
10 changed files
with
188 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as path from 'path'; | ||
import * as os from 'os'; | ||
import { writeFile, readFile } from 'fs/promises'; | ||
import { exec } from 'shelljs'; | ||
import { AsyncOptionalCreatable } from '@salesforce/kit'; | ||
|
||
export class BashRc extends AsyncOptionalCreatable { | ||
public static LOCATION = path.join(os.homedir(), '.bashrc'); | ||
public static COMMANDS = ['view', 'open', 'cd']; | ||
|
||
private contents!: string; | ||
|
||
public constructor() { | ||
super(); | ||
} | ||
|
||
public async read(): Promise<string> { | ||
this.contents = await readFile(BashRc.LOCATION, 'utf-8'); | ||
return this.contents; | ||
} | ||
|
||
public async write(): Promise<void> { | ||
await writeFile(BashRc.LOCATION, this.contents); | ||
} | ||
|
||
public has(str: string): boolean { | ||
return this.contents.includes(str); | ||
} | ||
|
||
public append(str: string): void { | ||
if (!this.has(str)) { | ||
this.contents += `${os.EOL}${str}`; | ||
} | ||
} | ||
|
||
public source(): void { | ||
exec(`source ${BashRc.LOCATION}`); | ||
} | ||
|
||
protected async init(): Promise<void> { | ||
if (process.platform === 'win32') return; | ||
await this.read(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Command } from '@oclif/core'; | ||
|
||
export class Cd extends Command { | ||
public static readonly description = 'cd into a github repository.'; | ||
public static readonly flags = {}; | ||
public static readonly args = [ | ||
{ | ||
name: 'repo', | ||
description: 'Name of repository.', | ||
required: true, | ||
}, | ||
]; | ||
|
||
public async run(): Promise<void> { | ||
/** | ||
* Do nothing. The cd command is written to .bashrc on setup since we cannot change the directory | ||
* of the executing shell - instead we must use bash. This command is here just so that it shows up in | ||
* the help output. | ||
* | ||
* See the MpmCd class for more context. | ||
*/ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Command, Flags } from '@oclif/core'; | ||
import { Repos } from '../repos'; | ||
|
||
export class View extends Command { | ||
public static readonly description = 'print location of a github repository.'; | ||
public static readonly flags = { | ||
remote: Flags.boolean({ | ||
description: 'Return url of repository', | ||
default: false, | ||
}), | ||
}; | ||
public static readonly args = [ | ||
{ | ||
name: 'repo', | ||
description: 'Name of repository.', | ||
required: true, | ||
}, | ||
]; | ||
public static readonly aliases = ['v']; | ||
|
||
public async run(): Promise<void> { | ||
const { args, flags } = await this.parse(View); | ||
const repos = await Repos.create(); | ||
const repo = repos.get(args.repo); | ||
if (!repo) { | ||
process.exitCode = 1; | ||
throw new Error(`${args.repo as string} has not been added yet.`); | ||
} | ||
|
||
this.log(flags.remote ? repo.urls.html : repo.location); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as path from 'path'; | ||
import { writeFile } from 'fs/promises'; | ||
import { AsyncOptionalCreatable } from '@salesforce/kit'; | ||
import { ConfigFile } from './configFile'; | ||
import { BashRc } from './bashRc'; | ||
|
||
const TEMPLATE = ` | ||
#/usr/bin/env bash | ||
function mpm { | ||
if [[ "$1" == "cd" ]]; then | ||
cd $(mpm where $2) | ||
else | ||
mpm "$@" | ||
fi | ||
} | ||
`; | ||
|
||
/** | ||
* It's not possible to use node to change the directory of the executing | ||
* shell so instead we write a mpm function to the .bashrc so that we can | ||
* capture the `mpm cd` execution and use bash instead. | ||
*/ | ||
export class MpmCd extends AsyncOptionalCreatable { | ||
public static LOCATION = path.join(ConfigFile.MPM_DIR, 'mpmcd.bash'); | ||
public constructor() { | ||
super(); | ||
} | ||
|
||
protected async init(): Promise<void> { | ||
if (process.platform === 'win32') return; | ||
|
||
await writeFile(MpmCd.LOCATION, TEMPLATE); | ||
const bashRc = await BashRc.create(); | ||
bashRc.append(`source ${MpmCd.LOCATION}`); | ||
await bashRc.write(); | ||
bashRc.source(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters