Skip to content

Commit

Permalink
Fixes #3178
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Apr 17, 2024
1 parent 278db9b commit 55f06d4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
18 changes: 16 additions & 2 deletions src/EleventyServe.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,27 @@ class EleventyServe {
// Look for peer dep in local project
let projectNodeModulesPath = TemplatePath.absolutePath("./node_modules/");
let serverPath = TemplatePath.absolutePath(projectNodeModulesPath, name);

// No references outside of the project node_modules are allowed
if (!serverPath.startsWith(projectNodeModulesPath)) {
throw new Error("Invalid node_modules name for Eleventy server instance, received:" + name);
}

let serverPackageJson = getModulePackageJson(serverPath);
// Normalize with `main` entry from
if (TemplatePath.isDirectorySync(serverPath)) {
if (serverPackageJson.main) {
serverPath = TemplatePath.absolutePath(
projectNodeModulesPath,
name,
serverPackageJson.main,
);
} else {
throw new Error(
`Eleventy server ${name} is missing a \`main\` entry in its package.json file. Traversed up from ${serverPath}.`,
);
}
}

let module = await EleventyImport(serverPath);

if (!("getServer" in module)) {
Expand All @@ -102,7 +117,6 @@ class EleventyServe {
);
}

let serverPackageJson = getModulePackageJson(serverPath);
if (serverPackageJson["11ty"]?.compatibility) {
try {
this.eleventyConfig.userConfig.versionCheck(serverPackageJson["11ty"].compatibility);
Expand Down
39 changes: 18 additions & 21 deletions src/Util/ImportJsonSync.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import debugUtil from "debug";

import { TemplatePath } from "@11ty/eleventy-utils";
Expand All @@ -10,27 +9,25 @@ import { normalizeFilePathInEleventyPackage } from "./Require.js";
const debug = debugUtil("Eleventy:ImportJsonSync");
const require = createRequire(import.meta.url);

function importJsonSync(filePath, lookInParentDirs = false) {
function findFileInParentDirs(dir, filename) {
// `package.json` searches look in parent dirs:
// https://docs.npmjs.com/cli/v7/configuring-npm/folders#more-information
let allDirs = TemplatePath.getAllDirs(dir);
for (let dir of allDirs) {
let newPath = TemplatePath.join(dir, filename);
if (fs.existsSync(newPath)) {
debug("Found %o searching parent directories at: %o", filename, dir);
return importJsonSync(newPath);
}
}
}

function importJsonSync(filePath) {
if (!filePath.endsWith(".json")) {
throw new Error(`importJsonSync expects a .json file extension (received: ${filePath})`);
}

try {
// `package.json` searches look in parent dirs:
// https://docs.npmjs.com/cli/v7/configuring-npm/folders#more-information
if (lookInParentDirs) {
let filename = path.parse(filePath).base;
let allDirs = TemplatePath.getAllDirs(filePath).slice(1);
for (let dir of allDirs) {
let newPath = TemplatePath.join(dir, filename);
if (fs.existsSync(newPath)) {
filePath = newPath;
debug("Found %o searching parent directories at: %o", filename, filePath);
break;
}
}
}

// TODO clear require.cache when these files change
return require(filePath);
} catch (e) {
Expand All @@ -45,17 +42,17 @@ function getEleventyPackageJson() {
}

function getModulePackageJson(dir) {
let filePath = TemplatePath.absolutePath(dir, "package.json");
return importJsonSync(filePath);
return findFileInParentDirs(TemplatePath.absolutePath(dir), "package.json");
}

function getWorkingProjectPackageJson() {
let filePath = TemplatePath.absolutePath(TemplatePath.getWorkingDir(), "package.json");
return importJsonSync(filePath, true);
let dir = TemplatePath.absolutePath(TemplatePath.getWorkingDir());
return findFileInParentDirs(dir, "package.json");
}

export {
importJsonSync,
findFileInParentDirs,
getEleventyPackageJson,
getModulePackageJson,
getWorkingProjectPackageJson,
Expand Down
6 changes: 3 additions & 3 deletions test/ImportJsonSyncTest.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import test from "ava";
import { TemplatePath } from "@11ty/eleventy-utils";
import { importJsonSync } from "../src/Util/ImportJsonSync.js";
import { importJsonSync, findFileInParentDirs } from "../src/Util/ImportJsonSync.js";

test("Import a JSON", t => {
t.deepEqual(Object.keys(importJsonSync("../../package.json")).sort().pop(), "version");
});

test("getWorkingProjectPackageJson() traverse parent dirs", t => {
let filePath = TemplatePath.absolutePath("test", "package.json");
t.deepEqual(Object.keys(importJsonSync(filePath, true)).sort().pop(), "version");
let json = findFileInParentDirs(TemplatePath.absolutePath("test"), "package.json");
t.deepEqual(Object.keys(json).sort().pop(), "version");
});

0 comments on commit 55f06d4

Please sign in to comment.