-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
059749a
commit 334b7fb
Showing
8 changed files
with
107 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('.')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
declare function fastFolderSizeSync(path: string): number | undefined | ||
export = fastFolderSizeSync |