From 071cf52db4ee9ccd369cec5ac138dc5cb82c0bb1 Mon Sep 17 00:00:00 2001 From: Fuxing Loh Date: Fri, 2 Feb 2024 13:22:45 +0800 Subject: [PATCH] feat: add index file mirroring --- .../{frontmatter.iml => crypto-frontmatter.iml} | 0 .idea/modules.xml | 2 +- packages/crypto-frontmatter/cli.ts | 12 +++++++++--- packages/crypto-frontmatter/index.ts | 16 +++++++++++++--- website/app/[caip2]/[slug]/AssetPage.tsx | 4 ++-- 5 files changed, 25 insertions(+), 9 deletions(-) rename .idea/{frontmatter.iml => crypto-frontmatter.iml} (100%) diff --git a/.idea/frontmatter.iml b/.idea/crypto-frontmatter.iml similarity index 100% rename from .idea/frontmatter.iml rename to .idea/crypto-frontmatter.iml diff --git a/.idea/modules.xml b/.idea/modules.xml index de7bb0d7c..654a3cdfb 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/packages/crypto-frontmatter/cli.ts b/packages/crypto-frontmatter/cli.ts index 0e647130e..6fdc29acc 100644 --- a/packages/crypto-frontmatter/cli.ts +++ b/packages/crypto-frontmatter/cli.ts @@ -3,7 +3,7 @@ import { join } from 'node:path'; import { Command, Option, runExit } from 'clipanion'; -import { FrontmatterNamespace, getIndex, getInstalledNamespaces, getNodeModulesPath } from './index'; +import { FrontmatterNamespace, getIndex, getIndexPath, getInstalledNamespaces, getNodeModulesPath } from './index'; export class MirrorCommand extends Command { static override paths = [[`mirror`]]; @@ -14,9 +14,9 @@ export class MirrorCommand extends Command { return this.include?.includes(type) ?? true; } - private async mirrorFile(namespace: FrontmatterNamespace, file: string): Promise { + private async mirrorFile(namespace: FrontmatterNamespace, file: string, toPath: string = file): Promise { const from = getNodeModulesPath(namespace.caip2, namespace.namespace, file); - const to = join(this.target, file); + const to = join(this.target, toPath); await copyFile(from, to); } @@ -26,6 +26,12 @@ export class MirrorCommand extends Command { for (const namespace of await getInstalledNamespaces()) { let count = 0; + if (this.includes('index.json')) { + const toPath = getIndexPath(namespace.caip2, namespace.namespace); + await this.mirrorFile(namespace, 'index.json', toPath); + count++; + } + for (const frontmatter of (await getIndex(namespace.caip2, namespace.namespace))!) { if (this.includes('frontmatter.json')) { await this.mirrorFile(namespace, frontmatter.fileId + '.json'); diff --git a/packages/crypto-frontmatter/index.ts b/packages/crypto-frontmatter/index.ts index 23fe186f2..f0e587085 100644 --- a/packages/crypto-frontmatter/index.ts +++ b/packages/crypto-frontmatter/index.ts @@ -72,10 +72,10 @@ function hasFile(filepath: string): Promise { } /** - * Encode CAIP-19 into fileId + * Convert CAIP-19 into fileId deterministically. * If Namespace is ERC20, the address field will be converted to lowercase. */ -export function computeFileId(caip19: string): string { +export function getFileId(caip19: string): string { const [caip2, namespace, reference] = decodeCaip19(caip19); if (namespace === 'erc20' && reference) { @@ -84,6 +84,13 @@ export function computeFileId(caip19: string): string { return sha256(caip19); } +/** + * Get the index path for a given CAIP-2 and Namespace + */ +export function getIndexPath(caip2: string, namespace: string): string { + return `index.${sha256(`${caip2}/${namespace}`)}.json`; +} + export function getNodeModulesPath(caip2: string, namespace: string, file: string) { const [caip2Type, caip2Reference] = caip2.split(':'); const packageName = `${caip2Type}-${caip2Reference}`.toLowerCase(); @@ -124,6 +131,8 @@ export async function getInstalledNamespaces(): Promise /** * Get the collection FrontmatterIndex with CAIP-2 and Asset TYPE + * Using readFile (node_modules/@crypto-frontmatter/{caip2}/_{namespace}/index.json) + * * @param caip2 {string} * @param namespace {string} * @return {FrontmatterIndex[]} @@ -148,13 +157,14 @@ export async function getIndex(caip2: string, namespace: string): Promise { - const fileId = computeFileId(caip19); + const fileId = getFileId(caip19); const [caip2, namespace] = decodeCaip19(caip19); const path = getNodeModulesPath(caip2, namespace, `${fileId}.json`); diff --git a/website/app/[caip2]/[slug]/AssetPage.tsx b/website/app/[caip2]/[slug]/AssetPage.tsx index 0ba56ed5c..c2ec26fa7 100644 --- a/website/app/[caip2]/[slug]/AssetPage.tsx +++ b/website/app/[caip2]/[slug]/AssetPage.tsx @@ -1,4 +1,4 @@ -import { computeFileId, FrontmatterContent } from 'crypto-frontmatter'; +import { FrontmatterContent, getFileId } from 'crypto-frontmatter'; import { Metadata } from 'next'; import Image from 'next/image'; import { notFound } from 'next/navigation'; @@ -8,7 +8,7 @@ import { ContentedProse } from '@/components/contented/ContentedProse'; import { renderHighlighterHtml } from '@/components/contented/ShikiHighlighter'; async function fetchFrontmatter(caip19: string): Promise { - const fileId = computeFileId(caip19); + const fileId = getFileId(caip19); const response = await fetch(`${process.env.BASE_URL}/_crypto-frontmatter/${fileId}.json`); if (!response.ok) { return notFound();