A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.
JavaScript claude-code-pluginclaude-code-skillscode-appsgithub-copilot-pluginpower-appspower-pagespower-platform
#!/usr/bin/env node
'use strict';
// genpage-upload: deploy ONE generative page through the SDK, from a code file and the files that carry its text.
//
// WHY THIS SCRIPT EXISTS (#589). A prompt is arbitrary user text: it carries quotes, newlines,
// `%VAR%`, `&`, `|` and non-ASCII, and it used to reach the deploy as part of a command line the
// /genpage skill composed in Markdown — observed live to fail with
//
// Error: Not a valid command.
// Parse failed on: Inspection
// Was it quote wrapped? No, be sure to wrap values that contain spaces.
//
// …for a prompt containing an ASCII-quoted multiword page name. The workaround people reach for is
// to EDIT the approved prompt (swap in typographic quotes), which silently breaks prompt
// provenance — the page is then built from text the user never approved.
//
// So this script is the one entry point both skills use, and the prompt never becomes part of a
// command line: it is read from a FILE and handed to the page backend (lib/genpage-sdk.js) as a value.
// The page is written through the SDK, so there is no longer a `pac` process whose arguments could
// mangle anything; the file inputs stay, because the agent that runs this writes text to files.
//
// Usage:
// node scripts/genpage-upload.js --env <orgUrl> --code-file <path>
// --prompt-file <path> --agent-message-file <path>
// [--app-id <guid>] [--page-id <guid>] [--name <text>] [--data-sources <csv>]
//
// `--app-id` is optional for a CREATE: a page needs no app, and one created without it is placed
// later (by the app that is built around it, or by a later `--add-to-sitemap` upload of another).
// An UPDATE (`--page-id`) needs it: the page must be proven to be in that app's navigation.
//
// With `--app-id` the upload takes the app's pages lease (lib/standalone-page-lease.js) for the whole write and placement, in the app folder's
// .maker-workspace — `--app-dir <folder>`, default the current folder, as a build of the spec in that folder uses — so a build of the app, or another
// edit of it from that folder, refuses with `pages-locked` (exit 1, `outcome: "nothing"`) instead of interleaving its writes. It costs one read (the
// app's unique name, which names the lock); an app whose name cannot be read is `GENPAGE_APP_LEASE_UNRESOLVED`, nothing written. A single-machine
// advisory lock in that folder, not a compare-and-swap on the service. A create with no `--app-id` touches no app and takes no lease.
//
// One update is different: `--page-id` for a page THIS TOOL created and did not finish — its publish failed, or the
// `--add-to-sitemap` placement it was asked for did — completes it. The base marker beside the code file records that
// (`unfinished`), the page is published and, with `--add-to-sitemap`, placed, and the proof of the page's place in the app's
// navigation that every other update makes is replaced by that marker, for that page only. The failure that leaves a page
// unfinished says to run the same command again with `--page-id <id>` added.
//
// `--add-to-sitemap` writes the page's entry in the app's navigation with a title in the environment's BASE language (read with the
// backend's `baseLanguage()`, as `genpage.js add-to-app` does; also when this completes a placement an earlier run began): a title in a
// language the environment has not provisioned does not show. A language that cannot be read fails the placement before the app is
// touched, and the report says only what is known — the page is written and published, its placement is unfinished, this run changed
// nothing in the app's navigation (and, when it completes a placement an earlier run began, did not look at what that run saved there) —
// and the page is completed by the same command with `--page-id` added.
//
// `--prompt` / `--agent-message` are accepted for short, shell-safe text, but `--prompt-file` is
// the documented path precisely because it cannot be mangled by a shell. `--compiled-code-file` is
// refused (see main): the page is compiled from the checked `--code-file` itself.
//
// `--solution <unique name>` is the solution the page joins (AB#6443257). A page made through the SDK joins none on its own, and nothing
// else brings it into one — the add of an app brings its sitemap, never the pages its navigation opens — so a run that was given a
// solution and made pages left them out of it. The solution is read BEFORE anything is written (a name that is no unmanaged solution, or
// an environment that cannot say the page table's component type, refuses the upload with nothing made), and the page is added LAST,
// once it is written, published and placed: the result then says `solution` and `addedToSolution: true`. A system solution (Default,
// Active, Basic) holds every component already, so it is neither read nor added to (`addedToSolution: false`, `systemSolution: true`).
// An add that fails is a failure (exit 1, `code: "solution-add-failed"`, the page's id): the page works and is not in the solution, and
// the error names the one command that adds it (add-page-to-solution.js) — never a second create. An update may name it too: the page
// then joins the solution once it is saved, which is how a create that did not finish, run again with `--page-id`, still joins it.
//
// `--draft` saves the page and publishes NOTHING (lib/genpage-sdk.js `publish: false`): the app that runs keeps serving what was published until a
// publish — a build, the designer, or the same upload without `--draft`. Every placement the upload makes is a draft too — the `--add-to-sitemap`
// entry, and the default views of the page's tables — so the app is neither published nor asked to publish anything before its write. The answer is
// the backend's: `draft: true`, `saved`, `pendingPublish` (null when nothing new was saved), `draftFingerprint` (never `fingerprint`: a draft is no
// proof of a publish, and nothing that records what was published is advanced from it), `publish: { kind: 'skipped', reason: 'draft' }` and, when it
// placed, `placementState`. A draft is finished as asked, so its base marker records no `unfinished` state; a placement that fails names
// `genpage.js add-to-app --draft`. A page this tool created and did not finish is completed by PUBLISHING it, so `--draft` is refused for it.
//
// `--turn-id <guid>` (with `--app-dir`, whose `.maker-workspace\turns` holds the turn's record) names ONE complete intended upload, for a caller that retries it — a
// host that may lose an answer, or one that correlates the page's history turn. The id is kept before the first send and given again, with the same files and flags,
// when that upload is retried: the turn's record decides what the run is (a create it began stays that create, with or without `--page-id`), the page's history holds
// one turn, and anything the retry would do differently (content, title, tables, app, placement, draft or publish) is refused with nothing written. A resumed create's
// placement is reported finished only once the app's published navigation is seen to show the page (`GENPAGE_APP_PUBLISH_UNPROVEN` when it cannot be read). Without
// `--turn-id` nothing of this runs.
const fs = require('node:fs');
const path = require('node:path');
const { parseArgs, validateFlags, emitResult } = require('./lib/dataverse-auth.js');
const { makeGenpageSdk, suppliedButBlank, sourceText, dataSourcesOf, normalizeTurnId, readTurnRecord } = require('./lib/genpage-sdk.js');
const { appTurnStore } = require('./lib/page-turn-store.js');
const { withStandalonePagesLease } = require('./lib/standalone-page-lease.js');
const { openStandaloneAppWriteFence } = require('./lib/managed-standalone-fence.js');
const { makeThrottleBudget } = require('./lib/throttle-budget.js');
const { leftOnDiskWarning } = require('./lib/left-on-disk.js');
const { griffelStyleProblems, griffelProblemsNotIn, describeGriffelRefusal, describeGriffelWarning } = require('./lib/griffel-styles.js');
const { commandArgOrPlaceholder } = require('./lib/cli-failure.js');
const genpageBase = require('./lib/genpage-base.js');
const { writeFileSafe } = require('./lib/safe-fs.js');
const { makeWritesBlocking } = require('./lib/output-redaction.js');
const { emitSkillCompleted } = require('./lib/telemetry/skill-completed.js');
const KNOWN = ['env', 'app-id', 'code-file', 'compiled-code-file', 'page-id', 'name', 'name-file',
'data-sources', 'clear-data-sources', 'prompt', 'prompt-file', 'agent-message', 'agent-message-file',
'model', 'connectors', 'actions', 'add-to-sitemap', 'overwrite-deployed', 'solution', 'app-dir', 'draft', 'turn-id'];
// Bare switches; every other flag carries a value.
const SWITCHES = ['add-to-sitemap', 'clear-data-sources', 'overwrite-deployed', 'draft'];
const NEED_VALUE = KNOWN.filter((f) => !SWITCHES.includes(f));
const USAGE = 'Usage: node scripts/genpage-upload.js --env <orgUrl> --code-file <path> '
+ '--prompt-file <path> --agent-message-file <path> [--app-id <guid>] [--page-id <guid>] [--name-file <path> | --name <text>] '
+ '[--data-sources <csv>] [--clear-data-sources] [--model <id>] '
+ '[--connectors <path>] [--actions <path>] [--add-to-sitemap] [--overwrite-deployed] [--solution <unique name>] [--app-dir <folder>] [--draft] [--turn-id <guid>]'
+ ' (--app-id is required with --page-id and with --add-to-sitemap; --page-id alone finishes a page this tool created and did not finish;'
+ ' --turn-id needs --app-dir: the turn record of a retried upload lives in that app folder)';
// The skill writes these inputs — the prompt, agent-message and page-name files, connectors.json, actions.json —
// into the working directory just before the upload, and a write through a link left at one of those names (a
// symbolic link, a junction, or a hard link whose other name lives elsewhere) rewrites the file it points to,
// outside the working directory included. The skill checks each name before it writes (SKILL.md Phase 6); this is
// the check that holds when that step was skipped: the upload stops, and says the linked file may have been
// changed. Returns null for a plain file, and for a path that is not there (the read reports that).
//
// Scope: both checks close a link left in the folder BEFORE the run, such as one shipped in a cloned repository. A
// process racing the skill to plant one between its check and its write is out of their reach. Such a process
// already holds the user's rights over the whole working directory (its pages, plan and manifest), and no check
// inside the skill can stop it.
function notPlainFile(abs) {
let st;
try { st = fs.lstatSync(abs); } catch (e) {
return e && e.code === 'ENOENT' ? null : `${abs} could not be inspected (${(e && e.code) || e})`;
}
if (st.isFile() && st.nlink <= 1) return null;
if (st.isSymbolicLink()) return `${abs} is a symbolic link or junction, not a file written in place — writing it may have changed the file it points to; check that file, remove the link and re-run`;
if (st.isFile()) return `${abs} is a hard link (${st.nlink} names for one file), not a file written in place — writing it changed its other names too; check them, remove this one and re-run`;
return `${abs} is not a plain file (a folder or a special file) — remove it and re-run`;
}
// Resolve one text input that may arrive inline or by file. Returns { ok, value } or { ok:false,
// error }. Supplying BOTH is rejected rather than silently preferring one: the two would be
// different text, and quietly picking either means deploying a prompt the caller did not intend.
function resolveText(flags, inlineFlag, fileFlag, readFile) {
const inline = flags[inlineFlag];
const file = flags[fileFlag];
const hasInline = typeof inline === 'string' && inline !== '';
const hasFile = typeof file === 'string' && file !== '';
if (hasInline && hasFile) {
return { ok: false, error: `pass only one of --${inlineFlag} or --${fileFlag}, not both` };
}
if (hasFile) {
// utf8, and NOT trimmed: a downloaded prompt is a multi-line conversation transcript whose
// blank lines — and whose final newline — are part of the text.
//
// A LEADING UTF-8 BOM is stripped. MEASURED against a live deployment: `pac model genpage
// download` (what an earlier release used) wrote the recovered `prompt.txt` starting `ef bb bf`,
// and the documented edit flow re-feeds exactly that file through `--prompt-file`, so a folder
// downloaded then still carries it. Node's 'utf8' decode keeps the BOM as U+FEFF, so the prompt
// handed to the deploy would begin with an invisible character — silently altering the first
// character of a prompt the user approved. Only the LEADING one is removed:
// a U+FEFF anywhere else is content, not an encoding marker.
// The bytes are otherwise passed through UNCHANGED — no trailing-newline normalization. This
// transport exists precisely so an arbitrary prompt survives verbatim, and the direct
// /app-builder wrapper path preserves a final newline, so stripping one here made the same text
// deploy differently depending on which path carried it. A prompt that legitimately ends with a
// newline (a downloaded transcript, a heredoc) keeps it.
const linked = notPlainFile(path.resolve(file));
if (linked) return { ok: false, error: `--${fileFlag} ${linked}` };
try {
return {
ok: true,
value: String(readFile(path.resolve(file), 'utf8')).replace(/^\uFEFF/, ''),
};
} catch (e) {
return { ok: false, error: `--${fileFlag} could not be read: ${e.message}` };
}
}
return { ok: true, value: hasInline ? inline : undefined };
}
// Reads the deployed page once, for the update's guards: the base-marker comparison needs its source, and the Griffel check
// compares a file with it. It never rejects — a page that cannot be read is `deployedError`, and one that is not there says
// so (`notFound`), so the caller reaches its verdicts in its own order. `page` is the SDK's projection of the page (its
// `dataSources` are what an upload that names none leaves in place).
async function probeDeployedPage(genpage, pageId) {
try {
const page = await genpage.read(pageId);
return { page, deployedText: page.source, deployedError: null, notFound: false };
} catch (e) {
return { page: null, deployedText: null, deployedError: (e && e.message) || String(e), notFound: !!(e && e.code === 'GENPAGE_NOT_FOUND') };
}
}
// What a file holding a JSON array of bindings (--connectors / --actions) says, or why it cannot be used. The array goes to the
// page's config.json as it is: the SDK neither validates nor resolves a binding.
function readBindings(flag, file, readFile) {
let parsed;
try {
// A BOM is not part of the JSON; an editor on Windows adds one.
parsed = JSON.parse(String(readFile(path.resolve(file), 'utf8')).replace(/^\uFEFF/, ''));
} catch (e) {
return { ok: false, error: `--${flag} could not be read as JSON: ${(e && e.message) || e}` };
}
if (!Array.isArray(parsed)) return { ok: false, error: `--${flag} must hold a JSON array (the bindings the page uses), not ${parsed === null ? 'null' : typeof parsed}` };
return { ok: true, value: parsed };
}
// After a successful upload, record the hash of the file we sent. A readback whose hash equals
// that file (a BOM and a final CRLF are not a difference) confirms the service stored it:
// source 'upload'. A readback that differs must not become the trusted deployed hash — a save
// can land between the upload and the readback, and recording it would make the next update
// treat someone else's page as our base. Record the uploaded hash as 'upload-unverified', with
// the line delta, so the next update refuses deployed-changed until genpage-base.js check. A
// failed readback is the same unverified marker. The whole step is best-effort: the upload
// already succeeded, and a throw here would report that create as a failure without a page id,
// so a retry could create a second page. A failed marker write deletes any previous marker: a
// stale base is worse than no base. Answers the marker it wrote, or null when it wrote none.
//
// `unfinished` marks a page this upload CREATED and has not finished: it is recorded with the marker (lib/genpage-base.js) so that naming the page again completes it
// (completionMarker) — and a marker that could not be written is no way to complete anything, which the caller has to know to say so (hence the answer).
async function recordObservedBase(args) {
try {
return await writeObservedBase(args);
} catch (e) {
args.warnings.push(`could not record a base marker (${(e && e.message) || e}) — a later update will refuse no-base until the page is recorded`);
return null;
}
}
async function writeObservedBase({ genpage, flags, pageId, base, warnings, checkedText, unfinished }) {
const codeFile = flags['code-file'];
// The marker sits beside the file the caller named, which must still be a regular file with one name: readPlainText refuses a link, or a file
// that is gone, as it always did. What the marker records is the hash of the bytes that were checked and sent (`checkedText`) — never a new
// read of the file, which may have been saved again since; a later check then sees that save as a local change, which it is.
try {
base.readPlainText(path.resolve(codeFile));
} catch (e) {
warnings.push(`could not read ${codeFile} to record a base marker (${e.message}) — a later update will refuse no-base until the page is recorded`);
try { base.deleteMarker(codeFile); } catch (del) {
warnings.push(`the previous base marker could not be removed (${del.message})`);
}
return null;
}
const localText = checkedText;
const localSha256 = base.pageHash(localText);
let deployedSha256 = localSha256;
let source = 'upload';
try {
const downloaded = (await genpage.read(pageId)).source;
if (base.pageHash(downloaded) === localSha256) {
source = 'upload';
deployedSha256 = localSha256;
} else {
source = 'upload-unverified';
deployedSha256 = localSha256;
const lines = base.lineDelta(localText, downloaded);
warnings.push(`the deployed page differs from what was just uploaded (${lines.added} lines added, ${lines.removed} removed) — the service rewrote it or another save landed right after, so the base marker records the uploaded file's hash as unverified. The next update will refuse deployed-changed until the page is compared with genpage-base.js check`);
}
} catch (e) {
source = 'upload-unverified';
deployedSha256 = localSha256;
warnings.push(`could not re-read the deployed page to confirm its hash (${(e && e.message) || e}), so the base marker records the uploaded file's hash as unverified — a later update may refuse deployed-changed if the service rewrote the page`);
}
try {
return base.writeMarker(codeFile, {
version: 1,
pageId,
// No app for a page created without one: the marker then records the page alone, and the first update that names
// the app the page was placed in goes ahead on it (lib/genpage-base.js compareWithMarker) and records the app.
...(flags['app-id'] ? { appId: flags['app-id'] } : {}),
deployedSha256,
localSha256,
source,
...(unfinished ? { unfinished } : {}),
});
} catch (e) {
warnings.push(`could not write the base marker (${e.message}) — a later update will refuse no-base until the page is recorded again`);
try { base.deleteMarker(codeFile); } catch (del) {
warnings.push(`the previous base marker could not be removed (${del.message})`);
}
return null;
}
}
// The base marker of the page this upload is COMPLETING, or null. Only an update that names a page can be one, and only a marker that says `unfinished` for exactly that
// page counts: another page's marker, a finished page's, none, or one that cannot be read is "not completing", and the ordinary rules apply, whatever they say.
// `strict` (a run that resumes a create under a turn) lets a read that THROWS reach the caller instead: see `main`.
function completionMarker(flags, base, { strict = false } = {}) {
if (!flags['page-id'] || typeof flags['code-file'] !== 'string' || !flags['code-file']) return null;
let marker;
try { marker = base.readMarker(flags['code-file']); } catch (e) { if (strict) throw e; return null; }
return marker && marker.unfinished && marker.pageId === String(flags['page-id']).toLowerCase() ? marker : null;
}
// A turn's state that cannot be read (its id, its record): nothing was sent, and the answer keeps the turn and the store's code.
const turnStateRefusal = (e, turnId) => ({ error: (e && e.message) || String(e), outcome: 'nothing', ...(e && typeof e.code === 'string' ? { code: e.code } : {}), ...(turnId ? { turnId } : {}) });
// What a run IS: an update, a create, a create its turn resumes, or the completion of a page an earlier run created and left unfinished. It is read from the turn's
// record (lib/page-turn-store.js) and the base marker beside --code-file. `main` reads it before anything is sent, so a command that cannot run is refused before the
// service is asked anything, and a run under a turn reads it AGAIN once it holds the app's pages lease (see `main`): another run of the same turn may bind it, and
// leave its page unfinished, while this one waits for that lease. Answers `{ refusal }` (a payload, nothing sent) or `{ resumesCreate, updating, completing }`.
async function classifyRun({ flags, base, turnId }) {
// The record is read FIRST, because it says what this run is: an upload whose create answered with a page id and did not finish is told to run again with
// --page-id added, and under a turn that run is still the CREATE its record names — dispatched as an update it would be refused (a page create is not a page update).
let turnRecord = null;
if (turnId) {
try {
turnRecord = await readTurnRecord(appTurnStore(flags['app-dir']), turnId);
} catch (e) {
return { refusal: turnStateRefusal(e, turnId) };
}
}
// `updating` is what `flags['page-id']` meant before turns: without --turn-id the two are the same.
const resumesCreate = !!(turnRecord && /create$/.test(turnRecord.operation));
const updating = !!flags['page-id'] && !resumesCreate;
// An upload that CREATED a page and did not finish it — the publish failed, or the placement it was asked for did — leaves a base marker that says so
// (lib/genpage-base.js `unfinished`). Naming that page again with --page-id COMPLETES it: it is published and, with --add-to-sitemap, placed, without the proof an
// ordinary update makes that the page is in the app's navigation — an unfinished page is not in it yet, so that proof could never be made. The marker is for one
// page, so this is for that page alone; for any other the rules below apply as they always did.
// Under a turn, a run that resumes the create its record names completes THAT page whether or not --page-id is given: the same command run again as it was is the
// documented retry. The record only says which page; that this upload created the page and has not finished it is still what the base marker beside --code-file says,
// as for --page-id — the record grants nothing on its own (no ownership, no overwrite), and without a marker for that page nothing is being completed.
const resumedPageId = resumesCreate && !flags['page-id'] ? turnRecord.pageId : undefined;
// A marker that is missing, malformed or for another page is "not completing" here too, and the placement then asks the app itself (`provingPlacement`, in deploy) —
// no marker is ever read as proof that a placement is finished. A marker store that THROWS (a host's `deps.base`; lib/genpage-base.js answers null for every failure)
// refuses a resumed create before anything is sent: this run could not record what it leaves unfinished either, so it is the turn's state that is unavailable, with
// the same code and remedy as a turn store that cannot be read.
try {
const completing = completionMarker(resumedPageId ? { ...flags, 'page-id': resumedPageId } : flags, base, { strict: resumesCreate });
return { resumesCreate, updating, completing };
} catch (e) {
return {
refusal: {
error: `the base marker beside ${flags['code-file']}, which records whether the create of page ${flags['page-id'] || resumedPageId} under turn ${turnId} was finished, could not be read (${(e && e.message) || e}). Nothing was changed by this attempt. Do not run it without its turn id or under a new one; make the marker readable again and run the same command again.`,
outcome: 'nothing', code: 'GENPAGE_TURN_STATE_UNAVAILABLE', turnId,
},
};
}
}
// The refusals that follow from what a run IS (classifyRun), not from its flags alone; each answers its words, or null. `main` checks each where it always has, and a
// run under a turn checks them all again, in this order, once it holds the app's pages lease — what the run is may have changed while it waited for it.
const KIND_REFUSALS = {
// A create needs no app — a page is a row of its own, placed by whatever app names it — but an update must prove the page is in the app
// it names, and a placement is an edit of that app: both need the app's id. A page that was created with no app and is being completed needs none either.
updateWithoutApp: ({ flags, updating, completing }) => (updating && (typeof flags['app-id'] !== 'string' || !flags['app-id']) && !(completing && !completing.appId)
? `--app-id is required with --page-id: an update must name the app the page is placed in\n${USAGE}` : null),
// A page created with no app is completed into an app only by placing it there: an app named without a placement would attach the page's tables to an app
// that does not hold the page, which is what an ordinary update proves it does not do.
completionWithoutPlacement: ({ flags, completing, addToSitemap }) => (completing && !completing.appId && typeof flags['app-id'] === 'string' && flags['app-id'] && !addToSitemap
? `page ${flags['page-id'] || completing.pageId} was created with no app, so completing it into app ${flags['app-id']} needs --add-to-sitemap: the page is placed in that app, not merely attached to it. Pass --add-to-sitemap, or drop --app-id to only publish the page\n${USAGE}` : null),
// A page whose create was asked to place it is not finished until it is placed. The marker keeps that, and the run that completes it has to ask for it again — it is
// the one thing in the command that the marker cannot stand in for, and a run that left it out would clear the page's unfinished state with the placement undone,
// leaving a page no later update could prove is in the app.
placementLeftOut: ({ flags, completing, addToSitemap }) => (completing && completing.unfinished === 'place' && !addToSitemap
? `page ${flags['page-id'] || completing.pageId} was created to be placed in app ${completing.appId || flags['app-id']}, and it is not finished until it is placed: run the same command again with --page-id ${flags['page-id'] || completing.pageId} added and --add-to-sitemap kept\n${USAGE}` : null),
// Completing a page this tool created and did not finish IS publishing it (and placing it, when that is what is left), and a draft publishes nothing: it would
// clear the marker that says the page is unfinished and leave the page unpublished, with nothing left to say so.
draftCompletion: ({ flags, completing, draft }) => (draft && completing
? `page ${flags['page-id'] || completing.pageId} was created by this tool and is not finished: completing it publishes it${completing.unfinished === 'place' ? ' and places it in the app' : ''}, and a draft publishes nothing. Nothing was changed. Run the same command again with --page-id ${flags['page-id'] || completing.pageId} kept and drop --draft to finish it\n${USAGE}` : null),
// REFUSED, not silently dropped. An update cannot add a sitemap entry, and one that quietly discards
// the flag leaves the caller believing a placement happened. (Completing a page this tool created and did not finish is the one update that places it.)
placementOnUpdate: ({ updating, completing, addToSitemap }) => (addToSitemap && updating && !completing
? '--add-to-sitemap cannot be combined with --page-id: an update cannot add a sitemap '
+ 'entry, and the page it names is already placed. Drop one of the two.' : null),
};
async function main(argv = process.argv.slice(2), deps = {}) {
const startedAt = Date.now();
const emit = deps.emit || emitResult;
const readFile = deps.readFile || fs.readFileSync;
const makeBackend = deps.makeGenpageSdk || makeGenpageSdk;
const flagError = validateFlags(argv, { known: KNOWN, needValue: NEED_VALUE });
if (flagError) return emit(false, { error: `${flagError}\n${USAGE}` });
const { flags } = parseArgs(argv);
for (const required of ['env', 'code-file']) {
if (typeof flags[required] !== 'string' || !flags[required]) {
return emit(false, { error: `--${required} is required\n${USAGE}` });
}
}
// A caller-held page turn id (lib/genpage-sdk.js, "A turn id the CALLER holds"): one id per complete intended upload, kept before the first send and given again, with
// the same files and flags, when that upload is retried; its record lives in the app folder's turn store (lib/page-turn-store.js), and it says what this run is
// (classifyRun). Without --turn-id nothing here runs.
// `--app-dir` alone is the folder of the app's pages lease (see the header), and it needs no turn; a turn id needs it, because the turn's record lives there.
let turnId;
if (flags['turn-id'] !== undefined) {
if (typeof flags['turn-id'] !== 'string' || typeof flags['app-dir'] !== 'string' || !flags['app-dir'].trim()) {
return emit(false, { error: `--turn-id and --app-dir go together: the turn's record lives in that app folder's turn store, so --turn-id needs --app-dir\n${USAGE}` });
}
try {
turnId = normalizeTurnId(flags['turn-id']);
} catch (e) {
return emit(false, turnStateRefusal(e, turnId));
}
}
const base = deps.base || genpageBase;
const kind = await classifyRun({ flags, base, turnId });
if (kind.refusal) return emit(false, kind.refusal);
const { resumesCreate, updating, completing } = kind;
let refused = KIND_REFUSALS.updateWithoutApp({ flags, updating, completing });
if (refused) return emit(false, { error: refused });
const addToSitemap = flags['add-to-sitemap'] === true || flags['add-to-sitemap'] === 'true';
if (addToSitemap && (typeof flags['app-id'] !== 'string' || !flags['app-id'])) {
return emit(false, { error: `--add-to-sitemap needs --app-id: the page is added to that app's navigation. Pass --app-id, or drop --add-to-sitemap to create the page with no app\n${USAGE}` });
}
refused = KIND_REFUSALS.completionWithoutPlacement({ flags, completing, addToSitemap }) || KIND_REFUSALS.placementLeftOut({ flags, completing, addToSitemap });
if (refused) return emit(false, { error: refused });
// `--draft` is a switch, but `--draft=<value>` reaches here as the STRING value (parseArgs), as the other switches do. Only "true" and "false" are read as a yes or
// a no; anything else is refused, so a typo neither publishes a page that was meant to stay a draft nor holds back one that was meant to go live.
if (flags.draft !== undefined && flags.draft !== true && flags.draft !== 'true' && flags.draft !== 'false') {
return emit(false, { error: `--draft takes no value (got ${JSON.stringify(flags.draft)}): pass --draft to save the page without publishing it, or leave it out\n${USAGE}` });
}
const draft = flags.draft === true || flags.draft === 'true';
refused = KIND_REFUSALS.draftCompletion({ flags, completing, draft });
if (refused) return emit(false, { error: refused });
// Refused, not passed on. The page is compiled from the TypeScript in --code-file, which is what the Griffel check below reads; JavaScript
// compiled elsewhere would be deployed in its place, and compiled output names makeStyles in ways the check does not read as Griffel's
// (`(0, x.makeStyles)(…)` after a require), with nothing to tie the file to the source that was checked — so a page could ship a shorthand
// Griffel drops with no check at all. No flow of this plugin sends it.
if (flags['compiled-code-file'] !== undefined) {
return emit(false, {
error: '--compiled-code-file is not accepted: the Griffel check reads the TypeScript in --code-file, and JavaScript compiled '
+ 'elsewhere would be deployed without it. Leave it out — the page is compiled from the --code-file it is given.',
});
}
const prompt = resolveText(flags, 'prompt', 'prompt-file', readFile);
if (!prompt.ok) return emit(false, { error: prompt.error });
const agentMessage = resolveText(flags, 'agent-message', 'agent-message-file', readFile);
if (!agentMessage.ok) return emit(false, { error: agentMessage.error });
// The page's display name travels by file too. It is the maker's text as well, and the skill substituted it
// into `--name "<name>"`: PowerShell expanded `$(…)` in it before this script ran — a name could run a
// command — and `Revenue $100` arrived as `Revenue `. By file it is never command text, and keeps every
// character. Its trailing line break, which an editor adds, is not part of the name.
const pageName = resolveText(flags, 'name', 'name-file', readFile);
if (!pageName.ok) return emit(false, { error: pageName.error });
if (typeof flags['name-file'] === 'string' && pageName.value !== undefined) pageName.value = pageName.value.replace(/(?:\r?\n)+$/, '');
// An EMPTY prompt or agent-message is refused rather than defaulted. The page backend substitutes
// `Generative page <name>` for a blank prompt and `Authored by app-builder` for a blank agent
// message, which is reasonable when nothing was supplied at all — but a caller who passed
// `--prompt-file` / `--agent-message-file` asked for THAT text, and silently deploying generated
// provenance instead is the failure this path exists to prevent. A file holding only whitespace,
// only a newline, or only a BOM is the realistic way this happens.
for (const [label, inlineFlag, fileFlag, resolved] of [
['prompt', 'prompt', 'prompt-file', prompt],
['agent message', 'agent-message', 'agent-message-file', agentMessage],
['page name', 'name', 'name-file', pageName],
]) {
const given = typeof flags[inlineFlag] === 'string' || typeof flags[fileFlag] === 'string';
if (given && suppliedButBlank(resolved.value === undefined ? '' : resolved.value)) {
return emit(false, {
error: `the ${label} resolved to empty — refusing to deploy generated text in place of the `
+ `${label} you supplied. Check the file is not blank, newline-only, or BOM-only.`,
});
}
}
// Same normalization as the switch above, and for the same reason: `parseArgs` yields the STRING
// "false" for `--clear-data-sources=false`, which is truthy. Testing the raw flag would have read
// an explicit refusal to clear as permission to clear — unbinding the page the guard protects.
const clearDataSources = flags['clear-data-sources'] === true || flags['clear-data-sources'] === 'true';
// Same normalization as the switches above: `parseArgs` yields the STRING "false" for
// `--overwrite-deployed=false`, which is truthy. An explicit refusal must not authorize an overwrite.
const overwriteDeployed = flags['overwrite-deployed'] === true || flags['overwrite-deployed'] === 'true';
// A contradiction, not a precedence question: one says "remove every binding", the other names the
// bindings to keep. Silently preferring either is a guess about intent on a destructive operation.
if (clearDataSources && flags['data-sources']) {
return emit(false, {
error: '--clear-data-sources cannot be combined with --data-sources: one unbinds the page and '
+ 'the other sets its bindings. Drop whichever you did not mean.',
});
}
refused = KIND_REFUSALS.placementOnUpdate({ updating, completing, addToSitemap });
if (refused) return emit(false, { error: refused });
// The two binding files are read here, as the page's `connectorBindings` / `actionBindings` (bare JSON arrays), and checked first for the
// same reason as the text files (notPlainFile). An omitted flag leaves a page's bindings as they are; `[]` clears them.
const bindings = {};
for (const [flag, key] of [['connectors', 'connectorBindings'], ['actions', 'actionBindings']]) {
if (typeof flags[flag] !== 'string' || !flags[flag]) continue;
const linked = notPlainFile(path.resolve(flags[flag]));
if (linked) return emit(false, { error: `--${flag} ${linked}` });
const read = readBindings(flag, flags[flag], readFile);
if (!read.ok) return emit(false, { error: read.error });
bindings[key] = read.value;
}
// The page is read ONCE, here, and everything after is decided on these bytes: the Griffel check below, an update's comparison with the deployed page, the base marker's hash,
// and what is written to the page. An update reads the deployed page before it writes — seconds in which the file can be saved again — and reading the file again then would
// deploy bytes nothing here checked. A file that cannot be read is refused: there is nothing to check, or to send. The text is the bytes as they are, bar one leading BOM
// (the maker stores none) — and bytes that are not UTF-8 are refused, since the page is stored as UTF-8 text and a decode that replaced a byte would send a page nobody checked.
let checkedText;
try {
checkedText = sourceText(fs.readFileSync(path.resolve(flags['code-file'])));
} catch (e) {
return emit(false, { error: `--code-file ${e && e.code === 'GENPAGE_SOURCE_NOT_UTF8' ? 'is not usable: ' : 'could not be read: '}${(e && e.message) || e}` });
}
// The Griffel check (AB#6691124), where every page upload passes: a /genpage create, an edit-flow update and a browser-verify fix all go through this script, so a page whose
// `makeStyles` writes one of the ten shorthands Griffel drops at runtime is refused here whether or not the skill's own gate (scripts/check-griffel-styles.js) was run before
// it — the compile does not execute makeStyles, and the page would otherwise deploy and render without its border, colour or reset. It is made on the bytes read above,
// which are the ones the page is written from.
// A CREATE is refused here, before anything is read from the environment. An UPDATE is held to the check by what it ADDS: the page is already deployed, possibly with a key Griffel drops that an
// earlier version of the plugin let through, and the same bytes sent again ship nothing new — so the file is compared with the deployed page, which an update reads anyway for
// its base check, and only a key that page did not have stops the upload (below, once it is read).
const styleProblems = griffelStyleProblems(checkedText).problems;
// The checker command the message suggests names the file: as one argument no shell reads anything in, or — for a path a shell cannot be given literally — as a
// placeholder, with the path named in a sentence instead (lib/cli-failure.js commandArg; a path can come from an untrusted checkout). For an update it adds
// `--existing`: the checker also judges the page's scroll context, which an already deployed page may lack, and holding it to that as a new page would turn a
// Griffel fix into a layout rewrite the maker did not ask for — with `--existing` those findings are notes (references/rules.md, Styling gotchas 6).
const griffelFile = commandArgOrPlaceholder(flags['code-file'], '<file>');
const griffelWords = { surface: 'upload', subject: flags['code-file'], command: `node scripts/check-griffel-styles.js --file ${griffelFile.arg}${updating ? ' --existing' : ''}`, ...(griffelFile.note ? { note: griffelFile.note } : {}) };
const stylesRefusal = (problems) => ({
ok: false,
code: 'griffel-shorthand',
error: describeGriffelRefusal(problems, flags['code-file'], griffelWords),
problems,
});
if (styleProblems.length && !updating) return emit(false, stylesRefusal(styleProblems));
// The waiting a run does for a throttled service is bounded at five minutes (lib/throttle-budget.js): this one budget is the page write's and the backend's HTTP client's alike — its reads and its publish wait out a throttle too. A wait of more
// than a few seconds is announced on stderr — stdout carries the JSON result — so that a command that is silent for minutes is not mistaken for a hung one.
const warn = (message) => process.stderr.write(`WARNING: ${message}\n`);
// An upload that names an app writes in it and publishes it, which puts live the saved forms the app holds. So the managed debt of the app's workspace
// (lib/managed-standalone-fence.js) is read as a build reads it: while a build's managed change is unproved, or may have been sent and is not
// confirmed, the upload is refused with nothing written. It is read now, so an upload that cannot run builds no backend, and again under the app's
// leases before the deploy reads or writes anything (`underLease`, in `run` below) — the read that decides, and the one that fills the barrier the
// backend's transport is gated by. No store changes nothing.
let fence = null;
if (typeof flags['app-id'] === 'string' && flags['app-id']) {
try {
fence = openStandaloneAppWriteFence({ environment: flags.env, appDir: flags['app-dir'] || deps.appDir, appId: flags['app-id'] });
} catch (e) {
return emit(false, { ok: false, appId: flags['app-id'], code: (e && e.code) || 'managed-binding-store-unreadable', outcome: 'nothing', error: String((e && e.message) || e) });
}
}
const genpage = makeBackend(flags.env, { warn, throttle: makeThrottleBudget({ warn }), ...(turnId ? { turnBindings: appTurnStore(flags['app-dir']) } : {}), ...(fence && fence.barrier ? { publicationBarrier: fence.barrier } : {}) });
// The backend's workspace is removed BEFORE anything is reported: emitResult calls process.exit, so a cleanup placed after an emit never
// runs, and that workspace holds the page's source. dispose() answers when the storage calls the backend had under way have settled and the
// folder is gone — or says, in `{ left }`, which folder is not (a handle a scanner holds), and that is reported with the result: a success that left
// the page's source in the temp directory says so.
let outcome;
let leftOnDisk = null;
// skill_completed telemetry covers an upload that was ATTEMPTED — from here on — not a refusal of the command line or its files above.
const report = deps.reportCompletion || emitSkillCompleted;
// An upload that names an app writes in it (its navigation, or the views of the page's tables), so the whole deploy runs under the app's pages lease.
// A refusal of the lease comes before the deploy reads or writes anything, and is a result like any other refusal (`entered` tells it from a deploy's throw).
let entered = false;
const appId = typeof flags['app-id'] === 'string' && flags['app-id'] ? flags['app-id'] : null;
const run = async ({ appUniqueName } = {}) => {
// Under the leases, before anything is read for the deploy or written: the managed fence that decides (see above). Its refusal comes before
// `entered`, so it is reported like the lease's own, with nothing done.
if (fence) fence.underLease({ appUniqueName });
entered = true;
// Under the lease, a run under a turn reads what it IS again (classifyRun), and takes its obligations — finishing a placement an earlier run saved, proving the
// page live in the app that runs — from what is held NOW. What was read above was read before the lease, and another run of the same turn may have bound it in
// between: created the page, saved its placement, failed to publish the app and let the lease go. The backend reads the turn's record again when it writes and
// resumes THAT create; a run that still took itself for a fresh create would place the page as an ordinary placement does — the saved navigation already shows
// it, so nothing is written or published — and report the page finished while the app that runs does not show it. Every refusal that follows from what the run
// is is checked again too: nothing has been read for the deploy or written yet, so one is a refusal with nothing done. Without a turn nothing is read again — no
// other run binds anything for it — and its requests are as they were.
let now = { resumesCreate, updating, completing };
if (turnId) {
now = await classifyRun({ flags, base, turnId });
if (now.refusal) return { ok: false, payload: { ok: false, ...now.refusal } };
const kindNow = { flags, ...now, addToSitemap, draft };
const again = Object.values(KIND_REFUSALS).map((refusal) => refusal(kindNow)).find(Boolean);
if (again) return { ok: false, payload: { ok: false, outcome: 'nothing', turnId, error: again } };
if (styleProblems.length && !now.updating) return { ok: false, payload: { ok: false, outcome: 'nothing', turnId, ...stylesRefusal(styleProblems) } };
}
return deploy({
flags, genpage, base, checkedText, styleProblems, griffelWords, stylesRefusal,
prompt, agentMessage, pageName, bindings, clearDataSources, overwriteDeployed, addToSitemap, draft, turnId,
completing: now.completing, updating: now.updating, resumesCreate: now.resumesCreate,
});
};
try {
outcome = appId ? await withStandalonePagesLease({ genpage, appId, appDir: flags['app-dir'] || deps.appDir, warn }, run) : await run();
} catch (e) {
// Before the deploy is entered nothing has been read for it or written: the lease was refused (`build-locked`, `pages-locked`), the app it is keyed by
// could not be named (GENPAGE_APP_LEASE_UNRESOLVED, which a failed read of that name is too), or the managed fence under the leases refused
// (`publication-withheld`, `managed-binding-store-unreadable`). Any such failure is a refusal with one JSON result, never a throw with
// nothing on stdout. A failure inside the deploy keeps its own handling: it may have written, so it is never relabelled "nothing".
if (!entered && appId) {
const code = e && typeof e.code === 'string' && e.code ? e.code : 'GENPAGE_APP_LEASE_UNRESOLVED';
outcome = { ok: false, payload: { ok: false, appId, code, outcome: 'nothing', error: String((e && e.message) || e) } };
} else {
await reportUpload(report, flags, startedAt, { error: e });
throw e;
}
} finally {
try { leftOnDisk = leftOnDiskWarning(await genpage.dispose()); } catch { /* the backend's own folder; nothing to say when it cannot even answer */ }
// A deploy that threw has no result to carry it: that failure reaches the caller as it is, so the warning goes to stderr beside it.
if (leftOnDisk && !outcome) process.stderr.write(`WARNING: ${leftOnDisk}\n`);
}
if (leftOnDisk) outcome.payload = { ...outcome.payload, warnings: [...(outcome.payload.warnings || []), leftOnDisk] };
await reportUpload(report, flags, startedAt, outcome);
return emit(outcome.ok, outcome.payload);
}
// What an upload's result says, in the closed vocabulary of lib/telemetry/skill-completed.js — never a message, a name or an id.
// ok → success; page created | updated | unchanged, placedInApp, addedToSolution
// code griffel-shorthand → invalid-input (the page was refused before anything was written)
// any other failed result → deploy-failed; a throw → unexpected
function uploadCompletion({ ok, payload, error } = {}) {
const eventInfo = { operation: 'page-upload' };
if (error) return { outcome: 'failure', errorClass: 'unexpected', eventInfo };
const p = payload || {};
if (ok) {
eventInfo.page = p.unchanged ? 'unchanged' : p.updated ? 'updated' : 'created';
eventInfo.placedInApp = p.addedToSitemap === true;
if (typeof p.addedToSolution === 'boolean') eventInfo.addedToSolution = p.addedToSolution;
return { outcome: 'success', eventInfo };
}
if (typeof p.addedToSolution === 'boolean') eventInfo.addedToSolution = p.addedToSolution;
return { outcome: 'failure', errorClass: p.code === 'griffel-shorthand' ? 'invalid-input' : 'deploy-failed', eventInfo };
}
async function reportUpload(report, flags, startedAt, result) {
try {
const { outcome, errorClass, eventInfo } = uploadCompletion(result);
await report({ skillName: 'genpage', outcome, errorClass, eventInfo, envUrl: flags.env, durationMs: Date.now() - startedAt });
} catch { /* telemetry never changes the upload's result */ }
}
// Everything after the flags and the files have been read and checked: an update's guards, the write, the base marker and the placement in an
// app. It RETURNS what to report — { ok, payload } — instead of reporting it, so that main can remove the backend's workspace first.
async function deploy(context) {
const { flags, genpage, base, completing, checkedText, styleProblems, griffelWords, stylesRefusal, prompt, agentMessage, pageName, bindings, clearDataSources, overwriteDeployed, addToSitemap, draft, turnId, updating, resumesCreate } = context;
// A draft asks the backend for a save and no publish, of the page and of every placement this upload makes (see the header).
const draftAsk = draft ? { publish: false } : {};
const done = (ok, payload) => ({ ok, payload });
let griffelWarnings = [];
// The page's own checks, before anything is written. A CREATE has none left (its file was refused above if it was bad); an UPDATE reads the deployed page and compares.
let overwroteDeployed = false;
let deployed = null; // what the update's probe read: the page as the SDK projects it
// The page's id as soon as it is known, kept outside the write's try: an update names it up front, and a create learns it from the backend — from its answer, or from the
// error it raises once it has sent (`pageId`). Whatever fails after that, the failure carries it.
let knownPageId = flags['page-id'] || null;
// The solution the page joins (--solution), read before anything is read for the update or written: a name that is no unmanaged solution, or an environment that
// cannot say the page table's component type, refuses the upload with nothing made. A system solution is not read (the backend answers it from its name).
let solutionTarget = null;
if (typeof flags.solution === 'string' && flags.solution.trim()) {
try {
solutionTarget = await genpage.solutionTarget(flags.solution);
} catch (e) {
return done(false, {
ok: false,
...(e && typeof e.code === 'string' && e.code ? { code: e.code } : {}),
error: `nothing was written: the solution ${JSON.stringify(flags.solution.trim())} the page is to join cannot be used — ${(e && e.message) || e}`,
});
}
}
if (updating) {
// An UPDATE must have a target that EXISTS, and it must be in the app it names: updating a real page through the wrong app would write
// the page and attach its tables to an app that does not hold it. The page read is the existence proof — a page that is not there is a
// 404, and an update of it is refused (it would never become a create, as an unknown id once did) — and the app's navigation is the
// membership proof, SAVED layer included (what the designer holds is the app's page set the maker sees). The two are independent reads, so
// they run together; the VERDICTS are still reached in order, existence first: a page that does not exist is reported as absent even if
// the app listing also broke, and a listing that throws surfaces only when its verdict is reached.
// A page this tool created and did not finish (`completing`) is not in the app's navigation yet — that is what is left to do — so for that page alone the base
// marker is the proof, and the navigation is not read. Everything else about the update is as for any page.
const settle = (fn) => Promise.resolve().then(fn).then((value) => ({ value }), (error) => ({ failed: true, error }));
const [probeResult, membership] = await Promise.all([
settle(() => probeDeployedPage(genpage, flags['page-id'])),
completing ? { value: null } : settle(() => genpage.enumeratePages(flags['app-id'], { includeUnpublished: true })),
]);
const probe = probeResult.value;
if (probe.notFound) {
return done(false, {
error: `page ${flags['page-id']} does not exist in this environment — refusing to "update" it. Check the id, or drop `
+ '--page-id to create a page deliberately.',
});
}
if (!completing) {
if (membership.failed) throw membership.error;
const appPages = membership.value;
if (!appPages || appPages.ok !== true) {
return done(false, {
error: `cannot verify that page ${flags['page-id']} belongs to app ${flags['app-id']} `
+ `(${(appPages && appPages.error) || 'unknown reason'})`,
});
}
const pages = Array.isArray(appPages.pages) ? appPages.pages : [];
if (!pages.length) {
return done(false, {
error: `app ${flags['app-id']} has no generative pages, or could not be found — cannot verify `
+ `that page ${flags['page-id']} belongs to it`,
});
}
const pageInApp = pages.some((p) => String(p && p.pageId || '').toLowerCase() === String(flags['page-id']).toLowerCase());
if (!pageInApp) {
return done(false, {
error: `page ${flags['page-id']} is not in app ${flags['app-id']}'s navigation — updating it `
+ 'with this app id would attach its tables to that app; use the id of the app the page belongs to',
});
}
}
// The deployed page is read on EVERY update: the base-marker check needs its source, and so does the Griffel comparison. An unreadable
// page is `deployed-unreadable`. `--overwrite-deployed` is the only bypass, and it is never the default. The read and the write are
// separate calls; a save that lands between them (seconds) is not detected here — the write itself refuses a page whose version
// moved after it was read.
// An update that names no data sources, model, name or bindings leaves them as they are (the SDK writes only what differs), so nothing
// of the page's own is read back and re-sent; `--clear-data-sources` unbinds on purpose.
deployed = probe.page;
if (probe.deployedError && !overwriteDeployed) {
return done(false, {
ok: false,
code: 'deployed-unreadable',
pageId: flags['page-id'],
appId: flags['app-id'],
error: `cannot read the deployed page ${flags['page-id']} (${probe.deployedError}) — refusing to update, `
+ 'because the page may have changed and this upload would replace it unseen. Pass --overwrite-deployed '
+ 'to replace it deliberately, or retry when the page can be read.',
});
}
// The Griffel check for an UPDATE (see the create check above): what the deployed page already has ships nothing new. A file that IS the deployed page's text (a BOM and final
// CRLF are not a difference) is a re-send as it is, so none of its problems is new; otherwise a key counts as already there when the deployed page has it (griffelProblemsNotIn).
// Only a key, or a call the checker cannot read, that the deployed page did not have stops the upload — and --overwrite-deployed is no way past it: that flag is about edits made
// to the deployed page, not about what this file writes. When the deployed page could not be read (possible only under --overwrite-deployed) nothing says which keys it had; the
// page exists, and the release before this check updated it as it was, so the update goes on with a warning — as the build does for a page it has no record of.
if (styleProblems.length) {
if (probe.deployedText === null) {
griffelWarnings = [describeGriffelWarning(styleProblems, flags['code-file'], { ...griffelWords, standing: 'undetermined' })];
} else {
const sameAsDeployed = base.pageHash(checkedText) === base.pageHash(probe.deployedText);
const { added, inherited } = sameAsDeployed
? { added: [], inherited: styleProblems }
: griffelProblemsNotIn(styleProblems, griffelStyleProblems(probe.deployedText).problems);
if (added.length) return done(false, stylesRefusal(added));
griffelWarnings = [describeGriffelWarning(inherited, flags['code-file'], { ...griffelWords, standing: 'inherited' })];
}
}
if (overwriteDeployed) {
overwroteDeployed = true;
} else {
const marker = base.readMarker(flags['code-file']);
const cmp = base.compareWithMarker(marker, {
pageId: flags['page-id'],
appId: flags['app-id'],
deployedText: probe.deployedText,
});
if (cmp.marker !== 'present') {
const why = marker
? `the base marker next to ${flags['code-file']} is for page ${marker.pageId}${marker.appId ? ` in app ${marker.appId}` : ''}, not page ${flags['page-id']} in app ${flags['app-id']}`
: `no base marker for page ${flags['page-id']} in app ${flags['app-id']} next to ${flags['code-file']}`;
return done(false, {
ok: false,
code: 'no-base',
pageId: flags['page-id'],
appId: flags['app-id'],
error: `${why} — refusing to update, because the deployed page may have changed since this file was downloaded or generated. `
+ 'Record a base with genpage-base.js record, or pass --overwrite-deployed to replace the deployed page deliberately.',
});
}
if (cmp.deployed === 'changed') {
const lines = base.lineDelta(checkedText, probe.deployedText);
let deployedCopy;
let copyError;
try {
const copyPath = base.deployedCopyPath(flags['code-file']);
writeFileSafe(copyPath, probe.deployedText, { encoding: 'utf8' });
deployedCopy = copyPath;
} catch (e) {
copyError = e.message;
}
return done(false, {
ok: false,
code: 'deployed-changed',
pageId: flags['page-id'],
appId: flags['app-id'],
deployedCopy,
lines,
error: `the deployed page ${flags['page-id']} has changed since the base marker was recorded `
+ `(${lines.added} lines added, ${lines.removed} removed versus ${flags['code-file']}) — refusing to update, `
+ 'because this upload would discard those edits. '
+ (deployedCopy ? `A copy of the deployed page is at ${deployedCopy}. ` : '')
+ (copyError ? `The deployed copy could not be written (${copyError}). ` : '')
+ 'Merge it, or pass --overwrite-deployed to replace the deployed page deliberately.',
});
}
}
}
// Under a turn, everything this upload will do beyond the page — the app, the solution, overwriting a changed deployed page, and the placement with the title, tables
// and language it writes — is frozen HERE, after every gate and before the write, and bound with the page (lib/page-turn-intent.js turnContextOf): a retry that would
// do any of it differently is another request and is refused with nothing written. The placement below uses these values and reads nothing again. The base
// language a placement titles its entry in is read now, before the write, so a run that cannot read it writes nothing (without a turn it is read after the write).
const tableList = dataSourcesOf(clearDataSources ? [] : (flags['data-sources'] || (deployed && deployed.dataSources) || [])) || [];
const placing = addToSitemap && !!flags['app-id'];
let turnContext;
let envelopeTitle;
let envelopeLanguage;
if (turnId) {
envelopeTitle = pageName.value || (updating ? (deployed && deployed.name) : 'Generative page');
if (placing && (typeof envelopeTitle !== 'string' || !envelopeTitle.trim())) {
return done(false, { ok: false, outcome: 'nothing', turnId, error: `nothing was written: page ${flags['page-id']} has no name to title its entry in app ${flags['app-id']}'s navigation with. Pass --name.` });
}
if (placing) {
try {
envelopeLanguage = await genpage.baseLanguage();
} catch (e) {
return done(false, { ok: false, outcome: 'nothing', turnId, error: `nothing was written: the environment's base language, which the page's entry in app ${flags['app-id']}'s navigation is titled in, could not be read (${(e && e.message) || e}). Run the same command again, with the same --turn-id, once it can be read.` });
}
}
const tables = { explicit: clearDataSources || !!flags['data-sources'], value: tableList };
// A draft's placement is a draft too (see the header), so the navigation the turn binds is saved, not published: the same turn cannot later publish it.
const publication = draft ? 'draft' : 'publish';
turnContext = {
operation: updating ? 'upload-update' : 'upload-create',
appId: flags['app-id'] || null,
solution: solutionTarget ? solutionTarget.uniqueName : null,
overwriteDeployed,
navigation: !flags['app-id'] ? null : placing
? { operation: 'place', title: { explicit: pageName.value !== undefined && pageName.value !== '', value: envelopeTitle }, tables, language: { explicit: false, value: envelopeLanguage }, publication }
: { operation: 'pin-views', title: null, tables, language: null, publication },
};
}
const turnArgs = turnId ? { turnId, turnContext } : {};
// The write's answer, outside the try: a failure after it still reports the turn it was made under.
let written;
// The page is written from the bytes checked above — `checkedText` is what the Griffel check, the comparison with the deployed page and the base marker's hash were made on —
// and never from a new read of the file, which may have been saved again since. There is no copy of it to stage for a process to read: the SDK takes the text.
try {
written = updating
? await genpage.update(flags['page-id'], {
source: checkedText,
name: pageName.value || undefined,
prompt: prompt.value,
agentMessage: agentMessage.value,
// An omitted data-source list leaves the page's own; `--clear-data-sources` is the one way to empty it.
dataSources: clearDataSources ? [] : (flags['data-sources'] || undefined),
model: flags.model || undefined,
...bindings,
caller: 'upload',
appId: flags['app-id'],
sourceFile: flags['code-file'],
...draftAsk,
...turnArgs,
})
: await genpage.create({
source: checkedText,
name: pageName.value || undefined,
prompt: prompt.value,
agentMessage: agentMessage.value,
dataSources: flags['data-sources'] || undefined,
model: flags.model || undefined,
...bindings,
...draftAsk,
// A create resumed under its turn names the page its record holds; the library reads it before anything is sent.
...turnArgs,
...(turnId && flags['page-id'] ? { pageId: flags['page-id'] } : {}),
});
const pageId = (written && (written.pageId || written.id)) || null;
knownPageId = pageId || knownPageId;
const warnings = [...griffelWarnings, ...((written && written.warnings) || [])];
// The page is written (and may be published): from here on a failure is reported WITH the page's id, so the caller can go on from it.
// `pageId` is the one value a caller needs in order to continue (placing the page in an app, a follow-up edit).
const failed = (error) => ({ ok: false, pageId, ...(flags['app-id'] ? { appId: flags['app-id'] } : {}), ...(written && written.turn ? { turn: written.turn } : {}), error });
// An upload that CREATED the page, or is completing one that was created, is not finished until the page is published and — when it was asked to place it — placed.
// Until then the base marker says what is left (`unfinished`: `place`, or `publish`), and that is what lets the page be completed by naming it again (completionMarker).
const creating = !updating;
// A draft is finished when it is saved, as asked: it is never an unfinished upload that naming the page again would complete — that completion publishes (main refuses it).
const finishing = !draft && (creating || !!completing);
// What the page now is, for a sentence that goes on to say what else is or is not done.
const writtenAs = `${creating ? 'created' : 'saved'} ${draft ? 'as a draft (not published)' : 'and published'}`;
// A draft placement that is not done is made by the page CLI's own draft placement: naming the page again would be refused (it is in no navigation yet).
const draftPlaceVerb = `node scripts/genpage.js add-to-app --env ${flags.env} --app-id ${flags['app-id']} --page-id ${pageId} --draft`;
const publishFailed = !!(written && written.publish && written.publish.kind === 'failed');
const placementWanted = addToSitemap && !!flags['app-id'];
// After the write, not before it is reported: the readback inside recordObservedBase is a read like any other, and the backend is disposed only when this returns.
const unfinished = finishing ? (placementWanted ? 'place' : (publishFailed ? 'publish' : undefined)) : undefined;
const recorded = pageId ? await recordObservedBase({ genpage, flags, pageId, base, warnings, checkedText, unfinished }) : null;
if (publishFailed) {
const detail = written.publish.detail;
if (!finishing) {
return done(false, failed(`page ${pageId} was updated and saved, but publishing it failed (${detail}) — `
+ `the runtime still serves the previously published copy. Run the upload again as an update (--page-id ${pageId}) to publish it: a page that already holds the content is published, not written again.`));
}
// A page that was just created is in no app's navigation, so run as an update it would be refused for that; the marker is what lets exactly this page through.
const way = recorded
? `To finish it, run the same command again with --page-id ${pageId} added and nothing else changed: the base marker beside ${flags['code-file']} records that this upload created the page and did not finish it, so that run publishes it${placementWanted ? ' and places it in the app' : ''} without the check of the app's navigation an ordinary update makes — for this page only — and a page that already holds the content is published, not written again.`
: `The base marker that would let this tool finish it could not be written, so publish page ${pageId} in Maker, or delete it (after asking the user — it is permanent: node scripts/genpage.js delete --env ${flags.env} --page-id ${pageId} --allow-destructive) and create it again.`;
return done(false, failed(`page ${pageId} was ${creating ? 'created' : 'saved'}, but publishing it failed (${detail}) — it is not live yet. ${way}`));
}
let placement = null;
// Placing the page in an app is an edit of that app, which pac's `--add-to-sitemap` did with the upload: the page becomes a sub-area of the
// app's navigation, and the default view of each table the page reads joins the app's components so the maker's "/" picker lists it.
// An upload that names an app but no placement pins the views alone (as pac did). A failed PLACEMENT is a failure — the page is written but not
// in the app — while failing to pin views is only a warning, since the page works without them. An upload with nothing to place and no table to pin
// does not touch the app at all: placing reads it, and a read that was not needed could only fail.
const tables = clearDataSources ? [] : (flags['data-sources'] || (deployed && deployed.dataSources) || []);
// Completing a placement an earlier run began: that run may have SAVED the navigation and failed to publish the app, which leaves nothing to write and the app
// still to publish — so the backend is told to finish it: publish the app, and check that the page shows in the navigation that runs. It throws when it cannot
// (the publish failed, or the published navigation does not show the page), and the marker is cleared only after it returns.
const finishingPlacement = !!(completing && completing.unfinished === 'place' && addToSitemap && flags['app-id']);
// A run that resumes a create under a turn with NO completion marker for its page cannot tell from the marker whether an earlier run left the placement saved and
// unpublished: the marker's write may have failed, it may be gone or malformed — and it is just as absent when the earlier run never reached the placement (its whole
// answer was lost). An ordinary placement would find the page in the saved navigation, write nothing, publish nothing and report it placed. So this run asks the APP,
// as `add-to-app` does (`finishIfUnpublished`): the app is published, and proven, unless its published navigation already shows the page. The turn record only says
// which page; what is published is this request's own placement (its envelope binds the app, title, tables and publication), so the record grants nothing more.
// A draft resumed under its turn publishes nothing (its turn binds `publication: 'draft'`), so there is no publication to prove, and the backend refuses to finish a draft placement.
const provingPlacement = !!(resumesCreate && !draft && !completing && addToSitemap && flags['app-id']);
if (pageId && flags['app-id'] && (addToSitemap || (dataSourcesOf(tables) || []).length)) {
// The title of the page's entry in the navigation is written in the environment's BASE language: a title in a language the environment has not provisioned does not show, and the SDK
// writes one in its own default, 1033, when it is told nothing — an English entry in a German-only environment, reported as a success. `genpage.js add-to-app` reads the language for the
// same reason, with the same operation, and so does the run that completes a placement an earlier one began: it is this placement again. An unreadable language fails the placement,
// nothing guessed in its place — as it fails `add-to-app`: the page is written and the marker says what is left. A pin has no title to write, so an upload that places no page asks the
// environment nothing.
let languageCode;
if (addToSitemap) {
try {
languageCode = turnContext ? envelopeLanguage : await genpage.baseLanguage();
} catch (e) {
// The READ failed, not the placement: the app was never read or touched, so the only thing known of its navigation is that this run changed nothing in it, and that is all this says. "NOT added … add
// the page in Maker" would be false for a page an earlier run already put in the app's saved navigation (its publish is what failed) or even in the published one, and it would send the user to add
// a page the navigation holds. Only the run that completes a placement an earlier one began (`finishingPlacement`) can be in that case, so only it says it did not look at what that run left; a page
// this run created is in no navigation, and one created with no app begins its first placement here. `saved` is not set: it claims a saved navigation, which on a first attempt does not exist.
// The marker was written before this (`unfinished: 'place'`), so the same command completes the placement once the language can be read. The read's own words
// (`HTTP 503 from …/organizations?$select=languagecode: …`) do not say what was being read or why it matters here.
const left = finishingPlacement ? ', and did not look at what an earlier run saved or published there, so the page may already be in it' : '';
const verb = `node scripts/genpage.js add-to-app --env ${flags.env} --app-id ${flags['app-id']} --page-id ${pageId}`;
const way = draft
? `Once the language can be read, ${draftPlaceVerb} places it as a draft.`
: recorded
? `To finish it, run the same command again with --page-id ${pageId} added and nothing else changed, once the language can be read: the page is already published, so only its placement is made, for this page only (the base marker beside ${flags['code-file']} records that it is not done). Or run ${verb}, which places the page, or finishes a placement that was saved, whichever is left.`
: `Once the language can be read, ${verb} places the page, or finishes a placement that was saved, whichever is left.`;
return done(false, failed(`page ${pageId} was ${writtenAs}, but its placement in app ${flags['app-id']}'s navigation is unfinished: the environment's base language, which the entry's title is written in, could not be read (${(e && e.message) || e}); no other language is assumed in its place. This run changed nothing in the app's navigation${left}. ${way}`));
}
}
try {
placement = await genpage.placeInApp(flags['app-id'], {
pages: addToSitemap ? [{ pageId, title: turnContext ? envelopeTitle : (written && written.name) || pageName.value || (deployed && deployed.name) }] : [],
dataSources: turnContext ? tableList : tables,
...(languageCode === undefined ? {} : { languageCode }),
...(finishingPlacement ? { finish: true } : {}),
// Spelled out, not through draftAsk: genpage-sdk-draft.test.js scans every placeInApp call for a draft, to keep the build's own placement free of one.
...(draft ? { publish: false } : {}),
...(provingPlacement ? { finishIfUnpublished: true } : {}),
});
for (const w of placement.warnings || []) warnings.push(w);
// A resumed create's placement is finished only on proof that the app that RUNS shows the page. `placeInApp` publishes the app and then reads its published
// navigation; when that read fails it does not throw — it answers `provedPublished: false` with a warning (the publish was accepted and may yet take effect),
// which is enough for an ordinary completion but not for a turn, whose retry trusts that a finished answer is final. So under a resumed create that answer is an
// unfinished placement: coded, the marker this run wrote left at `unfinished: 'place'`, and the same command again reads the published navigation once more.
// An answer with no `provedPublished` at all is the backend's no-write return: it READ the published navigation and found the page there, which is the proof.
if (resumesCreate && (finishingPlacement || provingPlacement) && placement.provedPublished === false) {
return done(false, {
...failed(`page ${pageId} was ${writtenAs}, and it is placed in app ${flags['app-id']}'s saved navigation, and the app was published, but its published navigation could not be read back to prove that the page shows in it, so the placement is not reported finished. To finish it, run the same command again with the same turn id and nothing else changed: the app's published navigation is read again, and the app is published again and checked unless it already shows the page.`),
code: 'GENPAGE_APP_PUBLISH_UNPROVEN',
...(warnings.length ? { warnings } : {}),
});
}
} catch (e) {
if (addToSitemap) {
// Under a turn the failure carries the backend's code (`GENPAGE_APP_PUBLISH_FAILED`, `GENPAGE_APP_PUBLISH_UNPROVEN`, …), so a host that retries the turn can tell an
// unfinished placement from a finished one without reading the words. Without a turn the answer is as it was.
const coded = turnContext && e && typeof e.code === 'string' ? { code: e.code } : {};
// A placement the backend SAVED and could not publish, or whose publish it could not see in what runs (`saved: true`), did not fail: the page is in the app's saved navigation, and the app's publish
// is what is left. "NOT added … add the page in Maker" sent the user to add a page the navigation already holds.
if (e && e.saved === true) {
// Under a turn with no marker written, the same command still finishes it: the resumed create asks the app (`provingPlacement`) instead of the marker.
const finish = recorded && finishing
? ` To finish it, run the same command again with --page-id ${pageId} added and nothing else changed: the page is already published and placed, so only the app is published again and checked (the base marker beside ${flags['code-file']} records that it is not done).`
: turnContext && finishing
? ` To finish it, run the same command again with the same turn id and nothing else changed: the app's published navigation is read, and the app is published again and checked unless it already shows the page.`
: '';
return done(false, { ...failed(`page ${pageId} was ${writtenAs}, and it is placed in app ${flags['app-id']}'s saved navigation, but the app's publish is not finished: ${(e && e.message) || e}.${finish}`), ...coded });
}
const again = recorded && finishing
? `, or run the same command again with --page-id ${pageId} added and nothing else changed: the page is already published, so only its placement is made, for this page only (the base marker beside ${flags['code-file']} records that it is not done)`
: '';
return done(false, { ...failed(`page ${pageId} was ${writtenAs}, but NOT added to app ${flags['app-id']}'s navigation: ${(e && e.message) || e}. `
+ `Add the page to the app navigation with node scripts/genpage.js add-to-app --env ${flags.env} --app-id ${flags['app-id']} --page-id ${pageId}${draft ? ' --draft' : ''}${again}.`), ...coded });
}
warnings.push(`the page was written, but its tables' default views could not be added to app ${flags['app-id']}'s components, so the maker's "/" picker may not list them (${(e && e.message) || e})`);
}
}
// Published, and placed where it was asked to be: the page is finished, and the marker stops saying it is not.
if (recorded && recorded.unfinished) {
try { base.writeMarker(flags['code-file'], { ...recorded, unfinished: false }); } catch (e) {
warnings.push(`could not clear the base marker's unfinished state (${(e && e.message) || e}) — running the same command with --page-id ${pageId} completes the page again, which changes nothing`);
}
}
// The solution the page joins, last: the page is written, published and placed by now, so an add that fails leaves a page that works and is not in the
// solution. That is a failure, said with the one command that adds the page — never a second create, which would make a second page.
let solutionFacts = {};
if (solutionTarget) {
const where = solutionTarget.uniqueName;
try {
const joined = await genpage.addToSolution(pageId, solutionTarget);
solutionFacts = { solution: where, addedToSolution: !!(joined && joined.added), ...(solutionTarget.systemSolution ? { systemSolution: true } : {}) };
} catch (e) {
const placedText = placement && placement.added && placement.added.length ? ` and placed in app ${flags['app-id']}'s navigation` : '';
return done(false, {
...failed(`page ${pageId} was ${writtenAs}${placedText}, but it was NOT added to solution ${where}: ${(e && e.message) || e}. `
+ `Do not create it again: add it with node scripts/add-page-to-solution.js ${flags.env} ${where} --page-ids ${pageId} (a page the solution already holds is accepted, so running it again changes nothing).`),
code: 'solution-add-failed',
solution: where,
addedToSolution: false,
...(warnings.length ? { warnings } : {}),
});
}
}
return done(true, {
ok: true,
pageId,
...(flags['app-id'] ? { appId: flags['app-id'] } : {}),
...(overwroteDeployed ? { overwroteDeployed: true } : {}),
updated: updating,
...(written && written.unchanged ? { unchanged: true } : {}),
...(placement && placement.added && placement.added.length ? { addedToSitemap: true } : {}),
// A draft's answer is the backend's, as it gave it (see the header): nothing here reads `draftFingerprint` as a publish, and no `fingerprint` is made from it.
// `pendingPublish` is the page's, or true when the page saved nothing new and the placement did save something that waits for a publish.
...(draft ? {
draft: true,
publish: written && written.publish,
saved: !!(written && written.saved),
pendingPublish: placement && placement.pendingPublish === true ? true : (written ? written.pendingPublish : null),
draftFingerprint: written && written.draftFingerprint,
...(placement && placement.placementState ? { placementState: placement.placementState } : {}),
} : {}),
...solutionFacts,
...(written && written.turn ? { turn: written.turn } : {}),
...(warnings.length ? { warnings } : {}),
});
} catch (e) {
// Everything the backend knows about a failed write goes out with it: the page's id (an unknown outcome has one to look for), whether anything was written, and the SDK's own code.
// The page's id is the one known BEFORE the try or learned in it — a failure after the write cannot drop it.
const id = (e && e.pageId) || knownPageId;
return done(false, {
error: e && e.message ? e.message : String(e),
...(id ? { pageId: id, ...(flags['app-id'] ? { appId: flags['app-id'] } : {}) } : {}),
...(e && e.outcome ? { outcome: e.outcome } : {}),
...(e && typeof e.code === 'string' && e.code ? { code: e.code } : {}),
...(written && written.turn ? { turn: written.turn } : turnId ? { turnId } : {}),
});
}
}
if (require.main === module) {
main().catch((e) => {
makeWritesBlocking(process.stdout, process.stderr);
process.stderr.write(`${e && e.message ? e.message : e}\n`);
process.exit(1);
});
}
module.exports = { main, resolveText, notPlainFile, uploadCompletion };