forked from
stevedylan.dev/sequoia
A CLI for publishing standard.site documents to ATProto
1import * as fs from "node:fs/promises";
2import * as path from "node:path";
3import type {
4 PublisherConfig,
5 PublisherState,
6 FrontmatterMapping,
7 BlueskyConfig,
8} from "./types";
9
10const CONFIG_FILENAME = "sequoia.json";
11const STATE_FILENAME = ".sequoia-state.json";
12
13async function fileExists(filePath: string): Promise<boolean> {
14 try {
15 await fs.access(filePath);
16 return true;
17 } catch {
18 return false;
19 }
20}
21
22export async function findConfig(
23 startDir: string = process.cwd(),
24): Promise<string | null> {
25 let currentDir = startDir;
26
27 while (true) {
28 const configPath = path.join(currentDir, CONFIG_FILENAME);
29
30 if (await fileExists(configPath)) {
31 return configPath;
32 }
33
34 const parentDir = path.dirname(currentDir);
35 if (parentDir === currentDir) {
36 // Reached root
37 return null;
38 }
39 currentDir = parentDir;
40 }
41}
42
43export async function loadConfig(
44 configPath?: string,
45): Promise<PublisherConfig> {
46 const resolvedPath = configPath || (await findConfig());
47
48 if (!resolvedPath) {
49 throw new Error(
50 `Could not find ${CONFIG_FILENAME}. Run 'sequoia init' to create one.`,
51 );
52 }
53
54 try {
55 const content = await fs.readFile(resolvedPath, "utf-8");
56 const config = JSON.parse(content) as PublisherConfig;
57
58 // Validate required fields
59 if (!config.siteUrl) throw new Error("siteUrl is required in config");
60 if (!config.contentDir) throw new Error("contentDir is required in config");
61 if (!config.publicationUri)
62 throw new Error("publicationUri is required in config");
63
64 return config;
65 } catch (error) {
66 if (error instanceof Error && error.message.includes("required")) {
67 throw error;
68 }
69 throw new Error(`Failed to load config from ${resolvedPath}: ${error}`);
70 }
71}
72
73export function generateConfigTemplate(options: {
74 siteUrl: string;
75 contentDir: string;
76 imagesDir?: string;
77 publicDir?: string;
78 outputDir?: string;
79 pathPrefix?: string;
80 publicationUri: string;
81 pdsUrl?: string;
82 frontmatter?: FrontmatterMapping;
83 ignore?: string[];
84 removeIndexFromSlug?: boolean;
85 stripDatePrefix?: boolean;
86 pathTemplate?: string;
87 textContentField?: string;
88 bluesky?: BlueskyConfig;
89}): string {
90 const config: Record<string, unknown> = {
91 $schema:
92 "https://tangled.org/stevedylan.dev/sequoia/raw/main/sequoia.schema.json",
93 siteUrl: options.siteUrl,
94 contentDir: options.contentDir,
95 };
96
97 if (options.imagesDir) {
98 config.imagesDir = options.imagesDir;
99 }
100
101 if (options.publicDir && options.publicDir !== "./public") {
102 config.publicDir = options.publicDir;
103 }
104
105 if (options.outputDir) {
106 config.outputDir = options.outputDir;
107 }
108
109 if (options.pathPrefix && options.pathPrefix !== "/posts") {
110 config.pathPrefix = options.pathPrefix;
111 }
112
113 config.publicationUri = options.publicationUri;
114
115 if (options.pdsUrl && options.pdsUrl !== "https://bsky.social") {
116 config.pdsUrl = options.pdsUrl;
117 }
118
119 if (options.frontmatter && Object.keys(options.frontmatter).length > 0) {
120 config.frontmatter = options.frontmatter;
121 }
122
123 if (options.ignore && options.ignore.length > 0) {
124 config.ignore = options.ignore;
125 }
126
127 if (options.removeIndexFromSlug) {
128 config.removeIndexFromSlug = options.removeIndexFromSlug;
129 }
130
131 if (options.stripDatePrefix) {
132 config.stripDatePrefix = options.stripDatePrefix;
133 }
134
135 if (options.pathTemplate) {
136 config.pathTemplate = options.pathTemplate;
137 }
138
139 if (options.textContentField) {
140 config.textContentField = options.textContentField;
141 }
142 if (options.bluesky) {
143 config.bluesky = options.bluesky;
144 }
145
146 return JSON.stringify(config, null, 2);
147}
148
149export async function loadState(configDir: string): Promise<PublisherState> {
150 const statePath = path.join(configDir, STATE_FILENAME);
151
152 if (!(await fileExists(statePath))) {
153 return { posts: {} };
154 }
155
156 try {
157 const content = await fs.readFile(statePath, "utf-8");
158 return JSON.parse(content) as PublisherState;
159 } catch {
160 return { posts: {} };
161 }
162}
163
164export async function saveState(
165 configDir: string,
166 state: PublisherState,
167): Promise<void> {
168 const statePath = path.join(configDir, STATE_FILENAME);
169 await fs.writeFile(statePath, JSON.stringify(state, null, 2));
170}
171
172export function getStatePath(configDir: string): string {
173 return path.join(configDir, STATE_FILENAME);
174}