Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
fix(alias): add alias cmd back
Browse files Browse the repository at this point in the history
re #661
  • Loading branch information
Ryan Garant committed Sep 18, 2019
1 parent f4de999 commit 841e2a2
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "node",
"request": "launch",
"runtimeArgs": ["-r", "ts-node/register"],
"args": ["${workspaceFolder}/src/debug.ts", "is", "test"],
"args": ["${workspaceFolder}/src/debug.ts", "user", "--user=zeno"],
// "args": ["${workspaceFolder}/src/debug.ts", "ji", "LWM-117", "--status"],
"console": "integratedTerminal"
},
Expand Down
80 changes: 70 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [Gists](#gists)
- [User](#user)
- [Milestone](#milestone)
- [Alias](#alias)
- [Config](#config)
- [Plugins](#plugins)
- [Team](#team)
Expand Down Expand Up @@ -112,6 +113,16 @@ gh pull-request
| `-r`, `--repo` | _Optional_ | `String` |
| `-u`, `--user` | _Optional_ | `String` |

```
gh pr
```

- Get information about a pull request.

```
gh pr --info number
```

### 2. List

| Option | Usage | Type |
Expand Down Expand Up @@ -139,16 +150,6 @@ gh pull-request
- **Shortcut** for listing open pull requests for the current repository.
- To turn off pretty printing of output in a table add `"pretty_print": false` to your `~/.gh-json` config

```
gh pr
```

- Get information about a pull request.

```
gh pr --info number
```

- List open pull requests for all branches from all your repositories.

```
Expand Down Expand Up @@ -1208,6 +1209,65 @@ gh ms --list --user node-gh --repo gh
gh ms --list --all --organization node-gh
```

## Alias

This cmd provides something similar to shell aliases. If there are aliases in your .gh.json file, we will attempt to resolve the user, PR forwarder or PR submitter to your alias.

```
gh alias
```

> **Alias:** `gh al`
### 1. List

| Option | Usage | Type |
| -------------- | ------------ | --------- |
| `-l`, `--list` | **Required** | `Boolean` |

#### Examples

- **Shortcut** for listing aliases.

```
gh alias
```

- List aliases.

```
gh alias --list
```

### 2. Add

| Option | Usage | Type |
| -------------- | ------------ | -------- |
| `-a`, `--add` | **Required** | `String` |
| `-u`, `--user` | **Required** | `String` |

#### Examples

- Create alias for username.

```
gh alias --add zeno --user zenorocha
```

### 3. Remove

| Option | Usage | Type |
| ---------------- | ------------ | -------- |
| `-r`, `--remove` | **Required** | `String` |

#### Examples

- Remove alias.

```
gh alias --remove zeno
```

## Config

There are some pretty useful configurations that you can set on [.gh.json](default.gh.json).
Expand Down
6 changes: 6 additions & 0 deletions __tests__/__snapshots__/alias.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E: Alias Module Test User alias is expanded \`gh user --user=zeno\` 1`] = `
"You're logged in as zenorocha
"
`;
14 changes: 14 additions & 0 deletions __tests__/alias.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
* (see file: CONTRIBUTORS)
* SPDX-License-Identifier: BSD-3-Clause
*/

import { runCmd } from './testUtils'

describe('E2E: Alias Module Test', () => {
it('User alias is expanded `gh user --user=zeno`', done => {
expect(runCmd('gh user --user=zeno')).toMatchSnapshot()
done()
})
})
6 changes: 5 additions & 1 deletion default.gh.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,9 @@

"ssh": true,

"signature": ""
"signature": "",

"alias": {
"zeno": "zenorocha"
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ export function clone(o) {

export function load() {}

/**
* Checks if there are aliases in your .gh.json file.
* If there are aliases in your .gh.json file, we will attempt to resolve the user, PR forwarder or PR submitter to your alias.
*/
export function expandAliases(options) {
if (config.alias) {
options.fwd = config.alias[options.fwd] || options.fwd
options.submit = config.alias[options.submit] || options.submit
options.user = config.alias[options.user] || options.user
}
}

export function find(filepath, opt_pattern) {
return fs.readdirSync(filepath).filter(file => {
return (opt_pattern || /.*/).test(file)
Expand Down
4 changes: 3 additions & 1 deletion src/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as fs from 'fs'
import * as nopt from 'nopt'
import * as path from 'path'
import * as updateNotifier from 'update-notifier'
import { find, getUser } from './base'
import { find, getUser, expandAliases } from './base'
import * as configs from './configs'
import * as git from './git'
import * as logger from './logger'
Expand Down Expand Up @@ -144,6 +144,8 @@ export async function setUp() {
}
}

expandAliases(options)

options.repo = options.repo || git.getRepoFromRemoteURL(remoteUrl)
options.currentBranch = testing ? 'master' : git.getCurrentBranch()
options.github_host = config.github_host
Expand Down
100 changes: 100 additions & 0 deletions src/cmds/alias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
* (see file: CONTRIBUTORS)
* SPDX-License-Identifier: BSD-3-Clause
*/

// -- Requires -------------------------------------------------------------------------------------

import * as base from '../base'
import * as configs from '../configs'
import * as logger from '../logger'

const config = base.getConfig()

// -- Constructor ----------------------------------------------------------------------------------

export default function Alias(options) {
this.options = options
}

// -- Constants ------------------------------------------------------------------------------------

Alias.DETAILS = {
alias: 'al',
description: 'Create alias for a username.',
commands: ['add', 'list', 'remove'],
options: {
add: String,
list: Boolean,
remove: String,
user: String,
},
shorthands: {
a: ['--add'],
l: ['--list'],
r: ['--remove'],
u: ['--user'],
},
payload(payload, options) {
if (payload[0]) {
options.add = payload[0]
options.user = payload[1]
} else {
options.list = true
}
},
}

// -- Commands -------------------------------------------------------------------------------------

Alias.prototype.run = function() {
const instance = this
const options = instance.options

if (options.add) {
if (!options.user) {
logger.error('You must specify an user, try --user username.')
}

logger.debug(`Creating alias ${options.add}`)
instance.add()
}

if (options.list) {
instance.list((err, data) => {
let item

for (item in data) {
if (data.hasOwnProperty(item)) {
logger.log(`${logger.colors.cyan(item)}: ${logger.colors.magenta(data[item])}`)
}
}
})
}

if (options.remove) {
logger.debug(`Removing alias ${options.remove}`)
instance.remove()
}
}

Alias.prototype.add = function() {
const instance = this
const options = instance.options

configs.writeGlobalConfig(`alias.${options.add}`, options.user)
}

Alias.prototype.list = function(opt_callback) {
opt_callback && opt_callback(null, config.alias)
}

Alias.prototype.remove = function() {
const instance = this
const options = instance.options

delete config.alias[options.remove]

configs.writeGlobalConfig('alias', config.alias)
}

0 comments on commit 841e2a2

Please sign in to comment.