MCPcopy
hub / github.com/bower/bower / extract

Function extract

lib/util/extract.js:203–275  ·  view source on GitHub ↗
(src, dst, opts)

Source from the content-addressed store, hash-verified

201// - keepArchive: true to keep the archive afterwards (defaults to false)
202// - keepStructure: true to keep the extracted structure unchanged (defaults to false)
203function extract(src, dst, opts) {
204 var extractor;
205 var promise;
206
207 opts = opts || {};
208 extractor = getExtractor(src);
209
210 // Try to get extractor from mime type
211 if (!extractor && opts.mimeType) {
212 extractor = getExtractor(opts.mimeType);
213 }
214
215 // If extractor is null, then the archive type is unknown
216 if (!extractor) {
217 return Q.reject(
218 createError(
219 'File ' + src + ' is not a known archive',
220 'ENOTARCHIVE'
221 )
222 );
223 }
224
225 // Extract to a temporary directory in case of file name clashes
226 return Q.nfcall(tmp.dir, {
227 template: dst + '-XXXXXX',
228 mode: 0777 & ~process.umask()
229 })
230 .then(function(tempDir) {
231 // nfcall may return multiple callback arguments as an array
232 return Array.isArray(tempDir) ? tempDir[0] : tempDir;
233 })
234 .then(function(tempDir) {
235 // Check archive file size
236 promise = Q.nfcall(fs.stat, src).then(function(stat) {
237 if (stat.size <= 8) {
238 throw createError(
239 'File ' + src + ' is an invalid archive',
240 'ENOTARCHIVE'
241 );
242 }
243
244 // Extract archive
245 return extractor(src, tempDir);
246 });
247
248 // Remove archive
249 if (!opts.keepArchive) {
250 promise = promise.then(function() {
251 return Q.nfcall(fs.unlink, src);
252 });
253 }
254
255 // Move contents from the temporary directory
256 // If the contents are a single directory (and we're not preserving structure),
257 // move its contents directly instead.
258 promise = promise
259 .then(function() {
260 return isSingleDir(tempDir);

Callers 3

GitHubResolver.jsFile · 0.85
UrlResolver.jsFile · 0.85
FsResolver.jsFile · 0.85

Calls 4

getExtractorFunction · 0.85
isSingleDirFunction · 0.85
moveDirectoryFunction · 0.85
createErrorFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…