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

Function setup

modules/standard-site-sync.ts:38–102  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

36export default defineNuxtModule({
37 meta: { name: 'standard-site-sync' },
38 async setup() {
39 const nuxt = useNuxt()
40 const { resolve } = createResolver(import.meta.url)
41 const contentDir = resolve('../app/pages/blog')
42
43 const config = getPDSConfig()
44 if (!config) return
45
46 const { pdsUrl, handle, password } = config
47
48 // Skip auth during prepare phase (nuxt prepare, nuxt generate --prepare, etc)
49 if (nuxt.options._prepare) return
50
51 const pdsPublicClient = new Client({ service: pdsUrl })
52 // If set we have a publication record to create
53 const possiblePublication = await checkPublication(handle, pdsPublicClient)
54
55 nuxt.hook('build:before', async () => {
56 const files = await Array.fromAsync(glob(join(contentDir, '**/*.md')))
57
58 // INFO: Arbitrarily chosen concurrency limit, can be changed if needed
59 const concurrencyLimit = 5
60 let documentsToSync: DocumentToSync[] = []
61 for (let i = 0; i < files.length; i += concurrencyLimit) {
62 const batch = files.slice(i, i + concurrencyLimit)
63 // Process files in parallel
64 let results = await Promise.all(
65 batch.map(file =>
66 syncFile(file, NPMX_SITE, handle, pdsPublicClient).catch(error =>
67 console.error(`[standard-site-sync] Error in ${file}:` + error),
68 ),
69 ),
70 )
71 // Filter out docs not needed to sync
72 documentsToSync.push(...results.filter(r => r !== undefined))
73 }
74
75 if (possiblePublication || documentsToSync.length > 0) {
76 try {
77 const session = await PasswordSession.login({
78 service: pdsUrl,
79 identifier: handle,
80 password: password,
81 })
82 const authenticatedClient = new Client(session)
83 if (possiblePublication) {
84 await authenticatedClient.create(
85 site.standard.publication,
86 possiblePublication.record,
87 {
88 rkey: possiblePublication.tid,
89 },
90 )
91 // Wait for the firehose and indexers to catch up if we create a publication
92 await new Promise(sleepResolve => setTimeout(sleepResolve, 2_000))
93 }
94 if (documentsToSync.length > 0) {
95 await syncsiteStandardDocuments(authenticatedClient, documentsToSync)

Callers

nothing calls this directly

Calls 5

createResolverFunction · 0.85
getPDSConfigFunction · 0.85
checkPublicationFunction · 0.85
syncFileFunction · 0.85

Tested by

no test coverage detected