-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add sync script #13
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c91e9e3
Add sync script
marcofugaro 5659cfd
Add example in README
marcofugaro f961e7e
Refactor isolating os-specific commands
marcofugaro 8dd67c0
Add tests and format
marcofugaro 32d2928
Add types for fast-folder-size/sync
marcofugaro 84103e2
Force execSync to output string
marcofugaro 0214a00
Add sync type to tsd test
marcofugaro 0672665
Tests: remove unnecessary try/catch
marcofugaro affd068
Correctly import sync
marcofugaro f7b4f72
Import fastFolderSizeSync type
marcofugaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I will!