Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor readCanisterId to allow reading the test_app id as well #1985

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Changes from 1 commit
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
28 changes: 16 additions & 12 deletions vite.plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import { minify } from "html-minifier-terser";
import { extname } from "path";
import { Plugin } from "vite";
import viteCompression from "vite-plugin-compression";

/**
* Read the II canister ID from dfx's local state
* Read a canister ID from dfx's local state
*/
const readCanisterId = (): string => {
const canisterIdsJsonFile = "./.dfx/local/canister_ids.json";

export const readCanisterId = (
frederikrothenberger marked this conversation as resolved.
Show resolved Hide resolved
canisterName: string,
canisterIdsJsonFile: string
): string => {
try {
const {
internet_identity: { local: canisterId },
} = JSON.parse(readFileSync(canisterIdsJsonFile, "utf-8"));

const canisterIds: Record<string, { local: string }> = JSON.parse(
readFileSync(canisterIdsJsonFile, "utf-8")
);
const canisterId = canisterIds[canisterName]?.local;
assertNonNullish(
canisterId,
`Could not get canister ID from ${canisterIdsJsonFile}`
);

console.log("Read canister ID:", canisterId);
console.log(
`Read canister ID '${canisterId} for canister with name '${canisterName}'`
);

return canisterId;
} catch (e) {
Expand All @@ -42,7 +43,10 @@ export const injectCanisterIdPlugin = (): {
const rgx = /<script type="module" src="(?<src>[^"]+)"><\/script>/;

return html.replace(rgx, (_match, src) => {
return `<script data-canister-id="${readCanisterId()}" type="module" src="${src}"></script>`;
return `<script data-canister-id="${readCanisterId(
"internet_identity",
"./.dfx/local/canister_ids.json"
)}" type="module" src="${src}"></script>`;
});
},
});
Expand Down