@firstdraft.com/cli 0.2.1 → 0.2.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.
@@ -1,6 +1,9 @@
1
1
  import { publishPlan } from "./plan-publish.js";
2
2
  import { compileAndDownload } from "./compilation.js";
3
- import { resolveOutputTarget } from "../compilation-artifact.js";
3
+ import {
4
+ prepareCompilationOutputTarget,
5
+ releaseCompilationOutputTarget,
6
+ } from "../compilation-artifact.js";
4
7
  import {
5
8
  PlanPushNetworkError,
6
9
  PlanPushProtocolError,
@@ -130,8 +133,9 @@ export async function compilePlan({
130
133
 
131
134
  /**
132
135
  * Submit and analyze the exact current local Plan, then start one direct
133
- * Compilation and materialize its verified artifact into an absent directory.
134
- * GitHub Publication remains the no-output mode owned by compilePlan.
136
+ * Compilation and materialize its verified artifact into an absent directory
137
+ * or the eligible current directory. GitHub Publication remains the no-output
138
+ * mode owned by compilePlan.
135
139
  *
136
140
  * @param {CompilePlanToDirectoryOptions} options
137
141
  */
@@ -152,45 +156,46 @@ export async function compilePlanToDirectory({
152
156
  readStatus = readPlanStatus,
153
157
  compile = compileAndDownload,
154
158
  }) {
155
- // Reject an unavailable destination before the Plan push can mutate remote
156
- // state. The Compilation boundary checks it again after Analysis in case the
157
- // filesystem changes while this command is waiting.
158
- resolveOutputTarget({ cwd, output });
159
-
160
- const prepared = await preparePlan({
161
- cwd,
162
- apiUrl,
163
- fetchFunction,
164
- fileSystem,
165
- createTemporaryId,
166
- createRequestSignal,
167
- analysisSleep,
168
- analysisNow,
169
- onProgress,
170
- push,
171
- readStatus,
172
- });
173
- const body = prepared.status.body;
159
+ const outputTarget = prepareCompilationOutputTarget({ cwd, output });
160
+ try {
161
+ const prepared = await preparePlan({
162
+ cwd,
163
+ apiUrl,
164
+ fetchFunction,
165
+ fileSystem,
166
+ createTemporaryId,
167
+ createRequestSignal,
168
+ analysisSleep,
169
+ analysisNow,
170
+ onProgress,
171
+ push,
172
+ readStatus,
173
+ });
174
+ const body = prepared.status.body;
174
175
 
175
- return compile({
176
- cwd,
177
- expectedEtag: prepared.pushed.etag,
178
- expected: {
179
- projectId: body.project.id,
180
- graphVersion: body.project.graph_version,
181
- headSourceSha256: body.analysis.head_source_sha256,
182
- analysisRunId: body.analysis.id,
183
- compilerRelease: body.analysis.compiler_release,
184
- target: body.analysis.target,
185
- },
186
- output,
187
- fetchFunction,
188
- fileSystem,
189
- createRequestSignal,
190
- sleep: compilationSleep,
191
- now: compilationNow,
192
- onProgress,
193
- });
176
+ return await compile({
177
+ cwd,
178
+ expectedEtag: prepared.pushed.etag,
179
+ expected: {
180
+ projectId: body.project.id,
181
+ graphVersion: body.project.graph_version,
182
+ headSourceSha256: body.analysis.head_source_sha256,
183
+ analysisRunId: body.analysis.id,
184
+ compilerRelease: body.analysis.compiler_release,
185
+ target: body.analysis.target,
186
+ },
187
+ output,
188
+ outputTarget,
189
+ fetchFunction,
190
+ fileSystem,
191
+ createRequestSignal,
192
+ sleep: compilationSleep,
193
+ now: compilationNow,
194
+ onProgress,
195
+ });
196
+ } finally {
197
+ releaseCompilationOutputTarget(outputTarget);
198
+ }
194
199
  }
195
200
 
196
201
  /**
@@ -14,6 +14,14 @@ import path from "node:path";
14
14
 
15
15
  import { isFileSystemError } from "./file-system.js";
16
16
  import { isUuidV7 } from "./plan-state.js";
17
+ import {
18
+ materializeRootOutput,
19
+ prepareRootOutput,
20
+ releaseRootOutput,
21
+ resolveRootOutputPath,
22
+ RootOutputMaterializationError,
23
+ RootOutputPathError,
24
+ } from "./root-output.js";
17
25
 
18
26
  export const ARTIFACT_MEDIA_TYPE =
19
27
  "application/vnd.firstdraft.compilation-artifact+json";
@@ -61,8 +69,21 @@ const DIRECTORY_MODE = 0o755;
61
69
  const POSIX_MODE_BITS_SUPPORTED = process.platform !== "win32";
62
70
 
63
71
  export class CompilationArtifactInvalidError extends Error {}
64
- export class CompilationMaterializationError extends Error {}
65
- export class CompilationOutputPathError extends Error {}
72
+ export class CompilationMaterializationError extends Error {
73
+ /** @param {string} message @param {{cause?: unknown, reason?: string, recoveryPath?: string}} [options] */
74
+ constructor(message, options = {}) {
75
+ super(message, options);
76
+ this.reason = options.reason;
77
+ this.recoveryPath = options.recoveryPath;
78
+ }
79
+ }
80
+ export class CompilationOutputPathError extends Error {
81
+ /** @param {string} message @param {{cause?: unknown, reason?: string}} [options] */
82
+ constructor(message, options = {}) {
83
+ super(message, options);
84
+ this.reason = options.reason;
85
+ }
86
+ }
66
87
 
67
88
  /**
68
89
  * @typedef {object} ValidatedCompilationProvenance
@@ -171,13 +192,15 @@ export function resolveOutputTarget({ cwd, output }) {
171
192
 
172
193
  const target = path.resolve(cwd, output);
173
194
  const parent = path.dirname(target);
195
+ const targetIsAbsent = isInitiallyAbsent(target);
174
196
  if (
175
197
  target === parent ||
176
198
  path.basename(target).length === 0 ||
177
- !isInitiallyAbsent(target)
199
+ !targetIsAbsent
178
200
  ) {
179
201
  throw new CompilationOutputPathError(
180
202
  "The compilation output path must be absent.",
203
+ targetIsAbsent ? undefined : { reason: "destination_exists" },
181
204
  );
182
205
  }
183
206
 
@@ -201,11 +224,80 @@ export function resolveOutputTarget({ cwd, output }) {
201
224
  return target;
202
225
  }
203
226
 
227
+ /**
228
+ * Validate an ordinary absent output or reserve root adoption before requests.
229
+ *
230
+ * @param {object} options
231
+ * @param {string} options.cwd
232
+ * @param {string} options.output
233
+ */
234
+ export function prepareCompilationOutputTarget({ cwd, output }) {
235
+ if (
236
+ typeof output !== "string" ||
237
+ output.length === 0 ||
238
+ output.includes("\0")
239
+ ) {
240
+ throw new CompilationOutputPathError(
241
+ "The compilation output path is invalid.",
242
+ );
243
+ }
244
+
245
+ const root = resolveRootOutputPath({ cwd, output });
246
+ if (root === null) return resolveOutputTarget({ cwd, output });
247
+
248
+ try {
249
+ return prepareRootOutput({ root });
250
+ } catch (error) {
251
+ if (!(error instanceof RootOutputPathError)) throw error;
252
+ throw new CompilationOutputPathError(error.message, {
253
+ cause: error,
254
+ reason: error.reason,
255
+ });
256
+ }
257
+ }
258
+
259
+ /** @param {string | import("./root-output.js").RootOutputTarget} target */
260
+ export function releaseCompilationOutputTarget(target) {
261
+ if (typeof target === "string") return;
262
+ try {
263
+ releaseRootOutput(target);
264
+ } catch (error) {
265
+ if (!(error instanceof RootOutputMaterializationError)) throw error;
266
+ throw new CompilationMaterializationError(error.message, {
267
+ cause: error,
268
+ reason: error.reason,
269
+ recoveryPath: error.recoveryPath,
270
+ });
271
+ }
272
+ }
273
+
204
274
  /**
205
275
  * @param {ValidatedCompilationArtifact} artifact
206
- * @param {string} target
276
+ * @param {string | import("./root-output.js").RootOutputTarget} target
207
277
  */
208
278
  export function materializeCompilationArtifact(artifact, target) {
279
+ if (typeof target !== "string") {
280
+ try {
281
+ return materializeRootOutput(target, artifact, {
282
+ writeArtifact: (root) => {
283
+ writeArtifactTree(artifact.files, root);
284
+ applyMode(root, DIRECTORY_MODE);
285
+ },
286
+ verifyArtifact: (root, ignoredRootEntries) =>
287
+ verifyArtifactTree(artifact.files, root, ignoredRootEntries, {
288
+ verifyRootMode: ignoredRootEntries === undefined,
289
+ }),
290
+ });
291
+ } catch (error) {
292
+ if (!(error instanceof RootOutputMaterializationError)) throw error;
293
+ throw new CompilationMaterializationError(error.message, {
294
+ cause: error,
295
+ reason: error.reason,
296
+ recoveryPath: error.recoveryPath,
297
+ });
298
+ }
299
+ }
300
+
209
301
  const parent = path.dirname(target);
210
302
  const prefix = path.join(parent, `.firstdraft-${path.basename(target)}-`);
211
303
  let temporaryDirectory = null;
@@ -222,10 +314,8 @@ export function materializeCompilationArtifact(artifact, target) {
222
314
  renameSync(temporaryDirectory, target);
223
315
  temporaryDirectory = null;
224
316
  } catch (error) {
225
- if (
226
- error instanceof CompilationMaterializationError ||
227
- isFileSystemError(error)
228
- ) {
317
+ if (error instanceof CompilationMaterializationError) throw error;
318
+ if (isFileSystemError(error)) {
229
319
  throw new CompilationMaterializationError(
230
320
  "The compilation artifact could not be materialized.",
231
321
  { cause: error },
@@ -502,8 +592,18 @@ function writeArtifactTree(files, root) {
502
592
  }
503
593
  }
504
594
 
505
- /** @param {ValidatedArtifactFile[]} files @param {string} root */
506
- function verifyArtifactTree(files, root) {
595
+ /**
596
+ * @param {ValidatedArtifactFile[]} files
597
+ * @param {string} root
598
+ * @param {Set<string>} [ignoredRootEntries]
599
+ * @param {{verifyRootMode?: boolean}} [options]
600
+ */
601
+ function verifyArtifactTree(
602
+ files,
603
+ root,
604
+ ignoredRootEntries = new Set(),
605
+ { verifyRootMode = true } = {},
606
+ ) {
507
607
  const expectedFiles = new Map(files.map((file) => [file.path, file]));
508
608
  const expectedDirectories = new Set();
509
609
  for (const file of files) {
@@ -519,13 +619,13 @@ function verifyArtifactTree(files, root) {
519
619
  if (
520
620
  !rootStat.isDirectory() ||
521
621
  rootStat.isSymbolicLink() ||
522
- !hasExpectedMode(rootStat.mode, DIRECTORY_MODE)
622
+ (verifyRootMode && !hasExpectedMode(rootStat.mode, DIRECTORY_MODE))
523
623
  ) {
524
624
  throw new CompilationMaterializationError(
525
625
  "The materialized compilation root is invalid.",
526
626
  );
527
627
  }
528
- walkTree(root, "", actualFiles, actualDirectories);
628
+ walkTree(root, "", actualFiles, actualDirectories, ignoredRootEntries);
529
629
  if (
530
630
  !setsEqual(actualFiles, new Set(expectedFiles.keys())) ||
531
631
  !setsEqual(actualDirectories, expectedDirectories)
@@ -581,16 +681,18 @@ function hasExpectedMode(actual, expected) {
581
681
  * @param {string} relative
582
682
  * @param {Set<string>} files
583
683
  * @param {Set<string>} directories
684
+ * @param {Set<string>} ignoredRootEntries
584
685
  */
585
- function walkTree(root, relative, files, directories) {
686
+ function walkTree(root, relative, files, directories, ignoredRootEntries) {
586
687
  const directory = relative ? path.join(root, ...relative.split("/")) : root;
587
688
  const entries = readdirSync(directory, { withFileTypes: true });
588
689
 
589
690
  for (const entry of entries) {
691
+ if (relative === "" && ignoredRootEntries.has(entry.name)) continue;
590
692
  const entryRelative = relative ? `${relative}/${entry.name}` : entry.name;
591
693
  if (entry.isDirectory()) {
592
694
  directories.add(entryRelative);
593
- walkTree(root, entryRelative, files, directories);
695
+ walkTree(root, entryRelative, files, directories, ignoredRootEntries);
594
696
  } else if (entry.isFile()) {
595
697
  files.add(entryRelative);
596
698
  } else {