@gameroom/cli
Version:
A command line tool for Gameroom
86 lines (84 loc) • 3.94 kB
JavaScript
const cosmetic = require('cosmetic'),
{ config: { limit }, models: { Image, Product, Unit } } = require('@gameroom/kit'),
{ grGreen, timeout } = require('../../../helpers'),
{ ShopifyV2 } = require('../../../models'),
{ config, spinner } = require('../../../refs'),
pushProduct = require('./pushProduct'),
LIMIT = 500
module.exports = async (options) => {
// setup options
const { verbose } = options
// Gameroom products >> Shopify products
// date filter
let filter
if (config.shopify_product_sync) {
const date = new Date(config.shopify_product_sync)
spinner.info(`last product sync ${date.toLocaleString()}`)
// just in case...
// date.setTime(date.getTime() - 1)
filter = { key: 'updated_at', comparison: '>', value: date }
} else {
spinner.info(`first product sync`)
}
// Get updated products and add or update or remove them on shopify
let done = false, processed = {}
// update shopify
let success = 0, fail = 0, total = 0
// spinner.text = `syncing ${grGreen('products')} ${success + fail}/${total}`
while (!done) {
// get all products
spinner.text = `getting ${grGreen('products')}`
let products = await Product.getAll({ filter, offset: total, sort: [{ updated_at: 1 }] },
(batch) => spinner.text = `getting ${grGreen('products')} (${batch.length})`,
(err) => spinner.fail(`error getting ${grGreen('products')}: ${err}`))
// filter out already processed updates
products = products.filter(i => !i.updated_at.equals(processed[i.id]))
// update vars
done = products.length === 0
total += products.length
if (verbose) spinner.info(`got ${products.length} ${grGreen('products')}`)
// filter products that are to be updated at or added, not removed
// const select = products.filter(i => i.properties.shopify_id && i.offered)
// // get images for updates
// spinner.text = `getting ${grGreen('images')}`
// const images = await select.get(Image, null,
// (batch) => spinner.text = `getting ${grGreen('images')} (${batch.length})`,
// (err) => spinner.fail(`error getting ${grGreen('images')}: ${err}`))
// if (verbose) spinner.info(`got ${images.length} ${grGreen('images')}`)
// // get units for updates
// spinner.text = `getting ${grGreen('units')}`
// const units = await select.get(Unit, { filter: { key: 'quantity', comparison: '>', value: 0 } },
// (batch) => spinner.text = `getting ${grGreen('units')} (${batch.length})`,
// (err) => spinner.fail(`error getting ${grGreen('units')}: ${err}`))
// if (verbose) spinner.info(`got ${units.length} ${grGreen('units')}`)
for (const i of products) {
try {
spinner.text = `syncing ${grGreen('products')} ${success + fail}/${total}`
await pushProduct(i, options)
success++
} catch (err) {
spinner.fail(`failed ${grGreen('product')} ${i.id} ${err}`)
console.log()
console.log(err)
fail++
}
config.shopify_product_sync = i.updated_at.toDate()
}
// const promises = products.reduce((a, i) => [ ...a, limit(async() => {
// try {
// spinner.text = `syncing ${grGreen('products')} ${success + fail}/${total}`
// // await timeout(500)
// await pushProduct(i, options)
// success++
// } catch (err) {
// spinner.fail(`failed ${grGreen('product')} ${i.id} ${err}`)
// fail++
// }
// }) ], [])
// await Promise.all(promises)
// const latest = products.reduce((a, b) => a && a.updated_at > b.updated_at ? a : b, null)
// if (latest) config.shopify_product_sync = latest.updated_at.toDate()
}
spinner.succeed(`synced ${total} ${grGreen('products')} ${success} ${cosmetic.green('successes')} ${fail} ${cosmetic.red('failures')}`)
spinner.succeed(`completed product push to ${cosmetic.green('shopify')} @ ${new Date().toLocaleString()}`)
}