From 9db0d3547ad77202611b5bc35ec8d1f0f4dbadaa Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Mon, 5 Mar 2018 21:21:28 -0800 Subject: [PATCH] fixup --- bin/mastodon-data.js | 21 ++++++++++++--------- bin/restore-mastodon-data.js | 13 ++++++++++++- bin/run-mastodon.js | 6 +++--- bin/wait-for-mastodon-to-start.js | 29 +++++++++++++++++++++++------ package.json | 2 +- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/bin/mastodon-data.js b/bin/mastodon-data.js index 995dd542..24b14597 100644 --- a/bin/mastodon-data.js +++ b/bin/mastodon-data.js @@ -101,7 +101,18 @@ export const actions = times(30, i => ({ }, { user: 'foobar', - follow: 'quux' + post: { + text: 'this is followers-only', + privacy: 'private' + } + }, + { + user: 'foobar', + post: { + internalId: 2, + text: 'this is unlisted', + privacy: 'unlisted' + } }, { user: 'admin', @@ -139,14 +150,6 @@ export const actions = times(30, i => ({ user: 'admin', boost: 1 }, - { - user: 'foobar', - post: { - internalId: 2, - text: 'this is unlisted', - privacy: 'private' - } - }, { user: 'admin', favorite: 2 diff --git a/bin/restore-mastodon-data.js b/bin/restore-mastodon-data.js index bb796204..3874e0b4 100644 --- a/bin/restore-mastodon-data.js +++ b/bin/restore-mastodon-data.js @@ -8,6 +8,10 @@ import { reblogStatus } from '../routes/_api/reblog' import fetch from 'node-fetch' import FileApi from 'file-api' import path from 'path' +import fs from 'fs' +import pify from 'pify' + +const readFile = pify(fs.readFile.bind(fs)) global.File = FileApi.File global.FormData = FileApi.FormData @@ -26,7 +30,13 @@ export async function restoreMastodonData () { inReplyTo = internalIdsToIds[inReplyTo] } let mediaIds = media && await Promise.all(media.map(async mediaItem => { - let file = new File(path.join(__dirname, '../tests/images/' + mediaItem)) + let type = mediaItem.endsWith('gif') ? 'image/gif' + : mediaItem.endsWith('jpg') ? 'image/jpg' : 'video/mp4' + let file = new File({ + name: mediaItem, + type: type, + buffer: await readFile(path.join(__dirname, '../tests/images/' + mediaItem)) + }) let mediaResponse = await uploadMedia('localhost:3000', accessToken, file) return mediaResponse.id })) @@ -43,4 +53,5 @@ export async function restoreMastodonData () { await reblogStatus('localhost:3000', accessToken, internalIdsToIds[action.boost]) } } + console.log('Restored mastodon data') } \ No newline at end of file diff --git a/bin/run-mastodon.js b/bin/run-mastodon.js index b23b19ba..df9d86c0 100644 --- a/bin/run-mastodon.js +++ b/bin/run-mastodon.js @@ -3,7 +3,7 @@ import pify from 'pify' import childProcessPromise from 'child-process-promise' import path from 'path' import fs from 'fs' -import { waitForMastodonToStart } from './wait-for-mastodon-to-start' +import { waitForMastodonApiToStart, waitForMastodonUiToStart } from './wait-for-mastodon-to-start' import mkdirpCB from 'mkdirp' const exec = childProcessPromise.exec @@ -68,15 +68,15 @@ async function runMastodon () { childProc = promise.childProcess childProc.stdout.pipe(log) childProc.stderr.pipe(log) - - await waitForMastodonToStart() } async function main () { await cloneMastodon() await setupMastodonDatabase() await runMastodon() + await waitForMastodonApiToStart() await restoreMastodonData() + await waitForMastodonUiToStart() } process.on('SIGINT', function () { diff --git a/bin/wait-for-mastodon-to-start.js b/bin/wait-for-mastodon-to-start.js index c7752855..b0f5b17d 100644 --- a/bin/wait-for-mastodon-to-start.js +++ b/bin/wait-for-mastodon-to-start.js @@ -1,23 +1,40 @@ import fetch from 'node-fetch' -export async function waitForMastodonToStart () { +export async function waitForMastodonUiToStart () { while (true) { try { - let json = await ((await fetch('http://127.0.0.1:3000/api/v1/instance')).json()) let html = await ((await fetch('http://127.0.0.1:3035/packs/common.js')).text()) - if (json.uri && html) { + if (html) { break } } catch (err) { - console.log('Waiting for Mastodon to start up...') + console.log('Waiting for Mastodon UI to start up...') await new Promise(resolve => setTimeout(resolve, 1000)) } } - console.log('Mastodon started up') + console.log('Mastodon UI started up') +} + +export async function waitForMastodonApiToStart () { + while (true) { + try { + let json = await ((await fetch('http://127.0.0.1:3000/api/v1/instance')).json()) + if (json.uri) { + break + } + } catch (err) { + console.log('Waiting for Mastodon API to start up...') + await new Promise(resolve => setTimeout(resolve, 1000)) + } + } + console.log('Mastodon API started up') } if (require.main === module) { - waitForMastodonToStart().catch(err => { + Promise.all([ + waitForMastodonApiToStart(), + waitForMastodonUiToStart() + ]).catch(err => { console.error(err) process.exit(1) }) diff --git a/package.json b/package.json index d54381a3..3fb8237c 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "test": "cross-env BROWSER=chrome:headless npm run test-browser", "test-browser": "run-p --race run-mastodon dev test-mastodon", "test-mastodon": "run-s wait-for-mastodon-to-start run-testcafe", - "wait-for-mastodon-to-start": "node bin/wait-for-mastodon-to-start.js", + "wait-for-mastodon-to-start": "node -r @std/esm bin/wait-for-mastodon-to-start.js", "globalize-css": "node ./bin/globalize-css.js", "deglobalize-css": "node ./bin/globalize-css.js --reverse", "backup-mastodon-data": "pg_dump -Fc mastodon_development > fixtures/dump.sql && cd mastodon/public/system && tar -czf ../../../fixtures/system.tgz ."