Skip to content

Commit

Permalink
feat: add index file mirroring
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingloh committed Feb 2, 2024
1 parent 66daa51 commit 071cf52
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions packages/crypto-frontmatter/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`]];
Expand All @@ -14,9 +14,9 @@ export class MirrorCommand extends Command {
return this.include?.includes(type) ?? true;
}

private async mirrorFile(namespace: FrontmatterNamespace, file: string): Promise<void> {
private async mirrorFile(namespace: FrontmatterNamespace, file: string, toPath: string = file): Promise<void> {
const from = getNodeModulesPath(namespace.caip2, namespace.namespace, file);
const to = join(this.target, file);
const to = join(this.target, toPath);
await copyFile(from, to);
}

Expand All @@ -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');
Expand Down
16 changes: 13 additions & 3 deletions packages/crypto-frontmatter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ function hasFile(filepath: string): Promise<boolean> {
}

/**
* 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) {
Expand All @@ -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();
Expand Down Expand Up @@ -124,6 +131,8 @@ export async function getInstalledNamespaces(): Promise<FrontmatterNamespace[]>

/**
* 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[]}
Expand All @@ -148,13 +157,14 @@ export async function getIndex(caip2: string, namespace: string): Promise<Frontm
/**
* Get FrontmatterContent using CAIP-19, returns undefined if not found
* This includes the HTML content of the Frontmatter.
* Using readFile (node_modules/@crypto-frontmatter/{caip2}/_{namespace}/{fileId}.json)
*
* @param caip19 {string}
* @see FrontmatterContent.html
* @return {FrontmatterContent | undefined}
*/
export async function getFrontmatter(caip19: string): Promise<FrontmatterContent | undefined> {
const fileId = computeFileId(caip19);
const fileId = getFileId(caip19);
const [caip2, namespace] = decodeCaip19(caip19);

const path = getNodeModulesPath(caip2, namespace, `${fileId}.json`);
Expand Down
4 changes: 2 additions & 2 deletions website/app/[caip2]/[slug]/AssetPage.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -8,7 +8,7 @@ import { ContentedProse } from '@/components/contented/ContentedProse';
import { renderHighlighterHtml } from '@/components/contented/ShikiHighlighter';

async function fetchFrontmatter(caip19: string): Promise<FrontmatterContent> {
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();
Expand Down

0 comments on commit 071cf52

Please sign in to comment.