Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

feat(extract): add 'safe mode' via extractOverwrite option #82

Open
wants to merge 4 commits into
base: latest
Choose a base branch
from

Conversation

aem
Copy link

@aem aem commented Apr 29, 2017

More or less a WIP, first round without tests. I've never worked with Promises IRL before, my only Promise experience is via API clients on the front-end and my node experience is limited, so I want to know what I did wrong here before digging into tests.

Adds the 'safe mode' outlined in #73, requiring a config option extractOverwrite to overwrite an existing directory, otherwise aborting the extract operation

@zkat

@aem
Copy link
Author

aem commented Apr 29, 2017

oops my VSCode settings overrode your style settings, changing some of that back

extract.js Outdated
@@ -85,3 +89,22 @@ function cleanUpCached (dest, cachePath, integrity, opts) {
cacache.rm.content(cachePath, integrity, opts)
)
}

function checkOverwrite (extractOverwrite, spec, dest) {
return new BB((resolve, reject) => {
Copy link
Owner

Choose a reason for hiding this comment

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

You can turn this bit into something like:

if (extractOverWrite) {
  return BB.resolve()
} else {
  return readdirAsync(dest).then(() => {
    const err = ...
    throw err // Promises catch throws and turn them into rejects if inside a promise handler scope
  }).catch({code: 'ENOENT'}, () => {}) // catching will automatically resolve
}

extract.js Outdated
opts.log.silly('pacote', `data for ${opts.integrity} not present. Using manifest.`)
return extractByManifest(startTime, spec, dest, opts)
}
checkOverwrite(opts.extractOverwrite, spec, dest)
Copy link
Owner

Choose a reason for hiding this comment

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

You should add a return here, so that this promise and its sub-promises become the return value for extract.

@zkat
Copy link
Owner

zkat commented Apr 29, 2017

I added some commentary since you mentioned you hadn't played with Promises before. Hopefully it helps :)

@zkat
Copy link
Owner

zkat commented Apr 29, 2017

Oh and also thank you! This is looking great so far 🙆

@aem
Copy link
Author

aem commented Apr 30, 2017

@zkat commentary greatly appreciated :) review fixes and tests coming soon!

@zkat
Copy link
Owner

zkat commented May 8, 2017

Hey! Sorry, I missed your last update!

This is looking great so far. The last thing I'd like to ask is whether you'd be willing/able to write a test for this: just a test that calls extract on an empty directory, a missing directory, and a directory with stuff in it.

If this seems like too much, that's fine -- I've been slacking a lot on tests myself.

And don't worry about the conflict! I can resolve that for you 👌

@wbern
Copy link

wbern commented Feb 26, 2019

@zkat can we merge this someday? I was looking for an ability to do this today.

@aem
Copy link
Author

aem commented Feb 26, 2019 via email

@wbern
Copy link

wbern commented Feb 26, 2019

If we want to make sure not to break anything, I think the safe mode should be opt in.

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

Successfully merging this pull request may close these issues.

3 participants