@gameroom/cli
Version:
A command line tool for Gameroom
46 lines (43 loc) • 1.61 kB
JavaScript
const pluralize = require('pluralize'),
{ getStorable, grGreen, prettyPrint, timeout } = require('../helpers'),
{ models: { Product, Unit } } = require('@gameroom/kit'),
{ spinner } = require('../refs')
module.exports = async () => {
spinner.text = `updating ${grGreen('units')}`
spinner.start()
const units = [], limit = 500, filter = { key: 'offered', value: true }
let done = false, offset = 0
while (!done) {
spinner.text = `getting units: ${units.length}`
const result = await Unit.get({ filter, limit, offset })
units.push(...result)
offset += limit
if (result.length < limit) done = true
}
spinner.info(`got ${units.length} units`)
const updates = []
for (let i = 0; i < units.length; i++) {
const unit = units[i]
if (!unit.product_id) {
spinner.warn(`orphaned ${grGreen('unit')} #${i} id: ${unit.id}`)
continue
}
spinner.text = `updating ${grGreen('units')}: ${i}/${units.length}`
try {
const product = await Product.find(unit.product_id)
if (product.id && unit.id !== product.id) {
spinner.text = `updating ${grGreen('unit')} #${i} id: ${unit.id}`
const update = await Unit.update({ id: unit.id, id: product.id })
updates.push(update)
spinner.info(`updated ${grGreen('unit')} #${i} id: ${unit.id}`)
}
} catch(err) {
spinner.warn(`error caught ${grGreen('unit')} #${i} id: ${unit.id}`)
spinner.text = 'retrying'
i--
await timeout(500)
}
}
spinner.succeed(`updated ${updates.length} ${grGreen('units')}`).stop()
prettyPrint(updates)
}