diff --git a/src/cli/commands/deploy/deploy.spec.ts b/src/cli/commands/deploy/deploy.spec.ts index e097919e..ba149559 100644 --- a/src/cli/commands/deploy/deploy.spec.ts +++ b/src/cli/commands/deploy/deploy.spec.ts @@ -6,7 +6,9 @@ import * as accountModule from "../../../core/account.js"; import * as deployClientModule from "../../../core/deploy-client.js"; import { deploy } from "./deploy.js"; import * as loginModule from "../login/login.js"; -import pkg from "../../../../package.json" with { type: "json" }; +import { loadPackageJson } from "../../../core/utils/json.js"; + +const pkg = loadPackageJson(); vi.mock("../../../core/utils/logger", () => { return { diff --git a/src/cli/commands/deploy/deploy.ts b/src/cli/commands/deploy/deploy.ts index 835b0ec5..0f0d9644 100644 --- a/src/cli/commands/deploy/deploy.ts +++ b/src/cli/commands/deploy/deploy.ts @@ -14,7 +14,9 @@ import { cleanUp, getDeployClientPath } from "../../../core/deploy-client.js"; import { swaCLIEnv } from "../../../core/env.js"; import { getDefaultVersion } from "../../../core/functions-versions.js"; import { login } from "../login/login.js"; -import packageInfo from "../../../../package.json" with { type: "json" }; +import { loadPackageJson } from "../../../core/utils/json.js"; + +const packageInfo = loadPackageJson(); export async function deploy(options: SWACLIConfig) { const { SWA_CLI_DEPLOYMENT_TOKEN, SWA_CLI_DEBUG } = swaCLIEnv(); diff --git a/src/cli/commands/start/start.ts b/src/cli/commands/start/start.ts index ae05b014..c3552f2c 100644 --- a/src/cli/commands/start/start.ts +++ b/src/cli/commands/start/start.ts @@ -17,7 +17,9 @@ import { DATA_API_BUILDER_BINARY_NAME, DATA_API_BUILDER_DEFAULT_CONFIG_FILE_NAME import { getDataApiBuilderBinaryPath } from "../../../core/dataApiBuilder/index.js"; import { swaCLIEnv } from "../../../core/env.js"; import { getCertificate } from "../../../core/ssl.js"; -import packageInfo from "../../../../package.json" with { type: "json" }; +import { loadPackageJson } from "../../../core/utils/json.js"; + +const packageInfo = loadPackageJson(); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); diff --git a/src/cli/index.spec.ts b/src/cli/index.spec.ts index 38729ded..56c2ac5e 100644 --- a/src/cli/index.spec.ts +++ b/src/cli/index.spec.ts @@ -1,9 +1,11 @@ import { fs, vol } from "memfs"; import { program } from "commander"; import { run } from "./index.js"; -import pkg from "../../package.json" with { type: "json" }; +import { loadPackageJson } from "../core/utils/json.js"; import * as builder from "./commands/build/build.js"; +const pkg = loadPackageJson(); + vi.mock("node:fs"); vi.mock("node:fs/promises", async () => { const memfs: { fs: typeof fs } = await vi.importActual("memfs"); diff --git a/src/cli/index.ts b/src/cli/index.ts index 8da5d84f..bf855a93 100755 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -20,7 +20,9 @@ import { default as registerBuild } from "./commands/build/register.js"; import { registerDocs } from "./commands/docs.js"; import { default as registerDb } from "./commands/db/init/register.js"; import { promptOrUseDefault } from "../core/prompts.js"; -import pkg from "../../package.json" with { type: "json" }; +import { loadPackageJson } from "../core/utils/json.js"; + +const pkg = loadPackageJson(); function printWelcomeMessage(argv?: string[]) { const args = argv?.slice(2) || []; diff --git a/src/core/utils/json.spec.ts b/src/core/utils/json.spec.ts new file mode 100644 index 00000000..e28d4de2 --- /dev/null +++ b/src/core/utils/json.spec.ts @@ -0,0 +1,12 @@ +import { loadPackageJson } from "./json.js"; + +describe("json functions", () => { + describe("loadPackageJson()", () => { + it("loads package.json", () => { + const pkg = loadPackageJson(); + expect(pkg).toHaveProperty("name"); + expect(pkg).toHaveProperty("version"); + expect(pkg.type).toBe("module"); + }); + }); +}); diff --git a/src/core/utils/json.ts b/src/core/utils/json.ts index 2709a87b..61cfcb05 100644 --- a/src/core/utils/json.ts +++ b/src/core/utils/json.ts @@ -2,10 +2,25 @@ import jsonSchemaLibrary from "json-schema-library"; import { promises as fs } from "node:fs"; import chalk from "chalk"; import { logger } from "./logger.js"; -import configSchema from "../../../schema/staticwebapp.config.json" with { type: "json" }; +import { createRequire } from "node:module"; + +// import of JSON files requires node v20.10 - we are supporting node v18 or later, +// so we need to create requires. Centralizing this into a single file ensures that +// we don't need to go hunting for import of JSON files. +const require = createRequire(import.meta.url); +const pkg = require("../../../package.json"); +const configSchema = require("../../../schema/staticwebapp.config.json"); const { Draft04 } = jsonSchemaLibrary; +/** + * Loads the package.json file from the root of the project. + * @returns the parsed package.json object, or null if an error occurred. + */ +export function loadPackageJson(): any { + return pkg; +} + /** * Loads JSON from the designated file path, printing any JSON errors to the console. * @param filePath The file path to the JSON file diff --git a/src/core/utils/update-notifier.ts b/src/core/utils/update-notifier.ts index 888e1e8e..e12df7c7 100644 --- a/src/core/utils/update-notifier.ts +++ b/src/core/utils/update-notifier.ts @@ -1,5 +1,7 @@ import updateNotifier from "update-notifier"; -import pkg from "../../../package.json" with { type: "json" }; +import { loadPackageJson } from "./json.js"; + +const pkg = loadPackageJson(); export function notifyOnUpdate() { updateNotifier({ pkg }).notify();