diff --git a/bin/spacesheep.js b/bin/spacesheep.js index ee6ae7c..5dfa09e 100755 --- a/bin/spacesheep.js +++ b/bin/spacesheep.js @@ -155,6 +155,7 @@ const HELP = ` --team-access what the team may do on a new space; default view --no-team publish personal, whatever a .spacesheep.json names --org publish under an organization with its own address (not a Team: use --team) + --skill <@handle/slug> the skill this page was made with, so the skill counts it -m, --message version name shown in the space's history --base the version your files are built on, or the start of its sha: anything published since is merged in (default: what this folder last published @@ -293,6 +294,7 @@ const commands = { // Every push says who can read it because of where it lives, team or personal. if (r.lives_in) log(` ⌂ Lives in ${r.lives_in}${r.team_from && !r.is_update ? ` (team from ${r.team_from === "--team" ? "--team" : path.relative(process.cwd(), r.team_from) || ".spacesheep.json"})` : ""}`); if (r.team_missed) log(` ! ${r.team_missed}`); + if (r.skill_missed) log(` ! ${r.skill_missed}`); // Built on an older version: what landed since was merged in, line by line. if (r.merged) { const m = r.merged, merged = m.merged || [], kept = m.kept || []; diff --git a/lib/deploy.js b/lib/deploy.js index caea827..6d2deac 100644 --- a/lib/deploy.js +++ b/lib/deploy.js @@ -347,6 +347,8 @@ async function deploy(client, target, opts, log) { } args.version_name = opts.versionName || opts.message || (uuid ? `Deploy from ${opts.via || "CLI"}` : "First deploy"); if (!uuid && opts.visibility) args.access = { visibility: opts.visibility }; + // The skill whose template this page came from (@handle/slug): the skill counts the page as made with it. + if (typeof opts.skill === "string" && opts.skill.trim()) args.skill = opts.skill.trim(); // What pushed this version — shown in the space's version history. A failure // here is swallowed, never a failed deploy. const session = await sessionP; @@ -364,8 +366,16 @@ async function deploy(client, target, opts, log) { if (base) args.base_sha = base; const again = `spacesheep deploy ${path.relative(process.cwd(), dir) || "."}${opts.space ? ` --space ${opts.space}` : ""}`; - let result; - try { result = await client.call("deploy", args); } catch (e) { + let result, skillMissed = false; + try { + result = await client.call("deploy", args).catch((e) => { + // A server from before `skill` refuses it as a field it doesn't take: the page still goes out, without it. + if (!args.skill || !(e && e.info && (e.info.violations || []).some((v) => v && v.field === "skill"))) throw e; + delete args.skill; + skillMissed = true; + return client.call("deploy", args); + }); + } catch (e) { if (e && e.info && isClash(e.info)) e.message = clashWords(e.info, again, uuid, merged, e.message); throw e; } @@ -401,6 +411,7 @@ async function deploy(client, target, opts, log) { fs.writeFileSync(manifestPath(dir), JSON.stringify(next, null, 2) + "\n"); } if (team) result.team_from = team.from; + if (skillMissed) result.skill_missed = `the server didn't take --skill (it is older than this CLI): ${result.url} was published, but not counted as made with ${opts.skill.trim()}.`; // A server from before `team` drops the field without a word: the page went out // personal while the person thinks their team has it. Say so, loudly. if (team && !uuid && !("lives_in" in result)) { diff --git a/package.json b/package.json index f7a1a1a..e981866 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "spacesheep", - "version": "1.38.1", + "version": "1.39.0", "description": "Publish web pages to spacesheep.dev from a terminal or CI: `npx spacesheep deploy ./dist`", "bin": { "spacesheep": "./bin/spacesheep.js" diff --git a/test/skill-flag.test.js b/test/skill-flag.test.js new file mode 100644 index 0000000..a06d104 --- /dev/null +++ b/test/skill-flag.test.js @@ -0,0 +1,62 @@ +"use strict"; +const test = require("node:test"); +const assert = require("node:assert"); +const fs = require("fs"); +const os = require("os"); +const path = require("path"); +const { deploy } = require("../lib/deploy"); + +// --skill names the skill a page was made with, so the skill counts it. A server from before the +// field refuses it; the page still goes out, without it, and the answer says so. + +function page() { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ss-skill-")); + fs.writeFileSync(path.join(dir, "index.html"), "Ss"); + return dir; +} + +async function run(opts, answer) { + const sent = []; + const client = { call: async (name, args) => { + if (name === "stage_begin") return { upload_url: "https://stage.example/u", plan: "pro", limits: { max_files: 12000 } }; + sent.push({ ...args }); + return answer(args); + } }; + const realFetch = global.fetch; + global.fetch = async () => ({ ok: true, json: async () => ({ sha: "s1" }) }); + const prev = process.env.GITHUB_ACTIONS; + delete process.env.GITHUB_ACTIONS; + try { return { r: await deploy(client, page(), { noManifest: true, ...opts }, () => {}), sent }; } + finally { global.fetch = realFetch; if (prev !== undefined) process.env.GITHUB_ACTIONS = prev; } +} +const ok = { uuid: "s-id", url: "https://spacesheep.dev/@y/s", sha: "v1" }; + +test("--skill is sent as skill", async () => { + const { r, sent } = await run({ skill: " @spacesheep/docs " }, () => ok); + assert.equal(sent.length, 1); + assert.equal(sent[0].skill, "@spacesheep/docs"); + assert.equal(r.skill_missed, undefined); +}); + +test("no --skill sends none", async () => { + const { sent } = await run({}, () => ok); + assert.equal("skill" in sent[0], false); +}); + +test("a server that refuses the field gets the deploy again without it, and the answer says so", async () => { + const { r, sent } = await run({ skill: "@spacesheep/docs" }, (args) => { + if (!("skill" in args)) return ok; + const err = new Error("create_space takes no skill"); + err.info = { code: "invalid", message: "create_space takes no skill", violations: [{ field: "skill", message: "create_space takes no skill" }] }; + throw err; + }); + assert.equal(sent.length, 2); + assert.equal("skill" in sent[1], false); + assert.match(r.skill_missed, /older than this CLI/); +}); + +test("any other refusal is the deploy's, not retried", async () => { + await assert.rejects(run({ skill: "@spacesheep/docs" }, () => { + const err = new Error("nope"); err.info = { code: "invalid", violations: [{ field: "title", message: "too long" }] }; throw err; + }), /nope/); +});