Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
106 changes: 106 additions & 0 deletions packages/@apphosting/adapter-nextjs/src/bin/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,112 @@ outputFiles:
async () => await validateOutputDirectory(outputBundleOptions, path.join(tmpDir, ".next")),
);
});
it(".apphosting gitignored correctly in a monorepo setup", async () => {
const { generateBuildOutput } = await importUtils;
const files = {
".next/standalone/apps/next-app/standalonefile": "",
".next/static/staticfile": "",
};
generateTestFiles(tmpDir, files);
await generateBuildOutput(
tmpDir,
"apps/next-app",
{
bundleYamlPath: path.join(tmpDir, ".apphosting", "bundle.yaml"),
outputDirectoryBasePath: path.join(tmpDir, ".apphosting"),
outputDirectoryAppPath: path.join(tmpDir, ".next", "standalone", "apps", "next-app"),
outputPublicDirectoryPath: path.join(
tmpDir,
".next",
"standalone",
"apps",
"next-app",
"public",
),
outputStaticDirectoryPath: path.join(
tmpDir,
".next",
"standalone",
"apps",
"next-app",
".next",
"static",
),
serverFilePath: path.join(tmpDir, ".next", "standalone", "apps", "next-app", "server.js"),
},
path.join(tmpDir, ".next"),
defaultNextVersion,
adapterMetadata,
);

const expectedFiles = {
".gitignore": "/.apphosting/",
};
const expectedPartialYaml = {
version: "v1",
runConfig: { runCommand: "node .next/standalone/apps/next-app/server.js" },
};
validateTestFiles(tmpDir, expectedFiles);
validatePartialYamlContents(tmpDir, ".apphosting/bundle.yaml", expectedPartialYaml);
});

it(".apphosting gitignored without existing .gitignore file", async () => {
const { generateBuildOutput, validateOutputDirectory } = await importUtils;
const files = {
// .next/standalone/.next/ must be created beforehand otherwise
// generateBuildOutput will attempt to copy
// .next/ into .next/standalone/.next
".next/standalone/.next/package.json": "",
".next/static/staticfile": "",
};
generateTestFiles(tmpDir, files);
await generateBuildOutput(
tmpDir,
tmpDir,
outputBundleOptions,
path.join(tmpDir, ".next"),
defaultNextVersion,
{
adapterPackageName: "@apphosting/adapter-nextjs",
adapterVersion: "14.0.1",
},
);
await validateOutputDirectory(outputBundleOptions, path.join(tmpDir, ".next"));

const expectedFiles = {
".gitignore": "/.apphosting/",
};
validateTestFiles(tmpDir, expectedFiles);
});
it(".apphosting gitignored in existing .gitignore file", async () => {
const { generateBuildOutput, validateOutputDirectory } = await importUtils;
const files = {
// .next/standalone/.next/ must be created beforehand otherwise
// generateBuildOutput will attempt to copy
// .next/ into .next/standalone/.next
".next/standalone/.next/package.json": "",
".next/static/staticfile": "",
".gitignore": "/.next/",
};
generateTestFiles(tmpDir, files);
await generateBuildOutput(
tmpDir,
tmpDir,
outputBundleOptions,
path.join(tmpDir, ".next"),
defaultNextVersion,
{
adapterPackageName: "@apphosting/adapter-nextjs",
adapterVersion: "14.0.1",
},
);
await validateOutputDirectory(outputBundleOptions, path.join(tmpDir, ".next"));

const expectedFiles = {
".gitignore": "/.next/\n/.apphosting/",
};
validateTestFiles(tmpDir, expectedFiles);
});
it("expects directories and other files to be copied over", async () => {
const { generateBuildOutput, validateOutputDirectory } = await importUtils;
const files = {
Expand Down
8 changes: 5 additions & 3 deletions packages/@apphosting/adapter-nextjs/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {
MiddlewareManifest,
} from "./interfaces.js";
import { NextConfigComplete } from "next/dist/server/config-shared.js";
import { OutputBundleConfig } from "@apphosting/common";
import { OutputBundleConfig, updateOrCreateGitignore } from "@apphosting/common";

// fs-extra is CJS, readJson can't be imported using shorthand
export const { copy, exists, writeFile, readJson, readdir, readFileSync, existsSync, mkdir } =
export const { copy, exists, writeFile, readJson, readdir, readFileSync, existsSync, ensureDir } =
fsExtra;

// Loads the user's next.config.js file.
Expand Down Expand Up @@ -181,7 +181,7 @@ async function generateBundleYaml(
nextVersion: string,
adapterMetadata: AdapterMetadata,
): Promise<void> {
await mkdir(opts.outputDirectoryBasePath);
await ensureDir(opts.outputDirectoryBasePath);
const outputBundle: OutputBundleConfig = {
version: "v1",
runConfig: {
Expand All @@ -203,6 +203,8 @@ async function generateBundleYaml(
}

await writeFile(opts.bundleYamlPath, yamlStringify(outputBundle));
const normalizedBundleDir = normalize(relative(cwd, opts.outputDirectoryBasePath));
updateOrCreateGitignore(cwd, [`/${normalizedBundleDir}/`]);
return;
}

Expand Down