Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
renovate
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
Renovate Bot
renovate
Commits
5bdb8210
Unverified
Commit
5bdb8210
authored
7 months ago
by
oxdev03
Committed by
GitHub
7 months ago
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/util/fs/index.spec.ts
+36
-0
36 additions, 0 deletions
lib/util/fs/index.spec.ts
lib/util/fs/index.ts
+16
-0
16 additions, 0 deletions
lib/util/fs/index.ts
with
52 additions
and
0 deletions
lib/util/fs/index.spec.ts
+
36
−
0
View file @
5bdb8210
...
@@ -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
'
);
...
...
This diff is collapsed.
Click to expand it.
lib/util/fs/index.ts
+
16
−
0
View file @
5bdb8210
...
@@ -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
},
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment