TypeScript CLI for InstaWP. Lets users create/manage WordPress sites from the terminal.
- Stack: TypeScript, ESM, Commander.js, Axios, Node 22+
- Package:
@instawp/clion npm (scoped, public) - Entry:
src/index.ts→ compiled todist/index.js - Binary:
instawp(registered viabinin package.json)
src/
├── index.ts # CLI entry point, command registration
├── types.ts # All TypeScript interfaces
├── commands/
│ ├── login.ts # OAuth browser flow + --token
│ ├── whoami.ts # Show current session
│ ├── sites.ts # sites list/create/delete + top-level create alias
│ ├── exec.ts # exec + wp commands (merged, --api/--ssh transport)
│ ├── ssh.ts # Interactive SSH shell
│ ├── sync.ts # rsync push/pull via SSH
│ ├── teams.ts # teams list/switch/members
│ ├── local.ts # local create/clone/start/stop/push/pull/list/delete
│ └── migrate.ts # migrate push (local WP dir → brand-new hosted site)
├── lib/
│ ├── api.ts # Axios client, auth interceptor, team_id injection
│ ├── auth.ts # OAuth flow (local HTTP server for callback)
│ ├── config.ts # Conf-based persistent config (~/.config/instawp/)
│ ├── wp-local.ts # WP-root/wp-config/version detection (pure, for migrate push)
│ ├── wp-archive.ts # zip (archiver) + DB export (wp/mysqldump) for migrate push
│ ├── local-env.ts # Playground server management, background mode
│ ├── output.ts # chalk/ora output helpers, --json mode
│ ├── site-resolver.ts # Resolve site by ID/name/domain with caching
│ ├── ssh-keys.ts # SSH key generation, upload, caching
│ └── ssh-connection.ts # SSH/rsync spawn helpers
├── __tests__/ # Vitest tests (148 tests)
scripts/
└── mysql2sqlite # MySQL→SQLite dump converter (vendored)
# Auth
instawp login [--token <t>] [--api-url <url>]
instawp whoami
# Sites (cloud)
instawp sites list [--status <s>] [--page <n>] [--per-page <n>] [--all]
instawp sites creds <site>
instawp create --name <n> [--php <v>] [--config <id>]
instawp sites delete <site> [--force]
instawp open <site> [--admin] [--magic] [--print]
# Versions (restorable point-in-time site copies)
instawp versions create <site> [--name <label>] [--no-wait]
instawp versions list <site>
instawp versions restore <site> <version-id> [--force] [--no-wait]
instawp versions delete <site> <version-id...> [--force]
# Remote access (wp is the primary command; exec is the escape hatch)
instawp wp <site> <args...> [--api] # WP-CLI on the site
instawp ssh <site> # Interactive shell
instawp sync push <site> [--path] [--exclude] [--dry-run]
instawp sync pull <site> [--path] [--exclude] [--dry-run]
instawp cache purge <site> [--object] # purge CDN edge cache (+ object cache)
instawp db push <site> <file> [--force] [--no-backup]
instawp db pull <site> [--output <path>] [--no-compress]
instawp logs <site> [--wp] [--php] [--nginx] [--follow] [--lines <n>]
instawp exec <site> <cmd...> [--api] [--timeout <s>] # Raw shell (non-WP)
instawp sql <site> "<query>" # SQL via WP-CLI (hits MySQL, cache-immune)
instawp plugin install <site> <zip|dir> [--activate] # install plugin (scp+wp for zip; rsync for dir)
# Self-update (notifier checks npm once/day; suppressed in --json/CI/non-TTY)
instawp upgrade [--check] # npm i -g @instawp/cli@latest
# Teams
instawp teams list
instawp teams switch [team] # client-side team context
instawp teams members <team>
# Local development (powered by WordPress Playground)
instawp local create [--name <n>] [--wp <v>] [--php <v>] [--background] [--no-open]
instawp local clone <cloud-site> [--name <n>] [--no-start]
instawp local start [name] [--background] [--no-open]
instawp local stop [name]
instawp local push <local-name> [cloud-site] [--dry-run]
instawp local pull <local-name> <cloud-site> [--dry-run]
instawp local list
instawp local delete <name> [--force]
# Migration (local WordPress dir → brand-new hosted site)
instawp migrate push [path] [--path <dir>] [--name <n>] [--source-url <url>] [--wp <v>] [--php <v>] [--keep-archives] [--dry-run]
All commands support --json for machine-readable output.
wp <site>is the canonical way to run anything on a remote site — prependswpto args, integrates with WP-CLI.exec <site>is the escape hatch for non-WP shell commands (ls,tail,ps, …). Same transport, nowpprefix.- Both accept
--as a POSIX end-of-options marker so users can forward raw args:instawp wp my-site -- post list --post_type=page - Both shell-escape each arg via single-quote wrapping before piping to remote stdin — fixes the
eval '...'parens issue. --ssh(default): real SSH connection, proper exit codes, real-time output--api: usesPOST /sites/{id}/run-cmdAPI → cloud-app → InstaCPv-instawp-run-cmd- Both transports can run arbitrary commands (API is not WP-only despite the name)
resolveSite()accepts ID (numeric), name, or domain- Numeric → direct
GET /sites/{id}/details - String → fetches list, matches by name/sub_domain/domain, then fetches details
- Caches name→ID mappings for 10 minutes (avoids list call on repeat lookups)
teams switchstores team_id locally (no server-side change)- API interceptor injects
team_idas query param on all requests - Client-app
SiteService::getList()already acceptsteam_idparameter
- Uses WordPress Playground (
@wp-playground/cli) — WASM PHP + SQLite, no Docker needed - NOT a hard dependency — auto-downloaded via
npx, faster if installed globally (npm i -g @wp-playground/cli) - Instance data stored at
~/.instawp/local/<name>/ - Fresh sites: mount entire
wp-contentbefore install (--mount-before-install) - Cloned sites: mount subdirs individually after install (
--mount) so Playground sets updb.phpinternally
- Export MySQL dump via SSH (
wp db export) - Strip SSH MOTD from dump output
- Convert MySQL → SQLite using
mysql2sqlite(awk script) - Import directly into
.ht.sqliteviasqlite3CLI - Rename table prefix to
wp_(tables + meta keys + option names) - Search-replace cloud URL →
http://127.0.0.1:<port>across all tables - Pull wp-content via rsync (plugins, themes, uploads)
- Pull non-core root files (CLAUDE.md, .htaccess, etc.)
- Generate blueprint with
WP_SQLITE_AST_DRIVER=true+loginstep with actual admin username - Write error suppression mu-plugin
Pushes the local Playground DB back to the cloud MySQL, OVERWRITING it. Implemented in pushDatabase() (commands/local.ts) + lib/sqlite-to-mysql.ts.
- Read local siteurl from
wp_options(authoritative; handles port drift) - Discover cloud
table_prefix+ existing tables via SSH — MOTD-stripped (parseTablePrefix/parseSqlTableNamesinlib/local-instance.ts); a banner leaking in would mismatch every table → silent empty push generateMysqlDump(): data-only (TRUNCATE+INSERT, noCREATE TABLE) for localwp_*tables whose cloud-prefixed name exists on the cloud (intersection — so a missing-table TRUNCATE can't abort the import). Pinssql_mode(backslash escaping),safeIntegers(true)(no >2^53 loss), BLOB→0xhex (empty→''), byte-budgeted INSERT batching- Refuse if 0 tables intersect (fail loud, never a silent no-op)
- Back up cloud DB (
wp db export | gzip), abort if it fails (unless--no-backup) - scp upload →
wp db import - If cloud prefix ≠
wp_: remap role/capability keys to the cloud prefix —{prefix}capabilities,{prefix}user_level(usermeta),{prefix}user_roles(options). Local iswp_-normalized, so without this the admin loses all capabilities and wp-admin becomes inaccessible (most InstaWP sites use a random prefix). Exact key names only — never touches plugin options. wp search-replace <local-url> <cloud-url>(serialization-safe — NOT done in SQL) →wp cache flush
Why no official tool: WordPress has no SQLite→MySQL exporter — sqlite-database-integration#36 is open since 2023; the community workaround regenerates schema with text→varchar(255) (truncates content) + addslashes. Our data-only approach (reuse cloud schema) avoids both. wp db export can't help (shells to mysqldump, absent in Playground). Caveat: assumes schema parity (true for clones); plugin tables created only-locally are skipped.
The standalone-CLI replacement for the plugin's wp instawp local push. Mirrors an arbitrary on-disk WordPress install (NOT a Playground instance — that's local push) up to a fresh hosted site. Implemented in commands/migrate.ts + lib/wp-local.ts + lib/wp-archive.ts.
- Locate the WP root — walk up from
[path]/--path/cwd towp-includes/version.php(findWpRoot); parsewp-config.phpfor DB creds +$table_prefix+WP_HOME/WP_SITEURL(parseWpConfig). - Detect source URL —
wp option get home/siteurl(if wp-cli works on the install), else theWP_HOME/WP_SITEURLconstants, else a directmysqlquery;--source-urloverrides. Normalized tosource_domain(no scheme/www./trailing slash) so the server regex accepts it. - Archive files — stream a zip via
archiver, excluding EXACTLY the plugin's skip-list (wp-content/instawpbackups,wp-content/upgrade, pluginsinstawp-connect/instawp-helper/iwp-migration). Files added relative to the WP root so the restore engine sees a normal WP tree. - Export DB —
wp db exportif wp-cli works on the install, elsemysqldumpdriven by the wp-config creds (password viaMYSQL_PWD, never in argv). Raw.sql, matching the plugin. - Create a reserved site —
POST /sites { is_reserved: true, wp_version }, pollGET /tasks/{id}/status. - Upload —
ensureSshAccess()(key-based), thenscpboth archives into/home/{user}/web/{sub_domain}/public_html/— the exact pathrestore-rawreconstructs server-side. - Restore —
PUT /sites/{id}/restore-raw { file_bkp, db_bkp, source_domain }→ poll the returnedtask_id. The server-side restore engine (cloud-app → InstaCP) unzips, imports, and search-replacessource_domain→ the new domain. This is what makes it a faithful mirror (prefix/URL/serialized-data rewrite happens server-side — the CLI does none of it). - Best-effort
rmof the uploaded archives from the new webroot;waitForHttpthe new URL; report.
Auth model (the key decision): runs entirely on the CLI's personal API token over site-scoped routes. The plugin uses connect-scoped routes (connects/{cid}/sites/...) because the plugin IS a connect; every one of those has a personal-token site-scoped equivalent (POST /sites, GET /tasks/{id}/status, POST /sites/{id}/update-sftp-status, GET /sites/{id}/sftp-details, PUT /sites/{id}/restore-raw). CheckConnectApiToken only gates tokens whose device_type == TOKEN_TYPE_CONNECT_MANAGE; a personal token passes through. So no Connect record and no migration-dashboard/tracking entry are created — only the files+DB migration, which is identical to the plugin's outcome. (The plugin's migrates-v3/local-push + finish-local-staging bookkeeping is intentionally skipped.)
Caveats: the new site has NO instawp-connect plugin (it's excluded, faithful to the local files) — manage via SSH / instawp wp / instawp open. After restore the site's admin login is the LOCAL site's users (the DB was replaced), not the freshly-minted creds. On Local-by-Flywheel and similar, wp/the DB socket usually aren't reachable from the host shell, so run from a shell where wp works or pass --source-url (the mysqldump fallback still needs DB reachability).
--backgroundflag spawns detached process, polls until server responds, returns immediately- PID stored at
<instance>/server.pid, logs at<instance>/server.log local stopkills the server's whole process group (kill(-pid); Windowstaskkill /T), not just the recorded pid — under the npx fallback that pid is the npx wrapper and Playground is its child. Waits ≤5s for exit, then SIGKILLlocal listshowsrunning/stoppedstatus
- Auto-generates RSA 4096 key at
~/.instawp/cli_keyif needed - Checks existing keys (
~/.ssh/id_rsa,id_ed25519) first - Uploads to InstaWP API, enables SSH+SFTP on site, attaches key
- Caches connection details for 1 hour in conf store
POST /sites/{id}/update-ssh-status returns the site's public hostname — for a CDN-fronted site (Bunny) that's the proxied edge (port 22 filtered), so SSH would hang ~2 min. ensureSshAccess(siteId, opts) resolves the true origin itself, in this host precedence:
- Explicit override —
opts.sshHost(--ssh-host) elsegetSshHostOverride()(INSTAWP_SSH_HOST_<id>>INSTAWP_SSH_HOST). Manual escape hatch; applied even to a cached host. - Auto-detected origin IP —
GET /sites/{id}/credentialsreturnssite_meta.ip_addr(the real server IP, exactly what InstaWP's ownSshBridgeuses:ip_addr ?? sub_domain). Preferred over the API host, so CDN-fronted sites work with no override. Verified: site 2495716 (instawp.com) →update-ssh-statusgives the CDN host,/credentials.ip_addrgives the reachable origin. - API host (the CDN edge /
sub_domain) — fallback when no override and noip_addr.
- Preflight:
probeTcp(host, port, 5000)(lib/ssh-preflight.ts) TCP-connects before returning. DefaultonUnreachable:'exit'prints the diagnostic +process.exit(1)(all SSH commands get fast-fail for free);'throw'raisesSshUnreachableErrorsowp/execcatch it and auto-fall-back to--api. - Self-heal: if a cached host fails preflight (and no override), the cache is dropped and re-resolved once — so a stale CDN host cached before this shipped heals without a manual
--refresh. - Docroot resolution: the remote web dir is
~/web/<domain>/public_html, but after a domain cutover the API'ssub_domain/main_domainis the new custom domain while the HestiaCP web dir keeps the original sub_domain — no API field returns the real name (verified: site 2495716 APIsub_domain=instawp.com, real dir=instawp-marketing.instawp.site). SoensureSshAccesscallsresolveRemoteWebDir(conn)(ssh-connection.ts) once — an SSHfind the public_html with wp-config.php— and stores the real name inconn.domain(cached viaconn.webDirResolved). All path construction (exec/wpcd,buildRemotePath,db,migrate) then targets the served dir. Best-effort: keeps the API domain if the lookup fails. --apitransport (lib/api-exec.ts→runViaApi) drivesPOST /sites/{id}/run-cmd(the only non-SSH channel, not behind the CDN).db pull/push --apibase64-round-trip the dump over it (single/chunked; small-medium DBs).run-cmdis request/response — no streaming/binary — per-calltimeoutis raised past the axios 30s default.sync --apiis NOT implemented (deferred).- Gotcha:
wp/execusepassThroughOptions()+allowUnknownOption()and re-extract flags from argv — a new SSH flag must be added to.option()AND the manual extraction block inexec.ts, or it's swallowed.
- Uses
confpackage →~/.config/instawp/config.json - Env overrides:
INSTAWP_TOKEN,INSTAWP_API_URL,INSTAWP_SSH_HOST(+ per-siteINSTAWP_SSH_HOST_<siteId>) - Stores: auth, SSH cache, site cache, team_id, local instances
- Source: https://github.com/dumblob/mysql2sqlite
- License: MIT
- What: AWK script that converts MySQL dump files to SQLite-compatible SQL
- Used by:
local clonefor database import - Version: Vendored from master branch (2026-03-23)
- Update procedure: Download latest from
https://raw.githubusercontent.com/dumblob/mysql2sqlite/master/mysql2sqliteand replacescripts/mysql2sqlite. Test withinstawp local cloneon a WooCommerce site to verify compatibility.
Windows ships with ssh/scp but not rsync, awk, or sqlite3. The CLI works on Windows with zero extra installs via:
better-sqlite3(optional npm dep) — replaces the sqlite3 CLI. Native module with prebuilt binaries per Node ABI: a Node version with no prebuild falls back to node-gyp, which fails without Visual Studio Build Tools, so it is anoptionalDependencyand lazy-loaded vialoadSqlite()(src/lib/sqlite.ts) — onlylocal clone/local push --with-dbneed it. Never import it statically from anythingindex.tsloads, or a skipped install breaks every command. When bumping it, check the release'snode-v<ABI>-win32-x64assets cover every Node line inpublish.yml's smoke matrix.vendor/win32/busybox.exe— providesawkfor themysql2sqlitescript (invoked asbusybox awk -f ...). Statically linked, no DLLs. This is the only bundled binary.- Pure-JS SFTP (
ssh2-sftp-client) for file transfers — see below. cross-spawnfor launching WordPress Playground (local create/start/clone).npx/wp-playground-cliare.cmdshims on Windows; Node won't spawn.cmdwithoutshell:true(CVE-2024-27980), solocal-env.tsusescross-spawn(resolves the shim + quotes mount-path args safely). Never use barechild_process.spawnfor npx/.cmd on Windows.- Playground mounts (
local-env.ts→buildMountArgs()): never colon-joinhost:vfson Windows. wp-playground-cli's--mount=<host>:<vfs>splits on:, and a Windows drive-letter colon (C:\...) makes the split fail withInvalid mount format. On Windows use--mount-dir/--mount-dir-before-install(host + vfs as two separatenargs:2args — identical mount, no colon). macOS/Linux keep the colon form. Works for both directory and file mounts.
syncFiles()insrc/lib/ssh-connection.tsis the dispatcher. On macOS/Linux it shells out torsync(delta sync). On Windows it callssyncViaSftp()(src/lib/sftp-sync.ts).- Why not bundle rsync on Windows? We tried (betas 4–9). msys2 rsync.exe cannot drive native Windows OpenSSH — incompatible pipe/signal semantics produce
connection unexpectedly closed (0 bytes)+sigpacket: Suppressing signal 30. Bundling an msys ssh too would drag in the whole Heimdal/Kerberos DLL chain (~3.5 MB, ~15 brittle DLLs). Pure-JS SFTP sidesteps all of it. - Trade-off: SFTP does full-file copy (no rsync delta). Fine for wp-content; slower on large repeat syncs.
syncViaSftpmirrors rsync's exclude/include patterns viamakeMatcher(exact names match at any depth;*globs don't cross/; patterns with/are anchored to the relative path).
findAwk()(src/commands/local.ts) prefers bundledbusybox.exeon Windows, thenawk/gawkin PATH, then common Git-for-Windows dirs.bundledBusybox()(src/lib/windows-binaries.ts) returns the path only on win32 when the file exists.
# Maintainer-only — just downloads busybox64u.exe (requires curl)
bash scripts/fetch-windows-binaries.sh
git add vendor/win32 && git commit -m 'chore: refresh busybox'See vendor/win32/NOTICE.md for source + license (BusyBox is GPL-2.0).
src/lib/paths.ts→toRsyncPath()convertsC:\foo\bar→/c/foo/bar. Retained for completeness, but only exercised byrsyncViaSshwhich no longer runs on Windows.
- WP_SQLITE_AST_DRIVER=true is required for complex plugins (WooCommerce). The new AST-based SQLite driver (v2.2.1+) handles 99% of MySQL queries.
- Some MySQL-specific queries may still fail at runtime (rare edge cases in complex plugins)
- PHP deprecation warnings can crash WASM PHP — suppressed via mu-plugin (
error_reporting(E_ERROR | E_PARSE)) downloads.w.orgis unreachable on some networks — connectivity pre-check warns the user
| Endpoint | Used By |
|---|---|
GET /api/v2/sites |
sites list, site resolver |
GET /api/v2/sites/{id}/details |
site resolver |
POST /api/v2/sites |
sites create, local push (auto-create), migrate push (new site) |
PUT /api/v2/sites/{id}/restore-raw |
migrate push (server-side restore of uploaded zip+sql) |
POST /api/v2/sites/{id}/purge-cache |
cache purge (CDN edge; 422 if no CDN) |
POST /api/v2/sites/{id}/clear-object-cache |
cache purge --object (Redis; 422 if not enabled) |
DELETE /api/v2/sites/{id} |
sites delete |
POST /api/v2/sites/{id}/run-cmd |
exec --api, wp --api, db pull/push --api (non-SSH transport) |
GET /api/v2/site-versions?site_id={id} |
versions list |
POST /api/v2/site-versions |
versions create |
PUT /api/v2/site-versions/{id} |
versions create (set --name) |
PUT /api/v2/sites/{id}/restore-versions/{versionId} |
versions restore |
DELETE /api/v2/site-versions |
versions delete (body {ids:[]}) |
GET /api/v2/tasks/{id}/status |
create, versions create/restore (poll task) |
GET /api/v2/ssh-keys |
SSH key matching |
POST /api/v2/ssh-keys |
SSH key upload |
POST /api/v2/sites/{id}/ssh-keys/{keyId} |
attach key to site |
POST /api/v2/sites/{id}/update-ssh-status |
enable SSH |
POST /api/v2/sites/{id}/update-sftp-status |
enable SFTP |
GET /api/v2/teams |
teams list, whoami |
GET /api/v2/teams/{id}/members |
teams members |
npm install # Install deps
npm run dev # Watch mode (tsc --watch)
npm run build # Build to dist/
npm test # Run vitest (148 tests)
npm run test:watch # Watch mode testsnpm run build
node dist/index.js --help
node dist/index.js login --token <test-token>
node dist/index.js sites list
node dist/index.js local create --name test --background --no-open
node dist/index.js local stop testOr link globally:
npm link
instawp --helpWorkflow: Push a v* tag → GitHub Actions builds, tests, publishes to npm. Full procedure: skill iwp-cli-release. Tag-based, not the Laravel repos' dev→main PR flow.
# 1. Bump version in package.json
# 2. Add a "## <version> (YYYY-MM-DD)" entry at the TOP of CHANGELOG.md (required)
# 3. Commit (explicit paths — repo has unrelated untracked files; dist/ is gitignored)
git tag -a v0.0.1-beta.2 -m "..." && git push origin v0.0.1-beta.2- Publishes to the
latestdist-tag via npm trusted publishing (OIDC) — noNPM_TOKEN. The trusted publisher is configured on npmjs.com (@instawp/cli→ Settings → Trusted Publisher: GitHub Actions,InstaWP/cli,publish.yml); renaming the workflow file breaks publishing until that is updated. OIDC cannot runnpm dist-tag, so thebetadist-tag is frozen at 0.0.1-beta.33 (nothing reads it). A publishE404 PUTmeans npm rejected the credential, not a missing package. publish.yml(tag-triggered): Windows smoke test → build → test → npm publish → GitHub Release.release-notify.yml(also tag-triggered, unlike siblings which fire on PR-merge) → Claude summary to Slack. Both extract the CHANGELOG section by exact## <version>header match, so the version inpackage.json, the tag, and the CHANGELOG header must all agree.- Pushing the tag is the release trigger; pushing
mainalone publishes nothing. Tag dispatch / secret listing needs repo admin (a normal token gets 403).
- All imports use
.jsextension (ESM requirement) process.exit(1)on fatal errors after printing message viaerror()- Spinners stop before printing output (no interleaved text)
- JSON mode returns
{ success, data }or{ success: false, error } - Version reads from package.json at runtime (single source of truth)
- rsync uses
--itemize-changes(only shows actually changed files) - Terminal restored with
stty saneafter Playground exits