diff --git a/bin/build-coinbase-latest.mjs b/bin/build-coinbase-latest.mjs new file mode 100644 index 0000000..197b933 --- /dev/null +++ b/bin/build-coinbase-latest.mjs @@ -0,0 +1,43 @@ +#!/usr/bin/env node --experimental-modules + +import { createReadStream, createWriteStream } from 'node:fs' +import { mkdir } from 'node:fs/promises' +import { EOL } from 'node:os' + +import { readCache, byExchange, readLastCachedJsonLineOf } from '../lib/cache.mjs' +import { csvDateReplacer, jsonDateReviver } from '../lib/date.mjs' + +console.log('bin/build-coinbase-latest load: data/* ') + +try { + console.time('bin/build-coinbase-latest create: @api/latest.{format:json,csv} time') + + await mkdir('www/api', { recursive: true }) + console.log('bin/build-coinbase-latest exists: www/api/') + + const latestJson = createWriteStream('www/api/latest.json') + latestJson.write('[') + + const latestCsv = createWriteStream('www/api/latest.csv') + latestCsv.write('id,interval,date,open,high,low,close,volume' + EOL) + + let isFirstLine = true + for (const file of await readCache(byExchange('coinbase'))) { + console.time(`→ load: ${file} appendLine: @api/latest.{format:json,csv} time`) + const lastCachedCandle = await readLastCachedJsonLineOf(createReadStream(file), jsonDateReviver) + const [, , id, interval] = file.split(/[/,.]/) + if (isFirstLine) { + isFirstLine = false + latestJson.write(JSON.stringify([id, interval, ...lastCachedCandle])) + } else { + latestJson.write(`,${JSON.stringify([id, interval, ...lastCachedCandle])}`) + } + latestCsv.write(`${id},${interval},${JSON.stringify(lastCachedCandle, csvDateReplacer).slice(1, -1).replaceAll('"', '')}${EOL}`) + console.timeEnd(`→ load: ${file} appendLine: @api/latest.{format:json,csv} time`) + } + latestJson.write(']' + EOL) +} catch (error) { + console.error(error) +} finally { + console.timeEnd('bin/build-coinbase-latest create: @api/latest.{format:json,csv} time') +} diff --git a/package.json b/package.json index bbe37aa..797356b 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "build-sitemap": "node --experimental-modules bin/build-sitemap.mjs", "build-coinbase": "node --experimental-modules bin/build-coinbase.mjs", "build-coinbase-history": "node --experimental-modules bin/build-coinbase-history.mjs", - "test": "NODE_V8_COVERAGE=coverage node --experimental-modules --test --test-reporter spec --experimental-test-coverage test", + "build-coinbase-latest": "node --experimental-modules bin/build-coinbase-latest.mjs", "watch": "serve www & node --experimental-modules --test --test-reporter dot --watch test & wait" }, "author": "Ivo von Putzer Reibegg (https://github.com/ivoputzer)",