Skip to content

Commit

Permalink
Merge pull request #4 from Dcard/docker-build-patch-protocol
Browse files Browse the repository at this point in the history
feat(docker-build): Copy patch files
  • Loading branch information
tommy351 committed Sep 14, 2020
2 parents e5a0084 + f87be02 commit 6d79056
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions .pnp.js

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

1 change: 1 addition & 0 deletions packages/docker-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@yarnpkg/core": "^2.2.2",
"@yarnpkg/fslib": "^2.2.1",
"@yarnpkg/plugin-pack": "^2.2.0",
"@yarnpkg/plugin-patch": "^2.1.1",
"typescript": "^4.0.2"
}
}
7 changes: 7 additions & 0 deletions packages/docker-build/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import copyCacheMarkedFiles from '../utils/copyCacheMarkedFiles';
import generateLockfile from '../utils/generateLockfile';
import packWorkspace from '../utils/packWorkspace';
import copyAdditional from '../utils/copyAdditional';
import copyPatchFiles from '../utils/copyPatchFiles';

export default class DockerBuildCommand extends BaseCommand {
@Command.String()
Expand Down Expand Up @@ -127,6 +128,12 @@ export default class DockerBuildCommand extends BaseCommand {
report,
});

await copyPatchFiles({
destination: manifestDir,
workspaces: project.workspaces,
report,
});

await copyCacheMarkedFiles({
destination: manifestDir,
project,
Expand Down
56 changes: 56 additions & 0 deletions packages/docker-build/src/utils/copyPatchFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Report, Workspace } from '@yarnpkg/core';
import { PortablePath, ppath, xfs } from '@yarnpkg/fslib';
import { patchUtils } from '@yarnpkg/plugin-patch';

// https://github.com/yarnpkg/berry/blob/d38d573/packages/plugin-patch/sources/patchUtils.ts#L10
const BUILTIN_REGEXP = /^builtin<([^>]+)>$/;

export default async function copyPatchFiles({
destination,
workspaces,
report,
}: {
destination: PortablePath;
workspaces: Workspace[];
report: Report;
}): Promise<void> {
const copiedPaths = new Set<string>();

for (const ws of workspaces) {
for (const descriptor of ws.dependencies.values()) {
if (!descriptor.range.startsWith('patch:')) continue;

const { parentLocator, patchPaths } = patchUtils.parseDescriptor(
descriptor,
);

for (const path of patchPaths) {
// Ignore builtin modules
if (BUILTIN_REGEXP.test(path)) continue;

// TODO: Handle absolute path
if (ppath.isAbsolute(path)) continue;

if (!parentLocator) continue;

// Get the workspace by parentLocator
const parentWorkspace = ws.project.getWorkspaceByLocator(parentLocator);

// The path relative to the project CWD
const relativePath = ppath.join(parentWorkspace.relativeCwd, path);

// Skip if the path has been copied already
if (copiedPaths.has(relativePath)) continue;

copiedPaths.add(relativePath);

const src = ppath.join(parentWorkspace.cwd, path);
const dest = ppath.join(destination, relativePath);

report.reportInfo(null, relativePath);
await xfs.mkdirpPromise(ppath.dirname(dest));
await xfs.copyFilePromise(src, dest);
}
}
}
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ __metadata:
"@yarnpkg/core": ^2.2.2
"@yarnpkg/fslib": ^2.2.1
"@yarnpkg/plugin-pack": ^2.2.0
"@yarnpkg/plugin-patch": ^2.1.1
clipanion: ^2.4.4
typescript: ^4.0.2
languageName: unknown
Expand Down

0 comments on commit 6d79056

Please sign in to comment.