diff --git a/src/fetchers/git-fetcher.js b/src/fetchers/git-fetcher.js index 1cf22642ac..284d594384 100644 --- a/src/fetchers/git-fetcher.js +++ b/src/fetchers/git-fetcher.js @@ -59,23 +59,24 @@ export default class GitFetcher extends BaseFetcher { return path.join(this.dest, constants.TARBALL_FILENAME); } - *getLocalPaths(override: ?string): Generator { - if (override) { - yield path.resolve(this.config.cwd, override); - } - yield this.getTarballMirrorPath(); - yield this.getTarballMirrorPath({ - withCommit: false, - }); - yield this.getTarballCachePath(); + getLocalPaths(override: ?string): Array { + const paths: Array = [ + override ? path.resolve(this.config.cwd, override) : null, + this.getTarballMirrorPath(), + this.getTarballMirrorPath({withCommit: false}), + this.getTarballCachePath(), + ]; + // $FlowFixMe: https://github.com/facebook/flow/issues/1414 + return paths.filter(path => path != null); } async fetchFromLocal(override: ?string): Promise { - const {stream, triedPaths} = await fsUtil.readFirstAvailableStream(this.getLocalPaths(override)); + const tarPaths = this.getLocalPaths(override); + const stream = await fsUtil.readFirstAvailableStream(tarPaths); return new Promise((resolve, reject) => { if (!stream) { - reject(new MessageError(this.reporter.lang('tarballNotInNetworkOrCache', this.reference, triedPaths))); + reject(new MessageError(this.reporter.lang('tarballNotInNetworkOrCache', this.reference, tarPaths))); return; } invariant(stream, 'cachedStream should be available at this point'); diff --git a/src/fetchers/tarball-fetcher.js b/src/fetchers/tarball-fetcher.js index b49fb4bf19..9bc2f21f16 100644 --- a/src/fetchers/tarball-fetcher.js +++ b/src/fetchers/tarball-fetcher.js @@ -118,20 +118,23 @@ export default class TarballFetcher extends BaseFetcher { return {validateStream, extractorStream}; } - *getLocalPaths(override: ?string): Generator { - if (override) { - yield path.resolve(this.config.cwd, override); - } - yield this.getTarballMirrorPath(); - yield this.getTarballCachePath(); + getLocalPaths(override: ?string): Array { + const paths: Array = [ + override ? path.resolve(this.config.cwd, override) : null, + this.getTarballMirrorPath(), + this.getTarballCachePath(), + ]; + // $FlowFixMe: https://github.com/facebook/flow/issues/1414 + return paths.filter(path => path != null); } async fetchFromLocal(override: ?string): Promise { - const {stream, triedPaths} = await fsUtil.readFirstAvailableStream(this.getLocalPaths(override)); + const tarPaths = this.getLocalPaths(override); + const stream = await fsUtil.readFirstAvailableStream(tarPaths); return new Promise((resolve, reject) => { if (!stream) { - reject(new MessageError(this.reporter.lang('tarballNotInNetworkOrCache', this.reference, triedPaths))); + reject(new MessageError(this.reporter.lang('tarballNotInNetworkOrCache', this.reference, tarPaths))); return; } invariant(stream, 'stream should be available at this point'); diff --git a/src/util/fs.js b/src/util/fs.js index 06263f6ea6..fdbb5c6028 100644 --- a/src/util/fs.js +++ b/src/util/fs.js @@ -814,24 +814,16 @@ export async function makeTempDir(prefix?: string): Promise { return dir; } -export async function readFirstAvailableStream( - paths: Iterable, -): Promise<{stream: ?ReadStream, triedPaths: Array}> { - let stream: ?ReadStream; - const triedPaths = []; - for (const tarballPath of paths) { - if (tarballPath) { - try { - const fd = await open(tarballPath, 'r'); - stream = fs.createReadStream(tarballPath, {fd}); - break; - } catch (err) { - // Try the next one - triedPaths.push(tarballPath); - } +export async function readFirstAvailableStream(paths: Iterable): Promise { + for (const path of paths) { + try { + const fd = await open(path, 'r'); + return fs.createReadStream(path, {fd}); + } catch (err) { + // Try the next one } } - return {stream, triedPaths}; + return null; } export async function getFirstSuitableFolder(