indexeddb-fs
Advanced tools
Comparing version 1.2.1 to 1.2.2
{ | ||
"name": "indexeddb-fs", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "An 'fs' kind of library dedicated to the browser", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
329
README.md
@@ -10,7 +10,7 @@ # indexeddb-fs | ||
### Motivation | ||
## Motivation | ||
Any other solutions I've found didn't work correctly. Problems I've found was about not enough validation and also directories were not being created. | ||
### Installation | ||
## Installation | ||
@@ -21,21 +21,35 @@ ```shell | ||
### Super quick start | ||
## Super quick start | ||
```js | ||
import fs from 'indexeddb-fs'; | ||
import { | ||
isFile, | ||
exists, | ||
writeFile, | ||
isDirectory, | ||
readDirectory, | ||
createDirectory, | ||
removeDirectory, | ||
rootDirectoryName, | ||
} from 'indexeddb-fs'; | ||
await fs.createDirectory('files'); | ||
await fs.createDirectory('/files/private'); | ||
await fs.createDirectory('root/files/public'); | ||
await createDirectory('files'); | ||
await createDirectory('/files/private'); | ||
await createDirectory(`${rootDirectoryName}/files/public`); | ||
console.log(await fs.isDirectory('root')); // true | ||
console.log(await fs.isDirectory('files')); // true | ||
console.log(await fs.isDirectory('/files/private')); // true | ||
await expect(isDirectory('files')).resolves.toBeTruthy(); | ||
await expect(isDirectory('/files/private')).resolves.toBeTruthy(); | ||
await expect(isDirectory(rootDirectoryName)).resolves.toBeTruthy(); | ||
await expect(isDirectory('root/files/private')).resolves.toBeTruthy(); | ||
await expect(isDirectory(`${rootDirectoryName}/files/private`)).resolves.toBeTruthy(); | ||
await fs.writeFile('files/public/file.txt', 'content'); | ||
await writeFile('files/public/file.txt', 'content'); | ||
await expect(isFile('files/public/file.txt')).resolves.toBeTruthy(); | ||
await expect(exists('files/public/file.txt')).resolves.toBeTruthy(); | ||
console.log(await fs.isFile('files/public/file.txt')); // true | ||
console.log(await fs.isDirectory('files/public/file.txt')); // false | ||
await removeDirectory(rootDirectoryName); | ||
const { filesCount, directoriesCount } = await readDirectory(rootDirectoryName); | ||
await fs.removeDirectory(fs.rootDirectoryName); | ||
expect(filesCount).toEqual(0); | ||
expect(directoriesCount).toEqual(0); | ||
``` | ||
@@ -92,2 +106,18 @@ | ||
## fs.remove(fullPath) | ||
- Parameters: [`fullPath`: string] | ||
- Returns: `Promise<void>` | ||
- Description: Removes files and directories. It does not remove directories recursively! | ||
- Throws an error when the path does not contain anything. | ||
Example of usage: | ||
```js | ||
await writeFile('file1.txt', 'test content'); | ||
await remove('file1.txt'); | ||
await expect(exists('file1.txt')).resolves.toBeFalsy(); | ||
``` | ||
## fs.details(fullPath) | ||
@@ -138,18 +168,2 @@ | ||
## fs.remove(fullPath) | ||
- Parameters: [`fullPath`: string] | ||
- Returns: `Promise<void>` | ||
- Description: Removes files and directories. It does not remove directories recursively! | ||
- Throws an error when the path does not contain anything. | ||
Example of usage: | ||
```js | ||
await writeFile('file1.txt', 'test content'); | ||
await remove('file1.txt'); | ||
await expect(exists('file1.txt')).resolves.toBeFalsy(); | ||
``` | ||
# FILES | ||
@@ -180,23 +194,2 @@ | ||
## fs.fileDetails(fullPath) | ||
- Parameters: [`fullPath`: string] | ||
- Returns: `Promise<FileEntry<TData>>` | ||
- Description: Returns an object with details about the file. | ||
- Throws an error when the path does not contain anything. | ||
- Throws an error when the destination file is not a file. | ||
Example of usage: | ||
```js | ||
const createdFile = await writeFile('file.txt', 'test 2 content'); | ||
const createdFileDetails = await fileDetails(createdFile.fullPath); | ||
expect(createdFileDetails.type).toEqual('file'); | ||
expect(createdFileDetails.name).toEqual('file.txt'); | ||
expect(createdFileDetails.directory).toEqual('root'); | ||
expect(createdFileDetails.data).toEqual('test 2 content'); | ||
expect(createdFileDetails.fullPath).toEqual('root/file.txt'); | ||
``` | ||
Example result for `FileEntry<TData>` type: | ||
@@ -249,2 +242,23 @@ | ||
## fs.fileDetails(fullPath) | ||
- Parameters: [`fullPath`: string] | ||
- Returns: `Promise<FileEntry<TData>>` | ||
- Description: Returns an object with details about the file. | ||
- Throws an error when the path does not contain anything. | ||
- Throws an error when the destination file is not a file. | ||
Example of usage: | ||
```js | ||
const createdFile = await writeFile('file.txt', 'test 2 content'); | ||
const createdFileDetails = await fileDetails(createdFile.fullPath); | ||
expect(createdFileDetails.type).toEqual('file'); | ||
expect(createdFileDetails.name).toEqual('file.txt'); | ||
expect(createdFileDetails.directory).toEqual('root'); | ||
expect(createdFileDetails.data).toEqual('test 2 content'); | ||
expect(createdFileDetails.fullPath).toEqual('root/file.txt'); | ||
``` | ||
## fs.readFile(fullPath) | ||
@@ -287,3 +301,3 @@ | ||
- Returns: `Promise<FileEntry<TData>>` | ||
- Description: Renames file at `fullPath` to `newFilename`. | ||
- Description: Rename file at `fullPath` to the new filename provided as `newFilename`. | ||
- Throws an error when the path does not contain anything. | ||
@@ -321,33 +335,122 @@ - Throws an error when the `fullPath` is not a file. | ||
# DIRECTORY | ||
## fs.copyFile(fullPath, destinationPath) | ||
### fs.createDirectory(fullPath) | ||
- Parameters: [`fullPath`: string, `destinationPath`: string] | ||
- Returns: `Promise<FileEntry<TData>>` | ||
- Description: Copy file at `fullPath` to `destinationPath` and return `destinationPath` record. | ||
- Throws an error when the path does not contain anything. | ||
- Throws an error when the `fullPath` is not a file. | ||
- Throws an error when the `destinationPath` is already taken. | ||
- Parameters: [`fullPath`: string] | ||
- Returns: `Promise<DirectoryEntry>` | ||
- Creates a directory at `fullPath` and returns a Promise. | ||
- Throws an error when the destination directory of the directory does not exist. | ||
- Throws an error when the path contains a file with the same name. | ||
Example of usage: | ||
Example result for `DirectoryEntry` type: | ||
```js | ||
await createDirectory('copied_files'); | ||
await writeFile('root_file.txt', 'root file content'); | ||
await copyFile('root_file.txt', 'copied_files/file.txt'); | ||
await expect(readFile('root_file.txt')).resolves.toEqual('root file content'); | ||
await expect(readFile('copied_files/file.txt')).resolves.toEqual('root file content'); | ||
``` | ||
Example result for `FileEntry<TData>` type: | ||
```object | ||
{ | ||
isRoot: false, | ||
type: 'file', | ||
name: 'file.txt', | ||
directory: 'root', | ||
type: 'directory', | ||
name: 'directory', | ||
createdAt: 1626882291087, | ||
fullPath: 'root/directory' | ||
data: 'test 2 content', | ||
createdAt: 1626882161631, | ||
fullPath: 'root/file.txt' | ||
} | ||
``` | ||
### fs.directoryDetails(fullPath) | ||
## fs.moveFile(fullPath, destinationPath) | ||
- Parameters: [`fullPath`: string, `destinationPath`: string] | ||
- Returns: `Promise<FileEntry<TData>>` | ||
- Description: Move file at `fullPath` to `destinationPath` and return `destinationPath` record. | ||
- Throws an error when the path does not contain anything. | ||
- Throws an error when the `fullPath` is not a file. | ||
- Throws an error when the `destinationPath` is already taken. | ||
Example of usage: | ||
```js | ||
await createDirectory('moved_files'); | ||
await writeFile('root_file.txt', 'root file content'); | ||
await moveFile('root_file.txt', 'moved_files/file.txt'); | ||
await expect(exists('root_file.txt')).resolves.toBeFalsy(); | ||
await expect(readFile('moved_files/file.txt')).resolves.toEqual('root file content'); | ||
``` | ||
Example result for `FileEntry<TData>` type: | ||
```object | ||
{ | ||
type: 'file', | ||
name: 'file.txt', | ||
directory: 'root', | ||
data: 'test 2 content', | ||
createdAt: 1626882161631, | ||
fullPath: 'root/file.txt' | ||
} | ||
``` | ||
# DIRECTORY | ||
## fs.isDirectory(fullPath) | ||
- Parameters: [`fullPath`: string] | ||
- Returns: `Promise<DirectoryEntry>` | ||
- Returns an object with details about the directory. | ||
- Returns: `Promise<boolean>` | ||
- Description: Returns true if the path contains a directory, false otherwise. | ||
- Throws an error when the path does not contain anything. | ||
- Throws an error when the destination directory is not a directory. | ||
Example of usage: | ||
```js | ||
await createDirectory('files'); | ||
await createDirectory('directories'); | ||
await expect(isDirectory('files')).resolves.toBeTruthy(); | ||
await expect(isDirectory('directories')).resolves.toBeTruthy(); | ||
await writeFile('file', 'content'); | ||
await expect(isDirectory('file')).resolves.toBeFalsy(); | ||
await writeFile('files/file', 'content'); | ||
await expect(isDirectory('files/file')).resolves.toBeFalsy(); | ||
await expect(isDirectory('files')).resolves.toBeTruthy(); | ||
``` | ||
## fs.createDirectory(fullPath) | ||
- Parameters: [`fullPath`: string] | ||
- Returns: `Promise<DirectoryEntry>` | ||
- Description: Creates a directory at `fullPath` and returns a Promise. | ||
- Throws an error when the destination directory of the directory does not exist. | ||
- Throws an error when the path contains a file with the same name. | ||
Example of usage: | ||
```js | ||
const resultForTest1 = await createDirectory('test2'); | ||
expect(resultForTest1.isRoot).toBeFalsy(); | ||
expect(resultForTest1.name).toEqual('test2'); | ||
expect(resultForTest1.type).toEqual('directory'); | ||
expect(resultForTest1.directory).toEqual('root'); | ||
expect(resultForTest1.fullPath).toEqual('root/test2'); | ||
const resultForTest2 = await createDirectory('test2/test3'); | ||
expect(resultForTest2.isRoot).toBeFalsy(); | ||
expect(resultForTest2.name).toEqual('test3'); | ||
expect(resultForTest2.type).toEqual('directory'); | ||
expect(resultForTest2.directory).toEqual('root/test2'); | ||
expect(resultForTest2.fullPath).toEqual('root/test2/test3'); | ||
``` | ||
Example result for `DirectoryEntry` type: | ||
@@ -366,10 +469,33 @@ | ||
### fs.readDirectory(fullPath) | ||
## fs.readDirectory(fullPath) | ||
- Parameters: [`fullPath`: string] | ||
- Returns: `Promise<ReadDirectoryDecoratorOutput>` | ||
- Reads the entire contents of a directory. | ||
- Description: Reads the entire contents of a directory. | ||
- Throws an error when the destination directory does not exist. | ||
- Throws an error when the destination directory is not a directory. | ||
Example of usage: | ||
```js | ||
await createDirectory('test_directory'); | ||
await writeFile('test_directory/file.txt', 'content'); | ||
await createDirectory('test_directory/folder'); | ||
const { files, directories, filesCount, directoriesCount } = await readDirectory('test_directory'); | ||
expect(filesCount).toEqual(1); | ||
expect(directoriesCount).toEqual(1); | ||
expect(files[0].type).toEqual('file'); | ||
expect(files[0].name).toEqual('file.txt'); | ||
expect(files[0].directory).toEqual('root/test_directory'); | ||
expect(files[0].fullPath).toEqual('root/test_directory/file.txt'); | ||
expect(directories[0].name).toEqual('folder'); | ||
expect(directories[0].type).toEqual('directory'); | ||
expect(directories[0].directory).toEqual('root/test_directory'); | ||
expect(directories[0].fullPath).toEqual('root/test_directory/folder'); | ||
``` | ||
Example result for `ReadDirectoryDecoratorOutput` type: | ||
@@ -387,19 +513,70 @@ | ||
### fs.isDirectory(fullPath) | ||
## fs.directoryDetails(fullPath) | ||
- Parameters: [`fullPath`: string] | ||
- Returns: `Promise<boolean>` | ||
- Returns true if the path contains a directory, false otherwise. | ||
- Returns: `Promise<DirectoryEntry>` | ||
- Description: Returns an object with details about the directory. | ||
- Throws an error when the path does not contain anything. | ||
- Throws an error when the destination directory is not a directory. | ||
### fs.removeDirectory(fullPath) | ||
Example of usage: | ||
```js | ||
const createdDirectory = await createDirectory('directory'); | ||
const createdDirectoryDetails = await directoryDetails(createdDirectory.fullPath); | ||
expect(createdDirectoryDetails.isRoot).toBeFalsy(); | ||
expect(createdDirectoryDetails.directory).toEqual('root'); | ||
expect(createdDirectoryDetails.type).toEqual('directory'); | ||
expect(createdDirectoryDetails.name).toEqual('directory'); | ||
expect(createdDirectoryDetails.fullPath).toEqual('root/directory'); | ||
``` | ||
Example result for `DirectoryEntry` type: | ||
```object | ||
{ | ||
isRoot: false, | ||
directory: 'root', | ||
type: 'directory', | ||
name: 'directory', | ||
createdAt: 1626882291087, | ||
fullPath: 'root/directory' | ||
} | ||
``` | ||
## fs.removeDirectory(fullPath) | ||
- Parameters: [`fullPath`: string] | ||
- Returns: `Promise<void>` | ||
- Removes the directory, recursively removing any files/subdirectories contained within. | ||
- Description: Removes the directory, recursively removing any files/subdirectories contained within. | ||
- Throws an error when the destination directory does not exist. | ||
- Throws an error when the destination directory is not a directory. | ||
Example of usage: | ||
```js | ||
await createDirectory('test_directory'); | ||
await writeFile('test_directory/file.txt', 'content'); | ||
await createDirectory('test_directory/foo'); | ||
await createDirectory('test_directory/folder'); | ||
await createDirectory('test_directory/folder/foo'); | ||
await createDirectory('test_directory/folder/foo/foo2'); | ||
await createDirectory('test_directory/folder/foo/foo2/foo5'); | ||
await createDirectory('test_directory/folder/foo/foo2/foo3'); | ||
await createDirectory('test_directory/folder/foo/foo2/file.txt'); | ||
await createDirectory('test_directory/folder/foo/foo2/foo3/foo4'); | ||
await removeDirectory('test_directory/folder/foo/foo2'); | ||
await expect(exists('test_directory/folder/foo/foo2')).resolves.toBeFalsy(); | ||
const { files, directories } = await readDirectory('test_directory'); | ||
expect([...files, ...directories]).toHaveLength(3); | ||
await removeDirectory('test_directory'); | ||
await expect(exists('test_directory')).resolves.toBeFalsy(); | ||
``` | ||
# License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
242945
573