UNPKG

3.08 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7function _path() {
8 const data = _interopRequireDefault(require("path"));
9 _path = function () {
10 return data;
11 };
12 return data;
13}
14function _fs() {
15 const data = _interopRequireDefault(require("fs"));
16 _fs = function () {
17 return data;
18 };
19 return data;
20}
21function _os() {
22 const data = _interopRequireDefault(require("os"));
23 _os = function () {
24 return data;
25 };
26 return data;
27}
28function _appdirsjs() {
29 const data = _interopRequireDefault(require("appdirsjs"));
30 _appdirsjs = function () {
31 return data;
32 };
33 return data;
34}
35function _chalk() {
36 const data = _interopRequireDefault(require("chalk"));
37 _chalk = function () {
38 return data;
39 };
40 return data;
41}
42var _logger = _interopRequireDefault(require("./logger"));
43function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
44function loadCache(name) {
45 try {
46 const cacheRaw = _fs().default.readFileSync(_path().default.resolve(getCacheRootPath(), name), 'utf8');
47 const cache = JSON.parse(cacheRaw);
48 return cache;
49 } catch (e) {
50 if (e.code === 'ENOENT') {
51 // Create cache file since it doesn't exist.
52 saveCache(name, {});
53 }
54 _logger.default.debug('No cache found');
55 return undefined;
56 }
57}
58function saveCache(name, cache) {
59 const fullPath = _path().default.resolve(getCacheRootPath(), name);
60 _fs().default.mkdirSync(_path().default.dirname(fullPath), {
61 recursive: true
62 });
63 _fs().default.writeFileSync(fullPath, JSON.stringify(cache, null, 2));
64}
65
66/**
67 * Returns the path string of `$HOME/.react-native-cli`.
68 *
69 * In case it doesn't exist, it will be created.
70 */
71function getCacheRootPath() {
72 const legacyPath = _path().default.resolve(_os().default.homedir(), '.react-native-cli', 'cache');
73 const cachePath = (0, _appdirsjs().default)({
74 appName: 'react-native-cli',
75 legacyPath
76 }).cache;
77 if (!_fs().default.existsSync(cachePath)) {
78 _fs().default.mkdirSync(cachePath, {
79 recursive: true
80 });
81 }
82 return cachePath;
83}
84function removeProjectCache(name) {
85 const cacheRootPath = getCacheRootPath();
86 try {
87 const fullPath = _path().default.resolve(cacheRootPath, name);
88 if (_fs().default.existsSync(fullPath)) {
89 _fs().default.rmSync(fullPath, {
90 recursive: true
91 });
92 }
93 } catch {
94 _logger.default.error(`Failed to remove cache for ${name}. If you experience any issues when running freshly initialized project, please remove the "${_chalk().default.underline(_path().default.join(cacheRootPath, name))}" folder manually.`);
95 }
96}
97function get(name, key) {
98 const cache = loadCache(name);
99 if (cache) {
100 return cache[key];
101 }
102 return undefined;
103}
104function set(name, key, value) {
105 const cache = loadCache(name);
106 if (cache) {
107 cache[key] = value;
108 saveCache(name, cache);
109 }
110}
111var _default = {
112 get,
113 set,
114 removeProjectCache,
115 getCacheRootPath
116};
117exports.default = _default;
118
119//# sourceMappingURL=cacheManager.ts.map
\No newline at end of file