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 all 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
22 changes: 22 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,24 @@ test('folder size is correct', t => {
t.end()
})
})

test('sync: folder size is larger than 0', t => {
const bytes = fastFolderSizeSync('.')
t.ok(Number.isFinite(bytes))
t.ok(bytes > 0)
t.end()
})

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

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

const bytes = fastFolderSizeSync(testdirName)
console.log('real size:', writtenBytes, 'found size:', bytes)
t.ok(bytes >= writtenBytes)
t.ok(bytes <= writtenBytes * 1.5)
t.end()
})
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ExecException, ChildProcess } from 'child_process'
import fastFolderSizeSync from './sync'

declare function fastFolderSize(
path: string,
callback: (err: ExecException | null, bytes?: number) => void
): ChildProcess

export = fastFolderSize

declare module 'fast-folder-size/sync' {
export = fastFolderSizeSync
}
Comment on lines +11 to +13
Copy link
Owner

@simoneb simoneb Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

despite the tests passing, I have a feeling that defining the types this way will not let a typescript consumer use it in the way you intend it:

import fastFolderSizeSync from 'fast-folder-size/sync'
// or
const fastFolderSizeSync = require('fast-folder-size/sync')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's correct, what are you proposing?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to merge and release a new version. Will you have a chance to try the released package to see if the typings work correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I will!

3 changes: 3 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { expectType } from 'tsd'
import { ExecException, ChildProcess } from 'child_process'
import fastFolderSize from '.'
import fastFolderSizeSync from './sync'

expectType<ChildProcess>(
fastFolderSize('.', (err, bytes) => {
expectType<ExecException | null>(err)
expectType<number | undefined>(bytes)
})
)

expectType<number | undefined>(fastFolderSizeSync('.'))
2 changes: 2 additions & 0 deletions types/sync.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare function fastFolderSizeSync(path: string): number | undefined
export = fastFolderSizeSync