create-expo-cljs-app
Version:
Create a react native application with Expo and Shadow-CLJS!
132 lines (111 loc) • 2.92 kB
JavaScript
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react_native
* @format
*/
const path = require('path');
const fs = require('fs');
const os = require('os');
const {sync: makeDirSync} = require('make-dir');
const packageJson = JSON.parse(
fs.readFileSync(require.resolve('../package.json'), 'utf8'),
);
/**
* This file is a fork of
* https://github.com/babel/babel/blob/2782a549e99d2ef1816332d23d7dfd5190f58a0f/packages/babel-register/src/cache.js#L1
*/
const FILENAME = path.join(
os.tmpdir(),
`.eslint-plugin-codegen.${packageJson.version}.disk-cache.json`,
);
let data = {};
let cacheDisabled = process.env.NODE_ENV === 'test';
function isCacheDisabled() {
return cacheDisabled;
}
/**
* Write stringified cache to disk.
*/
function save() {
if (isCacheDisabled()) {
return;
}
let serialised = '{}';
try {
serialised = JSON.stringify(data, null, ' ');
} catch (err) {
if (err.message === 'Invalid string length') {
err.message = "Cache too large so it's been cleared.";
console.error(err.stack);
} else {
throw err;
}
}
try {
makeDirSync(path.dirname(FILENAME));
fs.writeFileSync(FILENAME, serialised);
} catch (e) {
switch (e.code) {
// workaround https://github.com/nodejs/node/issues/31481
// todo: remove the ENOENT error check when we drop node.js 13 support
case 'ENOENT':
case 'EACCES':
case 'EPERM':
console.warn(
`Could not write cache to file: ${FILENAME} due to a permission issue. Cache is disabled.`,
);
cacheDisabled = true;
break;
case 'EROFS':
console.warn(
`Could not write cache to file: ${FILENAME} because it resides in a readonly filesystem. Cache is disabled.`,
);
cacheDisabled = true;
break;
default:
throw e;
}
}
}
/**
* Load cache from disk and parse.
*/
function load() {
if (isCacheDisabled()) {
data = {};
return;
}
process.on('exit', save);
process.nextTick(save);
let cacheContent;
try {
cacheContent = fs.readFileSync(FILENAME);
} catch (e) {
switch (e.code) {
// check EACCES only as fs.readFileSync will never throw EPERM on Windows
// https://github.com/libuv/libuv/blob/076df64dbbda4320f93375913a728efc40e12d37/src/win/fs.c#L735
case 'EACCES':
console.warn(
`Babel could not read cache file: ${FILENAME} due to a permission issue. Cache is disabled.`,
);
cacheDisabled = true;
/* fall through */
default:
return;
}
}
try {
data = JSON.parse(cacheContent);
} catch {}
}
/**
* Retrieve data from cache.
*/
function get() {
return data;
}
module.exports = {load, get};