2020-02-18 07:07:11 +00:00
|
|
|
const fs = require("fs").promises
|
2020-07-12 13:46:06 +00:00
|
|
|
const {gzip} = require("zlib")
|
|
|
|
const {promisify: p} = require("util")
|
2020-02-18 07:07:11 +00:00
|
|
|
const pj = require("path").join
|
|
|
|
const db = require("../src/lib/db")
|
|
|
|
|
2020-07-12 13:46:06 +00:00
|
|
|
const targetDir = process.argv.slice(2).includes("--publish") ? "../src/site/html" : ".."
|
|
|
|
const shouldGzip = process.argv.slice(2).includes("--gzip")
|
|
|
|
const filename = "users_export.json" + (shouldGzip ? ".gz" : "")
|
|
|
|
const target = pj(__dirname, targetDir, filename)
|
|
|
|
|
2020-07-12 14:08:26 +00:00
|
|
|
async function progress(message, callback) {
|
|
|
|
process.stdout.write(message)
|
|
|
|
const result = await callback()
|
|
|
|
process.stdout.write("done.\n")
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-02-18 07:07:11 +00:00
|
|
|
;(async () => {
|
2020-07-12 14:08:26 +00:00
|
|
|
let data = await progress("Preparing export data... ", () => {
|
|
|
|
const users = db.prepare("SELECT * FROM Users").all()
|
|
|
|
return Buffer.from(JSON.stringify(users), "utf8")
|
|
|
|
})
|
2020-07-12 13:46:06 +00:00
|
|
|
|
|
|
|
if (shouldGzip) {
|
2020-07-12 14:08:26 +00:00
|
|
|
data = await progress("Compressing... ", () => p(gzip)(data))
|
2020-07-12 13:46:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 14:08:26 +00:00
|
|
|
await progress("Writing file... ", () => fs.writeFile(target, data))
|
2020-07-12 13:46:06 +00:00
|
|
|
|
2020-02-18 07:07:11 +00:00
|
|
|
console.log(`Users exported to ${target}`)
|
|
|
|
})()
|