bemusic/bemuse

View on GitHub
packages/bemuse-tools/src/main.js

Summary

Maintainability
A
0 mins
Test Coverage
import * as Server from './server'

import { index } from './indexer'
import meow from 'meow'
import { packIntoBemuse } from './packer'

const commands = [
  {
    name: 'index',
    hints: '[-r]',
    description: 'Index BMS files in current directory',
    handle(args) {
      const recursive = args.flags['r']
      return index('.', { recursive })
    },
  },
  {
    name: 'pack',
    hints: '<path>',
    description: 'Packs sounds and BGAs into assets folder',
    handle(args) {
      const dir = args.input[0]
      if (!dir) throw new Error('Please specify the directory!')
      return packIntoBemuse(dir)
    },
  },
  {
    name: 'server',
    hints: '<path>',
    description: 'Serves a Bemuse server (no indexing or conversion)',
    handle(args) {
      const dir = args.input[0]
      if (!dir) throw new Error('Please specify the directory!')
      return Server.start(dir, args.flags.port || args.flags.p)
    },
  },
]

function main(args) {
  const targetCommand = args.input.shift()
  for (const command of commands) {
    if (command.name === targetCommand) {
      return command.handle(args)
    }
  }
  if (targetCommand) {
    console.error('Error: Unrecognized command.')
  } else {
    console.error('This is bemuse-tools v' + require('../package').version)
  }
  return args.showHelp()
}

function getHelpText(command) {
  return command.name + ' ' + command.hints + ' — ' + command.description
}

main(
  meow({
    help: commands.map(getHelpText).join('\n'),
    pkg: require('../package.json'),
  })
)