Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions deno.lock

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

26 changes: 22 additions & 4 deletions src/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CLIWatcher } from "./CLIWatcher.ts";
import { comMojangFolder, previewComMojangFolder } from "./comMojangFolder.ts";
import { Dash, isMatch } from "./deps.ts";
import { DenoFileSystem } from "./FileSystem.ts";
import { getLocalData, saveLocalData, tryInvalidateLocalData } from "./LocalCache.ts";
import { FileTypeImpl, PackTypeImpl } from "./McProjectCore.ts";

interface IDashOptions {
Expand All @@ -25,13 +26,28 @@ export class CLI {
mode,
verbose: true,

requestJsonData: (dataPath: string) =>
fetch(
requestJsonData: async (dataPath: string) => {
const cached = await getLocalData(dataPath);

if (cached) {
try {
return JSON.parse(cached);
} catch {
// empty
}
}

const data = await fetch(
dataPath.replace(
"data/",
"https://raw.githubusercontent.com/bridge-core/editor-packages/main/",
),
).then((resp) => resp.json()),
).then((resp) => resp.json());

saveLocalData(dataPath, JSON.stringify(data));

return data;
},
});

await dash.setup();
Expand Down Expand Up @@ -64,13 +80,15 @@ export class CLI {
async build(options: IDashOptions) {
this.verifyOptions(options);

await tryInvalidateLocalData();
const dash = await this.createDashService(options);
await dash.build();
}
async watch(options: IDashOptions) {
this.verifyOptions(options);
const dash = await this.createDashService(options);

await tryInvalidateLocalData();
const dash = await this.createDashService(options);
await dash.build();
await new CLIWatcher(dash).watch(options.reload);
}
Expand Down
71 changes: 71 additions & 0 deletions src/LocalCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { fs, path } from "./deps.ts";

export async function tryInvalidateLocalData() {
const localDataPath = await getLocalDataPath();

if (!localDataPath) return;

try {
let time = 0;

const timestampFilePath = path.join(localDataPath, ".timestamp");

if (await fs.exists(timestampFilePath)) {
const lastUpdatedTimestamp = await Deno.readTextFile(timestampFilePath);

time = parseInt(lastUpdatedTimestamp);
}

const now = Date.now();

if (now - time > 1000 * 60 * 60 * 24) {
console.log("Invalidating local cache of remote data!");

await fs.emptyDir(localDataPath);
}
} catch {
// empty
}
}

export async function getLocalDataPath(): Promise<string | undefined> {
const userDir = Deno.env.get("HOME") || Deno.env.get("USERPROFILE");

if (!userDir) return undefined;

const appDataPath = path.join(userDir, ".dash");

await fs.ensureDir(appDataPath);

return appDataPath;
}

export async function saveLocalData(filePath: string, content: string) {
const localDataPath = await getLocalDataPath();

if (!localDataPath) return;

const fullPath = path.join(localDataPath, filePath);

await fs.ensureDir(path.dirname(fullPath));

await Deno.writeTextFile(fullPath, content);

const timestampFilePath = path.join(localDataPath, ".timestamp");

if (!await fs.exists(timestampFilePath)) await Deno.writeTextFile(timestampFilePath, Date.now().toString());
}

export async function getLocalData(filePath: string): Promise<string | undefined> {
const localDataPath = await getLocalDataPath();

if (!localDataPath) return;

const fullPath = path.join(localDataPath, filePath);

try {
return await Deno.readTextFile(fullPath);
} catch {
return;
}
}
30 changes: 30 additions & 0 deletions src/McProjectCore.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
import { FileType, IFileType, PackType } from "./deps.ts";
import { getLocalData, saveLocalData } from "./LocalCache.ts";

export class PackTypeImpl extends PackType<void> {
async setup() {
const cached = await getLocalData("packDefinitions.json");

if (cached) {
try {
this.packTypes = JSON.parse(cached);

return;
} catch {
// empty
}
}

this.packTypes = await fetch(
"https://raw.githubusercontent.com/bridge-core/editor-packages/main/packages/minecraftBedrock/packDefinitions.json",
).then((resp) => resp.json());

saveLocalData("packDefinitions.json", JSON.stringify(this.packTypes));
}
}
export class FileTypeImpl extends FileType<void> {
protected _cache = new Map<string, IFileType | null>();

async setup() {
this._cache.clear();

const cached = await getLocalData("fileDefinitions.json");

if (cached) {
try {
this.fileTypes = JSON.parse(cached);

return;
} catch {
// empty
}
}

this.fileTypes = await fetch(
"https://raw.githubusercontent.com/bridge-core/editor-packages/main/dist/minecraftBedrock/fileDefinitions.json",
).then((resp) => resp.json());

saveLocalData("fileDefinitions.json", JSON.stringify(this.fileTypes));
}

override addPluginFileType(fileDef: IFileType) {
Expand Down
1 change: 1 addition & 0 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * as path from "jsr:@std/path";
export * as fs from "https://deno.land/std/fs/mod.ts";
export {
FileType,
type IFileType,
Expand Down