From d9d652ea399db88ba0d7a90d18dbc8de1bc6c996 Mon Sep 17 00:00:00 2001 From: Ryan Garant Date: Tue, 12 Feb 2019 13:55:11 -0800 Subject: [PATCH] feat(list based cmds): enable moment date formatting for cmds that have listing functionality re #625 --- CONTRIBUTING.md | 2 +- README.md | 35 +++++++++++++++++++++++++++++++++++ package-lock.json | 2 +- package.json | 2 +- src/cmds/gists.ts | 9 ++++----- src/cmds/issue.ts | 9 +++++---- src/cmds/milestone.ts | 5 ++++- src/cmds/notification.ts | 6 +++++- src/cmds/pull-request.ts | 10 ++++++---- src/cmds/repo.ts | 3 ++- src/logger.ts | 13 +++++++++---- 11 files changed, 73 insertions(+), 23 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 40e54ff0..842a25aa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -29,7 +29,7 @@ First, if you haven't already done so, run npm install: To run **incrementally** in watch mode: -- `npm run build:watch` +- `npm run dev` To run **once**: diff --git a/README.md b/README.md index cf5fdb6e..a367d0bb 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ gh pull-request | `-m`, `--me` | _Optional_ | `Boolean` | | `-d`, `--detailed` | _Optional_ | `Boolean` | | `--direction` | _Optional_ | [`asc`, `desc`] | +| `--date` | _Optional_ | `String` | | `-b`, `--branch` | _Optional_ | `String` | | `--remote` | _Optional_ | `String` | | `-r`, `--repo` | _Optional_ | `String` | @@ -198,6 +199,12 @@ gh pr --list --sort complexity gh pr --list --link ``` +- List open pull requests with a formatted date (_Any string that the moment library's formatter accepts should work: https://momentjs.com/docs/#/displaying/format/_). + +``` +gh pr --list --date "dddd, MMMM Do YYYY, h:mm:ss a" +``` + ### 3. Fetch | Option | Usage | Type | @@ -411,6 +418,7 @@ gh notification | `--remote` | _Optional_ | `String` | | `-r`, `--repo` | _Optional_ | `String` | | `-u`, `--user` | _Optional_ | `String` | +| `--date` | _Optional_ | `String` | #### Examples @@ -426,6 +434,12 @@ gh nt gh nt --latest --user eduardolundgren --repo node-gh ``` +- Diplay notifications with a formatted date (_Any string that the moment library's formatter accepts should work: https://momentjs.com/docs/#/displaying/format/_). + +``` +gh nt --date "dddd, MMMM Do YYYY, h:mm:ss a" +``` + ### 2. Watch | Option | Usage | Type | @@ -570,6 +584,7 @@ gh is 1 --close --user eduardolundgren | `-l`, `--list` | **Required** | `Boolean` | | `-a`, `--all` | _Optional_ | `Boolean` | | `-A`, `--assignee` | _Optional_ | `String` | +| `--date` | _Optional_ | `String` | | `-d`, `--detailed` | _Optional_ | `Boolean` | | `-L`, `--label` | _Optional_ | `String` | | `-M`, `--milestone` | _Optional_ | [`Number`, `String`] | @@ -610,6 +625,12 @@ gh is --list --detailed gh is --list --state closed ``` +- List issues with a formatted date (_Any string that the moment library's formatter accepts should work: https://momentjs.com/docs/#/displaying/format/_). + +``` +gh is --list --date "dddd, MMMM Do YYYY, h:mm:ss a" +``` + - List issues filtered by milestone. ``` @@ -755,6 +776,7 @@ gh re --browser --user eduardolundgren --repo node-gh | `-d`, `--detailed` | _Optional_ | `Boolean` | | `-u`, `--user` | _Optional_ | `String` | | `-t`, `--type` | _Optional_ | [`all`, `owner`, `public`, `private`, `member`] | +| `--date` | _Optional_ | `String` | #### Examples @@ -776,6 +798,12 @@ gh re --list --type private gh re --list --user zenorocha ``` +- List open repositories with a formatted date (_Any string that the moment library's formatter accepts should work: https://momentjs.com/docs/#/displaying/format/_). + +``` +gh re --list --detailed --date "dddd, MMMM Do YYYY, h:mm:ss a" +``` + ### 3. Create | Option | Usage | Type | @@ -1010,6 +1038,7 @@ gh gi --browser --id 5991877 | -------------- | ------------ | --------- | | `-l`, `--list` | **Required** | `Boolean` | | `-u`, `--user` | _Optional_ | `String` | +| `--date` | _Optional_ | `String` | #### Examples @@ -1025,6 +1054,12 @@ gh gi --list gh gi --list --user brunocoelho ``` +- List gists with a formatted date (_Any string that the moment library's formatter accepts should work: https://momentjs.com/docs/#/displaying/format/_). + +``` +gh gi --list --date "dddd, MMMM Do YYYY, h:mm:ss a" +``` + ### 3. Create | Option | Usage | Type | diff --git a/package-lock.json b/package-lock.json index 01cf9cac..482e6ba5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "gh", - "version": "1.14.0", + "version": "1.14.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 532ef3e6..9941e8de 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,7 @@ }, "scripts": { "build": "tsc", - "build:watch": "tsc --watch", + "dev": "tsc --watch", "commit": "git-cz", "format": "prettier 'lib/**/*.js' && prettier 'bin/*.js'", "env:test": "cross-env NODE_ENV=testing GH_USER=protoEvangelion GH_TOKEN=0001", diff --git a/src/cmds/gists.ts b/src/cmds/gists.ts index 8e2349f0..9aa2e136 100644 --- a/src/cmds/gists.ts +++ b/src/cmds/gists.ts @@ -31,6 +31,7 @@ Gists.DETAILS = { options: { browser: Boolean, content: String, + date: String, delete: [String, Array], description: String, fork: String, @@ -219,11 +220,9 @@ Gists.prototype.listCallback_ = function(err, gists, opt_callback) { if (gists && gists.length > 0) { gists.forEach(gist => { - logger.log( - `${logger.colors.yellow(`${gist.owner.login}/${gist.id}`)} ${logger.getDuration( - gist.updated_at - )}` - ) + const duration = logger.getDuration(gist.updated_at, options.date) + + logger.log(`${logger.colors.yellow(`${gist.owner.login}/${gist.id}`)} ${duration}`) if (gist.description) { logger.log(gist.description) diff --git a/src/cmds/issue.ts b/src/cmds/issue.ts index 6339a003..fdbeac91 100644 --- a/src/cmds/issue.ts +++ b/src/cmds/issue.ts @@ -39,6 +39,7 @@ Issue.DETAILS = { browser: Boolean, close: Boolean, comment: String, + date: String, detailed: Boolean, label: String, list: Boolean, @@ -439,7 +440,7 @@ Issue.prototype.list = function(user, repo, opt_callback) { }) if (issues && issues.length > 0) { - const formattedIssues = formatIssues(issues, options.detailed) + const formattedIssues = formatIssues(issues, options.detailed, options.date) logger.log(formattedIssues) } else { @@ -552,7 +553,7 @@ Issue.prototype.search = function(user, repo, opt_callback, options) { }) if (issues && issues.length > 0) { - var formattedIssues = formatIssues(issues, options.detailed) + var formattedIssues = formatIssues(issues, options.detailed, options.date) logger.log(formattedIssues) } else { @@ -563,7 +564,7 @@ Issue.prototype.search = function(user, repo, opt_callback, options) { }) } -function formatIssues(issues, showDetailed) { +function formatIssues(issues, showDetailed, dateFormatter?: string) { issues.sort((a, b) => { return a.number > b.number ? -1 : 1 }) @@ -572,7 +573,7 @@ function formatIssues(issues, showDetailed) { const formattedIssuesArr = issues.map(issue => { const issueNumber = logger.colors.green(`#${issue.number}`) const issueUser = logger.colors.magenta(`@${issue.user.login}`) - const issueDate = `(${logger.getDuration(issue.created_at)})` + const issueDate = `(${logger.getDuration(issue.created_at, dateFormatter)})` let formattedIssue = `${issueNumber} ${issue.title} ${issueUser} ${issueDate}` diff --git a/src/cmds/milestone.ts b/src/cmds/milestone.ts index 306285e9..7ad68a5f 100644 --- a/src/cmds/milestone.ts +++ b/src/cmds/milestone.ts @@ -33,6 +33,7 @@ Milestone.DETAILS = { commands: ['list'], options: { all: Boolean, + date: String, organization: String, list: Boolean, }, @@ -102,7 +103,9 @@ Milestone.prototype.list = function(user, repo, opt_callback) { if (milestones && milestones.length > 0) { milestones.forEach(milestone => { - const due = milestone.due_on ? logger.getDuration(milestone.due_on) : 'n/a' + const due = milestone.due_on + ? logger.getDuration(milestone.due_on, options.date) + : 'n/a' const description = milestone.description || '' const title = logger.colors.green(milestone.title) const state = logger.colors.magenta(`@${milestone.state} (due ${due})`) diff --git a/src/cmds/notification.ts b/src/cmds/notification.ts index 88512aa8..0944871d 100644 --- a/src/cmds/notification.ts +++ b/src/cmds/notification.ts @@ -29,6 +29,7 @@ Notifications.DETAILS = { description: 'Provides a set of util commands to work with Notifications.', commands: ['latest', 'watch'], options: { + date: String, latest: Boolean, remote: String, repo: String, @@ -121,7 +122,10 @@ Notifications.prototype.latest = function(opt_watch, done) { logger.log( `${logger.colors.magenta(`@${event.actor.login}`)} ${ event.txt - } ${logger.colors.cyan(options.repo)} ${logger.getDuration(event.created_at)}` + } ${logger.colors.cyan(options.repo)} ${logger.getDuration( + event.created_at, + options.date + )}` ) }) } diff --git a/src/cmds/pull-request.ts b/src/cmds/pull-request.ts index 90ce9c73..bb74d249 100755 --- a/src/cmds/pull-request.ts +++ b/src/cmds/pull-request.ts @@ -59,6 +59,7 @@ PullRequest.DETAILS = { browser: Boolean, close: Boolean, comment: String, + date: String, description: String, detailed: Boolean, direction: String, @@ -509,7 +510,8 @@ PullRequest.prototype.printPullsInfoTable_ = function(pulls) { pulls.forEach(pull => { const { createdTime, number, prInfo, user, status } = getColorizedFields( pull, - isCompact ? getTotalWidth() : tableWidths[1] + isCompact ? getTotalWidth() : tableWidths[1], + options.date ) const body: TCell = [ @@ -534,8 +536,8 @@ PullRequest.prototype.printPullsInfoTable_ = function(pulls) { return table.toString() } - function getColorizedFields(pull, length) { - const createdTime = logger.getDuration(pull.created_at) + function getColorizedFields(pull, length, dateFormatter) { + const createdTime = logger.getDuration(pull.created_at, dateFormatter) const number = logger.colors.green(`#${pull.number}`) const prInfo = formatPrInfo(pull, length) const user = logger.colors.magenta(`@${pull.user.login}`) @@ -637,7 +639,7 @@ PullRequest.prototype.printPullInfo_ = function(pull) { var headline = `${logger.colors.green(`#${pull.number}`)} ${pull.title} ${logger.colors.magenta( `@${pull.user.login}` - )} (${logger.getDuration(pull.created_at)})${status}` + )} (${logger.getDuration(pull.created_at, options.date)})${status}` if (options.link) { headline += ` ${logger.colors.blue(pull.html_url)}` diff --git a/src/cmds/repo.ts b/src/cmds/repo.ts index db0c292a..236c4485 100644 --- a/src/cmds/repo.ts +++ b/src/cmds/repo.ts @@ -34,6 +34,7 @@ Repo.DETAILS = { browser: Boolean, clone: Boolean, color: String, + date: String, delete: String, description: String, detailed: Boolean, @@ -492,7 +493,7 @@ Repo.prototype.listCallback_ = function(err, repos, opt_callback) { logger.log(logger.colors.blue(repo.homepage)) } - logger.log(`last update ${logger.getDuration(repo.updated_at)}`) + logger.log(`last update ${logger.getDuration(repo.updated_at, options.date)}`) } if (options.isTTY.out) { diff --git a/src/logger.ts b/src/logger.ts index cf91f91d..be6b15d0 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -65,11 +65,16 @@ export function log(...args) { console.log(...args) } -export function getDuration(start, opt_end?) { - if (!opt_end) { - opt_end = testing ? '2018-10-10T16:00:00Z' : Date.now() +export function getDuration(start, formatter?) { + if (testing) { + return moment(start).from('2018-10-10T16:00:00Z') } - return moment.duration(moment(start).diff(opt_end)).humanize(true) + + if (formatter) { + return moment(start).format(formatter) + } + + return moment(start).fromNow() } export function applyReplacements(output, replaceMap?: object) {