@anvil-works/anvil-cli 0.3.7

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) 2025 Anvil
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 ADDED
@@ -0,0 +1,358 @@
1
+ # anvil-cli
2
+
3
+ A CLI tool for syncing Anvil apps between your local filesystem and the Anvil IDE. Edit your Anvil apps in your favorite editor and sync changes in real-time.
4
+
5
+ ## Features
6
+
7
+ - **Real-time Sync**: Watch for file changes and automatically sync to Anvil
8
+ - **OAuth Authentication**: Secure authentication via OAuth with automatic token refresh
9
+ - **Auto-detection**: Automatically detects app IDs from git remotes or commit history
10
+ - **Python Module Support**: Syncs server-side and client-side Python modules
11
+ - **Form Support**: Syncs forms with templates and code
12
+ - **Theme Assets**: Syncs theme assets and configuration
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install -g @anvil-works/anvil-cli
18
+ ```
19
+
20
+ ## Quick Start
21
+
22
+ ### 0. Clone Your App (Required)
23
+
24
+ `anvil` watches a local clone of your Anvil app. If you haven't cloned the
25
+ app yet, follow the [cloning guide](https://anvil.works/docs/version-control/git/direct-checkout#cloning-your-app)
26
+ first:
27
+
28
+ ```bash
29
+ git clone https://anvil.works/git/YOUR_APP_ID my-app
30
+ cd my-app
31
+ ```
32
+
33
+ > **SSH-only tip:** If you sign in to Anvil with Google (and therefore don't
34
+ > have an Anvil password), Git requires SSH. Add your public key in the Anvil
35
+ > account settings, start `ssh-agent`, run `ssh-add`, then use the `git clone`
36
+ > command above.
37
+
38
+ ### 1. Login
39
+
40
+ Authenticate with Anvil using OAuth:
41
+
42
+ ```bash
43
+ anvil login
44
+ ```
45
+
46
+ This will:
47
+
48
+ 1. Open your browser to the Anvil OAuth authorization page
49
+ 2. Prompt you to authorize anvil-cli to access your Anvil account
50
+ 3. Automatically complete the authentication flow
51
+
52
+ Your access and refresh tokens are stored in:
53
+ - **macOS**: `~/Library/Preferences/anvil-cli/config.json`
54
+ - **Linux**: `~/.config/anvil-cli/config.json`
55
+ - **Windows**: `%APPDATA%\anvil-cli\Config\config.json`
56
+
57
+ **Smart URL Handling:** You can specify the Anvil server URL directly:
58
+
59
+ ```bash
60
+ # These all work - anvil automatically adds https://
61
+ anvil login anvil.works
62
+ anvil login anvil.mycompany.com
63
+
64
+ # localhost automatically uses http://
65
+ anvil login localhost:3000
66
+
67
+ # Full URLs also work
68
+ anvil login https://anvil.works
69
+ ```
70
+
71
+ **Multiple Accounts:** anvil-cli supports multiple Anvil installations simultaneously. Each URL has its own authentication tokens, so you can work with different enterprise installations or switch between them easily.
72
+
73
+ ### 2. Sync Your Changes
74
+
75
+ Navigate to your local Anvil app directory and start syncing:
76
+
77
+ ```bash
78
+ cd /path/to/your/anvil/app
79
+ anvil sync
80
+ ```
81
+
82
+ Or use the `watch` alias (for backward compatibility):
83
+
84
+ ```bash
85
+ anvil watch
86
+ ```
87
+
88
+ Or use the short form:
89
+
90
+ ```bash
91
+ anvil w
92
+ ```
93
+
94
+ You can specify the app ID explicitly:
95
+
96
+ ```bash
97
+ anvil sync -A YOUR_APP_ID
98
+ ```
99
+
100
+ **Sync Options:**
101
+
102
+ - `-A, --appid <APP_ID>` - Specify app ID directly
103
+ - `-f, --first` - Auto-select first detected app ID without confirmation
104
+ - `-s, --staged-only` - Only sync staged changes (use `git add` to stage files)
105
+ - `-a, --auto` - Auto mode: restart on branch changes and sync when behind
106
+ - `-u, --url <ANVIL_URL>` - Specify Anvil server URL (e.g., `anvil.works`, `localhost:3000`)
107
+ - `-V, --verbose` - Show detailed output
108
+
109
+ **Tip:** Open your app in the [Anvil IDE](https://anvil.works) to see your local changes appear in real-time. Changes made in the IDE will also sync back to your local files.
110
+
111
+ ## How It Works
112
+
113
+ ### Architecture
114
+
115
+ ```
116
+ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
117
+ │ Local Files │◄────►│ anvil-cli │◄────►│ Anvil Server │◄────►│ Anvil IDE │
118
+ │(your editor) │ │ (Node.js CLI)│ │(anvil.works) │ │ (Browser) │
119
+ └──────────────┘ └──────────────┘ └──────────────┘ └──────────────┘
120
+ ```
121
+
122
+ The diagram shows bidirectional sync:
123
+
124
+ - **Local → Anvil**: Your local file changes are synced to Anvil via API calls through anvil-cli
125
+ - **Anvil → Local**: Changes made in the Anvil IDE are automatically detected and synced back to your local files via git fetch through anvil-cli
126
+
127
+ ### CLI Commands
128
+
129
+ | Command | Description |
130
+ | -------------------------------------- | -------------------------------------------------------------------- |
131
+ | `anvil sync [path]` | Watch directory for changes and sync to Anvil |
132
+ | `anvil sync -V` or `--verbose` | Sync with verbose logging (detailed output) |
133
+ | `anvil watch [path]` | Alias for sync (backward compatibility) |
134
+ | `anvil w [path]` | Short form for sync |
135
+ | `anvil login [anvil-server-url]` | Authenticate with Anvil using OAuth (supports smart URL handling) |
136
+ | `anvil l [anvil-server-url]` | Short form for login |
137
+ | `anvil logout [anvil-server-url]` | Logout from Anvil (optionally specify URL for specific installation) |
138
+ | `anvil config <action> [key] [value]` | Manage configuration (set, get, list) |
139
+ | `anvil c <action> [key] [value]` | Short form for config |
140
+ | `anvil version` | Show version information |
141
+ | `anvil -h, --help` | Show help message |
142
+ | `anvil -v, --version` | Show version number |
143
+
144
+ ### App ID Detection
145
+
146
+ anvil-cli can auto-detect your app ID using multiple strategies:
147
+
148
+ 1. **Git Remotes**: Checks for Anvil git remotes in the repository
149
+
150
+ 2. **Commit Lookup**: Queries Anvil API with current commit ID and branch to find matching app
151
+
152
+ If auto-detection fails, you can specify the app ID explicitly:
153
+
154
+ ```bash
155
+ anvil sync -A YOUR_APP_ID
156
+ ```
157
+
158
+ ### Anvil URL Detection
159
+
160
+ anvil-cli automatically detects which Anvil server to use:
161
+
162
+ 1. **Explicit URL**: If you specify `--url` or `-u` flag
163
+ 2. **Git Remotes**: Automatically extracts URL from git remotes in your repository
164
+ 3. **User Prompt**: If multiple accounts are available, prompts you to select one
165
+ 4. **Global Config**: Falls back to `anvilUrl` in config file
166
+ 5. **Default**: Uses `https://anvil.works` (or `http://localhost:3000` in dev mode)
167
+
168
+ **Examples:**
169
+
170
+ ```bash
171
+ # Auto-detect from git remote
172
+ anvil sync
173
+
174
+ # Specify URL explicitly
175
+ anvil sync --url anvil.works
176
+ anvil sync --url localhost:3000
177
+ anvil sync --url anvil.mycompany.com
178
+
179
+ # If multiple accounts, you'll be prompted to select
180
+ anvil sync
181
+ # ? Multiple Anvil installations found. Which one would you like to use?
182
+ # ❯ https://anvil.works
183
+ # https://anvil.company.com
184
+ # Cancel
185
+ ```
186
+
187
+ ## Configuration
188
+
189
+ ### Config File Location
190
+
191
+ - **macOS**: `~/Library/Preferences/anvil-cli/config.json`
192
+ - **Linux**: `~/.config/anvil-cli/config.json`
193
+ - **Windows**: `%APPDATA%\anvil-cli\Config\config.json`
194
+
195
+ ### Stored Configuration
196
+
197
+ ```json
198
+ {
199
+ "authTokens": {
200
+ "https://anvil.works": {
201
+ "authToken": "your-access-token",
202
+ "refreshToken": "your-refresh-token",
203
+ "authTokenExpiresAt": 1234567890,
204
+ "username": "user@example.com"
205
+ },
206
+ "https://anvil.company.com": {
207
+ "authToken": "another-token",
208
+ "refreshToken": "another-refresh",
209
+ "authTokenExpiresAt": 1234567890,
210
+ "username": "user@company.com"
211
+ }
212
+ },
213
+ "anvilUrl": "https://anvil.works",
214
+ "verbose": false
215
+ }
216
+ ```
217
+
218
+ **Note:** The `authTokens` object stores tokens per URL, allowing you to work with multiple Anvil installations simultaneously. Legacy `authToken`, `refreshToken`, and `authTokenExpiresAt` fields are maintained for backward compatibility.
219
+
220
+ ### Verbose Logging
221
+
222
+ By default, anvil-cli shows minimal output. For more detailed logging:
223
+
224
+ **One-time:** Use the `-V` or `--verbose` flag:
225
+
226
+ ```bash
227
+ anvil sync --verbose
228
+ ```
229
+
230
+ **Persistent:** Set verbose mode in config:
231
+
232
+ ```bash
233
+ anvil config set verbose true
234
+ ```
235
+
236
+ Verbose output is prefixed with `[VERBOSE]` and includes file routing details, sync status checks, and other internal processing information.
237
+
238
+ ### Enterprise Configuration
239
+
240
+ If you're using an Anvil server other than `anvil.works` (e.g., enterprise/on-premise installation), you can work with multiple installations:
241
+
242
+ **Option 1: Login with URL** (recommended)
243
+
244
+ ```bash
245
+ # Login to your enterprise installation
246
+ anvil login anvil.mycompany.com
247
+
248
+ # Or with full URL
249
+ anvil login https://anvil.mycompany.com
250
+ ```
251
+
252
+ **Option 2: Specify URL when syncing**
253
+
254
+ ```bash
255
+ # Sync with specific URL
256
+ anvil sync --url anvil.mycompany.com
257
+ ```
258
+
259
+ **Option 3: Set global default**
260
+
261
+ ```bash
262
+ anvil config set anvilUrl https://anvil.mycompany.com
263
+ anvil login
264
+ ```
265
+
266
+ **Multiple Installations:**
267
+
268
+ anvil-cli supports multiple Anvil installations simultaneously. Each installation has its own authentication tokens:
269
+
270
+ ```bash
271
+ # Login to multiple installations
272
+ anvil login anvil.works
273
+ anvil login anvil.company-a.com
274
+ anvil login anvil.company-b.com
275
+
276
+ # When syncing, you'll be prompted to select which one to use
277
+ # Or specify explicitly:
278
+ anvil sync --url anvil.company-a.com
279
+ ```
280
+
281
+ **URL Resolution Priority:**
282
+
283
+ 1. Explicit `--url` flag
284
+ 2. Git remote detection (from current repository)
285
+ 3. User prompt (if multiple accounts available)
286
+ 4. `anvilUrl` in config file
287
+ 5. Default based on `NODE_ENV`:
288
+ - Production: `https://anvil.works`
289
+ - Development: `http://localhost:3000`
290
+
291
+ View all configuration:
292
+
293
+ ```bash
294
+ anvil config list
295
+ ```
296
+
297
+ ### Logout
298
+
299
+ Logout from Anvil installations:
300
+
301
+ **Logout from all accounts:**
302
+
303
+ ```bash
304
+ anvil logout
305
+ ```
306
+
307
+ **Logout from specific installation:**
308
+
309
+ ```bash
310
+ anvil logout anvil.works
311
+ anvil logout anvil.mycompany.com
312
+ anvil logout --url localhost:3000
313
+ ```
314
+
315
+ **Multiple accounts:** If you have multiple accounts and run `anvil logout` without specifying a URL, you'll be prompted to:
316
+
317
+ - Logout from one account (then select which one)
318
+ - Logout from all accounts
319
+ - Cancel
320
+
321
+ ## Troubleshooting
322
+
323
+ ### "No app ID found"
324
+
325
+ - Ensure you're in an Anvil app directory with `anvil.yaml`
326
+ - Check git remotes: `git remote -v`
327
+ - Specify app ID explicitly: `anvil sync -A YOUR_APP_ID`
328
+ - Use `-f, --first` to auto-select the first detected app ID: `anvil sync -f`
329
+
330
+ ### "Authentication failed" or "Not logged in to [URL]"
331
+
332
+ - Run `anvil login [url]` to authenticate with the specific Anvil installation
333
+ - If you see "Not logged in to [URL]", you need to login to that specific URL:
334
+
335
+ ```bash
336
+ anvil login https://anvil.mycompany.com
337
+ ```
338
+
339
+ - Your access token may have expired and refresh failed - re-login to get new tokens
340
+ - For multiple installations, make sure you're logged in to the correct one
341
+
342
+ ### Switch back to hosted Anvil
343
+
344
+ - Reset the server URL to the default cloud host:
345
+
346
+ ```bash
347
+ anvil config set anvilUrl https://anvil.works
348
+ ```
349
+
350
+ - Or reset all config values (also clears custom URLs):
351
+
352
+ ```bash
353
+ anvil config reset
354
+ ```
355
+
356
+ ## License
357
+
358
+ ISC