@spica-devkit/sync

Plan and apply Spica resource files from your own services, such as a deployment service that syncs a project from a git commit.

@spica-devkit/sync is the engine behind spica plan and spica apply, as a library. Use it when you build your own deployment flow: download a repository archive, show the plan to a person for approval, and apply exactly what they approved. Nothing has to be written to disk.

Terminal
npm install @spica-devkit/sync

Example

TypeScript
import {apply, plan, readTarball} from "@spica-devkit/sync";

const connection = {url: "https://api.example.com/api", authorization: `APIKEY ${process.env.SPICA_API_KEY}`};

// 1. Read the resource files of a commit, for example from GET /repos/{owner}/{repo}/tarball/{sha}
const archive = await fetch(tarballUrl, {headers: {Authorization: `Bearer ${githubToken}`}});
const files = await readTarball(archive.body, {path: "spica"});

// 2. Plan. The result is plain JSON: store it and show it for review.
const reviewed = await plan({connection, files});
console.log(reviewed.totals); // {creates, updates, deletes}

// 3. Apply after approval, with the same files.
const result = await apply({connection, files, fingerprint: reviewed.fingerprint});
console.log(result.status); // "succeeded" | "partial" | "failed" | "outdated"

apply plans again first and only applies when the new plan has the same fingerprint as the reviewed one. When the files or the instance changed in between, nothing is applied and the result has the status outdated and the new plan.

API

ExportDescription
plan(options)Compare files with the instance and resolve to a JSON-serializable plan.
apply(options)Apply when the plan still matches options.fingerprint.
readTarball(input, options?)Stream a (gzipped) tar archive and keep only the resource files, in memory.
SpicaRequestErrorRejection type for failed requests, with status and data but no credentials.

plan and apply options

OptionDescription
connection{url, authorization}.
filesA Map or object keyed by project-relative path, such as bucket/Posts/schema.yaml.
modulesLimit to some modules.
concurrencyParallel requests, default 10.
maxDiffLengthDiffs longer than this are listed in omittedDiffs instead, default 200000.
fingerprintapply only: the fingerprint of the reviewed plan.
abortOnErrorapply only: stop at the first failure.

readTarball options

OptionDefaultDescription
pathProject folder inside the repository.
stripComponents1Leading folders to remove, such as the <owner>-<repo>-<sha>/ folder of GitHub and GitLab archives.
maxFileBytes5 MiBLargest accepted file.
maxTotalBytes50 MiBLargest accepted total.
maxFiles10000Most accepted files.

Files that aren't resources are skipped without being buffered. A truncated archive is rejected, so a partial download can never be planned as a set of deletions.

The file format is the same as the CLI's. See Resources as code.