Skip to content

Commit

Permalink
Merge pull request #875 from adrianhall/issues/871
Browse files Browse the repository at this point in the history
(#871) fix: import JSON files using createRequire
  • Loading branch information
Timothyw0 authored Jul 24, 2024
2 parents 4a481ed + 1f32666 commit e06191e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/cli/commands/deploy/deploy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion src/cli/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/cli/commands/start/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/cli/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
4 changes: 3 additions & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) || [];
Expand Down
12 changes: 12 additions & 0 deletions src/core/utils/json.spec.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
});
17 changes: 16 additions & 1 deletion src/core/utils/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/core/utils/update-notifier.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down

0 comments on commit e06191e

Please sign in to comment.