Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dev): fix dev and tests on Windows #386

Merged
merged 16 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/pages/src/bin/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import path from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const filePrefix = path.sep !== path.posix.sep ? "file:\\\\" : "";
jwartofsky-yext marked this conversation as resolved.
Show resolved Hide resolved
const pathToPagesScript = path.resolve(__dirname, "./pages.js");

const pathToLoader = path.resolve(__dirname, "./loader.js");
const pathToLoader = filePrefix + path.resolve(__dirname, "./loader.js");
mkilpatrick marked this conversation as resolved.
Show resolved Hide resolved

const nodeVersion = Number(
spawnSync("node", ["-v"], { encoding: "utf-8" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ export class FunctionMetadataParser {
const testsPath = path.join("tests", "fixtures", "src", "functions");

// The path after /src/functions
const relativePath = absolutePathToFunction.split(`/${sourcePath}/`)[1];
const relativePath = absolutePathToFunction.split(
`${path.sep}${sourcePath}${path.sep}`
)[1];

// Should be onUrlChange or http
const functionType = relativePath.split("/")[0];
const functionType = relativePath.split(path.sep)[0];
if (!validFunctionTypes.find((ft) => ft === functionType)) {
throw new Error(
`Cannot load ${absolutePathToFunction}.\n` +
Expand All @@ -64,7 +66,7 @@ export class FunctionMetadataParser {

if (
functionType === "onUrlChange" &&
relativePath.split("/").length > 2 &&
relativePath.split(path.sep).length > 2 &&
functionsRoot !== distPath
) {
throw new Error(
Expand All @@ -76,7 +78,7 @@ export class FunctionMetadataParser {

// Slug is defined by the path and filename
const defaultSlug = relativePath
.replace(`${functionType}/`, "")
.replace(`${functionType}${path.sep}`, "")
.split(".")
.slice(0, -1)
.join(".");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@ import path from "path";
import { getFunctionFilepaths } from "./getFunctionFilepaths.js";
import { minimatch } from "minimatch";

const rootPath = "src/functions";
const multiLevelPath = "src/functions/http/api/fetch";
const rootPath = path.join("src", "functions");
const multiLevelPath = path.join("src", "functions", "http", "api", "fetch");

const filepaths = [
`${multiLevelPath}/test1.ts`,
`${multiLevelPath}/test2.js`,
`${rootPath}/test3.js`,
`${rootPath}/test4.ts`,
path.join(multiLevelPath, "test1.ts"),
path.join(multiLevelPath, "test2.js"),
path.join(rootPath, "test3.js"),
path.join(rootPath, "test4.ts"),
];

const expected = [
{
root: "/",
dir: process.cwd() + "/src/functions/http/api/fetch",
root: path.resolve("/"),
dir: path.join(process.cwd(), multiLevelPath),
base: "test1.ts",
ext: ".ts",
name: "test1",
},
{
root: "/",
dir: process.cwd() + "/src/functions/http/api/fetch",
root: path.resolve("/"),
dir: path.join(process.cwd(), multiLevelPath),
base: "test2.js",
ext: ".js",
name: "test2",
},
{
root: "/",
dir: process.cwd() + "/src/functions",
root: path.resolve("/"),
dir: path.join(process.cwd(), rootPath),
base: "test3.js",
ext: ".js",
name: "test3",
},
{
root: "/",
dir: process.cwd() + "/src/functions",
root: path.resolve("/"),
dir: path.join(process.cwd(), rootPath),
base: "test4.ts",
ext: ".ts",
name: "test4",
Expand All @@ -53,7 +53,7 @@ jest.mock("glob", () => {

describe("getFunctionFilepaths", () => {
it("collects all function files under the src/functions path", () => {
const templatesFilepath = getFunctionFilepaths("src/functions");
const templatesFilepath = getFunctionFilepaths(rootPath);
expect(JSON.stringify(templatesFilepath.sort())).toEqual(
JSON.stringify(expected)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { globSync } from "glob";
import path from "path";
import { Path } from "../../project/path.js";
import { convertToPosixPath } from "../../template/paths.js";

/**
* Get all the functions files in a directory
Expand All @@ -11,7 +12,9 @@ export const getFunctionFilepaths = (root: string): path.ParsedPath[] => {
const functionsRoot = new Path(root); // resolve the functions root
// Get all js/ts files in the directory or subdirectories
const filepaths = globSync(
`${functionsRoot.getAbsolutePath()}/**/*.{tsx,jsx,js,ts}`
convertToPosixPath(
jwartofsky-yext marked this conversation as resolved.
Show resolved Hide resolved
`${functionsRoot.getAbsolutePath()}/**/*.{tsx,jsx,js,ts}`
)
);

return filepaths.map((filepath) => {
Expand Down
18 changes: 14 additions & 4 deletions packages/pages/src/common/src/function/internal/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import path from "path";
import { loadFunctionModules, FunctionModuleCollection } from "./loader.js";
import { ProjectStructure } from "../../project/structure.js";

const httpPath = path.join("tests", "fixtures", "src", "functions", "http");

// our jest configuration doesn't support file urls so update pathToFileURL to do nothing during
// this test.
jest.mock("url", () => {
Expand All @@ -26,27 +28,35 @@ describe("loadTemplateModules", () => {

it("loads and transpiles raw templates", async () => {
const functionFile: path.ParsedPath[] = [
path.parse("tests/fixtures/src/functions/http/[param].ts"),
path.parse(path.join(httpPath, "[param].ts")),
];

const functionModules = await loadFunctionModules(
functionFile,
true,
projectStructure
);
commonTests(functionModules, "param-47543");
if (path.sep === path.posix.sep) {
commonTests(functionModules, "param-47543");
} else {
jwartofsky-yext marked this conversation as resolved.
Show resolved Hide resolved
commonTests(functionModules, "param-19926");
}
});

it("loads transpiled templates", async () => {
const functionFile: path.ParsedPath[] = [
path.parse(path.resolve("tests/fixtures/src/functions/http/[param].js")),
path.parse(path.resolve(path.join(httpPath, "[param].js"))),
jwartofsky-yext marked this conversation as resolved.
Show resolved Hide resolved
];
const functionModules = await loadFunctionModules(
functionFile,
false,
projectStructure
);
commonTests(functionModules, "param-47853");
if (path.sep === path.posix.sep) {
commonTests(functionModules, "param-47853");
} else {
commonTests(functionModules, "param-19616");
}
});

const commonTests = (
Expand Down
50 changes: 34 additions & 16 deletions packages/pages/src/common/src/function/internal/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const exampleOnUrlChangeArgument: SitesOnUrlChangeRequest = {
};

const createMockFilePath = (filepath: string): path.ParsedPath => {
return path.parse(path.resolve(path.join("src/functions/", filepath)));
return path.parse(path.resolve(path.join("src", "functions", filepath)));
};

describe("internal/types - convertFunctionModuleToFunctionModuleInternal", () => {
Expand All @@ -63,7 +63,7 @@ describe("internal/types - convertFunctionModuleToFunctionModuleInternal", () =>
};
const functionModuleInternal =
convertFunctionModuleToFunctionModuleInternal(
createMockFilePath("http/api/example.ts"),
createMockFilePath(path.join("http", "api", "example.ts")),
functionModule,
projectStructure
);
Expand All @@ -74,18 +74,21 @@ describe("internal/types - convertFunctionModuleToFunctionModuleInternal", () =>
event: "API",
},
filePath: {
root: "/",
dir: process.cwd() + "/src/functions/http/api",
root: path.resolve("/"),
dir: path.join(process.cwd(), "src", "functions", "http", "api"),
base: "example.ts",
ext: ".ts",
name: "example",
},
slug: {
original: "api/example",
dev: "api/example",
production: "api/example",
original: path.join("api", "example"),
dev: path.join("api", "example"),
production: path.join("api", "example"),
},
};
if (path.sep !== path.posix.sep) {
expected.config.name = "example-23673";
}
expect(JSON.stringify(functionModuleInternal)).toEqual(
JSON.stringify(expected)
);
Expand All @@ -107,7 +110,7 @@ describe("internal/types - convertFunctionModuleToFunctionModuleInternal", () =>
};
const functionModuleInternal =
convertFunctionModuleToFunctionModuleInternal(
createMockFilePath("onUrlChange/example.ts"),
createMockFilePath(path.join("onUrlChange", "example.ts")),
functionModule,
projectStructure
);
Expand All @@ -118,8 +121,8 @@ describe("internal/types - convertFunctionModuleToFunctionModuleInternal", () =>
event: "ON_URL_CHANGE",
},
filePath: {
root: "/",
dir: process.cwd() + "/src/functions/onUrlChange",
root: path.resolve("/"),
dir: path.join(process.cwd(), "src", "functions", "onUrlChange"),
base: "example.ts",
ext: ".ts",
name: "example",
Expand All @@ -130,6 +133,9 @@ describe("internal/types - convertFunctionModuleToFunctionModuleInternal", () =>
production: "example",
},
};
if (path.sep !== path.posix.sep) {
expected.config.name = "example-11807";
}
expect(JSON.stringify(functionModuleInternal)).toEqual(
JSON.stringify(expected)
);
Expand All @@ -149,7 +155,9 @@ describe("internal/types - convertFunctionModuleToFunctionModuleInternal", () =>
};
const functionModuleInternal =
convertFunctionModuleToFunctionModuleInternal(
createMockFilePath("http/api/example/[testParam].ts"),
createMockFilePath(
path.join("http", "api", "example", "[testParam].ts")
),
functionModule,
projectStructure
);
Expand All @@ -160,18 +168,28 @@ describe("internal/types - convertFunctionModuleToFunctionModuleInternal", () =>
event: "API",
},
filePath: {
root: "/",
dir: process.cwd() + "/src/functions/http/api/example",
root: path.resolve("/"),
dir: path.join(
process.cwd(),
"src",
"functions",
"http",
"api",
"example"
),
base: "[testParam].ts",
ext: ".ts",
name: "[testParam]",
},
slug: {
original: "api/example/[testParam]",
dev: "api/example/:testParam",
production: "api/example/{{testParam}}",
original: path.join("api", "example", "[testParam]"),
dev: path.join("api", "example", ":testParam"),
production: path.join("api", "example", "{{testParam}}"),
},
};
if (path.sep !== path.posix.sep) {
expected.config.name = "testParam-61704";
}
expect(JSON.stringify(functionModuleInternal)).toEqual(
JSON.stringify(expected)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { getTemplateFilepaths } from "./getTemplateFilepaths.js";
import { minimatch } from "minimatch";
import { Path } from "../../project/path.js";

const rootPath = "src/templates";
const domain1Path = "src/templates/some.domain1.com";
const domain2Path = "src/templates/some.domain2.com";
const rootPath = path.join("src", "templates");
const domain1Path = path.join("src", "templates", "some.domain1.com");
const domain2Path = path.join("src", "templates", "some.domain2.com");
jest.mock("glob", () => {
return {
globSync: (glob: string) => {
const filepaths = [
`${domain1Path}/brand1.tsx`,
`${domain1Path}/test.tsx`,
`${domain2Path}/brand2.tsx`,
`${domain2Path}/test.tsx`,
`${rootPath}/share.tsx`,
`${rootPath}/test.tsx`,
path.join(domain1Path, "brand1.tsx"),
path.join(domain1Path, "test.tsx"),
path.join(domain2Path, "brand2.tsx"),
path.join(domain2Path, "test.tsx"),
path.join(rootPath, "share.tsx"),
path.join(rootPath, "test.tsx"),
];
return filepaths.filter((f) => minimatch(path.resolve(f), glob));
},
Expand All @@ -28,7 +28,7 @@ describe("getTemplateFilepaths", () => {
new Path(path.join(process.cwd(), rootPath)),
]);
expect(templatesFilepath.sort()).toEqual(
[`${rootPath}/share.tsx`, `${rootPath}/test.tsx`].sort()
[path.join(rootPath, "share.tsx"), path.join(rootPath, "test.tsx")].sort()
);
});

Expand All @@ -39,9 +39,9 @@ describe("getTemplateFilepaths", () => {
]);
expect(templatesFilepath.sort()).toEqual(
[
`${rootPath}/share.tsx`,
`${domain1Path}/test.tsx`,
`${domain1Path}/brand1.tsx`,
path.join(rootPath, "share.tsx"),
path.join(domain1Path, "test.tsx"),
path.join(domain1Path, "brand1.tsx"),
].sort()
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ProjectStructure } from "../../project/structure.js";
import { ClientServerRenderTemplates } from "../types.js";
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import { convertToPosixPath } from "../paths.js";

/**
* Get all the template files in the provided template folder path(s).
Expand All @@ -21,7 +22,9 @@ export const getTemplateFilepaths = (paths: Path[]): string[] => {
const templateFilepaths: string[] = [];
const addedFilenames: Set<string> = new Set();
paths.forEach((p) => {
const filepaths = globSync(`${p.getAbsolutePath()}/*.{tsx,jsx,js,ts}`);
const filepaths = globSync(
convertToPosixPath(`${p.getAbsolutePath()}/*.{tsx,jsx,js,ts}`)
mkilpatrick marked this conversation as resolved.
Show resolved Hide resolved
);
filepaths
// Don't include the client/server rendering templates
.filter(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { glob } from "glob";
import path from "path";
import { loadTemplateModules } from "./loader.js";
import { convertToPosixPath } from "../paths.js";

// our jest configuration doesn't support file urls so update pathToFileURL to do nothing during
// this test.
Expand All @@ -23,7 +24,9 @@ afterAll(() => jest.unmock("url"));
describe("loadTemplateModules", () => {
it("loads and transpiles raw templates", async () => {
const templateFile = glob.sync(
path.join(process.cwd(), "tests/fixtures/template.tsx")
convertToPosixPath(
path.join(process.cwd(), "tests/fixtures/template.tsx")
)
);
const templateModules = await loadTemplateModules(
templateFile,
Expand All @@ -36,7 +39,7 @@ describe("loadTemplateModules", () => {

it("loads transpiled templates", async () => {
const templateFile = glob.sync(
path.join(process.cwd(), "tests/fixtures/template.js")
convertToPosixPath(path.join(process.cwd(), "tests/fixtures/template.js"))
);
const templateModules = await loadTemplateModules(
templateFile,
Expand Down
Loading