Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
NodeSharedCode
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Dojo Project (HES-SO)
Projects
Shared
NodeSharedCode
Commits
0c9528be
Commit
0c9528be
authored
1 year ago
by
michael.minelli
Browse files
Options
Downloads
Patches
Plain Diff
Toolbox => Add some files functions
parent
fc0c547b
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
helpers/Toolbox.ts
+40
-0
40 additions, 0 deletions
helpers/Toolbox.ts
with
40 additions
and
0 deletions
helpers/Toolbox.ts
+
40
−
0
View file @
0c9528be
import
fs
from
'
fs/promises
'
;
import
path
from
'
path
'
;
class
Toolbox
{
class
Toolbox
{
public
urlToPath
(
url
:
string
):
string
{
public
urlToPath
(
url
:
string
):
string
{
return
url
.
replace
(
/^
([
a-z
]{3,5}
:
\/{2})?[
a-z.@
]
+
(
:
[
0-9
]{1,5})?
.
(
.*
)
/
,
'
$3
'
).
replace
(
'
.git
'
,
''
);
return
url
.
replace
(
/^
([
a-z
]{3,5}
:
\/{2})?[
a-z.@
]
+
(
:
[
0-9
]{1,5})?
.
(
.*
)
/
,
'
$3
'
).
replace
(
'
.git
'
,
''
);
}
}
/*
Source of getAllFiles and getTotalSize (modified for this project): https://coderrocketfuel.com/article/get-the-total-size-of-all-files-in-a-directory-using-node-js
*/
private
async
getAllFiles
(
dirPath
:
string
,
arrayOfFiles
:
Array
<
string
>
=
[]):
Promise
<
Array
<
string
>>
{
let
files
=
await
fs
.
readdir
(
dirPath
);
await
Promise
.
all
(
files
.
map
(
async
file
=>
{
if
(
(
await
fs
.
stat
(
dirPath
+
'
/
'
+
file
)).
isDirectory
()
)
{
arrayOfFiles
=
await
this
.
getAllFiles
(
dirPath
+
'
/
'
+
file
,
arrayOfFiles
);
}
else
{
arrayOfFiles
.
push
(
path
.
join
(
dirPath
,
file
));
}
}));
return
arrayOfFiles
;
};
private
async
getTotalSize
(
directoryPath
:
string
):
Promise
<
number
>
{
const
arrayOfFiles
=
await
this
.
getAllFiles
(
directoryPath
);
let
totalSize
=
0
;
for
(
const
filePath
of
arrayOfFiles
)
{
totalSize
+=
(
await
fs
.
stat
(
filePath
)).
size
;
}
return
totalSize
;
};
get
fs
()
{
return
{
getAllFiles
:
this
.
getAllFiles
.
bind
(
this
),
getTotalSize
:
this
.
getTotalSize
.
bind
(
this
)
};
}
}
}
...
...
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