botodon/src/tools/data/push.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-06-14 15:10:20 +00:00
import fs from 'fs'
import { rest, rootStatus } from '../../prepare'
import Logger from '../../utils/Logger'
async function run() {
2019-06-14 15:30:54 +00:00
const [,, file, title, visibility] = process.argv
2019-06-14 15:10:20 +00:00
if (!file) {
Logger.error('require file')
return
}
2019-06-14 15:30:54 +00:00
2019-06-17 15:06:01 +00:00
const post = async (inReplyToId: string, status: string) => rest.postStatus({
in_reply_to_id: inReplyToId,
status,
2019-06-14 15:30:54 +00:00
visibility: visibility as any || 'direct'
}).then(s => s.id)
2019-06-17 15:06:01 +00:00
// TODO: push database.json
2019-06-14 15:10:20 +00:00
const data = JSON.parse(fs.readFileSync(file, 'utf8'))
2019-06-14 15:30:54 +00:00
const folder = await post(rootStatus, title || file)
2019-06-17 15:06:01 +00:00
Logger.info('Content length', data.length)
2019-06-14 15:30:54 +00:00
for (const row of data) {
if (typeof row === 'string') {
await post(folder, row)
} else if (typeof row === 'object') {
const content = '\n' + Object.entries(row).map(([lang, message]) => `[${lang}] ${message}`).join('\n\n')
const global = await post(folder, content)
for (const [lang, message] of Object.entries(row)) {
await post(global, `${message} #${lang}`)
}
} else {
throw new Error('bad type ' + typeof row)
}
}
2019-06-17 15:06:01 +00:00
Logger.info('Data added', folder)
2019-06-14 15:10:20 +00:00
}
run()