RN cli: remove project temp dir experiment

Reviewed By: mjesun

Differential Revision: D5621056

fbshipit-source-id: 464353bc3e68e9e3657e1fbb02aca467b2daa5ca
This commit is contained in:
Jean Lauliac 2017-08-14 03:48:53 -07:00 committed by Facebook Github Bot
parent 30d85a4b05
commit 08947acde3
2 changed files with 2 additions and 117 deletions

View File

@ -1,53 +0,0 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @format
*/
'use strict';
const crypto = require('crypto');
const fs = require('fs');
class CannotCreateTempDirError extends Error {
constructor() {
super("couldn't create a temporary directory");
}
}
/**
* Claim a temporary directory that doesn't exist already and that cannot be
* predicted, so that nobody can race us to it. This is similar to `mkdtemp(3)`.
*/
function create(pathPrefix: string, mode: number): string {
let resultPath;
let i = 0;
do {
const rndBase64 = crypto.randomBytes(15).toString('base64');
resultPath = pathPrefix + rndBase64.replace(/\//g, '-');
if (++i === 10) {
throw new CannotCreateTempDirError();
}
} while (!tryMkdirSync(resultPath, mode));
return resultPath;
}
function tryMkdirSync(dirPath: string, mode?: number): boolean {
try {
fs.mkdirSync(dirPath, mode);
return true;
} catch (error) {
if (error.code == 'EEXIST') {
return false;
}
throw error;
}
}
module.exports = {CannotCreateTempDirError, create, tryMkdirSync};

View File

@ -12,8 +12,6 @@
'use strict'; 'use strict';
const TempDirs = require('./TempDirs');
const crypto = require('crypto'); const crypto = require('crypto');
const debugRead = require('debug')('Metro:TransformCache:Read'); const debugRead = require('debug')('Metro:TransformCache:Read');
const fs = require('fs'); const fs = require('fs');
@ -438,73 +436,13 @@ function useTempDir(): TransformCache {
if (process.getuid != null) { if (process.getuid != null) {
hash.update(process.getuid().toString()); hash.update(process.getuid().toString());
} }
const tmpDir = require('os').tmpdir(); const tmpDir = tmpdir();
const cacheName = 'react-native-packager-cache'; const cacheName = 'react-native-packager-cache';
const rootPath = path.join(tmpDir, cacheName + '-' + hash.digest('hex')); const rootPath = path.join(tmpDir, cacheName + '-' + hash.digest('hex'));
mkdirp.sync(rootPath); mkdirp.sync(rootPath);
return new FileBasedCache(rootPath); return new FileBasedCache(rootPath);
} }
/**
* Keep track of the path used for the cache in a local file so that a fresh
* clone gets a fresh cache, and so that the temp dir used cannot be predicted.
* Sometimes a different user build the same project, in which case we use a
* different subfolder of the temp directory.
*
* If the directory specified in the `.metro-bundler` doesn't exist anymore
* (ex. after a machine reboot and `/tmp` has been cleaned), we just create
* a new one.
*/
function useProjectDir(projectPath: string): TransformCache {
invariant(path.isAbsolute(projectPath), 'project path must be absolute');
const metaFilePath = path.resolve(projectPath, '.metro-bundler');
const metaDirPath = readMetaDirPath(metaFilePath);
if (metaDirPath != null) {
try {
return useMetaDirPath(metaDirPath);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
}
return useMetaDirPath(createMetaDir(metaFilePath));
}
/**
* Use the specified path as cache, by creating a subdirectory specific to the
* current user ID. This could fail if `metaDirPath` doesn't exist.
*/
function useMetaDirPath(metaDirPath: string): TransformCache {
const uidStr = process.getuid != null ? process.getuid().toString() : 'g';
const finalPath = path.join(metaDirPath, '_' + uidStr);
TempDirs.tryMkdirSync(finalPath, 0o700);
return new FileBasedCache(finalPath);
}
/**
* Return the path of the cache directory, if it's already been set for that
* project, in the shape of a `.metro-bundler` file.
*/
function readMetaDirPath(metaFilePath: string): ?string {
const metaFile = tryReadFileSync(metaFilePath);
if (metaFile == null) {
return null;
}
return metaFile.split('\n')[0];
}
/**
* Create a new top-level directory to be used for the cache. We give all
* permissions because different users might share that directory.
*/
function createMetaDir(metaFilePath: string): string {
const tmpDirPath = tmpdir();
const metaDirPath = TempDirs.create(path.join(tmpDirPath, 'mb-'), 0o777);
fs.writeFileSync(metaFilePath, metaDirPath + '\n');
return metaDirPath;
}
function tmpdir(): string { function tmpdir(): string {
const tmpDirPath = require('os').tmpdir(); const tmpDirPath = require('os').tmpdir();
invariant( invariant(
@ -525,4 +463,4 @@ function tryReadFileSync(filePath): ?string {
} }
} }
module.exports = {FileBasedCache, none, useTempDir, useProjectDir}; module.exports = {FileBasedCache, none, useTempDir};