Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sync script #13

Merged
merged 10 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ npm i fast-folder-size
```js
const { promisify } = require('util')
const fastFolderSize = require('fast-folder-size')
const fastFolderSizeSync = require('fast-folder-size/sync')

// callback
fastFolderSize('.', (err, bytes) => {
Expand All @@ -36,6 +37,11 @@ fastFolderSize('.', (err, bytes) => {
const fastFolderSizeAsync = promisify(fastFolderSize)
const bytes = await fastFolderSizeAsync('.')

console.log(bytes)

// sync
const bytes = fastFolderSizeSync('.')

console.log(bytes)
```

Expand Down
43 changes: 5 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,16 @@
'use strict'

const path = require('path')
const { exec } = require('child_process')
const { commands, processOutput } = require('./os.js')

function fastFolderSize(target, cb) {
// windows
if (process.platform === 'win32') {
return exec(
`"${path.join(
__dirname,
'bin',
'du.exe'
)}" -nobanner -accepteula -q -c .`,
{ cwd: target },
(err, stdout) => {
if (err) return cb(err)
const command = commands[process.platform] || commands['linux']

// query stats indexes from the end since path can contain commas as well
const stats = stdout.split('\n')[1].split(',')

cb(null, +stats.slice(-2)[0])
}
)
}

// mac
if (process.platform === 'darwin') {
return exec(`du -sk .`, { cwd: target }, (err, stdout) => {
if (err) return cb(err)

const match = /^(\d+)/.exec(stdout)

const bytes = Number(match[1]) * 1024

cb(null, bytes)
})
}

// others
return exec(`du -sb .`, { cwd: target }, (err, stdout) => {
return exec(command, { cwd: target }, (err, stdout) => {
if (err) return cb(err)

const match = /^(\d+)/.exec(stdout)

const bytes = Number(match[1])
const processFn = processOutput[process.platform] || processOutput['linux']
const bytes = processFn(stdout)

cb(null, bytes)
})
Expand Down
48 changes: 48 additions & 0 deletions os.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const path = require('path')

const commands = {
// windows
win32: `"${path.join(
__dirname,
'bin',
'du.exe'
)}" -nobanner -accepteula -q -c .`,

// macos
darwin: `du -sk .`,

// any linux
linux: `du -sb .`,
}

const processOutput = {
// windows
win32(stdout) {
// query stats indexes from the end since path can contain commas as well
const stats = stdout.split('\n')[1].split(',')

const bytes = +stats.slice(-2)[0]

return bytes
},

// macos
darwin(stdout) {
const match = /^(\d+)/.exec(stdout)

const bytes = Number(match[1]) * 1024

return bytes
},

// any linux
linux(stdout) {
const match = /^(\d+)/.exec(stdout)

const bytes = Number(match[1])

return bytes
},
}

module.exports = { commands, processOutput }
16 changes: 16 additions & 0 deletions sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const { execSync } = require('child_process')
const { commands, processOutput } = require('./os.js')

function fastFolderSize(target) {
const command = commands[process.platform] || commands['linux']
const stdout = execSync(command, { cwd: target }).toString()

const processFn = processOutput[process.platform] || processOutput['linux']
const bytes = processFn(stdout)

return bytes
}

module.exports = fastFolderSize
30 changes: 30 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { test } = require('tap')
const crypto = require('crypto')

const fastFolderSize = require('.')
const fastFolderSizeSync = require('./sync')

test('folder size is larger than 0', t => {
fastFolderSize('.', (err, bytes) => {
Expand All @@ -27,3 +28,32 @@ test('folder size is correct', t => {
t.end()
})
})

test('sync: folder size is larger than 0', t => {
try {
const bytes = fastFolderSizeSync('.')
t.ok(Number.isFinite(bytes))
t.ok(bytes > 0)
t.end()
} catch (err) {
t.error(err)
}
marcofugaro marked this conversation as resolved.
Show resolved Hide resolved
})

test('sync: folder size is correct', t => {
const writtenBytes = 8 * 1024

const testdirName = t.testdir({
whatever: crypto.randomBytes(writtenBytes),
})

try {
const bytes = fastFolderSizeSync(testdirName)
console.log('real size:', writtenBytes, 'found size:', bytes)
t.ok(bytes >= writtenBytes)
t.ok(bytes <= writtenBytes * 1.5)
t.end()
} catch (err) {
t.error(err)
}
})
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ declare function fastFolderSize(
): ChildProcess

export = fastFolderSize

declare module 'fast-folder-size/sync' {
function fastFolderSize(path: string): number
export = fastFolderSize
}