Skip to content

Commit 68a3b15

Browse files
authored
Merge pull request #106 from ryoppippi/feature/fix-cache
Refactor Synchronous File Operations to Asynchronous & use Bun
2 parents 736a1e1 + b16be98 commit 68a3b15

File tree

1 file changed

+30
-13
lines changed
  • packages/unplugin-typia/src/core

1 file changed

+30
-13
lines changed

packages/unplugin-typia/src/core/cache.ts

+30-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { accessSync, constants, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
1+
import { existsSync } from 'node:fs';
2+
import { access, constants, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
23
import { createHash } from 'node:crypto';
34
import { basename, dirname, join } from 'pathe';
45
import type { Tagged } from 'type-fest';
@@ -33,14 +34,25 @@ export async function getCache(
3334
const key = getKey(id, source);
3435
const path = getCachePath(key, option);
3536

36-
if (existsSync(path)) {
37-
const data = readFileSync(path, 'utf8');
37+
let data: Data | null = null;
38+
if (isBun()) {
39+
const file = Bun.file(path);
40+
if (!(await file.exists())) {
41+
return null;
42+
}
43+
data = await file.text();
44+
}
45+
else {
46+
if (!(existsSync(path))) {
47+
return null;
48+
}
49+
data = await readFile(path, { encoding: 'utf8' });
50+
}
3851

39-
const hashComment = await getHashComment(key);
52+
const hashComment = await getHashComment(key);
4053

41-
if (data.endsWith(hashComment)) {
42-
return data;
43-
}
54+
if (data.endsWith(hashComment)) {
55+
return data;
4456
}
4557

4658
return null;
@@ -69,12 +81,17 @@ export async function setCache(
6981
const hashComment = await getHashComment(key);
7082

7183
if (data == null) {
72-
rmSync(path);
84+
await rm(path);
7385
return;
7486
}
7587

7688
const cache = data + hashComment;
77-
writeFileSync(path, cache, { encoding: 'utf8' });
89+
if (isBun()) {
90+
Bun.write(path, cache, { createPath: true });
91+
}
92+
else {
93+
await writeFile(path, cache, { encoding: 'utf8' });
94+
}
7895
}
7996

8097
type CacheKey = Tagged<string, 'cache-key'>;
@@ -104,11 +121,11 @@ function getCachePath(
104121
return join(option.base, key) as CachePath;
105122
}
106123

107-
function prepareCacheDir(option: ResolvedCacheOptions): void {
124+
async function prepareCacheDir(option: ResolvedCacheOptions) {
108125
if (cacheDir != null) {
109126
return;
110127
}
111-
mkdirSync(option.base, { recursive: true });
128+
await mkdir(option.base, { recursive: true });
112129

113130
if (!isWritable) {
114131
throw new Error('Cache directory is not writable.');
@@ -117,9 +134,9 @@ function prepareCacheDir(option: ResolvedCacheOptions): void {
117134
cacheDir = option.base;
118135
}
119136

120-
function isWritable(filename: string): boolean {
137+
async function isWritable(filename: string): Promise<boolean> {
121138
try {
122-
accessSync(filename, constants.W_OK);
139+
await access(filename, constants.W_OK);
123140
return true;
124141
}
125142
catch {

0 commit comments

Comments
 (0)