Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should raises an error when pass invalid arguments', () => {
// $ExpectError: first argument is required
readDirectoryAsync();
// $ExpectError: first argument must be a string
readDirectoryAsync(69);
});
});
it('should passes when used properly', () => {
readDirectoryAsync('fileUri').then(files => {
(files: Array);
// $ExpectError: check any
(files: number);
});
});
async _truncate () {
// this isn't atomic so only enter this method once at any time
if (this._truncating) return
this._truncating = true
try {
// list the payloads in order
const payloads = (await FileSystem.readDirectoryAsync(this._path))
.filter(f => filenameRe.test(f)).sort()
// figure out how many over MAX_ITEMS we are
const diff = payloads.length - MAX_ITEMS
// do nothing if within the limit
if (diff < 0) {
this._truncating = false
return
}
// wait for each of the items over the limit to be removed
await Promise.all(
payloads.slice(0, diff)
.map(f => this.remove(`${this._path}/${f}`))
)
async peek () {
try {
const payloads = await FileSystem.readDirectoryAsync(this._path)
const payloadFileName = payloads.filter(f => filenameRe.test(f)).sort()[0]
if (!payloadFileName) return null
const id = `${this._path}/${payloadFileName}`
try {
const payloadJson = await FileSystem.readAsStringAsync(id)
const payload = JSON.parse(payloadJson)
return { id, payload }
} catch (e) {
// if we got here it's because
// a) JSON.parse failed or
// b) the file can no longer be read (maybe it was truncated?)
// in both cases we want to speculatively remove it and try peeking again
await this.remove(id)
return this.peek()
}
async function doesOldFilesDirectoryContainLock(path) {
const children = await FileSystem.readDirectoryAsync(path);
return children.indexOf(LOCK_FILE_NAME) > -1;
}
async function addLockToOldFilesDirectory(path) {
async function treeSearch(relativePath, legacyPath, newPath, resolveConflict) {
const currentNewPath = `${newPath}${relativePath}`;
const currentLegacyPath = `${legacyPath}${relativePath}`;
const legacyPathInfo = await FileSystem.getInfoAsync(currentLegacyPath);
const newPathInfo = await FileSystem.getInfoAsync(currentNewPath);
if (legacyPathInfo.exists && !newPathInfo.exists) {
await FileSystem.copyAsync({
from: currentLegacyPath,
to: currentNewPath,
});
await FileSystem.deleteAsync(currentLegacyPath);
return;
}
if (legacyPathInfo.isDirectory) {
const children = await FileSystem.readDirectoryAsync(currentLegacyPath);
for (let child of children) {
await treeSearch(relativePath + `${child}/`, legacyPath, newPath, resolveConflict);
}
}
else {
await resolveConflict(currentLegacyPath, currentNewPath);
}
}
async function doesOldFilesDirectoryContainLock(path) {
withCallback(callback, async () => {
await FileSystem.makeDirectoryAsync(baseFolder, {
intermediates: true,
});
const baseFolderLength = baseFolder.length;
const files = await FileSystem.readDirectoryAsync(baseFolder);
return files.map(fileUri =>
decodeURIComponent(fileUri.substring(baseFolderLength)),
);
});
componentDidMount = async () => {
const photos = await FileSystem.readDirectoryAsync(PHOTOS_DIR);
this.setState({ photos });
}