Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8156e4b
[wip] added duration example
jdx Mar 14, 2017
b4a29a2
remove debug statements
jdx Mar 14, 2017
94f11d9
flow fix
jdx Mar 14, 2017
6872182
use new mixin syntax
jdx Mar 15, 2017
98cb5c1
use new mixin syntax
jdx Mar 15, 2017
07ed6fa
example showing parsing of a custom flag
KarateCowboy Mar 16, 2017
82092c2
Merge branch 'master' into feature/duration-mjo
KarateCowboy Mar 16, 2017
bcd88e3
review feedback
KarateCowboy Mar 16, 2017
81dbbc5
Merge branch 'feature/duration-mjo' of github.com:heroku/cli-engine-e…
KarateCowboy Mar 16, 2017
690c7e5
use promise race to demonstrate timing out
KarateCowboy Mar 16, 2017
19fdf1c
remove empty line in package.json
KarateCowboy Mar 16, 2017
7c56151
reformat per 'Standard' rules
KarateCowboy Mar 16, 2017
4d787fc
fix bad import statement
KarateCowboy Mar 16, 2017
2fb752e
remove semi-colons
KarateCowboy Mar 16, 2017
2000cea
one more semi-colon
KarateCowboy Mar 16, 2017
da3a4f6
requested changes
KarateCowboy Mar 16, 2017
a25b98b
fix errs
KarateCowboy Mar 16, 2017
4659c9a
move declaration of wait function
KarateCowboy Mar 16, 2017
d6746f4
test to see version of node on CI
KarateCowboy Mar 17, 2017
ec829e4
Standard formatting
KarateCowboy Mar 17, 2017
8afdeaa
remove version check
KarateCowboy Mar 17, 2017
d9e1a76
use async/await
KarateCowboy Mar 17, 2017
57ba207
flow type compliance
KarateCowboy Mar 17, 2017
e3847e9
fixed argument passed to parse
KarateCowboy Mar 17, 2017
7e27a68
added a fallback default to stop flow complaining
KarateCowboy Mar 17, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lib
node_modules
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"testEnvironment": "node",
"rootDir": "./src"
},
"keywords": [
"heroku-plugin"
],
"license": "ISC",
"main": "lib/index.js",
"scripts": {
Expand Down
16 changes: 16 additions & 0 deletions src/commands/duration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @flow

import Command from 'cli-engine-command'
import Duration from '../mixins/duration'

export default class extends Command {
static topic = 'cli'
static command = 'duration'
static description = 'this is an example command showing duration parsing'

duration = new Duration(this)

run () {
this.log(`duration: ${this.duration.toString()}`)
}
}
27 changes: 27 additions & 0 deletions src/commands/dynorestart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @flow

import Command from 'cli-engine-command'
import { default as Duration, FLAG as DurationFlag } from '../mixins/duration'

export default class extends Command {
static topic = 'cli'
static command = 'dynorestart'
static description = 'this is an example command showing duration parsing. It takes five seconds to complete.'
static flags = [
DurationFlag
]

async sleep (ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) }

duration = new Duration(this)

run () {
console.log('Restarting dynos. This will take five seconds. That is too long')
return Promise.race([this.duration.wait(), this.main()])
}

async main () {
await this.sleep(5000)
console.log('Done restarting dynos')
}
}
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// @flow

export const topics = [
{name: 'cli', description: 'example CLI engine topic'}
{
name: 'cli',
description: 'example CLI engine topic'
}
]

import CLI from './commands/cli'
export const commands = [CLI]
export const commands = [
require('./commands/cli'),
require('./commands/dynorestart')
]
60 changes: 60 additions & 0 deletions src/mixins/duration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// @flow

import type Command from 'cli-engine-command'

export const FLAG = {
name: 'duration',
description: 'a duration',
required: true,
hasValue: true
}

export default class Duration {
cmd: Command

constructor (cmd: Command) {
this.cmd = cmd
}

parse (arg: string) {
let result = arg.match(/^(\d+) ?(ms|[sm]|milliseconds?|seconds?|minutes?)$/)
if (result) {
let magnitude = parseInt(result[1])
let unit = result[2]
let multiplier = 1
switch (unit) {
case 'ms':
case 'millisecond':
case 'milliseconds':
multiplier = 1
break
case 's':
case 'second':
case 'seconds':
multiplier = 1000
break
case 'm':
case 'minute':
case 'minutes':
multiplier = 1000 * 60
break
default:
}
return magnitude * multiplier
}
}

async sleep (ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) }

async wait () {
let ms = this.parse(this.cmd.flags.duration) || 5000
let resolved = await this.sleep(ms)
console.log('operation did not complete in provided duration')
return resolved
}

get duration (): number {
let duration = this.parse(this.cmd.flags.duration)
return Number(duration)
}
}
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ cli-cursor@^1.0.1:
dependencies:
restore-cursor "^1.0.1"


cli-engine-command@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/cli-engine-command/-/cli-engine-command-2.1.4.tgz#54a4196968a1a58b43084aa323b6045aadf2ba5f"
Expand All @@ -590,6 +591,7 @@ cli-engine-command@^2.1.4:
string "^3.3.3"
supports-color "^3.2.3"


cli-width@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.1.0.tgz#b234ca209b29ef66fc518d9b98d5847b00edf00a"
Expand Down