Skip to content

Commit

Permalink
feat: add sync script (#13)
Browse files Browse the repository at this point in the history
* Add sync script

* Add example in README

* Refactor isolating os-specific commands

* Add tests and format

* Add types for fast-folder-size/sync

* Force execSync to output string

* Add sync type to tsd test

* Tests: remove unnecessary try/catch

* Correctly import sync

* Import fastFolderSizeSync type
  • Loading branch information
marcofugaro committed Nov 29, 2021
1 parent 059749a commit 334b7fb
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 38 deletions.
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
}
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

0 comments on commit 334b7fb

Please sign in to comment.