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 0ae4836..99e7e45 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": { diff --git a/src/commands/duration.js b/src/commands/duration.js new file mode 100644 index 0000000..7ca8b96 --- /dev/null +++ b/src/commands/duration.js @@ -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()}`) + } +} diff --git a/src/commands/dynorestart.js b/src/commands/dynorestart.js new file mode 100644 index 0000000..35a69d9 --- /dev/null +++ b/src/commands/dynorestart.js @@ -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') + } +} diff --git a/src/index.js b/src/index.js index 143f283..c1d60f3 100644 --- a/src/index.js +++ b/src/index.js @@ -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') +] diff --git a/src/mixins/duration.js b/src/mixins/duration.js new file mode 100644 index 0000000..73b8b6f --- /dev/null +++ b/src/mixins/duration.js @@ -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) + } +} diff --git a/yarn.lock b/yarn.lock index 1f71bc4..0cdf3cd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"