Skip to content
Snippets Groups Projects
Unverified Commit 5bdb8210 authored by oxdev03's avatar oxdev03 Committed by GitHub
Browse files

chore(utils): extend fs utils with createCacheReadStream and statCach… (#30600)

parent 1141b9d3
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
cachePathExists, cachePathExists,
cachePathIsFile, cachePathIsFile,
chmodLocalFile, chmodLocalFile,
createCacheReadStream,
createCacheWriteStream, createCacheWriteStream,
deleteLocalFile, deleteLocalFile,
ensureCacheDir, ensureCacheDir,
...@@ -32,6 +33,7 @@ import { ...@@ -32,6 +33,7 @@ import {
readSystemFile, readSystemFile,
renameLocalFile, renameLocalFile,
rmCache, rmCache,
statCacheFile,
statLocalFile, statLocalFile,
writeLocalFile, writeLocalFile,
writeSystemFile, writeSystemFile,
...@@ -332,6 +334,29 @@ describe('util/fs/index', () => { ...@@ -332,6 +334,29 @@ describe('util/fs/index', () => {
}); });
}); });
describe('createCacheReadStream', () => {
it('creates read stream', async () => {
const path = `${cacheDir}/file.txt`;
const fileContent = 'foo';
await fs.outputFile(path, fileContent);
const stream = createCacheReadStream('file.txt');
expect(stream).toBeInstanceOf(fs.ReadStream);
let data = '';
stream.on('data', (chunk) => {
data += chunk.toString();
});
await new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
});
expect(data).toBe(fileContent);
});
});
describe('localPathIsFile', () => { describe('localPathIsFile', () => {
it('returns true for file', async () => { it('returns true for file', async () => {
const path = `${localDir}/file.txt`; const path = `${localDir}/file.txt`;
...@@ -431,6 +456,17 @@ describe('util/fs/index', () => { ...@@ -431,6 +456,17 @@ describe('util/fs/index', () => {
}); });
}); });
describe('statCacheFile', () => {
it('returns stat object', async () => {
expect(await statCacheFile('foo')).toBeNull();
await fs.outputFile(`${cacheDir}/foo`, 'foobar');
const stat = await statCacheFile('foo');
expect(stat).toBeTruthy();
expect(stat!.isFile()).toBeTrue();
});
});
describe('listCacheDir', () => { describe('listCacheDir', () => {
it('lists directory', async () => { it('lists directory', async () => {
await fs.outputFile(`${cacheDir}/foo/bar.txt`, 'foobar'); await fs.outputFile(`${cacheDir}/foo/bar.txt`, 'foobar');
......
...@@ -176,6 +176,11 @@ export function createCacheWriteStream(path: string): fs.WriteStream { ...@@ -176,6 +176,11 @@ export function createCacheWriteStream(path: string): fs.WriteStream {
return fs.createWriteStream(fullPath); return fs.createWriteStream(fullPath);
} }
export function createCacheReadStream(path: string): fs.ReadStream {
const fullPath = ensureCachePath(path);
return fs.createReadStream(fullPath);
}
export async function localPathIsFile(pathName: string): Promise<boolean> { export async function localPathIsFile(pathName: string): Promise<boolean> {
const path = ensureLocalPath(pathName); const path = ensureLocalPath(pathName);
try { try {
...@@ -249,6 +254,17 @@ export async function statLocalFile( ...@@ -249,6 +254,17 @@ export async function statLocalFile(
} }
} }
export async function statCacheFile(
pathName: string,
): Promise<fs.Stats | null> {
const path = ensureCachePath(pathName);
try {
return await fs.stat(path);
} catch (_) {
return null;
}
}
export function listCacheDir( export function listCacheDir(
path: string, path: string,
options: { recursive: boolean } = { recursive: false }, options: { recursive: boolean } = { recursive: false },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment