Skip to content

Commit

Permalink
fetch more files
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Sep 10, 2024
1 parent 3065fe1 commit af7b0b9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@ export function Editor({
</Button>
</RightContainer>
</PanelHeader>
<MarkdownContainer isHidden={fileName !== "README"}>
<MarkdownContainer isHidden={!fileName.endsWith(".md")}>
<ReactMarkdown linkTarget="_blank">
{fileName === "README" ? fileCode : ""}
{fileName.endsWith(".md") ? fileCode : ""}
</ReactMarkdown>
</MarkdownContainer>
<EditorContainer isHidden={fileName === "README"}>
<EditorContainer isHidden={fileName.endsWith(".md")}>
<MonacoEditor
defaultLanguage={"motoko"}
value={fileName === "README" ? "" : fileCode}
value={fileName.endsWith(".md") ? "" : fileCode}
path={fileName}
beforeMount={configureMonaco}
onMount={onEditorMount}
Expand Down
8 changes: 4 additions & 4 deletions src/components/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { PackageModal } from "./PackageModal";
import { FileModal } from "./FileModal";
import { CanisterModal } from "./CanisterModal";
import { DeploySetter } from "./DeplayModal";
import { DeploySetter } from "./DeployModal";
import { PackageInfo } from "../workers/file";
import { ILoggingStore } from "./Logger";
import { deleteCanister } from "../build";
Expand Down Expand Up @@ -99,7 +99,7 @@ export function Explorer({ state, ttl, logger, deploySetter }: ExplorerProps) {
if (action === "delete") {
const canisterInfo = state.canisters[selectedCanister];
logger.log(
`Deleting canister ${selectedCanister} with id ${canisterInfo.id.toText()}...`
`Deleting canister ${selectedCanister} with id ${canisterInfo.id.toText()}...`,
);
await deleteCanister(canisterInfo);
logger.log("Canister deleted");
Expand Down Expand Up @@ -132,7 +132,7 @@ export function Explorer({ state, ttl, logger, deploySetter }: ExplorerProps) {
}
const timer = setTimeout(() => {
const times: Array<[string, number | undefined]> = Object.values(
state.canisters
state.canisters,
).map((info) => {
return [info.name!, calcTimeLeft(info.timestamp)];
});
Expand Down Expand Up @@ -162,7 +162,7 @@ export function Explorer({ state, ttl, logger, deploySetter }: ExplorerProps) {
} else {
return { status: "Expired" };
}
})
}),
);
}, 1000);
// Clear timeout if the component is unmounted
Expand Down
3 changes: 3 additions & 0 deletions src/contexts/WorkplaceState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ function selectFirstFile(files: Record<string, string>): string | null {
if ("README" in files) {
return "README";
}
if ("README.md" in files) {
return "README.md";
}
if ("Main.mo" in files) {
return "Main.mo";
}
Expand Down
6 changes: 2 additions & 4 deletions src/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export const exampleProjects: ExampleProject[] = [
},
{
name: "Counter",
repo: { dir: "motoko/counter/src", ...example },
readme: `${readmeURL}/counter/README.md`,
repo: { dir: "motoko/minimal-counter-dapp", ...example },
},
{
name: "Calculator",
Expand All @@ -46,8 +45,7 @@ export const exampleProjects: ExampleProject[] = [
},
{
name: "Random Maze",
repo: { dir: "motoko/random_maze/src/random_maze", ...example },
readme: `${readmeURL}/random_maze/README.md`,
repo: { dir: "motoko/random_maze", ...example },
},
{
name: "Game of Life",
Expand Down
45 changes: 13 additions & 32 deletions src/workers/file.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import { loadMoc } from "./mocShim";

interface ExtraFile {
match: RegExp;
resolveName(results: RegExpExecArray): string;
}

const extraFiles: ExtraFile[] = [
{
match: /^readme\.md$/i,
resolveName: () => "README",
},
];

export interface PackageInfo {
name: string;
repo: string;
Expand Down Expand Up @@ -50,7 +38,7 @@ export async function fetchPackage(info: PackageInfo): Promise<boolean> {

export async function fetchGithub(
repo: RepoInfo,
target_dir = ""
target_dir = "",
): Promise<Record<string, string> | undefined> {
const possiblyCDN = !(
(repo.branch.length % 2 === 0 && /^[A-F0-9]+$/i.test(repo.branch)) ||
Expand All @@ -74,9 +62,15 @@ export async function saveWorkplaceToMotoko(files: Record<string, string>) {
}
}

const isValidFile = (path: string) => {
const validFiles =
/\.(mo|md|js|ts|json|txt|png|jpg|jpeg|gif|svg|ico|css|html|tsx|jsx)$/;
return validFiles.test(path) && !path.includes("/declarations/");
};

async function fetchFromCDN(
repo: RepoInfo,
target_dir = ""
target_dir = "",
): Promise<Record<string, string> | undefined> {
const Motoko = await loadMoc();
const meta_url = `https://data.jsdelivr.com/v1/package/gh/${repo.repo}@${repo.branch}/flat`;
Expand All @@ -89,18 +83,10 @@ async function fetchFromCDN(
const promises: any[] = [];
const files = {};
for (const f of json.files) {
const extraFileName = extraFiles.flatMap(({ match, resolveName }) => {
const results = match.exec(f.path);
return results ? [resolveName(results)] : [];
})[0];
if (
f.name.startsWith(`/${repo.dir}/`) &&
(extraFileName || /\.mo$/i.test(f.name))
) {
if (f.name.startsWith(`/${repo.dir}/`) && isValidFile(f.name)) {
const promise = (async () => {
const content = await (await fetch(base_url + f.name)).text();
const stripped =
extraFileName ||
target_dir + f.name.slice(repo.dir ? repo.dir.length + 1 : 0);
Motoko.saveFile(stripped, content);
files[stripped] = content;
Expand All @@ -118,7 +104,7 @@ async function fetchFromCDN(

async function fetchFromGithub(
repo: RepoInfo,
target_dir = ""
target_dir = "",
): Promise<Record<string, string> | undefined> {
const Motoko = await loadMoc();
const meta_url = `https://api.github.com/repos/${repo.repo}/git/trees/${repo.branch}?recursive=1`;
Expand All @@ -131,22 +117,17 @@ async function fetchFromGithub(
const promises: any[] = [];
const files = {};
for (const f of json.tree) {
const extraFileName = extraFiles.flatMap(({ match, resolveName }) => {
const results = match.exec(f.path);
return results ? [resolveName(results)] : [];
})[0];
if (
f.path.startsWith(repo.dir ? `${repo.dir}/` : "") &&
f.type === "blob" &&
(extraFileName || /\.mo$/i.test(f.path))
isValidFile(f.path)
) {
const promise = (async () => {
const content = await (await fetch(base_url + f.path)).text();
const stripped =
extraFileName ||
target_dir +
(target_dir ? "/" : "") +
f.path.slice(repo.dir ? repo.dir.length + 1 : 0);
(target_dir ? "/" : "") +
f.path.slice(repo.dir ? repo.dir.length + 1 : 0);
Motoko.saveFile(stripped, content);
files[stripped] = content;
})();
Expand Down

0 comments on commit af7b0b9

Please sign in to comment.