From 5c38554a38acd6369a39e5e7f65421507b94a370 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Mon, 5 Mar 2018 20:51:42 -0800 Subject: [PATCH] update scripts --- bin/mastodon-data.js | 20 ++++++++++---------- bin/restore-mastodon-data.js | 23 +++++++++++++++-------- bin/run-mastodon.js | 24 +++++++++++++----------- bin/wait-for-mastodon-to-start.js | 6 ++---- package.json | 3 ++- tests/users.js | 12 ++++++++---- 6 files changed, 50 insertions(+), 38 deletions(-) diff --git a/bin/mastodon-data.js b/bin/mastodon-data.js index bd488fc5..995dd542 100644 --- a/bin/mastodon-data.js +++ b/bin/mastodon-data.js @@ -1,8 +1,8 @@ -const times = require('lodash/times') +import times from 'lodash/times' export const actions = times(30, i => ({ post: { - text: (i + 1) + text: '' + (i + 1) }, user: 'admin' })).concat([ @@ -95,6 +95,14 @@ export const actions = times(30, i => ({ user: 'quux', follow: 'foobar' }, + { + user: 'admin', + follow: 'quux' + }, + { + user: 'foobar', + follow: 'quux' + }, { user: 'admin', post: { @@ -139,10 +147,6 @@ export const actions = times(30, i => ({ privacy: 'private' } }, - { - user: 'admin', - boost: 2 - }, { user: 'admin', favorite: 2 @@ -180,10 +184,6 @@ export const actions = times(30, i => ({ user: 'quux', pin: 6 }, - { - user: 'admin', - boost: 5 - }, { user: 'admin', favorite: 5 diff --git a/bin/restore-mastodon-data.js b/bin/restore-mastodon-data.js index f4710c92..060f5c80 100644 --- a/bin/restore-mastodon-data.js +++ b/bin/restore-mastodon-data.js @@ -5,16 +5,20 @@ import { uploadMedia } from '../routes/_api/media' import { followAccount } from '../routes/_api/follow' import { favoriteStatus } from '../routes/_api/favorite' import { reblogStatus } from '../routes/_api/reblog' - +import fetch from 'node-fetch' +import FileApi from 'file-api' import path from 'path' -global.File = require('file-api').File -global.FormData = require('file-api').FormData -global.fetch = require('node-fetch') -async function restoreMastodonData () { +global.File = FileApi.File +global.FormData = FileApi.FormData +global.fetch = fetch + +export async function restoreMastodonData () { + debugger console.log('Restoring mastodon data...') let internalIdsToIds = {} for (let action of actions) { + console.log(JSON.stringify(action)) let accessToken = users[action.user].accessToken if (action.post) { let { text, media, sensitive, spoiler, privacy, inReplyTo, internalId } = action.post @@ -32,13 +36,16 @@ async function restoreMastodonData () { internalIdsToIds[internalId] = status.id } } else if (action.follow) { - await followAccount('localhost:3000', accessToken, action.follow) + await followAccount('localhost:3000', accessToken, users[action.follow].id) } else if (action.favorite) { await favoriteStatus('localhost:3000', accessToken, internalIdsToIds[action.favorite]) } else if (action.boost) { - await reblogStatus('localhost:3000', accessToken, internalIdsToIds[action.favorite]) + await reblogStatus('localhost:3000', accessToken, internalIdsToIds[action.boost]) } } } -module.exports = restoreMastodonData \ No newline at end of file +restoreMastodonData().catch(err => { + console.error(err) + process.exit(1) +}) \ No newline at end of file diff --git a/bin/run-mastodon.js b/bin/run-mastodon.js index fc3bfb70..ef8b2832 100644 --- a/bin/run-mastodon.js +++ b/bin/run-mastodon.js @@ -1,14 +1,16 @@ -const restoreMastodonData = require('./restore-mastodon-data') -const pify = require('pify') -const exec = require('child-process-promise').exec -const spawn = require('child-process-promise').spawn -const dir = __dirname -const path = require('path') -const fs = require('fs') +import { restoreMastodonData } from './restore-mastodon-data' +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' + +const exec = childProcessPromise.exec +const spawn = childProcessPromise.spawn +const mkdirp = pify(mkdirpCB) const stat = pify(fs.stat.bind(fs)) const writeFile = pify(fs.writeFile.bind(fs)) -const mkdirp = pify(require('mkdirp')) -const waitForMastodonToStart = require('./wait-for-mastodon-to-start') +const dir = __dirname const envFile = ` PAPERCLIP_SECRET=foo @@ -71,9 +73,9 @@ async function runMastodon () { async function main () { await cloneMastodon() - //await setupMastodonDatabase() + await setupMastodonDatabase() await runMastodon() - //await restoreMastodonData() + await restoreMastodonData() } process.on('SIGINT', function () { diff --git a/bin/wait-for-mastodon-to-start.js b/bin/wait-for-mastodon-to-start.js index 0e906d8e..c7752855 100644 --- a/bin/wait-for-mastodon-to-start.js +++ b/bin/wait-for-mastodon-to-start.js @@ -1,6 +1,6 @@ -const fetch = require('node-fetch') +import fetch from 'node-fetch' -async function waitForMastodonToStart () { +export async function waitForMastodonToStart () { while (true) { try { let json = await ((await fetch('http://127.0.0.1:3000/api/v1/instance')).json()) @@ -16,8 +16,6 @@ async function waitForMastodonToStart () { console.log('Mastodon started up') } -module.exports = waitForMastodonToStart - if (require.main === module) { waitForMastodonToStart().catch(err => { console.error(err) diff --git a/package.json b/package.json index 02cf66bf..d54381a3 100644 --- a/package.json +++ b/package.json @@ -109,6 +109,7 @@ ] }, "@std/esm": { - "mode": "js" + "mode": "js", + "cjs": "vars" } } diff --git a/tests/users.js b/tests/users.js index b134b916..1c4780fc 100644 --- a/tests/users.js +++ b/tests/users.js @@ -2,21 +2,25 @@ export const users = { admin: { username: 'admin', password: 'mastodonadmin', - accessToken: '1a62cc93aa6ccdad5a1048bd37b627c9df72f68c7f4b87d2daaeb83010ff44ab' + accessToken: '1a62cc93aa6ccdad5a1048bd37b627c9df72f68c7f4b87d2daaeb83010ff44ab', + id: 1 }, foobar: { username: 'foobar', password: 'foobarfoobar', - accessToken: '67868cd5b1f1279697ffffdb44c144b0ae427d74f4540717b67bf4a9c5c5c346' + accessToken: '67868cd5b1f1279697ffffdb44c144b0ae427d74f4540717b67bf4a9c5c5c346', + id: 2 }, quux: { username: 'quux', password: 'quuxquuxquux', - accessToken: 'd100083c1749392da7e1c12e63a6fba75f15ea1fbce93361d722889abf418464' + accessToken: 'd100083c1749392da7e1c12e63a6fba75f15ea1fbce93361d722889abf418464', + id: 3 }, ExternalLinks: { username: 'ExternalLinks', password: 'ExternalLinksExternalLink', - accessToken: 'e9a463ba1729ae0049a97a312af702cb3d08d84de1cc8d6da3fad90af068117b' + accessToken: 'e9a463ba1729ae0049a97a312af702cb3d08d84de1cc8d6da3fad90af068117b', + id: 4 } } \ No newline at end of file