126 lines
4.5 KiB
TypeScript
126 lines
4.5 KiB
TypeScript
import useDatabase from "~/composables/useDatabase";
|
|
import { extname, basename } from 'node:path';
|
|
import type { FileType } from '~/types/content';
|
|
import type { CanvasColor, CanvasContent } from "~/types/canvas";
|
|
import { explorerContentTable } from "~/db/schema";
|
|
import { convertToStorableLinks } from "../api/file.post";
|
|
import { parsePath } from "~/shared/general.util";
|
|
|
|
const typeMapping: Record<string, FileType> = {
|
|
".md": "markdown",
|
|
".canvas": "canvas"
|
|
};
|
|
|
|
export default defineTask({
|
|
meta: {
|
|
name: 'pull',
|
|
description: 'Pull the data from Git',
|
|
},
|
|
async run(event) {
|
|
try {
|
|
//@ts-ignore
|
|
const tree = await $fetch('https://git.peaceultime.com/api/v1/repos/peaceultime/system-aspect/git/trees/master', {
|
|
method: 'get',
|
|
headers: {
|
|
accept: 'application/json',
|
|
},
|
|
params: {
|
|
recursive: true,
|
|
per_page: 1000,
|
|
}
|
|
}) as { tree: any[] } & Record<string, any>;
|
|
|
|
const files: typeof explorerContentTable.$inferInsert[] = await Promise.all(tree.tree.filter((e: any) => !e.path.startsWith(".")).map(async (e, i) => {
|
|
if(e.type === 'tree')
|
|
{
|
|
const title = basename(e.path);
|
|
const order = /(\d+)\. ?(.+)/gsmi.exec(title);
|
|
return {
|
|
path: e.path,
|
|
order: i,
|
|
title: title,
|
|
type: 'folder',
|
|
content: null,
|
|
owner: 1,
|
|
navigable: true,
|
|
private: e.path === '98. Privé',
|
|
timestamp: new Date(),
|
|
}
|
|
}
|
|
|
|
const extension = extname(e.path);
|
|
const title = basename(e.path, extension);
|
|
const order = /(\d+)\. ?(.+)/gsmi.exec(title);
|
|
const content = (await $fetch(`https://git.peaceultime.com/api/v1/repos/peaceultime/system-aspect/raw/${encodeURIComponent(e.path)}`));
|
|
|
|
return {
|
|
path: extension === '.md' ? e.path.replace(extension, '') : e.path,
|
|
order: i,
|
|
title: title,
|
|
type: (typeMapping[extension] ?? 'file'),
|
|
content: reshapeContent(content as string, typeMapping[extension] ?? 'File'),
|
|
owner: 1,
|
|
navigable: true,
|
|
private: e.path === '98. Privé',
|
|
timestamp: new Date(),
|
|
}
|
|
}));
|
|
|
|
files.forEach(e => {
|
|
const content = reshapeLinks(e.content as string | null, files) ?? null;
|
|
e.content = content ? Buffer.from(content, 'utf-8') : null;
|
|
});
|
|
|
|
const db = useDatabase();
|
|
db.delete(explorerContentTable).run();
|
|
db.insert(explorerContentTable).values(files).run();
|
|
|
|
return { result: true };
|
|
}
|
|
catch(e)
|
|
{
|
|
console.error(e);
|
|
|
|
return { result: false, error: e };
|
|
}
|
|
},
|
|
})
|
|
function reshapeLinks(content: string | null, all: typeof explorerContentTable.$inferInsert[])
|
|
{
|
|
return content?.replace(/!?\[\[([^\[\]\|\#]+)?(#+[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/g, (str, link, header, title) => {
|
|
return `[[${link ? all.find(e => e.path.endsWith(link))?.path ?? link : ''}${header ?? ''}${title ?? ''}]]`;
|
|
});
|
|
}
|
|
|
|
function reshapeContent(content: string, type: FileType): string | null
|
|
{
|
|
switch(type)
|
|
{
|
|
case "markdown":
|
|
case "file":
|
|
return content;
|
|
case "canvas":
|
|
const data = JSON.parse(content) as CanvasContent;
|
|
data.edges?.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined);
|
|
data.nodes?.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined);
|
|
return JSON.stringify(data);
|
|
default:
|
|
case 'folder':
|
|
return null;
|
|
}
|
|
}
|
|
function getColor(color: string): CanvasColor
|
|
{
|
|
const colors: Record<string, string> = {
|
|
'1': 'red',
|
|
'2': 'orange',
|
|
'3': 'yellow',
|
|
'4': 'green',
|
|
'5': 'cyan',
|
|
'6': 'purple',
|
|
};
|
|
if(colors.hasOwnProperty(color))
|
|
return { class: colors[color] };
|
|
else
|
|
return { hex: color };
|
|
} |