Skip to content

Commit

Permalink
Refactor readCanisterId to allow reading the test_app id as well
Browse files Browse the repository at this point in the history
This PR is in preparation for the migration of the selnium tests out
of docker. After the migration, vite needs to know about the test_app
canister id too. This refactoring allows using the existing helper for
that.
  • Loading branch information
Frederik Rothenberger committed Oct 26, 2023
1 parent bcf5bfa commit d173124
Showing 1 changed file with 16 additions and 12 deletions.
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 = (
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

0 comments on commit d173124

Please sign in to comment.