Skip to content

Latest commit

 

History

History
223 lines (150 loc) · 5.85 KB

File metadata and controls

223 lines (150 loc) · 5.85 KB

dandanplay-resource-service: ts-impl

此目录中包含 dandanplay-resource-service 的 TypeScript 相关实现,并支持:

  • 部署到 Cloudflare Workers
  • 部署到 Deno Deploy
  • 本地或服务器以 Node.js 运行
  • 本地或服务器以 Deno 运行

Setup

若需修改源码,则需要准备相关开发环境

拉取源码并安装依赖

git clone https://github.com/LussacZheng/dandanplay-resource-service.git
cd dandanplay-resource-service/typescript
pnpm i

cd cf-worker

开发

  1. 启动开发服务器

    pnpm run dev
  2. 修改该文件夹内代码,浏览器访问 localhost:8787 以观察效果。

  3. 但若需修改 packages/ 下的代码,则需要退出开发服务器并重新运行上述命令。

部署到 Cloudflare Workers

  • 普通构建

    pnpm run build

    输出文件位于 ./cf-worker/dist/main.js

  • 发布构建

    pnpm run dist

    输出文件位于 ./dist/worker_*.js

复制上述构建产物 worker.js 内容到 workers.dev 脚本编辑页面 中,部署即可。

本地或服务器以 Node.js 运行

  • 通过 Miniflare 再利用上述构建产物,直接运行

    pnpx miniflare --modules /path/to/worker.js
  • 或自行编写 HTTP 服务器,并引用 dandanplay-resource-api 作为依赖,参考 cf-worker ,只需定义路由并监听 Request 即可。(HINT: Express, Koa, Vanilla Node, etc.)

cd deno-deploy 并用 vscode 以独立窗口打开该文件夹。

开发

  1. 启动开发服务器

    deno task dev
  2. 修改该文件夹内代码,浏览器访问 localhost:8000 以观察效果。

  3. 但若需修改 packages/ 下的代码,则需要退出开发服务器并重新运行上述命令。

部署到 Deno Deploy

  • 普通构建

    deno task build

    输出文件位于 ./deno-deploy/dist/main.js ,但还需要额外将该文件中的 \" 全部删除或替换为 ' 才能正常运行

  • 发布构建

    deno task dist

    输出文件位于 ./dist/playground_*.js

复制上述构建产物 playground.js 内容到 Deno Playground 脚本编辑页面 中,部署即可。

本地或服务器以 Deno 运行

  • 以项目整体形式运行

    deno task start
  • 或先构建再以单文件形式运行

    deno run --allow-net /path/to/playground.js
  • 或打包为 独立可执行文件 再运行

    注意,如果需要独立可执行文件 (standalone executable),更建议使用 GoRust 实现,此处只是提供一种可行性。

    此外,打包时的 Warning 可忽略

    deno task compile

Contributing Guide

Install dependencies

pnpm -w i -r

Build and distribute all

You can build worker.js (for Cloudflare Workers) and playground.js (for Deno Deploy) with a single command:

pnpm -w run dist:all

Find the built artifacts under ./.dist/.

Project Structure

Both cfw-impl and deno-impl are simple wrappers of packages/dandanplay-resource-api, with additional HTTP servers (Request handler and router).

.
|-- cf-worker/
|   |-- dist.ts                     # bundle script
|   |-- main.ts                     # entrypoint of `cfw-impl`
|   `-- ...
|-- deno-deploy/
|   |-- dist.ts                     # bundle script
|   |-- main.ts                     # entrypoint of `deno-impl`
|   |-- routes.ts                   # part of `./main.ts`
|   `-- ...
|-- packages/
|   |-- dandanplay-resource-api/    # **core implementation codes**
|   `-- utils/                      # utils for both main and dist script
|-- README.md                       # the document you are reading now  <--
|-- pnpm-workspace.yaml             # `pnpm` workspace definition
|-- tsconfig.base.json              # common config for `tsc`
|-- vitest.config.ts                # common config for `vitest`
`-- ...

Modify Packages

Whenever you edit the source codes under ./packages/*, you will need to recompile/rebuild the library files.

# While in `packages/{library}/` dir itself, you can rebuild this library via
$ pnpm run build

# or rebuild all libraries inside `packages/`
$ pnpm -w run sync-deps

You should also write unit tests for your code.

# While in `packages/{library}/` dir, you can test this library
$ pnpm run test

# or test all the libraries
$ pnpm -w run test

Likewise, get code coverage report under ./.coverage/:

pnpm -w run coverage

And remember to format codes before committing:

pnpm -w run format

Notes on NPM scripts

  • For all the commands mentioned above related to pnpm run, you can omit the run. For example, pnpm -w test is equivalent to pnpm -w run test.

  • Highlight/list the important or frequently used npm scripts defined in the package.json of this project/sub-package:

    pnpm run list