Skip to content

Commit

Permalink
Refactor: Add an abstraction layer for command type as #11
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhtungdu committed Sep 15, 2019
1 parent 457e187 commit 22b5aac
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 44 deletions.
6 changes: 2 additions & 4 deletions src/commands/destroy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {spawn} from 'child_process'

import Base from '../lib/base'
import {isChassisDir} from '../lib/helpers'

export default class Destroy extends Base {
static description = 'Destroy current chassis VM'
Expand All @@ -10,10 +9,9 @@ export default class Destroy extends Base {
'$ chassis destroy',
]

async run() {
if (! isChassisDir())
this.error('Please run this command again in a Chassis directory.')
isLocalCommand = true

async run() {
spawn('vagrant', ['destroy'], {stdio: 'inherit'})
}
}
13 changes: 6 additions & 7 deletions src/commands/extension/disable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {spawn} from 'child_process'
import inquirer from 'inquirer'

import Base from '../../lib/base'
import * as helpers from '../../lib/helpers'
import {getLocalConfig, updateLocalConfig} from '../../lib/helpers'

export default class Disable extends Base {
static description = 'Disable Chassis extensions'
Expand All @@ -11,12 +11,11 @@ export default class Disable extends Base {
'$ chassis extension:disable',
]

async run() {
if (! helpers.isChassisDir())
this.error('Please run this command again in a Chassis directory.')
isLocalCommand = true

const enabledExtensions = helpers.getLocalConfig('extensions') || []
const disabledExtensions = helpers.getLocalConfig('disabled_extensions') || []
async run() {
const enabledExtensions = getLocalConfig('extensions') || []
const disabledExtensions = getLocalConfig('disabled_extensions') || []

if (enabledExtensions.length === 0)
this.error("Nothing to do! You don't have any extension.")
Expand All @@ -37,7 +36,7 @@ export default class Disable extends Base {

const newDisabledExtensions = [...new Set(disabledExtensions.concat(extensions))]

await helpers.updateLocalConfig({
await updateLocalConfig({
disabled_extensions: newDisabledExtensions,
extensions: enabledExtensions.filter((e: string) => !newDisabledExtensions.includes(e))
})
Expand Down
11 changes: 5 additions & 6 deletions src/commands/extension/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {spawn} from 'child_process'
import inquirer from 'inquirer'

import Base from '../../lib/base'
import * as helpers from '../../lib/helpers'
import {getLocalConfig, updateLocalConfig} from '../../lib/helpers'

export default class Enable extends Base {
static description = 'Enable Chassis extensions'
Expand All @@ -11,11 +11,10 @@ export default class Enable extends Base {
'$ chassis extension:enable',
]

async run() {
if (! helpers.isChassisDir())
this.error('Please run this command again in a Chassis directory.')
isLocalCommand = true

const disabledExtensions = helpers.getLocalConfig('disabled_extensions') || []
async run() {
const disabledExtensions = getLocalConfig('disabled_extensions') || []

if (disabledExtensions.length === 0)
this.error("Nothing to do! You don't have any disabled extension.")
Expand All @@ -34,7 +33,7 @@ export default class Enable extends Base {

this.log('Enabling selected extensions..')

await helpers.updateLocalConfig({
await updateLocalConfig({
disabled_extensions: disabledExtensions.filter((e: string) => !extensions.includes(e))
})

Expand Down
9 changes: 4 additions & 5 deletions src/commands/extension/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {spawn} from 'child_process'
import inquirer from 'inquirer'

import Base from '../../lib/base'
import * as helpers from '../../lib/helpers'
import {getLocalConfig} from '../../lib/helpers'

export default class Update extends Base {
static description = 'Update Chassis extensions'
Expand All @@ -11,12 +11,11 @@ export default class Update extends Base {
'$ chassis extension:update',
]

async run() {
if (! await helpers.isChassisDir())
this.error('Please run this command again in a Chassis directory.')
isLocalCommand = true

async run() {
const extensionDir = `${process.cwd()}/extensions`
const enabledExtensions = helpers.getLocalConfig('extensions') || []
const enabledExtensions = getLocalConfig('extensions') || []

let {extensions} = await inquirer.prompt([
{
Expand Down
6 changes: 2 additions & 4 deletions src/commands/provision.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {spawn} from 'child_process'

import Base from '../lib/base'
import {isChassisDir} from '../lib/helpers'

export default class Provision extends Base {
static description = 'Provision current chassis VM'
Expand All @@ -10,10 +9,9 @@ export default class Provision extends Base {
'$ chassis provision',
]

async run() {
if (! isChassisDir())
this.error('Please run this command again in a Chassis directory.')
isLocalCommand = true

async run() {
spawn('vagrant', ['reload', '--provision'], {stdio: 'inherit'})
}
}
6 changes: 2 additions & 4 deletions src/commands/restart.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {spawn} from 'child_process'

import Base from '../lib/base'
import {isChassisDir} from '../lib/helpers'

export default class Restart extends Base {
static description = 'Restart current chassis VM'
Expand All @@ -10,10 +9,9 @@ export default class Restart extends Base {
'$ chassis restart',
]

async run() {
if (! isChassisDir())
this.error('Please run this command again in a Chassis directory.')
isLocalCommand = true

async run() {
spawn('vagrant', ['reload'], {stdio: 'inherit'})
}
}
6 changes: 2 additions & 4 deletions src/commands/start.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {spawn} from 'child_process'

import Base from '../lib/base'
import {isChassisDir} from '../lib/helpers'

export default class Start extends Base {
static description = 'Start current chassis VM'
Expand All @@ -10,10 +9,9 @@ export default class Start extends Base {
'$ chassis start',
]

async run() {
if (! isChassisDir())
this.error('Please run this command again in a Chassis directory.')
isLocalCommand = true

async run() {
spawn('vagrant', ['up'], {stdio: 'inherit'})
}
}
7 changes: 3 additions & 4 deletions src/commands/status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Base from '../lib/base'
import {getLocalConfig, getVMStatus, isChassisDir} from '../lib/helpers'
import {getLocalConfig, getVMStatus} from '../lib/helpers'

export default class Status extends Base {
static description = 'View chassis logs'
Expand All @@ -8,10 +8,9 @@ export default class Status extends Base {
'$ chassis status',
]

async run() {
if (! isChassisDir())
this.error('Please run this command again in a Chassis directory.')
isLocalCommand = true

async run() {
const vmStatus = `
=== Curent VM status =====================
Domain: ${getLocalConfig('hosts')}
Expand Down
6 changes: 2 additions & 4 deletions src/commands/stop.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {spawn} from 'child_process'

import Base from '../lib/base'
import {isChassisDir} from '../lib/helpers'

export default class Stop extends Base {
static description = 'Stop current chassis VM'
Expand All @@ -10,10 +9,9 @@ export default class Stop extends Base {
'$ chassis stop',
]

async run() {
if (! isChassisDir())
this.error('Please run this command again in a Chassis directory.')
isLocalCommand = true

async run() {
spawn('vagrant', ['halt'], {stdio: 'inherit'})
}
}
13 changes: 11 additions & 2 deletions src/lib/base.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import {Command} from '@oclif/command'
import {sync as commandExists} from 'command-exists'

import {isChassisDir} from './helpers'

export default abstract class Base extends Command {
isLocalCommand = false

async init() {
if (!commandExists('vagrant'))
this.error(`Vagrant not found!
this.error(
`Vagrant not found!
You need Vagrant and Virtual Box to use Chassis.
Get them here: http://docs.chassis.io/en/latest/quickstart/#prerequisites`)
Get them here: http://docs.chassis.io/en/latest/quickstart/#prerequisites`
)

if (this.isLocalCommand && !isChassisDir())
this.error('Please run this command again in a Chassis directory.')

return super.init()
}
Expand Down

0 comments on commit 22b5aac

Please sign in to comment.