@firstdraft.com/cli 0.1.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,150 @@
1
+ import { publishPlan } from "./plan-publish.js";
2
+ import {
3
+ PlanPushNetworkError,
4
+ PlanPushProtocolError,
5
+ pushPlan,
6
+ } from "./plan-push.js";
7
+ import { PlanStatusChangedError, readPlanStatus } from "./plan-status.js";
8
+
9
+ export class PlanCompilePushRejectedError extends Error {
10
+ /**
11
+ * @param {{status: number, responseKind: "diagnostics" | "problem" | null, body: Record<string, unknown> | null}} result
12
+ */
13
+ constructor(result) {
14
+ super("First Draft rejected the current Foundation Plan.");
15
+ this.result = result;
16
+ }
17
+ }
18
+
19
+ export class PlanCompileAnalysisRejectedError extends Error {
20
+ /**
21
+ * @param {{status: number, responseKind: "problem" | null, body: Record<string, unknown> | null}} result
22
+ */
23
+ constructor(result) {
24
+ super("First Draft rejected the analysis status request.");
25
+ this.result = result;
26
+ }
27
+ }
28
+
29
+ export class PlanCompileAnalysisNotValidError extends Error {
30
+ /** @param {import("./plan-status.js").AnalysisResponse} current */
31
+ constructor(current) {
32
+ super("The current Foundation Plan analysis is not valid.");
33
+ this.current = current;
34
+ }
35
+ }
36
+
37
+ export class PlanCompileAnalysisUnavailableError extends Error {
38
+ /** @param {number | undefined} status */
39
+ constructor(status) {
40
+ super("The current analysis status is unavailable.");
41
+ this.status = status;
42
+ }
43
+ }
44
+
45
+ export class PlanCompileAnalysisInvalidError extends Error {
46
+ /** @param {number} status */
47
+ constructor(status) {
48
+ super("The current analysis status response is invalid.");
49
+ this.status = status;
50
+ }
51
+ }
52
+
53
+ /**
54
+ * @typedef {object} CompilePlanOptions
55
+ * @property {string} cwd
56
+ * @property {string} [apiUrl]
57
+ * @property {typeof globalThis.fetch} [fetchFunction]
58
+ * @property {import("./plan-push.js").PlanPushFileSystem} [fileSystem]
59
+ * @property {() => string} [createTemporaryId]
60
+ * @property {(timeoutMs?: number) => AbortSignal} [createRequestSignal]
61
+ * @property {(delayMs: number) => Promise<void>} [analysisSleep]
62
+ * @property {() => number} [analysisNow]
63
+ * @property {(delayMs: number) => Promise<void>} [publicationSleep]
64
+ * @property {() => number} [publicationNow]
65
+ * @property {typeof pushPlan} [push]
66
+ * @property {typeof readPlanStatus} [readStatus]
67
+ * @property {typeof publishPlan} [publish]
68
+ */
69
+
70
+ /**
71
+ * Submit and analyze the exact current local Plan before invoking the internal
72
+ * GitHub Publication lifecycle. The Publication function performs the final
73
+ * local-byte check immediately before its conditional mutation.
74
+ *
75
+ * @param {CompilePlanOptions} options
76
+ */
77
+ export async function compilePlan({
78
+ cwd,
79
+ apiUrl,
80
+ fetchFunction,
81
+ fileSystem,
82
+ createTemporaryId,
83
+ createRequestSignal,
84
+ analysisSleep,
85
+ analysisNow,
86
+ publicationSleep,
87
+ publicationNow,
88
+ push = pushPlan,
89
+ readStatus = readPlanStatus,
90
+ publish = publishPlan,
91
+ }) {
92
+ const pushed = await push({
93
+ cwd,
94
+ apiUrl,
95
+ fetchFunction,
96
+ fileSystem,
97
+ createTemporaryId,
98
+ createRequestSignal,
99
+ });
100
+ if (!("outcome" in pushed)) {
101
+ throw new PlanCompilePushRejectedError(pushed);
102
+ }
103
+ const acceptedGraphVersion = /** @type {{graph_version: number}} */ (
104
+ pushed.body.project
105
+ ).graph_version;
106
+
107
+ let status;
108
+ try {
109
+ status = await readStatus({
110
+ cwd,
111
+ wait: true,
112
+ expectedGraphVersion: acceptedGraphVersion,
113
+ fetchFunction,
114
+ fileSystem,
115
+ createRequestSignal,
116
+ sleep: analysisSleep,
117
+ now: analysisNow,
118
+ });
119
+ } catch (error) {
120
+ if (error instanceof PlanPushNetworkError) {
121
+ throw new PlanCompileAnalysisUnavailableError(error.status);
122
+ }
123
+ if (error instanceof PlanPushProtocolError) {
124
+ throw new PlanCompileAnalysisInvalidError(error.status);
125
+ }
126
+ throw error;
127
+ }
128
+ if ("responseKind" in status) {
129
+ throw new PlanCompileAnalysisRejectedError(status);
130
+ }
131
+ if (
132
+ status.body.project.graph_version !== acceptedGraphVersion ||
133
+ status.body.analysis.graph_version !== acceptedGraphVersion
134
+ ) {
135
+ throw new PlanStatusChangedError(status.body);
136
+ }
137
+ if (status.body.analysis.status !== "valid") {
138
+ throw new PlanCompileAnalysisNotValidError(status.body);
139
+ }
140
+
141
+ return publish({
142
+ cwd,
143
+ fetchFunction,
144
+ fileSystem,
145
+ createRequestSignal,
146
+ sleep: publicationSleep,
147
+ now: publicationNow,
148
+ expectedEtag: pushed.etag,
149
+ });
150
+ }
@@ -0,0 +1,79 @@
1
+ import { mkdirSync, writeFileSync } from "node:fs";
2
+ import path from "node:path";
3
+
4
+ import { FOUNDATION_PLAN_FORMAT } from "../compilation-artifact.js";
5
+
6
+ /**
7
+ * @typedef {object} FileSystem
8
+ * @property {typeof mkdirSync} mkdirSync
9
+ * @property {typeof writeFileSync} writeFileSync
10
+ */
11
+
12
+ /** @type {FileSystem} */
13
+ const DEFAULT_FILE_SYSTEM = { mkdirSync, writeFileSync };
14
+
15
+ /**
16
+ * @typedef {object} InitializePlanOptions
17
+ * @property {string} applicationKey
18
+ * @property {string} name
19
+ * @property {string} projectId
20
+ * @property {string} cwd
21
+ * @property {FileSystem} [fileSystem]
22
+ */
23
+
24
+ /** @param {InitializePlanOptions} options */
25
+ export function initializePlan({
26
+ applicationKey,
27
+ name,
28
+ projectId,
29
+ cwd,
30
+ fileSystem = DEFAULT_FILE_SYSTEM,
31
+ }) {
32
+ const plan = `${JSON.stringify(emptyPlan(applicationKey, name), null, 2)}\n`;
33
+ const state = `${JSON.stringify(projectState(projectId), null, 2)}\n`;
34
+ const directory = path.join(cwd, ".firstdraft");
35
+ const writeOptions = { flag: "wx", mode: 0o600, flush: true };
36
+
37
+ fileSystem.mkdirSync(directory, { recursive: false, mode: 0o700 });
38
+ fileSystem.writeFileSync(
39
+ path.join(directory, ".gitignore"),
40
+ "*\n",
41
+ writeOptions,
42
+ );
43
+ fileSystem.writeFileSync(
44
+ path.join(directory, "foundation-plan.json"),
45
+ plan,
46
+ writeOptions,
47
+ );
48
+ fileSystem.writeFileSync(
49
+ path.join(directory, "state.json"),
50
+ state,
51
+ writeOptions,
52
+ );
53
+ }
54
+
55
+ /** @param {string} applicationKey @param {string} name */
56
+ function emptyPlan(applicationKey, name) {
57
+ return {
58
+ format: FOUNDATION_PLAN_FORMAT,
59
+ target: {
60
+ id: "rails",
61
+ profile: "rails-sketch/2026-08",
62
+ },
63
+ application: {
64
+ key: applicationKey,
65
+ name,
66
+ native: {},
67
+ delivery: {},
68
+ entities: [],
69
+ },
70
+ };
71
+ }
72
+
73
+ /** @param {string} projectId */
74
+ function projectState(projectId) {
75
+ return {
76
+ format: "firstdraft.cli-state/1",
77
+ project_id: projectId,
78
+ };
79
+ }