react-native-ota-hot-update
Version:
Hot update for react native
152 lines (144 loc) • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.writeFile = exports.unlink = exports.symlink = exports.stat = exports.rmdir = exports.readlink = exports.readdir = exports.readFile = exports.mkdir = exports.lstat = exports.chmod = void 0;
var _buffer = require("buffer");
let RNFS = {
unlink: console.log,
readdir: console.log,
mkdir: console.log,
readFile: console.log,
writeFile: console.log,
stat: console.log
};
try {
RNFS = require('react-native-fs');
} catch {}
function Err(name) {
return class extends Error {
code = name;
constructor(...args) {
super(...args);
if (this.message) {
this.message = name + ': ' + this.message;
} else {
this.message = name;
}
}
};
}
// const EEXIST = Err('EEXIST'); // <-- Unused because RNFS's mkdir never throws
const ENOENT = Err('ENOENT');
const ENOTDIR = Err('ENOTDIR');
// const ENOTEMPTY = Err('ENOTEMPTY'); // <-- Unused because RNFS's unlink is recursive by default
const readdir = async path => {
try {
return await RNFS.readdir(path);
} catch (err) {
switch (err.message) {
case 'Attempt to get length of null array':
{
throw new ENOTDIR(path);
}
case 'Folder does not exist':
{
throw new ENOENT(path);
}
default:
throw err;
}
}
};
exports.readdir = readdir;
const mkdir = async path => {
return RNFS.mkdir(path);
};
exports.mkdir = mkdir;
const readFile = async (path, opts) => {
let encoding;
if (typeof opts === 'string') {
encoding = opts;
} else if (typeof opts === 'object') {
encoding = opts.encoding;
}
// @ts-ignore
let result = await RNFS.readFile(path, encoding || 'base64');
if (!encoding) {
// @ts-ignore
result = _buffer.Buffer.from(result, 'base64');
}
return result;
};
exports.readFile = readFile;
const writeFile = async (path, content, opts) => {
let encoding;
if (typeof opts === 'string') {
encoding = opts;
} else if (typeof opts === 'object') {
encoding = opts.encoding;
}
if (typeof content === 'string') {
encoding = encoding || 'utf8';
} else {
encoding = 'base64';
content = _buffer.Buffer.from(content).toString('base64');
}
await RNFS.writeFile(path, content, encoding);
};
exports.writeFile = writeFile;
const stat = async path => {
try {
const r = await RNFS.stat(path);
// we monkeypatch the result with a `isSymbolicLink` method because isomorphic-git needs it.
// Since RNFS doesn't appear to support symlinks at all, we'll just always return false.
// @ts-ignore
r.isSymbolicLink = () => false;
return r;
} catch (err) {
switch (err.message) {
case 'File does not exist':
{
throw new ENOENT(path);
}
default:
throw err;
}
}
};
// Since there are no symbolic links, lstat and stat are equivalent
exports.stat = stat;
const lstat = exports.lstat = stat;
const unlink = async path => {
try {
await RNFS.unlink(path);
} catch (err) {
switch (err.message) {
case 'File does not exist':
{
throw new ENOENT(path);
}
default:
throw err;
}
}
};
// RNFS doesn't have a separate rmdir method, so we can use unlink for deleting directories too
exports.unlink = unlink;
const rmdir = exports.rmdir = unlink;
// These are optional, which is good because there is no equivalent in RNFS
const readlink = async () => {
throw new Error('not implemented');
};
exports.readlink = readlink;
const symlink = async () => {
throw new Error('not implemented');
};
// Technically we could pull this off by using `readFile` + `writeFile` with the `mode` option
// However, it's optional, because isomorphic-git will do exactly that (a readFile and a writeFile with the new mode)
exports.symlink = symlink;
const chmod = async () => {
throw new Error('not implemented');
};
exports.chmod = chmod;
//# sourceMappingURL=fs.js.map