@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.
npm install @spica-devkit/syncExample
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
| Export | Description |
|---|---|
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. |
SpicaRequestError | Rejection type for failed requests, with status and data but no credentials. |
plan and apply options
| Option | Description |
|---|---|
connection | {url, authorization}. |
files | A Map or object keyed by project-relative path, such as bucket/Posts/schema.yaml. |
modules | Limit to some modules. |
concurrency | Parallel requests, default 10. |
maxDiffLength | Diffs longer than this are listed in omittedDiffs instead, default 200000. |
fingerprint | apply only: the fingerprint of the reviewed plan. |
abortOnError | apply only: stop at the first failure. |
readTarball options
| Option | Default | Description |
|---|---|---|
path | Project folder inside the repository. | |
stripComponents | 1 | Leading folders to remove, such as the <owner>-<repo>-<sha>/ folder of GitHub and GitLab archives. |
maxFileBytes | 5 MiB | Largest accepted file. |
maxTotalBytes | 50 MiB | Largest accepted total. |
maxFiles | 10000 | Most 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.