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

Programmatically usage options #38

Open
scaljeri opened this issue Nov 26, 2018 · 2 comments
Open

Programmatically usage options #38

scaljeri opened this issue Nov 26, 2018 · 2 comments

Comments

@scaljeri
Copy link

I'm trying to use this tool programatically, but I'm getting errors

const file = fs.createReadStream(fileName);
hashmark(file, {length: 8, digest: 'md5', pattern: '{hash}'})
    .on('file', (oldN, newN) => console.log(newN))
    .on('end', (jsonMap) => console.log(jsonMap));

I get the following error:

path.js:39
    throw new ERR_INVALID_ARG_TYPE('path', 'string', path);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type 
    at assertPath (path.js:39:11)

I can however fix this by adding the cwd option

hashmark(file, {length: 8, digest: 'md5', pattern: '{hash}', cwd: './'})

but I cannot find any documentation about this, so I'm wondering if there is an other problem here?

Furthermore, the jsonMap contains the following

{ '<anonymous>': '76f7816d' }

It is always <anonymouse>, and the reason behind this is file.fileName === '<anonymouse>' and is used here. Is this a bug?

@keithamus
Copy link
Owner

Thanks for the issue @scaljeri!

I'm afraid the programmatic usage docs haven't got much attention. PRs welcome 😉.

As you discovered, cwd is required. It should probably default to './' - I think this could be considered a bug.

With the fileName thing - I think what you might want to do is this:

const file = fs.createReadStream(fileName);
file.fileName = fileName // set fileName so hashmark knows what file this is
hashmark(file, {length: 8, digest: 'md5', pattern: '{hash}', cwd: './'})
    .on('file', (oldN, newN) => console.log(newN))
    .on('end', (jsonMap) => console.log(jsonMap));

Alternatively you can pass a file name and hashmark will do the createReadStream step for you:

hashmark(fileName, {length: 8, digest: 'md5', pattern: '{hash}', cwd: './'})
    .on('file', (oldN, newN) => console.log(newN))
    .on('end', (jsonMap) => console.log(jsonMap));

@scaljeri
Copy link
Author

nice, thanks. If I have time I'll create a PR for the documentation!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants