MCPcopy
hub / github.com/npmx-dev/npmx.dev / syncFile

Function syncFile

modules/standard-site-sync.ts:191–254  ·  view source on GitHub ↗
(
  filePath: string,
  siteUrl: string,
  identifier: AtIdentifierString,
  pdsPublicClient: Client,
)

Source from the content-addressed store, hash-verified

189 * WARN: DOES NOT CATCH ERRORS, THIS MUST BE HANDLED
190 */
191const syncFile = async (
192 filePath: string,
193 siteUrl: string,
194 identifier: AtIdentifierString,
195 pdsPublicClient: Client,
196) => {
197 const { data: frontmatter } = read(filePath)
198
199 const normalizedFrontmatter = normalizeBlogFrontmatter(frontmatter)
200 // formats dates to ISO string for records
201 if (normalizedFrontmatter['date']) {
202 const rawDate = normalizedFrontmatter['date']
203 normalizedFrontmatter.date = new Date(
204 rawDate instanceof Date ? rawDate : String(rawDate),
205 ).toISOString()
206 }
207
208 const result = safeParse(BlogPostSchema, normalizedFrontmatter)
209 if (!result.success) {
210 console.warn(`[standard-site-sync] Validation failed for ${filePath}`, result.issues)
211 return
212 }
213
214 const data = result.output
215
216 // filter drafts
217 if (data.draft) {
218 if (process.env.DEBUG === 'true') {
219 console.debug(`[standard-site-sync] Skipping draft: ${data.path}`)
220 }
221 return
222 }
223
224 const hash = createContentHash(data)
225
226 if (syncedDocuments.get(data.path) === hash) {
227 return
228 }
229
230 const tid = generateBlogTID(data.date, data.slug)
231
232 let checkForBlogResult = await pdsPublicClient.xrpcSafe(com.atproto.repo.getRecord, {
233 params: {
234 rkey: tid,
235 repo: identifier,
236 collection: site.standard.document.$nsid,
237 },
238 })
239
240 if (checkForBlogResult.success) {
241 console.log(`[standard-site-sync]: ${data.title} is already synced`)
242 // Every thing is synced can now return
243 return
244 }
245 if (checkForBlogResult instanceof XrpcResponseError) {
246 //Means it's not been uploaded and we can do that now
247 if (checkForBlogResult.error === 'RecordNotFound') {
248 const document = buildATProtoDocument(siteUrl, data)

Callers 1

setupFunction · 0.85

Calls 7

generateBlogTIDFunction · 0.90
readFunction · 0.85
normalizeBlogFrontmatterFunction · 0.85
safeParseFunction · 0.85
createContentHashFunction · 0.85
buildATProtoDocumentFunction · 0.85
getMethod · 0.65

Tested by

no test coverage detected