From 8156e4bdbb7428e284325e968c30b1321350054c Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Tue, 14 Mar 2017 12:58:33 -0700 Subject: [PATCH 01/23] [wip] added duration example --- src/commands/duration.js | 18 ++++++++++++++++++ src/index.js | 6 ++++-- src/mixins/duration.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/commands/duration.js create mode 100644 src/mixins/duration.js diff --git a/src/commands/duration.js b/src/commands/duration.js new file mode 100644 index 0000000..de9921b --- /dev/null +++ b/src/commands/duration.js @@ -0,0 +1,18 @@ +// @ flow + +import Command from 'cli-engine-command' +import DurationMixin from '../mixins/duration' + +export default class extends DurationMixin(Command) { + static topic = 'cli' + static command = 'duration' + static description = 'this is an example command showing duration parsing' + + static flags = [ + {name: 'foo'} + ] + + run () { + this.log(`duration: ${this.duration}`) + } +} diff --git a/src/index.js b/src/index.js index 143f283..f826392 100644 --- a/src/index.js +++ b/src/index.js @@ -4,5 +4,7 @@ export const topics = [ {name: 'cli', description: 'example CLI engine topic'} ] -import CLI from './commands/cli' -export const commands = [CLI] +export const commands = [ + require('./commands/cli'), + require('./commands/duration') +] diff --git a/src/mixins/duration.js b/src/mixins/duration.js new file mode 100644 index 0000000..02d0310 --- /dev/null +++ b/src/mixins/duration.js @@ -0,0 +1,29 @@ +// @flow + +import Command, {type Flag} from 'cli-engine-command' // eslint-disable-line + +const FLAG: Flag = { + name: 'duration', + description: 'a duration', + required: true, + hasValue: true +} + +declare class App extends Command { + duration: Date +} + +export default function > (Base: T): $Shape> { + return class DurationMixin extends Base { + static get flags (): Flag[] { return this._flags.concat([FLAG]) } + static set flags (flags: Flag[]) { this._flags = flags } + + get duration (): Date { + this.inspect(this.constructor.flags) + + // this is a little gross but I think needed to cast it to a string + let duration = (((this.flags.duration): any): string) + return new Date(duration) + } + } +} From b4a29a2d1887fc40d2c39ded16682435c0787151 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Tue, 14 Mar 2017 13:20:36 -0700 Subject: [PATCH 02/23] remove debug statements --- src/commands/duration.js | 4 ---- src/mixins/duration.js | 4 +--- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/commands/duration.js b/src/commands/duration.js index de9921b..bbd4d5a 100644 --- a/src/commands/duration.js +++ b/src/commands/duration.js @@ -8,10 +8,6 @@ export default class extends DurationMixin(Command) { static command = 'duration' static description = 'this is an example command showing duration parsing' - static flags = [ - {name: 'foo'} - ] - run () { this.log(`duration: ${this.duration}`) } diff --git a/src/mixins/duration.js b/src/mixins/duration.js index 02d0310..8262439 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -15,12 +15,10 @@ declare class App extends Command { export default function > (Base: T): $Shape> { return class DurationMixin extends Base { - static get flags (): Flag[] { return this._flags.concat([FLAG]) } + static get flags (): Flag[] { return super.flags.concat([FLAG]) } static set flags (flags: Flag[]) { this._flags = flags } get duration (): Date { - this.inspect(this.constructor.flags) - // this is a little gross but I think needed to cast it to a string let duration = (((this.flags.duration): any): string) return new Date(duration) From 94f11d9dfc1b1cf94c9d00625b59ed6b3694df15 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Tue, 14 Mar 2017 13:28:53 -0700 Subject: [PATCH 03/23] flow fix --- src/commands/duration.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/duration.js b/src/commands/duration.js index bbd4d5a..8e504d2 100644 --- a/src/commands/duration.js +++ b/src/commands/duration.js @@ -1,4 +1,4 @@ -// @ flow +// @flow import Command from 'cli-engine-command' import DurationMixin from '../mixins/duration' @@ -9,6 +9,6 @@ export default class extends DurationMixin(Command) { static description = 'this is an example command showing duration parsing' run () { - this.log(`duration: ${this.duration}`) + this.log(`duration: ${this.duration.toString()}`) } } From 68721826a6aad702f0e632499a3b9a99354b1509 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Wed, 15 Mar 2017 09:15:31 -0700 Subject: [PATCH 04/23] use new mixin syntax --- package.json | 2 +- src/commands/duration.js | 6 ++++-- src/mixins/duration.js | 26 +++++++++++--------------- yarn.lock | 6 +++--- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index fb300c8..98da989 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "2.0.5", "author": "Jeff Dickey @dickeyxxxx", "dependencies": { - "cli-engine-command": "^2.1.0" + "cli-engine-command": "^2.1.3" }, "devDependencies": { "babel-cli": "^6.24.0", diff --git a/src/commands/duration.js b/src/commands/duration.js index 8e504d2..7ca8b96 100644 --- a/src/commands/duration.js +++ b/src/commands/duration.js @@ -1,13 +1,15 @@ // @flow import Command from 'cli-engine-command' -import DurationMixin from '../mixins/duration' +import Duration from '../mixins/duration' -export default class extends DurationMixin(Command) { +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()}`) } diff --git a/src/mixins/duration.js b/src/mixins/duration.js index 8262439..9b90b34 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -1,27 +1,23 @@ // @flow -import Command, {type Flag} from 'cli-engine-command' // eslint-disable-line +import type Command from 'cli-engine-command' -const FLAG: Flag = { +export const FLAG = { name: 'duration', description: 'a duration', required: true, hasValue: true } -declare class App extends Command { - duration: Date -} - -export default function > (Base: T): $Shape> { - return class DurationMixin extends Base { - static get flags (): Flag[] { return super.flags.concat([FLAG]) } - static set flags (flags: Flag[]) { this._flags = flags } +export default class DurationMixin { + cmd: Command + constructor (cmd: Command) { + this.cmd = cmd + } - get duration (): Date { - // this is a little gross but I think needed to cast it to a string - let duration = (((this.flags.duration): any): string) - return new Date(duration) - } + get duration (): Date { + // this is a little gross but I think needed to cast it to a string + let duration = this.cmd.flags.duration + return new Date(duration) } } diff --git a/yarn.lock b/yarn.lock index d6efd83..cf381e9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -573,9 +573,9 @@ cli-cursor@^1.0.1: dependencies: restore-cursor "^1.0.1" -cli-engine-command@^2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/cli-engine-command/-/cli-engine-command-2.1.0.tgz#7d117a6296b72adb6542af6986f756a293422939" +cli-engine-command@^2.1.3: + version "2.1.3" + resolved "https://registry.yarnpkg.com/cli-engine-command/-/cli-engine-command-2.1.3.tgz#a6744839bb31dee6186bf836b8fb86025a872427" dependencies: ansi-escapes "1.4.0" cardinal "1.0.0" From 98cb5c154aec9ee68c0d4125a68e1153897fdba2 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Wed, 15 Mar 2017 09:16:47 -0700 Subject: [PATCH 05/23] use new mixin syntax --- src/mixins/duration.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mixins/duration.js b/src/mixins/duration.js index 9b90b34..5f737c2 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -9,14 +9,13 @@ export const FLAG = { hasValue: true } -export default class DurationMixin { +export default class Duration { cmd: Command constructor (cmd: Command) { this.cmd = cmd } get duration (): Date { - // this is a little gross but I think needed to cast it to a string let duration = this.cmd.flags.duration return new Date(duration) } From 07ed6fa255c70cae07ab594d23474f6debca9ff5 Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 11:45:20 -0500 Subject: [PATCH 06/23] example showing parsing of a custom flag --- .gitignore | 1 + package.json | 1 + src/commands/printbricks.js | 21 +++++++++++++++++++++ src/index.js | 7 +++++-- src/mixins/duration.js | 35 ++++++++++++++++++++++++++++++++--- 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 src/commands/printbricks.js diff --git a/.gitignore b/.gitignore index a65b417..3063f07 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ lib +node_modules diff --git a/package.json b/package.json index 98da989..2b1e9f8 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "release": "np", "test": "jest && flow && standard && npm run prepare" }, + "keywords": [ "heroku-plugin" ], "standard": { "parser": "babel-eslint", "env": "jest", diff --git a/src/commands/printbricks.js b/src/commands/printbricks.js new file mode 100644 index 0000000..7c6270f --- /dev/null +++ b/src/commands/printbricks.js @@ -0,0 +1,21 @@ +// @flow + +import Command from 'cli-engine-command' +import Duration from '../mixins/duration' +import { FLAG as DurationFlag } from '../mixins/duration' + +export default class extends Command { + static topic = 'cli' + static command = 'printbricks' + static description = 'this is an example command showing duration parsing' + static flags = [ + DurationFlag + ] + run() { + let duration = new Duration(this) + for(let i = 1; i <= 10; i++){ + console.log('#') + } + console.log(`Hypothetically, you could have this command abort if it took longer than your supplied ${duration.duration} ms`) + } +} diff --git a/src/index.js b/src/index.js index f826392..931dbd2 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,13 @@ // @flow export const topics = [ - {name: 'cli', description: 'example CLI engine topic'} + { + name: 'cli', + description: 'example CLI engine topic' + } ] export const commands = [ require('./commands/cli'), - require('./commands/duration') + require('./commands/printbricks') ] diff --git a/src/mixins/duration.js b/src/mixins/duration.js index 5f737c2..da671b9 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -13,10 +13,39 @@ export default class Duration { cmd: Command constructor (cmd: Command) { this.cmd = cmd + // setTimeout(this.cmd.reject, this.parse(this.cmd.flags.duration)) + } + 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: + // throw new Exception(`could not parse '${arg}'`) + } + return magnitude * multiplier; + } } - get duration (): Date { - let duration = this.cmd.flags.duration - return new Date(duration) + get duration (): number { + let duration = this.parse(this.cmd.flags.duration) + return Number(duration) } } From bcd88e36d4e677ac0bfa4223857fa901ce818422 Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 13:03:01 -0500 Subject: [PATCH 07/23] review feedback --- package.json | 4 +++- src/commands/printbricks.js | 4 ++-- src/mixins/duration.js | 1 - 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 2b1e9f8..e96396d 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,9 @@ "testEnvironment": "node", "rootDir": "./src" }, + "keywords": [ + "heroku-plugin" + ], "license": "ISC", "main": "lib/index.js", "scripts": { @@ -33,7 +36,6 @@ "release": "np", "test": "jest && flow && standard && npm run prepare" }, - "keywords": [ "heroku-plugin" ], "standard": { "parser": "babel-eslint", "env": "jest", diff --git a/src/commands/printbricks.js b/src/commands/printbricks.js index 7c6270f..adda74c 100644 --- a/src/commands/printbricks.js +++ b/src/commands/printbricks.js @@ -11,11 +11,11 @@ export default class extends Command { static flags = [ DurationFlag ] + duration = new Duration(this) run() { - let duration = new Duration(this) for(let i = 1; i <= 10; i++){ console.log('#') } - console.log(`Hypothetically, you could have this command abort if it took longer than your supplied ${duration.duration} ms`) + console.log(`Hypothetically, you could have this command abort if it took longer than your supplied ${this.duration.duration} ms`) } } diff --git a/src/mixins/duration.js b/src/mixins/duration.js index da671b9..1addcaf 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -13,7 +13,6 @@ export default class Duration { cmd: Command constructor (cmd: Command) { this.cmd = cmd - // setTimeout(this.cmd.reject, this.parse(this.cmd.flags.duration)) } parse(arg : string){ let result = arg.match(/^(\d+) ?(ms|[sm]|milliseconds?|seconds?|minutes?)$/); From 690c7e521c968f54f6c794956d76fbc655dff1fb Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 14:11:08 -0500 Subject: [PATCH 08/23] use promise race to demonstrate timing out --- src/commands/dynorestart.js | 29 +++++++++++++++++++++++++++++ src/commands/printbricks.js | 21 --------------------- src/index.js | 2 +- src/mixins/duration.js | 16 ++++++++++++---- 4 files changed, 42 insertions(+), 26 deletions(-) create mode 100644 src/commands/dynorestart.js delete mode 100644 src/commands/printbricks.js diff --git a/src/commands/dynorestart.js b/src/commands/dynorestart.js new file mode 100644 index 0000000..2bee221 --- /dev/null +++ b/src/commands/dynorestart.js @@ -0,0 +1,29 @@ +// @flow + +import Command from 'cli-engine-command' +import Duration from '../mixins/duration' +import {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 + ] + 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()]) + } + + main() { + return new Promise(resolve => { + setTimeout(() => { + console.log('Done restarting dynos'); + resolve() + }, 5000) + }) + } +} diff --git a/src/commands/printbricks.js b/src/commands/printbricks.js deleted file mode 100644 index adda74c..0000000 --- a/src/commands/printbricks.js +++ /dev/null @@ -1,21 +0,0 @@ -// @flow - -import Command from 'cli-engine-command' -import Duration from '../mixins/duration' -import { FLAG as DurationFlag } from '../mixins/duration' - -export default class extends Command { - static topic = 'cli' - static command = 'printbricks' - static description = 'this is an example command showing duration parsing' - static flags = [ - DurationFlag - ] - duration = new Duration(this) - run() { - for(let i = 1; i <= 10; i++){ - console.log('#') - } - console.log(`Hypothetically, you could have this command abort if it took longer than your supplied ${this.duration.duration} ms`) - } -} diff --git a/src/index.js b/src/index.js index 931dbd2..c1d60f3 100644 --- a/src/index.js +++ b/src/index.js @@ -9,5 +9,5 @@ export const topics = [ export const commands = [ require('./commands/cli'), - require('./commands/printbricks') + require('./commands/dynorestart') ] diff --git a/src/mixins/duration.js b/src/mixins/duration.js index 1addcaf..6d1bc56 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -11,10 +11,12 @@ export const FLAG = { export default class Duration { cmd: Command - constructor (cmd: Command) { + + constructor(cmd: Command) { this.cmd = cmd } - parse(arg : string){ + + parse(arg: string) { let result = arg.match(/^(\d+) ?(ms|[sm]|milliseconds?|seconds?|minutes?)$/); if (result) { let magnitude = parseInt(result[1]); @@ -37,13 +39,19 @@ export default class Duration { multiplier = 1000 * 60; break; default: - // throw new Exception(`could not parse '${arg}'`) } return magnitude * multiplier; } } - get duration (): number { + wait() { + return new Promise(resolve => setTimeout(() => { + console.log('operation did not complete in provided duration'); + resolve() + }, this.parse(this.cmd.flags.duration))) + } + + get duration(): number { let duration = this.parse(this.cmd.flags.duration) return Number(duration) } From 19fdf1cd7371c745116c358a7a424b6472823a1e Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 14:12:36 -0500 Subject: [PATCH 09/23] remove empty line in package.json --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 950450d..99e7e45 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,6 @@ "version": "2.0.6", "author": "Jeff Dickey @dickeyxxxx", "dependencies": { - "cli-engine-command": "^2.1.4" }, "devDependencies": { From 7c56151993b2e4538fc9d0e65842ff0715824066 Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 16:06:20 -0500 Subject: [PATCH 10/23] reformat per 'Standard' rules --- src/commands/dynorestart.js | 7 +++---- src/mixins/duration.js | 8 ++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/commands/dynorestart.js b/src/commands/dynorestart.js index 2bee221..41b1153 100644 --- a/src/commands/dynorestart.js +++ b/src/commands/dynorestart.js @@ -1,8 +1,7 @@ // @flow import Command from 'cli-engine-command' -import Duration from '../mixins/duration' -import {FLAG as DurationFlag} from '../mixins/duration' +import { Duration, FLAG as DurationFlag } from '../mixins/duration' export default class extends Command { static topic = 'cli' @@ -13,12 +12,12 @@ export default class extends Command { ] duration = new Duration(this) - run() { + run () { console.log('Restarting dynos. This will take five seconds. That is too long') return Promise.race([this.duration.wait(), this.main()]) } - main() { + main () { return new Promise(resolve => { setTimeout(() => { console.log('Done restarting dynos'); diff --git a/src/mixins/duration.js b/src/mixins/duration.js index 6d1bc56..b65fb09 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -12,11 +12,11 @@ export const FLAG = { export default class Duration { cmd: Command - constructor(cmd: Command) { + constructor (cmd: Command) { this.cmd = cmd } - parse(arg: string) { + parse (arg: string) { let result = arg.match(/^(\d+) ?(ms|[sm]|milliseconds?|seconds?|minutes?)$/); if (result) { let magnitude = parseInt(result[1]); @@ -44,14 +44,14 @@ export default class Duration { } } - wait() { + wait () { return new Promise(resolve => setTimeout(() => { console.log('operation did not complete in provided duration'); resolve() }, this.parse(this.cmd.flags.duration))) } - get duration(): number { + get duration (): number { let duration = this.parse(this.cmd.flags.duration) return Number(duration) } From 4d787fc92a28eedcf78825acd4958cd869fc5c28 Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 16:10:23 -0500 Subject: [PATCH 11/23] fix bad import statement --- src/commands/dynorestart.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/dynorestart.js b/src/commands/dynorestart.js index 41b1153..9e05121 100644 --- a/src/commands/dynorestart.js +++ b/src/commands/dynorestart.js @@ -1,7 +1,7 @@ // @flow import Command from 'cli-engine-command' -import { Duration, FLAG as DurationFlag } from '../mixins/duration' +import { default as Duration, FLAG as DurationFlag } from '../mixins/duration' export default class extends Command { static topic = 'cli' From 2fb752e386cfcc83b44fedb238a94f5ebb5a1ca7 Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 16:14:58 -0500 Subject: [PATCH 12/23] remove semi-colons --- src/mixins/duration.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/mixins/duration.js b/src/mixins/duration.js index b65fb09..14f778c 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -17,36 +17,36 @@ export default class Duration { } parse (arg: string) { - let result = arg.match(/^(\d+) ?(ms|[sm]|milliseconds?|seconds?|minutes?)$/); + let result = arg.match(/^(\d+) ?(ms|[sm]|milliseconds?|seconds?|minutes?)$/) if (result) { - let magnitude = parseInt(result[1]); - let unit = result[2]; - let multiplier = 1; + let magnitude = parseInt(result[1]) + let unit = result[2] + let multiplier = 1 switch (unit) { case 'ms': case 'millisecond': case 'milliseconds': - multiplier = 1; - break; + multiplier = 1 + break case 's': case 'second': case 'seconds': - multiplier = 1000; - break; + multiplier = 1000 + break case 'm': case 'minute': case 'minutes': - multiplier = 1000 * 60; - break; + multiplier = 1000 * 60 + break default: } - return magnitude * multiplier; + return magnitude * multiplier } } wait () { return new Promise(resolve => setTimeout(() => { - console.log('operation did not complete in provided duration'); + console.log('operation did not complete in provided duration') resolve() }, this.parse(this.cmd.flags.duration))) } From 2000ceaf5d7db5a211c45455cccd24acdf0b1afb Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 16:17:37 -0500 Subject: [PATCH 13/23] one more semi-colon --- src/commands/dynorestart.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/dynorestart.js b/src/commands/dynorestart.js index 9e05121..10c52f4 100644 --- a/src/commands/dynorestart.js +++ b/src/commands/dynorestart.js @@ -20,7 +20,7 @@ export default class extends Command { main () { return new Promise(resolve => { setTimeout(() => { - console.log('Done restarting dynos'); + console.log('Done restarting dynos') resolve() }, 5000) }) From da3a4f6a1f93d64b086fbdb3175b7c95bd84525c Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 17:22:22 -0500 Subject: [PATCH 14/23] requested changes --- src/commands/dynorestart.js | 12 +++++------- src/mixins/duration.js | 9 ++++----- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/commands/dynorestart.js b/src/commands/dynorestart.js index 10c52f4..76e1932 100644 --- a/src/commands/dynorestart.js +++ b/src/commands/dynorestart.js @@ -11,18 +11,16 @@ export default class extends Command { DurationFlag ] duration = new Duration(this) + wait = async function wait(ms: int) { return new Promise(resolve => setTimeout(resolve, ms)) } run () { console.log('Restarting dynos. This will take five seconds. That is too long') return Promise.race([this.duration.wait(), this.main()]) } - main () { - return new Promise(resolve => { - setTimeout(() => { - console.log('Done restarting dynos') - resolve() - }, 5000) - }) + + async main () { + await this.wait(5000) + console.log('Done restarting dynos') } } diff --git a/src/mixins/duration.js b/src/mixins/duration.js index 14f778c..aa0cd44 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -44,11 +44,10 @@ export default class Duration { } } - wait () { - return new Promise(resolve => setTimeout(() => { - console.log('operation did not complete in provided duration') - resolve() - }, this.parse(this.cmd.flags.duration))) + async wait () { + let resolved = await this.cmd.wait(this.parse(this.cmd.flags.duration)) + console.log('operation did not complete in provided duration') + return resolved } get duration (): number { From a25b98b8317d1b95e7d1405aaa3ab9a255e41b01 Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 17:30:16 -0500 Subject: [PATCH 15/23] fix errs --- src/commands/dynorestart.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/dynorestart.js b/src/commands/dynorestart.js index 76e1932..6d87423 100644 --- a/src/commands/dynorestart.js +++ b/src/commands/dynorestart.js @@ -11,7 +11,7 @@ export default class extends Command { DurationFlag ] duration = new Duration(this) - wait = async function wait(ms: int) { return new Promise(resolve => setTimeout(resolve, ms)) } + async wait(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) } run () { console.log('Restarting dynos. This will take five seconds. That is too long') From 4659c9af6e27f1d86e05aaa37cf8e6b83501c298 Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 17:44:43 -0500 Subject: [PATCH 16/23] move declaration of wait function --- src/commands/dynorestart.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/dynorestart.js b/src/commands/dynorestart.js index 6d87423..a76023d 100644 --- a/src/commands/dynorestart.js +++ b/src/commands/dynorestart.js @@ -10,8 +10,8 @@ export default class extends Command { static flags = [ DurationFlag ] - duration = new Duration(this) async wait(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') From d6746f4c6d5b85d2c0a84987171151d53952973d Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 22:04:23 -0500 Subject: [PATCH 17/23] test to see version of node on CI --- src/mixins/duration.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/mixins/duration.js b/src/mixins/duration.js index aa0cd44..2b9ff71 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -45,9 +45,11 @@ export default class Duration { } async wait () { - let resolved = await this.cmd.wait(this.parse(this.cmd.flags.duration)) - console.log('operation did not complete in provided duration') - return resolved + console.log(`current version of node is ${process.version}`) + return new Promise(resolve => setTimeout(resolve, this.parse(this.cmd.flags.duration))) + .then(() => { + console.log('operation did not complete in provided duration') + }) } get duration (): number { From ec829e48677c9ebad8c86eb05745ec3e4509385b Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 22:07:06 -0500 Subject: [PATCH 18/23] Standard formatting --- src/commands/dynorestart.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/commands/dynorestart.js b/src/commands/dynorestart.js index a76023d..e425bd5 100644 --- a/src/commands/dynorestart.js +++ b/src/commands/dynorestart.js @@ -10,7 +10,9 @@ export default class extends Command { static flags = [ DurationFlag ] - async wait(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) } + + async wait (ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) } + duration = new Duration(this) run () { @@ -18,7 +20,6 @@ export default class extends Command { return Promise.race([this.duration.wait(), this.main()]) } - async main () { await this.wait(5000) console.log('Done restarting dynos') From 8afdeaa294f74efcd0eb7803e212df2be4d984f4 Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Thu, 16 Mar 2017 22:10:02 -0500 Subject: [PATCH 19/23] remove version check --- src/mixins/duration.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/mixins/duration.js b/src/mixins/duration.js index 2b9ff71..a5e887d 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -45,7 +45,6 @@ export default class Duration { } async wait () { - console.log(`current version of node is ${process.version}`) return new Promise(resolve => setTimeout(resolve, this.parse(this.cmd.flags.duration))) .then(() => { console.log('operation did not complete in provided duration') From d9e1a76871f1758ee9f2b17a890e44b012c47e0e Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Fri, 17 Mar 2017 10:55:25 -0500 Subject: [PATCH 20/23] use async/await --- src/commands/dynorestart.js | 4 ++-- src/mixins/duration.js | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/commands/dynorestart.js b/src/commands/dynorestart.js index e425bd5..35a69d9 100644 --- a/src/commands/dynorestart.js +++ b/src/commands/dynorestart.js @@ -11,7 +11,7 @@ export default class extends Command { DurationFlag ] - async wait (ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) } + async sleep (ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) } duration = new Duration(this) @@ -21,7 +21,7 @@ export default class extends Command { } async main () { - await this.wait(5000) + await this.sleep(5000) console.log('Done restarting dynos') } } diff --git a/src/mixins/duration.js b/src/mixins/duration.js index a5e887d..92260c0 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -43,12 +43,13 @@ export default class Duration { return magnitude * multiplier } } + async sleep (ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) } async wait () { - return new Promise(resolve => setTimeout(resolve, this.parse(this.cmd.flags.duration))) - .then(() => { - console.log('operation did not complete in provided duration') - }) + + let resolved = await this.sleep(this.parse(this.cmd.flags.duration)) + console.log('operation did not complete in provided duration') + return resolved } get duration (): number { From 57ba207e3cce36ce71d1536ef97a819a36c25317 Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Fri, 17 Mar 2017 10:58:22 -0500 Subject: [PATCH 21/23] flow type compliance --- src/mixins/duration.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mixins/duration.js b/src/mixins/duration.js index 92260c0..6736d3f 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -46,8 +46,8 @@ export default class Duration { async sleep (ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) } async wait () { - - let resolved = await this.sleep(this.parse(this.cmd.flags.duration)) + let ms = this.parse(this.cmd.duration) + let resolved = await this.sleep(ms) console.log('operation did not complete in provided duration') return resolved } From e3847e97af19c44c50dfc1b48f52a2fdd47fe9e2 Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Fri, 17 Mar 2017 11:03:13 -0500 Subject: [PATCH 22/23] fixed argument passed to parse --- src/mixins/duration.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mixins/duration.js b/src/mixins/duration.js index 6736d3f..a3f34c2 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -43,11 +43,11 @@ export default class Duration { return magnitude * multiplier } } + async sleep (ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) } async wait () { - let ms = this.parse(this.cmd.duration) - let resolved = await this.sleep(ms) + let resolved = await this.sleep(this.parse(this.cmd.flags.duration)) console.log('operation did not complete in provided duration') return resolved } From 7e27a68b36732188d20db4bbb84c14686e4eaed3 Mon Sep 17 00:00:00 2001 From: Matthew Origer Date: Fri, 17 Mar 2017 13:23:41 -0500 Subject: [PATCH 23/23] added a fallback default to stop flow complaining --- src/mixins/duration.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mixins/duration.js b/src/mixins/duration.js index a3f34c2..73b8b6f 100644 --- a/src/mixins/duration.js +++ b/src/mixins/duration.js @@ -47,7 +47,8 @@ export default class Duration { async sleep (ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) } async wait () { - let resolved = await this.sleep(this.parse(this.cmd.flags.duration)) + 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 }