pinafore/bin/wait-for-mastodon-data.js
Nolan Lawson 16e66346d7
fix!: remove esm package, use native Node ES modules (#2064)
BREAKING CHANGE: Node v12.20+, v14.14+, or v16.0+ is required

* fix!: remove esm package, use native Node ES modules

* fix: fix some CJS imports
2021-07-04 20:19:04 -07:00

36 lines
953 B
JavaScript

import fetch from 'node-fetch'
import { actions } from './mastodon-data.js'
import esMain from 'es-main'
const numStatuses = actions
.map(_ => _.post || _.boost)
.filter(Boolean)
.filter(_ => _.privacy !== 'direct')
.length
async function waitForMastodonData () {
while (true) {
try {
const json = await ((await fetch('http://127.0.0.1:3000/api/v1/instance')).json())
if (json.stats.status_count === numStatuses) {
break
} else {
console.log('Waiting for number of statuses to equal ' +
numStatuses + ' (currently ' + json.stats.status_count + ')...')
}
} catch (err) {
console.log('Waiting for Mastodon API to be available...')
} finally {
await new Promise(resolve => setTimeout(resolve, 5000))
}
}
console.log('Mastodon data populated')
}
if (esMain(import.meta)) {
waitForMastodonData().catch(err => {
console.error(err)
process.exit(1)
})
}