|
1 | | -# 内置模块 |
| 1 | +# 使用模块 |
2 | 2 |
|
3 | | -为方便插件开发者使用,Serein按照NodeJs的风格内置了一些基本的函数 |
| 3 | +除了被主动加载的文件(插件入口点),其他 Js 文件只能通过作为模块被读取和加载 |
4 | 4 |
|
5 | | -所有模块已默认导入到上下文中,所以你可以直接使用 |
| 5 | +## 编写模块 |
6 | 6 |
|
7 | | -## `fs` |
| 7 | +使用 [`export`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/export) 导出变量、函数、类 |
8 | 8 |
|
9 | | -```ts title="示例" |
10 | | -fs.writeFileSync("1.txt", "Hello, World!"); |
11 | | -fs.copyFileSync("1.txt", "2.txt"); |
| 9 | +```js title="Serein/plugins/test/myModule.js" |
| 10 | +// highlight-next-line |
| 11 | +import { test } from './anotherLib.js' |
| 12 | + |
| 13 | +export const value = 114514; |
| 14 | + |
| 15 | +export async function getPosts() { |
| 16 | + // ... |
| 17 | +} |
| 18 | + |
| 19 | +export class Student { |
| 20 | + // ... |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +模块中可使用`import ... from '...'`导入其他模块 |
| 25 | + |
| 26 | +```js title="Serein/plugins/test/myModule.js" |
| 27 | +// highlight-next-line |
| 28 | +import { test } from './anotherLib.js' |
12 | 29 | ``` |
13 | 30 |
|
14 | | -:::warning |
| 31 | +## 导入 |
15 | 32 |
|
16 | | -- ~~因懒得实现`Buffer`类~~,所有`Buffer`均使用`integer[]`代替,方便内部.NET代码转换 |
17 | | -- 只实现了部分同步函数,剩余的函数可能在未来补全 |
18 | | -- 没有进行过严格的测试,**其行为可能和NodeJs中的存在差异** |
| 33 | +在被加载的 JS 文件中,你可以通过 `require()` 函数导入其他模块中导出的内容 |
19 | 34 |
|
20 | | -::: |
| 35 | +```ts |
| 36 | +require(file: string): any |
| 37 | +``` |
21 | 38 |
|
22 | | -- [`appendFileSync(path: string, data: string, options?: string | object): void`](https://nodejs.org/docs/latest/api/fs.html#fsappendfilesyncpath-data-options) |
23 | | -- [`appendFileSync(path: string, data: integer[], options?: string | object): void`](https://nodejs.org/docs/latest/api/fs.html#fsappendfilesyncpath-data-options) |
24 | | -- [`closeSync(fd: integer): void`](https://nodejs.org/docs/latest/api/fs.html#fsclosesyncfd) |
25 | | -- [`copyFileSync(src: string, dest: string, flags: integer = 0): void`](https://nodejs.org/docs/latest/api/fs.html#fscopyfilesyncsrc-dest-mode) |
26 | | - - `flags != 1`时会覆盖原有文件(如果存在) |
27 | | -- [`existsSync(path: string): boolean`](https://nodejs.org/docs/latest/api/fs.html#fsexistssyncpath) |
28 | | -- [`fsyncSync(fd: integer)`](https://nodejs.org/docs/latest/api/fs.html#fsfsyncsyncfd) |
29 | | -- [`ftruncateSync(fd: integer, len: integer = 0): void`](https://nodejs.org/docs/latest/api/fs.html#fsftruncatesyncfd-len) |
30 | | -- [`futimesSync(fd: integer, atime: Date, mtime: Date): void`](https://nodejs.org/docs/latest/api/fs.html#fsfutimessyncfd-atime-mtime) |
31 | | -- [`globSync(pattern: string, options?: object): string`](https://nodejs.org/docs/latest/api/fs.html#fsglobsyncpattern-options) |
32 | | -- [`globSync(pattern: string[], options?: object): string`](https://nodejs.org/docs/latest/api/fs.html#fsglobsyncpattern-options) |
33 | | -- [`mkdirSync(path: string, options?: object): void`](https://nodejs.org/docs/latest/api/fs.html#fsmkdirsyncpath-options) |
34 | | - - `options.mode`仅在非Windows平台上生效,这与NodeJs中一致,下同 |
35 | | -- [`mkdirSync(path: string, options?: integer = 0o777): void`](https://nodejs.org/docs/latest/api/fs.html#fsmkdirsyncpath-options) |
36 | | -- [`mkdtempSync(prefix: string, options?: object): string`](https://nodejs.org/docs/latest/api/fs.html#fsmkdtempsyncprefix-options) |
37 | | -- [`openSync(path: string, flags: string, mode?: string | integer): integer`](https://nodejs.org/docs/latest/api/fs.html#fsopensyncpath-flags-mode) |
38 | | - - `mode`总是会被忽略 |
39 | | -- [`readdirSync(path: string, options?: object): string[]`](https://nodejs.org/docs/latest/api/fs.html#fsreaddirsyncpath-options) |
40 | | - - 仅有`options.recursive`会生效 |
41 | | -- [`readFileSync(path: string, options?: object): string`](https://nodejs.org/docs/latest/api/fs.html#fsreadfilesyncpath-options) |
42 | | -- [`readSync(fd: integet, buffer: integer[], offset: integer, length: integer, position: integer = 0): integer`](https://nodejs.org/docs/latest/api/fs.html#fsreadsyncfd-buffer-offset-length-position) |
43 | | -- [`renameSync(oldPath: string, newPath: string): void`](https://nodejs.org/docs/latest/api/fs.html#fsrenamesyncoldpath-newpath) |
44 | | -- [`rmdirSync(path: string, options?: object): void`](https://nodejs.org/docs/latest/api/fs.html#fsrmdirsyncpath-options) |
45 | | - - 仅有`options.recursive`会生效 |
46 | | -- [`rmSync(path: string, options?: object): void`](https://nodejs.org/docs/latest/api/fs.html#fsrmsyncpath-options) |
47 | | - - 仅有`options.recursive`会生效 |
48 | | -- [`symlinkSync(target: string, path: string, type?: string): void`](https://nodejs.org/docs/latest/api/fs.html#fssymlinksynctarget-path-type) |
49 | | - - `type`总是会被忽略 |
50 | | -- [`truncateSync(path: string, len: integer = 0): void`](https://nodejs.org/docs/latest/api/fs.html#fstruncatesyncpath-len) |
51 | | -- [`unlinkSync(path: string): void`](https://nodejs.org/docs/latest/api/fs.html#fsunlinksyncpath) |
52 | | - - 实际上是删除`path` |
53 | | -- [`utimesSync(path: string, atime: Date, mtime: Date): void`](https://nodejs.org/docs/latest/api/fs.html#fsutimessyncpath-atime-mtime) |
54 | | -- [`writeFileSync(path: string, data: string, options?: string | object): void`](https://nodejs.org/docs/latest/api/fs.html#fswritefilesyncpath-data-options) |
55 | | -- [`writeFileSync(path: string, data: integer[], options?: string | object): void`](https://nodejs.org/docs/latest/api/fs.html#fswritefilesyncpath-data-options) |
56 | | -- [`writeSync(fd: integer, buffer: integer[], offset: integer, length: integer, position: integer): integer`](https://nodejs.org/docs/latest/api/fs.html#fswritesyncfd-buffer-offset-length-position) |
57 | | -- [`writeSync(fd: integer, data: string, position: integer = 0, encoding: string = 'utf8'): integer`](https://nodejs.org/docs/latest/api/fs.html#fswritesyncfd-buffer-offset-length-position) |
| 39 | +| 参数 | 类型 | 说明 | |
| 40 | +| ------ | -------- | ---------------------- | |
| 41 | +| `file` | `string` | 相对于被加载文件的路径 | |
58 | 42 |
|
59 | | -## `process` |
| 43 | +```js title="Serein/plugins/test/index.js" |
60 | 44 |
|
61 | | -- `arch: string` |
62 | | - - 实际上只会返回`"x64"`或`"arm64"`,这是因为Serein的发行版没有x86的构建( |
63 | | -- `argv: string[]` |
64 | | -- `argv0: string` |
65 | | - - 即`argv[0]` |
66 | | -- `chdir(directory: string): string` |
67 | | - - 不推荐使用此函数,否则可能**导致文件错位** |
68 | | -- `cwd(): string` |
69 | | -- `exit(code: integer = 0): void` |
70 | | - - 不推荐使用此函数 |
71 | | -- `env: { [key: string]: string }` |
72 | | -- `execPath: string` |
73 | | -- `exitCode: integer` |
74 | | -- `kill(pid: integer)` |
75 | | -- `pid: integer` |
76 | | -- `platform: string` |
77 | | - - 实际上仅会返回`"win32nt"`、`"unix"`或`"other"` |
78 | | -- `version: string` |
79 | | - - 返回的是.NET运行时版本 |
| 45 | +const { value, getPosts, Student } = require('./myModule.js') |
| 46 | +``` |
0 commit comments