@firstdraft.com/cli 0.2.2 → 0.4.0

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.
@@ -23,7 +23,7 @@ import path from "node:path";
23
23
  import { isFileSystemError } from "./file-system.js";
24
24
 
25
25
  export const ROOT_TRANSACTION_NAME = ".firstdraft-root-output";
26
- export const ROOT_DESIGN_NAME = "design";
26
+ export const ROOT_DESIGN_PATH = ".firstdraft/design";
27
27
 
28
28
  const JOURNAL_FORMAT = "firstdraft.root-output-transaction/1";
29
29
  const JOURNAL_NAME = "journal.json";
@@ -37,10 +37,7 @@ const PRIVATE_DIRECTORY_MODE = 0o700;
37
37
  const PRIVATE_FILE_MODE = 0o600;
38
38
  const MAX_GIT_OUTPUT_BYTES = 128 * 1024 * 1024;
39
39
  const GIT_HASH_PATTERN = /^[0-9a-f]{40,64}$/;
40
- const RESERVED_NAMES = new Set([
41
- ROOT_DESIGN_NAME.toLowerCase(),
42
- ROOT_TRANSACTION_NAME.toLowerCase(),
43
- ]);
40
+ const RESERVED_NAMES = new Set([ROOT_TRANSACTION_NAME.toLowerCase()]);
44
41
 
