Implement "clear" method in "metro-cache" stores

Reviewed By: rubennorte

Differential Revision: D7628735

fbshipit-source-id: a129bd32f30be968116e0efa065badc6a7d55d51
This commit is contained in:
Miguel Jimenez Esun 2018-04-16 12:57:59 -07:00 committed by Facebook Github Bot
parent 8fcc40edf2
commit c56e414560
4 changed files with 27 additions and 8 deletions

View File

@ -14,6 +14,7 @@
"dependencies": { "dependencies": {
"jest-serializer": "22.4.3", "jest-serializer": "22.4.3",
"metro-core": "0.32.0", "metro-core": "0.32.0",
"mkdirp": "^0.5.1" "mkdirp": "^0.5.1",
"rimraf": "^2.5.4"
} }
} }

View File

@ -13,6 +13,7 @@
const fs = require('fs'); const fs = require('fs');
const mkdirp = require('mkdirp'); const mkdirp = require('mkdirp');
const path = require('path'); const path = require('path');
const rimraf = require('rimraf');
import type {TransformedCode} from 'metro/src/JSTransformer/worker'; import type {TransformedCode} from 'metro/src/JSTransformer/worker';
@ -27,13 +28,8 @@ class FileStore {
_root: string; _root: string;
constructor(options: Options) { constructor(options: Options) {
const root = options.root; this._root = options.root;
this._createDirs();
for (let i = 0; i < 256; i++) {
mkdirp.sync(path.join(root, ('0' + i.toString(16)).slice(-2)));
}
this._root = root;
} }
get(key: Buffer): ?TransformedCode { get(key: Buffer): ?TransformedCode {
@ -65,6 +61,11 @@ class FileStore {
fs.writeFileSync(this._getFilePath(key), data); fs.writeFileSync(this._getFilePath(key), data);
} }
clear() {
this._removeDirs();
this._createDirs();
}
_getFilePath(key: Buffer): string { _getFilePath(key: Buffer): string {
return path.join( return path.join(
this._root, this._root,
@ -72,6 +73,18 @@ class FileStore {
key.slice(1).toString('hex'), key.slice(1).toString('hex'),
); );
} }
_createDirs() {
for (let i = 0; i < 256; i++) {
mkdirp.sync(path.join(this._root, ('0' + i.toString(16)).slice(-2)));
}
}
_removeDirs() {
for (let i = 0; i < 256; i++) {
rimraf.sync(path.join(this._root, ('0' + i.toString(16)).slice(-2)));
}
}
} }
module.exports = FileStore; module.exports = FileStore;

View File

@ -149,6 +149,10 @@ class HttpStore {
gzip.end(JSON.stringify(value) || 'null'); gzip.end(JSON.stringify(value) || 'null');
}); });
} }
clear() {
// Not implemented.
}
} }
module.exports = HttpStore; module.exports = HttpStore;

View File

@ -13,4 +13,5 @@
export type CacheStore<T> = { export type CacheStore<T> = {
get(key: Buffer): ?T | Promise<?T>, get(key: Buffer): ?T | Promise<?T>,
set(key: Buffer, value: T): void | Promise<void>, set(key: Buffer, value: T): void | Promise<void>,
clear(): void | Promise<void>,
}; };