File size: 1,311 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import fs from 'fs'
import path from 'path'
import type { TelemetryEvent } from './storage'
import { Telemetry } from './storage'
import loadConfig from '../server/config'
import { getProjectDir } from '../lib/get-project-dir'
import { PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants'
// this process should be started with following arg order
// 1. mode e.g. dev, export, start
// 2. project dir
;(async () => {
const args = [...process.argv]
let dir = args.pop()
const mode = args.pop()
if (!dir || mode !== 'dev') {
throw new Error(
`Invalid flags should be run as node detached-flush dev ./path-to/project`
)
}
dir = getProjectDir(dir)
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, dir)
const distDir = path.join(dir, config.distDir || '.next')
const eventsPath = path.join(distDir, '_events.json')
let events: TelemetryEvent[]
try {
events = JSON.parse(fs.readFileSync(eventsPath, 'utf8'))
} catch (err: any) {
if (err.code === 'ENOENT') {
// no events to process we can exit now
process.exit(0)
}
throw err
}
const telemetry = new Telemetry({ distDir })
await telemetry.record(events)
await telemetry.flush()
// finished flushing events clean-up/exit
fs.unlinkSync(eventsPath)
process.exit(0)
})()
|