Skip to content

Commit

Permalink
Refactor readCanisterId to allow reading the test_app id as well (#1985)
Browse files Browse the repository at this point in the history
* Refactor readCanisterId to allow reading the test_app id as well

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.

* Name parameters of
  • Loading branch information
frederikrothenberger authored Oct 30, 2023
1 parent 14cb21f commit 743ed5b
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions vite.plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ 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,
canisterIdsJsonFile,
}: {
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 +46,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({
canisterName: "internet_identity",
canisterIdsJsonFile: "./.dfx/local/canister_ids.json",
})}" type="module" src="${src}"></script>`;
});
},
});
Expand Down

0 comments on commit 743ed5b

Please sign in to comment.