@micropue/drop-cli 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Micropue
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,308 @@
1
- # Temporary Holding Version
1
+ # DROP CLI
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 把本地静态网站持续发布到 [DROP](https://drop.micropue.com.cn/developers)。`drop` 会监听当前目录,在文件变化后发布一份完整快照,并显示可直接访问的公网链接。
4
+
5
+ 它适合本地预览、快速分享和轻量静态站点部署,使用体验接近内网穿透工具,但部署内容会持久保存在 DROP:
6
+
7
+ ```text
8
+ 本地目录 ──监听/防抖──> 完整快照 ──DROP REST API──> 公网链接
9
+ │ │
10
+ └── .drop-ignore └── .drop/drop.json
11
+ ```
12
+
13
+ 按 `Ctrl+C` 只会停止本地监听,不会删除已经上线的网站。
14
+
15
+ ## 环境要求
16
+
17
+ - Node.js `>= 20.12`
18
+ - 一个 DROP Personal API Key
19
+
20
+ API v1 目前只接受 UTF-8 编码的 `.html`、`.css` 和 `.js` 文件,不能上传图片、字体等二进制资源。
21
+
22
+ ## 安装与登录
23
+
24
+ 全局安装:
25
+
26
+ ```bash
27
+ npm install -g @micropue/drop-cli
28
+ ```
29
+
30
+ 也可以直接运行:
31
+
32
+ ```bash
33
+ npx @micropue/drop-cli --help
34
+ ```
35
+
36
+ 在 [DROP API Keys](https://drop.micropue.com.cn/developers/keys) 创建密钥。至少需要以下权限:
37
+
38
+ ```text
39
+ projects:read
40
+ projects:write
41
+ deployments:write
42
+ ```
43
+
44
+ 然后保存并验证密钥:
45
+
46
+ ```bash
47
+ drop login
48
+ ```
49
+
50
+ 密钥保存在当前用户配置目录下的 `micropue-drop/auth.json`,文件权限为 `0600`。也可以用环境变量临时覆盖:
51
+
52
+ ```bash
53
+ export DROP_API_KEY="你的 API Key"
54
+ drop whoami
55
+ ```
56
+
57
+ 不要把 API Key 写入 `.drop`、网站源码或 Git 仓库。
58
+
59
+ ## 快速开始
60
+
61
+ 进入网站根目录后直接运行:
62
+
63
+ ```bash
64
+ cd my-site
65
+ drop
66
+ ```
67
+
68
+ 也可以显式使用 `watch`:
69
+
70
+ ```bash
71
+ drop watch .
72
+ ```
73
+
74
+ 首次发布时,如果目录名不能直接作为 slug,CLI 会生成一个安全 slug。也可以在命令行指定:
75
+
76
+ ```bash
77
+ drop watch . --slug my-site --name "My Site"
78
+ ```
79
+
80
+ 发布成功后,终端会显示公网地址;后续改动会在防抖窗口结束后自动覆盖线上旧文件。按 `Ctrl+C` 结束监听。
81
+
82
+ ### 私有站点
83
+
84
+ `--private` 等价于 `--visibility password`:
85
+
86
+ ```bash
87
+ drop watch . --private
88
+ ```
89
+
90
+ 交互式终端会隐藏输入站点密码。CI 或非交互环境应使用环境变量:
91
+
92
+ ```bash
93
+ DROP_PASSWORD="至少 6 位" drop watch . --private
94
+ ```
95
+
96
+ 也可以使用 `--password <password>`,但不建议这样操作,因为参数可能留在 shell 历史或进程列表中。站点密码不会写入 `drop.json`。
97
+
98
+ ## 命令
99
+
100
+ ```text
101
+ drop [directory] 监听并发布,默认命令
102
+ drop watch [directory] 监听并持续发布
103
+ drop deploy [directory] 发布一次完整快照后退出
104
+ drop init [directory] 只生成 .drop,不调用 DROP API
105
+ drop status [directory] 显示本地部署记录
106
+ drop login 验证并保存 API Key
107
+ drop logout 删除本地保存的 API Key
108
+ drop whoami 显示当前账号、权限和配额
109
+ ```
110
+
111
+ 查看完整帮助:
112
+
113
+ ```bash
114
+ drop --help
115
+ drop watch --help
116
+ ```
117
+
118
+ ### 发布选项
119
+
120
+ | 选项 | 说明 |
121
+ | --- | --- |
122
+ | `--slug <slug>` | DROP 项目 slug,1–40 位小写字母、数字或连字符 |
123
+ | `--name <name>` | 项目显示名称,1–80 个字符 |
124
+ | `--visibility <mode>` | `public` 或 `password` |
125
+ | `--private` | `--visibility password` 的快捷方式 |
126
+ | `--password <password>` | 受保护站点的密码,6–128 个字符 |
127
+ | `--api-url <url>` | DROP API 地址 |
128
+ | `--message <message>` | 部署说明,最多 200 个字符 |
129
+ | `--interval <ms>` | 文件变化后的防抖时间,默认 `2000`,最小 `500` |
130
+ | `--poll` | 使用轮询监听,适合网络盘或原生监听不可用的环境 |
131
+ | `--force` | 即使内容哈希未变化也重新部署 |
132
+
133
+ `--slug`、`--name` 和可见性发生变化时,CLI 会更新线上项目元数据,并递增本地部署版本。
134
+
135
+ ## `.drop` 目录
136
+
137
+ 每次发布都会把本次可部署的文件完整同步到 `<site>/.drop/`。下次启动时,如果源码目录暂时没有可扫描的页面,CLI 会回退读取这份快照。
138
+
139
+ ```text
140
+ my-site/
141
+ ├── index.html
142
+ ├── assets/site.css
143
+ ├── .drop-ignore
144
+ └── .drop/
145
+ ├── drop.json
146
+ ├── index.html
147
+ └── assets/site.css
148
+ ```
149
+
150
+ `.drop` 中不保存 API Key。若站点本身包含私有内容,不要把它提交到公开仓库。
151
+
152
+ ### `drop.json`
153
+
154
+ `drop.json` 是本地部署记录,主要字段如下:
155
+
156
+ | 字段 | 说明 |
157
+ | --- | --- |
158
+ | `schemaVersion` | 配置格式版本 |
159
+ | `slug` / `name` | 项目标识与名称 |
160
+ | `visibility` | `public` 或 `password` |
161
+ | `projectId` | 线上 DROP 项目 ID |
162
+ | `url` | 公网访问地址 |
163
+ | `revision` | 线上项目修订号,用于 `If-Match` 并发保护 |
164
+ | `version` | CLI 部署版本;内容或元数据变化时递增 |
165
+ | `contentHash` | 本次快照哈希,用于跳过无变化部署 |
166
+ | `files` | 文件树,以及每个文件的字节数和 SHA-256 |
167
+ | `folders` | 空目录和文件目录组成的目录树 |
168
+ | `totalBytes` | 项目总字节数 |
169
+ | `hasPassword` / `enabled` | 线上访问保护与启用状态 |
170
+ | `deploymentId` | 最近一次部署 ID |
171
+ | `createdAt` / `updatedAt` / `deployedAt` | 本地记录和部署时间 |
172
+
173
+ 不要手工修改 `projectId`、`revision` 或 `contentHash`。需要重新开始时,可删除 `.drop` 后重新运行;这不会删除线上项目,但会丢失本地部署记录。
174
+
175
+ ## 排除文件
176
+
177
+ 在网站根目录创建 `.drop-ignore`,使用与 `.gitignore` 相同的规则:
178
+
179
+ ```gitignore
180
+ node_modules/
181
+ draft-*.html
182
+ private/
183
+ *.min.js
184
+ !private/public.html
185
+ ```
186
+
187
+ 以下内容始终不会被发布:
188
+
189
+ - `.drop/`
190
+ - `.git/`
191
+ - `node_modules/`
192
+ - 隐藏文件和隐藏目录
193
+ - 符号链接
194
+ - 非 `.html`、`.css`、`.js` 文件
195
+
196
+ 监听 `.drop-ignore` 后,规则变化会自动触发一次重新扫描。
197
+
198
+ ## 发布机制
199
+
200
+ - 每次部署发送完整文件集合,线上旧文件会被原子替换;失败时旧快照保持不变。
201
+ - 文件内容、文件路径或目录结构变化都会更新快照哈希。
202
+ - 每次实际变更都会递增 `drop.json` 中的 `version`。
203
+ - 写请求携带 `Idempotency-Key`;更新和部署携带 `If-Match` revision。
204
+ - 遇到 `412` 修订冲突时,CLI 会读取最新 revision 并重试一次。
205
+ - `429` / `503` 的 `Retry-After` 会在后续监听重试中生效。
206
+ - 监听过程中重复错误会去重显示,随后自动重试;按 `Ctrl+C` 可随时退出。
207
+
208
+ ## 限制
209
+
210
+ DROP API v1 当前限制:
211
+
212
+ | 项目 | 限制 |
213
+ | --- | --- |
214
+ | 每个项目文件数 | 20 |
215
+ | 单文件大小 | 512 KiB |
216
+ | 项目总大小 | 1 MiB |
217
+ | 每个项目目录数 | 50 |
218
+ | 单个文件路径 | 120 个字符 |
219
+ | 文件类型 | `.html`、`.css`、`.js`,扩展名必须小写 |
220
+
221
+ CLI 会在发送请求前检查这些限制并给出具体错误。
222
+
223
+ ## 环境变量
224
+
225
+ | 变量 | 默认值 | 说明 |
226
+ | --- | --- | --- |
227
+ | `DROP_API_KEY` | 无 | API Key;优先于本地保存的密钥 |
228
+ | `DROP_API_BASE` | `https://drop.micropue.com.cn/v1` | API 基础地址 |
229
+ | `DROP_PASSWORD` | 无 | 私有站点密码,适合非交互环境 |
230
+ | `DROP_AUTH_FILE` | 系统配置目录 | 自定义认证文件路径 |
231
+ | `DROP_REQUEST_TIMEOUT_MS` | `20000` | 请求超时,范围 1,000–120,000 毫秒 |
232
+ | `NO_COLOR` | 未设置 | 设置后禁用终端颜色 |
233
+
234
+ ## 常见问题
235
+
236
+ **提示 API Key 无效**
237
+
238
+ ```bash
239
+ drop logout
240
+ drop login
241
+ drop whoami
242
+ ```
243
+
244
+ 同时确认密钥拥有 `projects:read`、`projects:write` 和 `deployments:write`。
245
+
246
+ **slug 已被占用**
247
+
248
+ 换一个 slug,或在 `drop.json` 中恢复该站点原有的 slug。API v1 不会自动重定向旧地址。
249
+
250
+ **文件已修改但没有上线**
251
+
252
+ 确认文件扩展名为小写 `.html`、`.css` 或 `.js`,且没有被 `.drop-ignore` 排除。网络盘可尝试 `--poll`。需要强制发布时运行:
253
+
254
+ ```bash
255
+ drop deploy . --force
256
+ ```
257
+
258
+ **`Ctrl+C` 后网站还在吗**
259
+
260
+ 在。退出只停止监听和本次 CLI 进程,不会发送删除请求。
261
+
262
+ ## 开发
263
+
264
+ ```bash
265
+ npm ci
266
+ npm test
267
+ npm pack --dry-run
268
+ ```
269
+
270
+ 主要代码:
271
+
272
+ ```text
273
+ src/cli.js 命令定义
274
+ src/commands/ CLI 命令
275
+ src/lib/api.js DROP REST 客户端
276
+ src/lib/deployer.js 项目创建、元数据更新与快照部署
277
+ src/lib/discovery.js 文件扫描和限制校验
278
+ src/lib/config.js .drop 同步与 drop.json 原子写入
279
+ src/lib/snapshot.js 源码快照与 .drop 回退
280
+ ```
281
+
282
+ ## GitHub Actions 发布
283
+
284
+ 仓库使用两个工作流:
285
+
286
+ - `.github/workflows/ci.yml`:在 push 到 `main` 和 Pull Request 时测试 Node.js 20.12 / 22。
287
+ - `.github/workflows/release.yml`:推送 `v*` tag 时测试、发布 npm 并创建 GitHub Release。
288
+
289
+ 首次发布需要手动执行:
290
+
291
+ ```bash
292
+ npm login
293
+ npm publish --access public
294
+ ```
295
+
296
+ 之后在 GitHub 仓库设置 Secrets 中添加 `NPM_TOKEN`,或配置 npm Trusted Publisher,然后发布新版本:
297
+
298
+ ```bash
299
+ npm version patch
300
+ git push
301
+ git push --follow-tags
302
+ ```
303
+
304
+ tag 必须与 `package.json` 版本一致,例如 `v0.1.1`。如果该版本已经存在于 npm,工作流会跳过 npm 发布,但仍会创建 GitHub Release。
305
+
306
+ ## License
307
+
308
+ [MIT](./LICENSE)
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "@micropue/drop-cli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Watch a local static site and publish it to DROP.",
5
+ "type": "module",
6
+ "main": "./src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js"
9
+ },
10
+ "bin": {
11
+ "drop": "./src/cli.js",
12
+ "drop-cli": "./src/cli.js"
13
+ },
14
+ "files": [
15
+ "src",
16
+ "README.md",
17
+ "LICENSE"
18
+ ],
19
+ "scripts": {
20
+ "start": "node src/cli.js",
21
+ "test": "node --test --test-reporter=spec",
22
+ "test:coverage": "node --test --experimental-test-coverage",
23
+ "pack:check": "npm pack --dry-run"
24
+ },
25
+ "engines": {
26
+ "node": ">=20.12"
27
+ },
28
+ "keywords": [
29
+ "drop",
30
+ "deploy",
31
+ "static-site",
32
+ "cli",
33
+ "watch",
34
+ "micropue"
35
+ ],
36
+ "author": "Micropue",
37
+ "license": "MIT",
38
+ "homepage": "https://drop.micropue.com.cn/developers",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/Micropue/micropue-drop-CLI.git"
42
+ },
43
+ "bugs": {
44
+ "url": "https://github.com/Micropue/micropue-drop-CLI/issues"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "dependencies": {
50
+ "cac": "^6.7.14",
51
+ "chokidar": "^4.0.3",
52
+ "ignore": "^7.0.5"
53
+ }
54
+ }
package/src/cli.js ADDED
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { cac } from "cac";
4
+
5
+ import { loginCommand, logoutCommand, whoamiCommand } from "./commands/auth.js";
6
+ import { deployCommand } from "./commands/deploy.js";
7
+ import { initCommand } from "./commands/init.js";
8
+ import { statusCommand } from "./commands/status.js";
9
+ import { watchCommand } from "./commands/watch.js";
10
+ import { formatError, isAbortError } from "./lib/errors.js";
11
+ import { printError } from "./lib/terminal.js";
12
+ import { packageVersion } from "./lib/runtime.js";
13
+
14
+ function addPublishOptions(command) {
15
+ return command
16
+ .option("--slug <slug>", "DROP project slug")
17
+ .option("--name <name>", "DROP project display name")
18
+ .option("--visibility <visibility>", "public or password")
19
+ .option("--private", "Shortcut for password-protected access")
20
+ .option("--password <password>", "Password for a protected site")
21
+ .option("--api-url <url>", "DROP API base URL")
22
+ .option("--message <message>", "Deployment message");
23
+ }
24
+
25
+ export async function main(argv = process.argv) {
26
+ const cli = cac("drop");
27
+ let task;
28
+ const action = (handler) => (...args) => {
29
+ task = Promise.resolve().then(() => handler(...args));
30
+ };
31
+
32
+ addPublishOptions(
33
+ cli
34
+ .command("watch [directory]", "Watch a directory and publish changes")
35
+ .option("--interval <ms>", "Deployment debounce interval in milliseconds")
36
+ .option("--poll", "Use polling for the file watcher")
37
+ .option("--force", "Deploy even when the content hash is unchanged"),
38
+ ).action(action(watchCommand));
39
+
40
+ addPublishOptions(
41
+ cli
42
+ .command("deploy [directory]", "Publish one complete snapshot and exit")
43
+ .option("--force", "Deploy even when the content hash is unchanged"),
44
+ ).action(action(deployCommand));
45
+
46
+ cli
47
+ .command("init [directory]", "Initialize .drop without publishing")
48
+ .option("--slug <slug>", "DROP project slug")
49
+ .option("--name <name>", "DROP project display name")
50
+ .option("--visibility <visibility>", "public or password")
51
+ .option("--private", "Shortcut for password-protected access")
52
+ .action(action(initCommand));
53
+
54
+ cli
55
+ .command("status [directory]", "Show the local DROP deployment record")
56
+ .action(action(statusCommand));
57
+
58
+ cli
59
+ .command("login", "Validate and save a DROP API key")
60
+ .option("--api-url <url>", "DROP API base URL")
61
+ .action(action(loginCommand));
62
+
63
+ cli
64
+ .command("logout", "Remove the saved DROP API key")
65
+ .action(action(logoutCommand));
66
+
67
+ cli
68
+ .command("whoami", "Show the account connected to the current API key")
69
+ .option("--api-url <url>", "DROP API base URL")
70
+ .action(action(whoamiCommand));
71
+
72
+ addPublishOptions(
73
+ cli
74
+ .command("[directory]", "Watch and publish the current directory")
75
+ .option("--interval <ms>", "Deployment debounce interval in milliseconds")
76
+ .option("--poll", "Use polling for the file watcher")
77
+ .option("--force", "Deploy even when the content hash is unchanged"),
78
+ ).action(action(watchCommand));
79
+
80
+ cli.help();
81
+ cli.version(await packageVersion());
82
+ cli.parse(argv);
83
+ if (task) {
84
+ await task;
85
+ }
86
+ }
87
+
88
+ main().catch((error) => {
89
+ if (isAbortError(error)) {
90
+ process.exitCode = 130;
91
+ return;
92
+ }
93
+ printError(formatError(error));
94
+ process.exitCode = error?.exitCode ?? 1;
95
+ });
@@ -0,0 +1,71 @@
1
+ import { DropClient } from "../lib/api.js";
2
+ import {
3
+ clearStoredKey,
4
+ resolveApiKey,
5
+ saveStoredKey,
6
+ storedKeyPath,
7
+ } from "../lib/auth.js";
8
+ import { DEFAULT_API_BASE } from "../lib/constants.js";
9
+ import { formatError } from "../lib/errors.js";
10
+ import { promptHidden } from "../lib/prompt.js";
11
+ import {
12
+ printDropBanner,
13
+ printInfo,
14
+ printProgress,
15
+ printSuccess,
16
+ } from "../lib/terminal.js";
17
+
18
+ function apiBase(options) {
19
+ return (
20
+ options.apiUrl ??
21
+ process.env.DROP_API_BASE ??
22
+ DEFAULT_API_BASE
23
+ ).replace(/\/+$/g, "");
24
+ }
25
+
26
+ async function verify(apiKey, baseUrl) {
27
+ const client = new DropClient({ apiKey, baseUrl });
28
+ return client.getMe();
29
+ }
30
+
31
+ export async function loginCommand(options = {}) {
32
+ printDropBanner();
33
+ const key =
34
+ process.env.DROP_API_KEY?.trim() ||
35
+ (await promptHidden("DROP API key")).trim();
36
+ if (!key) {
37
+ throw new Error("No API key was provided.");
38
+ }
39
+ const baseUrl = apiBase(options);
40
+ printProgress("Verifying API key");
41
+ let me;
42
+ try {
43
+ me = await verify(key, baseUrl);
44
+ } catch (error) {
45
+ throw new Error(formatError(error), { cause: error });
46
+ }
47
+ const path = await saveStoredKey(key);
48
+ printSuccess(
49
+ `Logged in as ${me.account.nickname || me.account.username} (@${me.account.username})`,
50
+ );
51
+ printInfo(`scopes ${me.key.scopes.join(", ")}`);
52
+ printInfo(`stored ${path}`);
53
+ }
54
+
55
+ export async function logoutCommand() {
56
+ await clearStoredKey();
57
+ printSuccess(`Removed saved API key from ${storedKeyPath()}`);
58
+ }
59
+
60
+ export async function whoamiCommand(options = {}) {
61
+ printDropBanner();
62
+ const key = await resolveApiKey();
63
+ const me = await verify(key, apiBase(options));
64
+ printSuccess(
65
+ `${me.account.nickname || me.account.username} (@${me.account.username})`,
66
+ );
67
+ printInfo(`scopes ${me.key.scopes.join(", ")}`);
68
+ printInfo(
69
+ `limits ${me.limits.filesPerProject} files · ${me.limits.projectBytes} bytes`,
70
+ );
71
+ }
@@ -0,0 +1,81 @@
1
+ import { DropClient } from "../lib/api.js";
2
+ import { resolveApiKey } from "../lib/auth.js";
3
+ import { readConfig } from "../lib/config.js";
4
+ import { publishSnapshot } from "../lib/deployer.js";
5
+ import { formatError } from "../lib/errors.js";
6
+ import { loadSnapshot } from "../lib/snapshot.js";
7
+ import {
8
+ printDropBanner,
9
+ printInfo,
10
+ printProgress,
11
+ printSuccess,
12
+ printUrl,
13
+ } from "../lib/terminal.js";
14
+ import {
15
+ metadataChanged,
16
+ resolveDirectory,
17
+ resolveSettings,
18
+ } from "../lib/runtime.js";
19
+
20
+ export async function deployCommand(directory, options = {}) {
21
+ const root = await resolveDirectory(directory);
22
+ const config = await readConfig(root);
23
+ const settings = resolveSettings(root, options, config);
24
+ const snapshot = await loadSnapshot(root, { allowBundleFallback: true });
25
+ const needsWork =
26
+ settings.force ||
27
+ !config?.projectId ||
28
+ config.contentHash !== snapshot.hash ||
29
+ metadataChanged(config, settings);
30
+
31
+ printDropBanner();
32
+ printInfo(`site ${root}`);
33
+ printInfo(`project ${settings.slug} · ${settings.visibility}`);
34
+
35
+ if (!needsWork) {
36
+ printSuccess(`Already up to date · v${config.version}`);
37
+ if (config.url) {
38
+ printUrl(config.url);
39
+ }
40
+ return;
41
+ }
42
+
43
+ if (
44
+ settings.visibility === "password" &&
45
+ (!config?.projectId ||
46
+ config.visibility !== "password" ||
47
+ settings.explicitPassword) &&
48
+ !settings.password &&
49
+ !process.env.DROP_PASSWORD?.trim()
50
+ ) {
51
+ if (!process.stdin.isTTY) {
52
+ throw new Error(
53
+ "This site is password-protected but no password is available. Set DROP_PASSWORD.",
54
+ );
55
+ }
56
+ const { promptHidden } = await import("../lib/prompt.js");
57
+ settings.password = (await promptHidden("Site password")).trim();
58
+ }
59
+
60
+ const apiKey = await resolveApiKey();
61
+ const client = new DropClient({ apiKey, baseUrl: settings.apiBase });
62
+ let result;
63
+ try {
64
+ result = await publishSnapshot({
65
+ root,
66
+ config,
67
+ settings,
68
+ snapshot,
69
+ client,
70
+ onProgress: printProgress,
71
+ });
72
+ } catch (error) {
73
+ throw new Error(formatError(error), { cause: error });
74
+ }
75
+ if (result.changed || result.metadataChanged) {
76
+ printUrl(result.config.url);
77
+ printInfo(
78
+ `v${result.config.version} · revision ${result.config.revision} · ${result.config.files.length} files`,
79
+ );
80
+ }
81
+ }
@@ -0,0 +1,50 @@
1
+ import { readConfig, syncDropBundle } from "../lib/config.js";
2
+ import { DropConfigError } from "../lib/errors.js";
3
+ import { loadSnapshot } from "../lib/snapshot.js";
4
+ import {
5
+ printDropBanner,
6
+ printInfo,
7
+ printSuccess,
8
+ } from "../lib/terminal.js";
9
+ import {
10
+ resolveDirectory,
11
+ resolveSettings,
12
+ } from "../lib/runtime.js";
13
+
14
+ export async function initCommand(directory, options = {}) {
15
+ const root = await resolveDirectory(directory);
16
+ if (await readConfig(root)) {
17
+ throw new DropConfigError("This directory already has .drop/drop.json.", {
18
+ hint: "Run `drop status` or remove .drop manually before initializing again.",
19
+ });
20
+ }
21
+
22
+ const settings = resolveSettings(root, options, null);
23
+ const snapshot = await loadSnapshot(root, { allowBundleFallback: false });
24
+ const now = new Date().toISOString();
25
+ const config = {
26
+ schemaVersion: 1,
27
+ slug: settings.slug,
28
+ name: settings.name,
29
+ visibility: settings.visibility,
30
+ version: 0,
31
+ contentHash: snapshot.hash,
32
+ files: snapshot.files.map(({ path: filePath, bytes, sha256 }) => ({
33
+ path: filePath,
34
+ bytes,
35
+ sha256,
36
+ })),
37
+ folders: snapshot.folders,
38
+ totalBytes: snapshot.totalBytes,
39
+ createdAt: now,
40
+ updatedAt: now,
41
+ };
42
+
43
+ await syncDropBundle(root, snapshot, config);
44
+ printDropBanner();
45
+ printSuccess(`Initialized .drop for ${settings.slug}`);
46
+ printInfo(
47
+ `${snapshot.files.length} files · ${snapshot.totalBytes} bytes · ${settings.visibility}`,
48
+ );
49
+ printInfo("Run `drop watch` when you are ready to publish.");
50
+ }