~mna/hockeysim

7afce25f552ad01ddcf7fe315958c303fe55f2dc — Martin Angers 1 year, 1 month ago f73a541
implement list leagues command
M package-lock.json => package-lock.json +14 -21
@@ 990,7 990,6 @@
      "version": "6.10.0",
      "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.0.tgz",
      "integrity": "sha512-nffhOpkymDECQyR0mnsUtoCE8RlX38G0rYP+wgLWFyZuUyuuojSSvi/+euOiQBIn63whYwYVIIH1TvE3tu4OEg==",
      "dev": true,
      "requires": {
        "fast-deep-equal": "^2.0.1",
        "fast-json-stable-stringify": "^2.0.0",


@@ 1152,8 1151,7 @@
    "astral-regex": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/astral-regex/-/astral-regex-1.0.0.tgz",
      "integrity": "sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==",
      "dev": true
      "integrity": "sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg=="
    },
    "async-each": {
      "version": "1.0.3",


@@ 2288,6 2286,11 @@
        "assert-plus": "^1.0.0"
      }
    },
    "dateformat": {
      "version": "3.0.3",
      "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-3.0.3.tgz",
      "integrity": "sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q=="
    },
    "debug": {
      "version": "2.6.9",
      "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",


@@ 3071,14 3074,12 @@
    "fast-deep-equal": {
      "version": "2.0.1",
      "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz",
      "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=",
      "dev": true
      "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk="
    },
    "fast-json-stable-stringify": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz",
      "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=",
      "dev": true
      "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I="
    },
    "fast-levenshtein": {
      "version": "2.0.6",


@@ 4837,8 4838,7 @@
    "json-schema-traverse": {
      "version": "0.4.1",
      "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
      "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==",
      "dev": true
      "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg=="
    },
    "json-stable-stringify-without-jsonify": {
      "version": "1.0.1",


@@ 6121,8 6121,7 @@
    "punycode": {
      "version": "2.1.1",
      "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
      "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==",
      "dev": true
      "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A=="
    },
    "qs": {
      "version": "6.5.2",


@@ 6613,7 6612,6 @@
      "version": "2.1.0",
      "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-2.1.0.tgz",
      "integrity": "sha512-Qu+VC3EwYLldKa1fCxuuvULvSJOKEgk9pi8dZeCVK7TqBfUNTH4sFkk4joj8afVSfAYgJoSOetjx9QWOJ5mYoQ==",
      "dev": true,
      "requires": {
        "ansi-styles": "^3.2.0",
        "astral-regex": "^1.0.0",


@@ 6965,10 6963,9 @@
      }
    },
    "table": {
      "version": "5.4.0",
      "resolved": "https://registry.npmjs.org/table/-/table-5.4.0.tgz",
      "integrity": "sha512-nHFDrxmbrkU7JAFKqKbDJXfzrX2UBsWmrieXFTGxiI5e4ncg3VqsZeI4EzNmX0ncp4XNGVeoxIWJXfCIXwrsvw==",
      "dev": true,
      "version": "5.4.1",
      "resolved": "https://registry.npmjs.org/table/-/table-5.4.1.tgz",
      "integrity": "sha512-E6CK1/pZe2N75rGZQotFOdmzWQ1AILtgYbMAbAjvms0S1l5IDB47zG3nCnFGB/w+7nB3vKofbLXCH7HPBo864w==",
      "requires": {
        "ajv": "^6.9.1",
        "lodash": "^4.17.11",


@@ 6979,14 6976,12 @@
        "ansi-regex": {
          "version": "4.1.0",
          "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
          "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==",
          "dev": true
          "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg=="
        },
        "string-width": {
          "version": "3.1.0",
          "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
          "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==",
          "dev": true,
          "requires": {
            "emoji-regex": "^7.0.1",
            "is-fullwidth-code-point": "^2.0.0",


@@ 6997,7 6992,6 @@
          "version": "5.2.0",
          "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
          "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
          "dev": true,
          "requires": {
            "ansi-regex": "^4.1.0"
          }


@@ 7760,7 7754,6 @@
      "version": "4.2.2",
      "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz",
      "integrity": "sha512-KY9Frmirql91X2Qgjry0Wd4Y+YTdrdZheS8TFwvkbLWf/G5KNJDCh6pKL5OZctEW4+0Baa5idK2ZQuELRwPznQ==",
      "dev": true,
      "requires": {
        "punycode": "^2.1.0"
      }

M package.json => package.json +2 -0
@@ 60,9 60,11 @@
    "@babel/runtime": "^7.4.5",
    "cfonts": "^2.4.3",
    "chalk": "^2.4.2",
    "dateformat": "^3.0.3",
    "inquirer": "^6.3.1",
    "mysql2": "^1.6.5",
    "ramda": "^0.26.1",
    "table": "^5.4.1",
    "yargs": "^13.2.4"
  },
  "engines": {

M src/cli.js => src/cli.js +1 -0
@@ 2,6 2,7 @@
import yargs from 'yargs'

void yargs
  .scriptName ('hockeysim')
  .commandDir ('cmds')
  .demandCommand ()
  .strict ()

M src/cmds/league_cmds/list.js => src/cmds/league_cmds/list.js +16 -3
@@ 1,3 1,7 @@
import CFonts from 'cfonts'
import { DB } from 'mymigrate'
import exec from '../../handlers/league_cmds/list'

export const command = ['list', '$0']
export const desc = 'List existing leagues'



@@ 7,7 11,16 @@ export function builder (yargs) {
    .boolean ('all')
}

export function handler (argv) {
  console.log ('list leagues')
  console.log (argv)
export async function handler (argv) {
  CFonts.say ('List Leagues', {
    font: 'chrome',
    colors: ['#0ff', 'green', '#ff0'],
  })

  const conn = await new DB ().connect ()
  try {
    await exec (conn, argv)
  } finally {
    await conn.end ()
  }
}

A src/handlers/league_cmds/list.js => src/handlers/league_cmds/list.js +26 -0
@@ 0,0 1,26 @@
import { table } from 'table'
import chalk from 'chalk'
import dateFormat from 'dateformat'
import { listLeagues } from '../../leagues'
import { LeagueState } from '../../constants'

export default async function (conn, { all } = {}) {
  const leagues = await listLeagues (conn, all)
  let data = [
    [
      chalk.bold ('id'),
      chalk.bold ('name'),
      chalk.bold ('archived'),
      chalk.bold ('date'),
    ],
  ]
  for (const l of leagues) {
    data.push ([
      l.id,
      l.name,
      l.state === LeagueState.archived ? '*' : '',
      dateFormat (l.updated, 'yyyy-mm-dd'),
    ])
  }
  console.log (table (data))
}

M src/leagues.js => src/leagues.js +14 -0
@@ 5,3 5,17 @@ export async function createLeague (conn, name, state = LeagueState.active) {
  const [res] = await conn.query ('insert into leagues (name, state) values (?, ?)', [name, state])
  return res.insertId
}

// returns the list of leagues corresponding to the criteria
export async function listLeagues (conn, all) {
  let args = []
  let stmt = 'select id, name, state, updated from leagues'
  if (!all) {
    stmt += ' where state != ?'
    args.push (LeagueState.archived)
  }
  stmt += ' order by state, name'

  const [res] = await conn.query (stmt, args)
  return res
}