Hello world
This commit is contained in:
parent
9e2ec6e427
commit
c2309d72bd
15 changed files with 130920 additions and 0 deletions
42
util.js
Normal file
42
util.js
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
// https://www.npmjs.com/package/get-folder-size
|
||||
// Adapted for CommonJS and synchronous Filesystem calls.
|
||||
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
async function getFolderSize (rootItemPath, options = {}) {
|
||||
const fileSizes = new Map()
|
||||
|
||||
await processItem(rootItemPath)
|
||||
|
||||
async function processItem (itemPath) {
|
||||
if (options.ignore?.test(itemPath)) return
|
||||
|
||||
const stats = fs.lstatSync(itemPath, { bigint: true })
|
||||
if (typeof stats !== 'object') return
|
||||
fileSizes.set(stats.ino, stats.size)
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
const directoryItems = fs.readdirSync(itemPath)
|
||||
if (typeof directoryItems !== 'object') return
|
||||
await Promise.all(
|
||||
directoryItems.map(directoryItem =>
|
||||
processItem(path.join(itemPath, directoryItem))
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
let folderSize = Array.from(fileSizes.values()).reduce((total, fileSize) => total + fileSize, 0n)
|
||||
|
||||
if (!options.bigint) {
|
||||
if (folderSize > BigInt(Number.MAX_SAFE_INTEGER)) {
|
||||
throw new RangeError('The folder size is too large to return as a Number. You can instruct this package to return a BigInt instead.')
|
||||
}
|
||||
folderSize = Number(folderSize)
|
||||
}
|
||||
|
||||
return folderSize
|
||||
}
|
||||
|
||||
module.exports = { getFolderSize }
|
||||
Loading…
Add table
Add a link
Reference in a new issue