Skip to content

Commit

Permalink
enough to build
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Sep 11, 2024
1 parent b7dad2d commit a1013ff
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
22 changes: 13 additions & 9 deletions src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
WorkerContext,
WorkplaceDispatchContext,
ContainerContext,
convertNonMotokoFilesToWebContainer,
generateNonMotokoFilesToWebContainer,
} from "../contexts/WorkplaceState";
import { compileCandid } from "../build";
import { didToJs } from "../config/actor";
Expand Down Expand Up @@ -174,14 +174,18 @@ export function Editor({
};
const deployClick = async () => {
if (selectFrontend) {
const files = convertNonMotokoFilesToWebContainer(state);
console.log(files);
await container.container!.fs.mkdir("user", { recursive: true });
await container.container!.mount(files, { mountPoint: "user" });
await container.run_cmd("npm", ["install"], { cwd: "user" });
await container.run_cmd("npm", ["run", "build"], { cwd: "user" });
const read = await container.container!.fs.readdir("/");
console.log(read);
try {
const { files, env } = generateNonMotokoFilesToWebContainer(state);
console.log(files);
await container.container!.fs.mkdir("user", { recursive: true });
await container.container!.mount(files, { mountPoint: "user" });
await container.run_cmd("npm", ["install"], { cwd: "user" });
await container.run_cmd("npm", ["run", "build"], { cwd: "user", env });
const read = await container.container!.fs.readdir("/");
console.log(read);
} catch (e) {
logger.log(e.message);
}
return;
}
const aliases = getActorAliases(state.canisters);
Expand Down
31 changes: 29 additions & 2 deletions src/contexts/WorkplaceState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export function getShareableProject(state: WorkplaceState) {
];
return { files, packages, canisters };
}
export function convertNonMotokoFilesToWebContainer(state: WorkplaceState) {
export function generateNonMotokoFilesToWebContainer(state: WorkplaceState) {
const { env, canister_ids } = generateEnv(state);
const files = Object.entries(state.files)
.filter(([path]) => !path.endsWith(".mo"))
.reduce((acc, [path, content]) => {
Expand All @@ -86,7 +87,33 @@ export function convertNonMotokoFilesToWebContainer(state: WorkplaceState) {
};
return acc;
}, {});
return files;
files["canister_ids.json"] = {
file: { contents: JSON.stringify(canister_ids, null, 2) },
};
files[".env"] = {
file: {
contents: Object.entries(env)
.map(([key, value]) => `${key}=${value}`)
.join("\n"),
},
};
return { files, env };
}
function generateEnv(state: WorkplaceState) {
const env: Record<string, string> = {
DFX_NETWORK: "ic",
};
const canister_ids = {};
Object.entries(state.canisters).forEach(([name, info]) => {
if (info.name && info.candid && !info.isFrontend) {
env[`CANISTER_ID_${name.toUpperCase()}`] = info.id.toString();
canister_ids[name] = { ic: info.id.toString() };
}
});
if (Object.keys(canister_ids).length === 0) {
throw new Error("Please deploy at least one backend canister first.");
}
return { env, canister_ids };
}

export type WorkplaceReducerAction =
Expand Down
2 changes: 1 addition & 1 deletion src/workers/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ 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/");
return validFiles.test(path); // && !path.includes("/declarations/");
};

async function fetchFromCDN(
Expand Down

0 comments on commit a1013ff

Please sign in to comment.