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: resolve symlinks for pnpm windows bundle #192

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 0 additions & 4 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ const project = new awscdk.AwsCdkConstructLibrary({
'@aws-sdk/client-s3',
'@mrgrain/jsii-struct-builder',
'@smithy/signature-v4',
'@types/adm-zip',
'@types/aws-lambda',
'@types/micromatch',
'@types/mime-types',
'@types/node@^18',
'@types/node@^20',
'aws-lambda',
'constructs',
'jszip',
Expand Down
1 change: 0 additions & 1 deletion package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/NextjsDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export class NextjsDistribution extends Construct {
...this.commonBehaviorOptions,
origin,
allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
// ALL_VIEWER_EXCEPT_HOST_HEADER needed Lambda Function URL; see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-origin-request-policies.html#managed-origin-request-policy-all-viewer-except-host-header
originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER,
cachePolicy,
edgeLambdas: this.edgeLambdas.length ? this.edgeLambdas : undefined,
Expand Down Expand Up @@ -351,6 +352,7 @@ export class NextjsDistribution extends Construct {
allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS,
cachePolicy,
// ALL_VIEWER_EXCEPT_HOST_HEADER needed Lambda Function URL; see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-origin-request-policies.html#managed-origin-request-policy-all-viewer-except-host-header
originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER,
edgeLambdas: this.edgeLambdas,
responseHeadersPolicy,
Expand Down
44 changes: 33 additions & 11 deletions src/utils/create-archive.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execSync } from 'node:child_process';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { Dirent, cpSync, existsSync, mkdtempSync, readdirSync, renameSync, rmSync, unlinkSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

export interface CreateArchiveArgs {
readonly directory: string;
Expand All @@ -17,18 +17,24 @@ export interface CreateArchiveArgs {
* for PNPM monorepos. See more here: https://github.com/aws/aws-cdk/issues/9251
*/
export function createArchive({ directory, zipFileName, fileGlob = '.', quiet }: CreateArchiveArgs): string {
const zipOutDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdk-nextjs-archive-'));
const zipFilePath = path.join(zipOutDir, zipFileName);
const zipOutDir = mkdtempSync(join(tmpdir(), 'cdk-nextjs-archive-'));
const zipFilePath = join(zipOutDir, zipFileName);

// delete existing zip file
if (fs.existsSync(zipFilePath)) {
fs.unlinkSync(zipFilePath);
if (existsSync(zipFilePath)) {
// delete existing zip file
unlinkSync(zipFilePath);
}

// run script to create zipfile, preserving symlinks for node_modules (e.g. pnpm structure)
const isWindows = process.platform === 'win32';
// needed for PNPM
if (isWindows) {
// TODO: test on windows
const nodeModulesDir = join(directory, 'node_modules');
const files = readdirSync(nodeModulesDir, { recursive: true, withFileTypes: true });
if (files.some((f) => f.isSymbolicLink())) {
// Compress-Archive doesn't support resolving symlinks like `zip` command
// does with -y option. Therefore, we need to resolve symlinks then compress.
resolveSymlinks(files);
}
execSync(`Compress-Archive -Path '${directory}\\*' -DestinationPath '${zipFilePath}' -CompressionLevel Optimal`, {
stdio: 'inherit',
shell: 'powershell.exe',
Expand All @@ -40,11 +46,27 @@ export function createArchive({ directory, zipFileName, fileGlob = '.', quiet }:
});
}
// check output
if (!fs.existsSync(zipFilePath)) {
if (!existsSync(zipFilePath)) {
throw new Error(
`There was a problem generating the archive for ${directory}; the archive is missing in ${zipFilePath}.`
);
}

return zipFilePath;
}

/**
* Resolve symlinks
*/
function resolveSymlinks(files: Dirent[]) {
for (const file of files) {
if (file.isSymbolicLink()) {
const tmpDir = mkdtempSync(join(tmpdir(), `${file.name}-`));
// cannot do `cpSync` to resolve symlinks in-place so must use temp dir
const path = join(file.path, file.name);
cpSync(path, tmpDir, { recursive: true, dereference: true, force: true });
rmSync(path, { recursive: true });
renameSync(tmpDir, path);
}
}
}
7 changes: 0 additions & 7 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading