Skip to content

Commit

Permalink
Merge pull request #9 from BakaFT/dev
Browse files Browse the repository at this point in the history
`PluginFS` -> `context.fs`
  • Loading branch information
nomi-san authored Nov 17, 2023
2 parents f427b5c + 53a3699 commit f5c771e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ function sidebar() {
{ text: '[CommandBar]', link: '/runtime-api/command-bar' },
{ text: '[DataStore]', link: '/runtime-api/data-store' },
{ text: '[Effect]', link: '/runtime-api/effect' },
{ text: '[PluginFS]', link: '/runtime-api/plugin-fs' },
{ text: '[Toast]', link: '/runtime-api/toast' },
{ text: '[rcp] context.rcp', link: '/runtime-api/rcp' },
{ text: 'context.socket', link: '/runtime-api/socket' },
{ text: 'context.fs', link: '/runtime-api/fs' },
]
},
{
Expand Down
93 changes: 49 additions & 44 deletions docs/runtime-api/plugin-fs.md → docs/runtime-api/fs.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
# PluginFS
# PluginFS <br><Badge type="danger" text="experimental" />

This API allows plugins to access **their own directory** and perform some basic file operations.
This namespace helps you to gain access to **plugin's own directory** and perform some basic file system operations.

::: warning
::: tip

**Top-level** plugins don't have this namespace since they don't own a directory.

**Top-level** plugin is not allowed since they don't own a directory.
:::

::: warning

This API currently does not support calls in **remote** script.
All paths passed into this API are **relative to the root** directory of your plugin.

:::

::: tip
::: danger

All paths passed into this API are relative to the root directory of your plugin.
APIs under this namespace will return a **Promise of undefined** if [illegal paths](https://github.com/PenguLoader/PenguLoader/blob/dev/plugins/src/preload/api/PluginFS.ts#L52) detected.

:::

## PluginFS.read(path)
## context.fs.read(path)

<Badge type="info" text="function" />
<Badge type="tip" text="since v1.1.0" />
<Badge type="tip" text="since v1.1.1" />

Read a file in text mode.

Expand All @@ -29,42 +33,42 @@ Read a file in text mode.

### Return value

A `Promise` of the content string on success.
A `Promise` of content `string` on success.

A `Promise` of `undefined` on failure.

### Example

```javascript
PluginFS.read("./index.js").then( content => {
context.fs.read("./index.js").then( content => {
console.log(content)
})

const content = await PluginFs.read("./README.md")
const content = await context.fs.read("./README.md")
```

## PluginFS.write(path,content,enableAppendMode?)
## context.fs.write(path,content,enableAppendMode?)

<Badge type="info" text="function" />
<Badge type="tip" text="since v1.1.0" />
<Badge type="tip" text="since v1.1.1" />

Write to a file in text mode.
Write a file in text mode.

### Params

- `path` - The path of the file you want to access with respect to the plugin root directory.
- `content` - The content string you want to write into.
- `content` - The content `string` you want to write into the file.
- `enableAppendMode` - Append to file if set to `true` or overwrite file if `false`. This is `false` by default.

### Return value

A `Promise` of a boolean result indicating success or failure.
A `Promise` of `boolean` indicating success or failure.

### Example

```javascript
// Create test.txt and write "Hello" into it
PluginFS.write("./test.txt","Hello").then( result => {
context.fs.write("./test.txt","Hello").then( result => {
if(result){
// success
}else{
Expand All @@ -73,7 +77,7 @@ PluginFS.write("./test.txt","Hello").then( result => {
})

// Appending " World!" to it
const result = await PluginFs.write("./test.txt"," World!",true)
const result = await context.fs.write("./test.txt"," World!",true)
```

::: tip
Expand All @@ -82,10 +86,10 @@ This API can create a file but can't create a file under a non-existing director

:::

## PluginFS.mkdir(path)
## context.fs.mkdir(path)

<Badge type="info" text="function" />
<Badge type="tip" text="since v1.1.0" />
<Badge type="tip" text="since v1.1.1" />

Create directories recursively.

Expand All @@ -95,36 +99,39 @@ Create directories recursively.

### Return Value

A `Promise` of a boolean result indicating success or failure.
A `Promise` of `boolean` indicating success or failure.

### Example

```javascript
const bMkdir0 = await PluginFS.mkdir("utils")
const bMkdir1 = await PluginFS.mkdir("/a/b")
const bMkdir2 = await PluginFS.mkdir("/a\\c")
const bMkdir0 = await context.fs.mkdir("utils")
const bMkdir1 = await context.fs.mkdir("/a/b")
const bMkdir2 = await context.fs.mkdir("/a\\c")
// false because it already exists
const bMkdir3 = await PluginFS.mkdir("a\\b/")
const bMkdir3 = await context.fs.mkdir("a\\b/")
```

## PluginFS.stat(path)
## context.fs.stat(path)

<Badge type="info" text="function" />
<Badge type="tip" text="since v1.1.0" />
<Badge type="tip" text="since v1.1.1" />

Get the status of a file.
Get status of a file.

### Params

- `path` - The file path with respect to the plugin root directory.

### Return value

A `Promise` of `FileStat` or `undefined` depending on success or failure.
A `Promise` of `FileStat` on success.
A `Promise` of `undefined` on failure.


```typescript
interface FileStat{
fileName: string

// 0 if isDir is true
length: number
isDir: boolean
Expand All @@ -134,17 +141,17 @@ interface FileStat{
### Example

```javascript
const stat1 = await PluginFS.stat("a/b")
const stat1 = await context.fs.stat("a/b")
if(stat1){
console.log("it's a directory")
}
const stat2 = await PluginFS.stat("a/random.js")
const stat2 = await context.fs.stat("a/random.js")
```

## PluginFS.ls(path)
## context.fs.ls(path)

<Badge type="info" text="function" />
<Badge type="tip" text="since v1.1.0" />
<Badge type="tip" text="since v1.1.1" />

List files and directories under given path.

Expand All @@ -154,14 +161,14 @@ List files and directories under given path.

### Return value

A `Promise` of `Array` of file name strings on success.
A `Promise` of `string[]` of file name strings on success.

A `Promise` of `undefined` on failure.

## PluginFS.rm(path,recursively?)
## context.fs.rm(path,recursively?)

<Badge type="info" text="function" />
<Badge type="tip" text="since v1.1.0" />
<Badge type="tip" text="since v1.1.1" />

::: danger

Expand All @@ -180,22 +187,20 @@ Just like `rm` command in Unix-like systems.

### Return value

A `Promise` of a `number` showing how many files and directories is deleted.

When deleting with `recursively` set to `true`, the number value is sum of deleted `directories` and `files`.
A `Promise` of `number` showing how many files and directories is deleted.

### Example

You can only delete a non-empty directory with `recursively` set to `true`

```javascript
// 1
const bRm1 = await PluginFS.rm("./empty-dir")
const bRm1 = await context.fs.rm("./empty-dir")
// 1
const bRm2 = await PluginFS.rm("./random-file-under-plugin-root")
const bRm2 = await context.fs.rm("./random-file-under-plugin-root")

// bRm3 == 0 because it's not empty
const bRm3 = await PluginFS.rm("./non-empty-dir")
const bRm3 = await context.fs.rm("./non-empty-dir")
// bRm4 >= 1 with recursively set to true
const bRm4 = await PluginFS.rm("./non-empty-dir",true)
const bRm4 = await context.fs.rm("./non-empty-dir",true)
```

0 comments on commit f5c771e

Please sign in to comment.