上传新应用
api.applications.create(file) 上传一个 zip 并创建一个全新的应用。file 参数可以是本地文件路径或 Buffer。
import { SquareCloudAPI } from "@squarecloud/api";
import { join } from "node:path";
const api = new SquareCloudAPI(process.env.SQUARE_API_KEY!);
const result = await api.applications.create(join(__dirname, "my-app.zip"));
console.log(result.id); // newly created application ID
console.log(result.name); // display name
console.log(result.lang); // language slug (e.g. "javascript")
console.log(result.ram); // allocated RAM in MB
该 zip 至少必须包含:
- 主文件 —— 应用的入口点
- 依赖文件 ——
package.json、requirements.txt 等
squarecloud.app —— 包含名称、描述、主文件、版本等的配置文件。参见配置文件指南
将文件 commit 到现有应用
app.commit(file, fileName?) 将文件上传到一个已部署的应用中。传入一个 zip 可一次性替换多个文件,或传入单个文件以仅更新该文件。
| 参数 | 类型 | 说明 |
|---|
file | string | Buffer | 本地文件路径或 Buffer 内容 |
fileName | string? | 附加到上传的名称。.zip 会在服务端被解压;任何其他扩展名都会作为单个文件写入。 |
Commit 单个文件 (Buffer)
从磁盘 commit 一个 zip
按路径 commit 一个文件
const content = Buffer.from("console.log('hello')\n");
await app.commit(content, "index.js");
import { readFile } from "node:fs/promises";
const zip = await readFile("./dist.zip");
await app.commit(zip, "dist.zip");
import { join } from "node:path";
const filePath = join(__dirname, "index.js");
await app.commit(filePath, "index.js");
在 v3 中,commit() 接受第三个 restart 参数。在 v4 中该第三个参数已被移除 —— 如果你需要重启,请在 commit 之后使用 app.restart()。
await app.commit(zip, "dist.zip");
await app.restart();