45
42
  export class RootOutputPathError extends Error {
46
43
  /** @param {string} message @param {string} reason @param {{cause?: unknown}} [options] */
@@ -270,7 +267,7 @@ export function releaseRootOutput(target) {
270
267
  * @param {string} artifact.manifest_sha256
271
268
  * @param {object} callbacks
272
269
  * @param {(root: string) => void} callbacks.writeArtifact
273
- * @param {(root: string, ignoredRootEntries?: Set<string>) => void} callbacks.verifyArtifact
270
+ * @param {(root: string, ignoredPaths?: Set<string>) => void} callbacks.verifyArtifact
274
271
  * @param {(from: string, to: string) => void} [callbacks.rename]
275
272
  */
276
273
  export function materializeRootOutput(
@@ -310,7 +307,7 @@ export function materializeRootOutput(
310
307
  design_path:
311
308
  movedEntries.length === 0
312
309
  ? null
313
- : path.join(target.path, ROOT_DESIGN_NAME),
310
+ : path.join(target.path, ROOT_DESIGN_PATH),
314
311
  moved_entry_count: movedEntries.length,
315
312
  git_repository_preserved: target.git !== null,
316
313
  git_index_replaced: target.git !== null,
@@ -344,12 +341,6 @@ function assertReservedPathsAvailable(root) {
344
341
  const names = readdirSync(root);
345
342
  for (const name of names) {
346
343
  const folded = name.toLowerCase();
347
- if (folded === ROOT_DESIGN_NAME.toLowerCase()) {
348
- throw new RootOutputPathError(
349
- "The root output design path is reserved.",
350
- "root_reserved_path",
351
- );
352
- }
353
344
  if (folded !== ROOT_TRANSACTION_NAME.toLowerCase()) continue;
354
345
  if (name === ROOT_TRANSACTION_NAME) throw existingTransactionError(root);
355
346
  throw new RootOutputPathError(
@@ -357,6 +348,20 @@ function assertReservedPathsAvailable(root) {
357
348
  "root_reserved_path",
358
349
  );
359
350
  }
351
+ for (const name of names) assertDesignPathAvailable(root, name);
352
+ }
353
+
354
+ /** @param {string} root @param {string} name */
355
+ function assertDesignPathAvailable(root, name) {
356
+ if (name.toLowerCase() !== ".firstdraft") return;
357
+ const entry = path.join(root, name);
358
+ if (!lstatSync(entry).isDirectory()) return;
359
+ if (readdirSync(entry).some((child) => child.toLowerCase() === "design")) {
360
+ throw new RootOutputPathError(
361
+ "The root output design path is reserved.",
362
+ "root_reserved_path",
363
+ );
364
+ }
360
365
  }
361
366
 
362
367
  /** @param {string} root */
@@ -388,6 +393,7 @@ function captureRootSnapshot(root, rootDevice) {
388
393
  const names = readdirSync(root).sort(compareStrings);
389
394
  for (const name of names) {
390
395
  if (name === ROOT_TRANSACTION_NAME) continue;
396
+ assertDesignPathAvailable(root, name);
391
397
  if (RESERVED_NAMES.has(name.toLowerCase())) {
392
398
  throw new RootOutputPathError(
393
399
  "A reserved root output path appeared during preflight.",
@@ -549,7 +555,14 @@ function removeCompletedTransaction(target) {
549
555
  function assertArtifactRootNames(files) {
550
556
  for (const file of files) {
551
557
  const top = file.path.split("/", 1)[0] ?? "";
552
- if (RESERVED_NAMES.has(top.toLowerCase())) {
558
+ const folded = file.path.toLowerCase();
559
+ if (
560
+ RESERVED_NAMES.has(top.toLowerCase()) ||
561
+ folded === ".firstdraft" ||
562
+ (top.toLowerCase() === ".firstdraft" && top !== ".firstdraft") ||
563
+ folded === ROOT_DESIGN_PATH ||
564
+ folded.startsWith(`${ROOT_DESIGN_PATH}/`)
565
+ ) {
553
566
  throw new RootOutputMaterializationError(
554
567
  "The compilation artifact owns a reserved root path.",
555
568
  "root_artifact_collision",
@@ -567,10 +580,15 @@ function performRootTransaction(target, files, rename) {
567
580
  const designEntries = target.snapshot.filter(
568
581
  (entry) => entry.name !== ".git",
569
582
  );
570
- const designPath = path.join(target.path, ROOT_DESIGN_NAME);
571
583
  const artifactStage = path.join(target.transactionPath, ARTIFACT_STAGE_NAME);
584
+ // Keep the old .firstdraft inside the staged archive until the generated
585
+ // .firstdraft and its archive can be installed together at the root.
586
+ const designPath = path.join(artifactStage, ROOT_DESIGN_PATH);
572
587
  const artifactTopNames = [
573
- ...new Set(files.map((file) => file.path.split("/", 1)[0] ?? "")),
588
+ ...new Set([
589
+ ...files.map((file) => file.path.split("/", 1)[0] ?? ""),
590
+ ...(designEntries.length > 0 ? [".firstdraft"] : []),
591
+ ]),
574
592
  ].sort(compareStrings);
575
593
 
576
594
  if (designEntries.length > 0) {
@@ -578,6 +596,11 @@ function performRootTransaction(target, files, rename) {
578
596
  kind: "create_design",
579
597
  path: designPath,
580
598
  });
599
+ mkdirSync(path.dirname(designPath), {
600
+ recursive: true,
601
+ mode: DIRECTORY_MODE,
602
+ });
603
+ chmodSync(path.dirname(designPath), DIRECTORY_MODE);
581
604
  mkdirSync(designPath, { mode: DIRECTORY_MODE });
582
605
  target.journal.design_created = true;
583
606
  chmodSync(designPath, DIRECTORY_MODE);
@@ -644,7 +667,7 @@ function finishIrreversibleStep(target, phase) {
644
667
  /**
645
668
  * @param {RootOutputTarget} target
646
669
  * @param {Array<{path: string}>} files
647
- * @param {(root: string, ignoredRootEntries?: Set<string>) => void} verifyArtifact
670
+ * @param {(root: string, ignoredPaths?: Set<string>) => void} verifyArtifact
648
671
  */
649
672
  function verifyRootResult(target, files, verifyArtifact) {
650
673
  const ignored = new Set([ROOT_TRANSACTION_NAME]);
@@ -652,11 +675,11 @@ function verifyRootResult(target, files, verifyArtifact) {
652
675
  ignored.add(".git");
653
676
  }
654
677
  if (target.snapshot.some((entry) => entry.name !== ".git")) {
655
- ignored.add(ROOT_DESIGN_NAME);
678
+ ignored.add(ROOT_DESIGN_PATH);
656
679
  }
657
680
  verifyArtifact(target.path, ignored);
658
681
 
659
- const designPath = path.join(target.path, ROOT_DESIGN_NAME);
682
+ const designPath = path.join(target.path, ROOT_DESIGN_PATH);
660
683
  for (const identity of target.snapshot) {
661
684
  const currentPath =
662
685
  identity.name === ".git"
@@ -698,7 +721,7 @@ function verifyRootResult(target, files, verifyArtifact) {
698
721
  const expectedTopNames = new Set(
699
722
  files.map((file) => file.path.split("/", 1)[0] ?? ""),
700
723
  );
701
- for (const name of ignored) expectedTopNames.add(name);
724
+ for (const name of ignored) expectedTopNames.add(name.split("/", 1)[0] ?? "");
702
725
  const actualTopNames = new Set(readdirSync(target.path));
703
726
  if (!setsEqual(expectedTopNames, actualTopNames)) {
704
727
  throw new RootOutputMaterializationError(
@@ -750,7 +773,11 @@ function rollbackRootTransaction(target, rename) {
750
773
 
751
774
  if (target.journal.design_created) {
752
775
  try {
753
- const designPath = path.join(target.path, ROOT_DESIGN_NAME);
776
+ const designPath = path.join(
777
+ target.transactionPath,
778
+ ARTIFACT_STAGE_NAME,
779
+ ROOT_DESIGN_PATH,
780
+ );
754
781
  if (readdirSync(designPath).length === 0) {
755
782
  rmdirSync(designPath);
756
783
  } else {
@@ -1019,7 +1046,7 @@ function assertIgnoreProtection(target) {
1019
1046
  }
1020
1047
 
1021
1048
  const expected = target.git.ignoredPaths.map(
1022
- (candidate) => `${ROOT_DESIGN_NAME}/${candidate}`,
1049
+ (candidate) => `${ROOT_DESIGN_PATH}/${candidate}`,
1023
1050
  );
1024
1051
  const result = invokeGit(
1025
1052
  target.path,
@@ -1066,7 +1093,7 @@ function copyApplicableIgnoreFiles(root, previewWorktree, ignoredPath) {
1066
1093
 
1067
1094
  const destination = path.join(
1068
1095
  previewWorktree,
1069
- ROOT_DESIGN_NAME,
1096
+ ROOT_DESIGN_PATH,
1070
1097
  ...ancestor,
1071
1098
  ".gitignore",
1072
1099
  );
@@ -1084,7 +1111,7 @@ function createPreviewEntry(root, previewWorktree, ignoredPath) {
1084
1111
  const source = path.join(root, ...ignoredPath.split("/"));
1085
1112
  const destination = path.join(
1086
1113
  previewWorktree,
1087
- ROOT_DESIGN_NAME,
1114
+ ROOT_DESIGN_PATH,
1088
1115
  ...ignoredPath.split("/"),
1089
1116
  );
1090
1117
  let stat;
@@ -1123,7 +1150,7 @@ function prepareGitIndex(target, files) {
1123
1150
  for (const entry of git.indexEntries) {
1124
1151
  indexRecords.push(
1125
1152
  Buffer.from(
1126
- `${entry.mode} ${entry.object} 0\t${ROOT_DESIGN_NAME}/${entry.path}\0`,
1153
+ `${entry.mode} ${entry.object} 0\t${ROOT_DESIGN_PATH}/${entry.path}\0`,
1127
1154
  ),
1128
1155
  );
1129
1156
  }
@@ -1317,7 +1344,7 @@ function rollbackIndex(target) {
1317
1344
  function assertMovedIgnoreProtection(target) {
1318
1345
  if (target.git === null || target.git.ignoredPaths.length === 0) return;
1319
1346
  const expected = target.git.ignoredPaths.map(
1320
- (candidate) => `${ROOT_DESIGN_NAME}/${candidate}`,
1347
+ (candidate) => `${ROOT_DESIGN_PATH}/${candidate}`,
1321
1348
  );
1322
1349
  const result = invokeGit(
1323
1350
  target.path,