Skip to content
This repository has been archived by the owner on May 31, 2023. It is now read-only.

Commit

Permalink
Fix issues with writing cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
elmasse committed Jan 31, 2022
1 parent 13039e2 commit 514c17f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/entries/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export default () => {
let isValid

async function build (path = CACHE_FILE_PATH) {
const raw = await readFile(path, { encoding: 'utf-8' })
let raw
try {
raw = await readFile(path, { encoding: 'utf-8' })
cache = JSON.parse(raw)
isValid = true
} catch (error) {
Expand Down
13 changes: 10 additions & 3 deletions src/plugins/worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { writeFile as writeFileFS } from 'fs'
import { writeFile as writeFileFS, mkdir as mkdirFS } from 'fs'
import { dirname } from 'path'
import { promisify } from 'util'

import { Worker, isMainThread, parentPort } from 'worker_threads'
Expand All @@ -7,9 +8,15 @@ import { CACHE_FILE_PATH } from '../entries/cache'
import { run, subscribe } from './bootstrap'

const writeFile = promisify(writeFileFS)
const mkdir = promisify(mkdirFS)

function updateTmpFile (data) {
writeFile(CACHE_FILE_PATH, JSON.stringify(data))
async function updateTmpFile (data) {
try {
await mkdir(dirname(CACHE_FILE_PATH), { recursive: true })
await writeFile(CACHE_FILE_PATH, JSON.stringify(data), { encoding: 'utf-8', flag: 'w+' })
} catch (error) {
console.error('Error writing cache.', error)
}
}

if (isMainThread) {
Expand Down

0 comments on commit 514c17f

Please sign in to comment.