From bad3d97a6c864f515d4387ab6035fb3154870f13 Mon Sep 17 00:00:00 2001 From: Tommy Chen Date: Thu, 10 Sep 2020 18:29:33 +0800 Subject: [PATCH 1/3] feat(docker-build): Copy patch files --- .pnp.js | 1 + packages/docker-build/package.json | 1 + packages/docker-build/src/commands/build.ts | 7 ++++ .../docker-build/src/utils/copyPatchFiles.ts | 39 +++++++++++++++++++ yarn.lock | 1 + 5 files changed, 49 insertions(+) create mode 100644 packages/docker-build/src/utils/copyPatchFiles.ts diff --git a/.pnp.js b/.pnp.js index 941a831..7b11767 100755 --- a/.pnp.js +++ b/.pnp.js @@ -570,6 +570,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["@yarnpkg/core", "npm:2.2.2"], ["@yarnpkg/fslib", "npm:2.2.1"], ["@yarnpkg/plugin-pack", "virtual:c2e80915340bb809f3de434cc78ee9a45b9161014b960659e8f8ced61c68c2bd767880ffe16710532315879b144bfb59d5d9f7d28251b1f74d73c69feaaf71a2#npm:2.2.0"], + ["@yarnpkg/plugin-patch", "virtual:c2e80915340bb809f3de434cc78ee9a45b9161014b960659e8f8ced61c68c2bd767880ffe16710532315879b144bfb59d5d9f7d28251b1f74d73c69feaaf71a2#npm:2.1.1"], ["clipanion", "npm:2.4.4"], ["typescript", "patch:typescript@npm%3A4.0.2#builtin::version=4.0.2&hash=5b02a2"] ], diff --git a/packages/docker-build/package.json b/packages/docker-build/package.json index e85b563..a3150c7 100644 --- a/packages/docker-build/package.json +++ b/packages/docker-build/package.json @@ -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" } } diff --git a/packages/docker-build/src/commands/build.ts b/packages/docker-build/src/commands/build.ts index f1bf620..793fb27 100644 --- a/packages/docker-build/src/commands/build.ts +++ b/packages/docker-build/src/commands/build.ts @@ -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() @@ -127,6 +128,12 @@ export default class DockerBuildCommand extends BaseCommand { report, }); + await copyPatchFiles({ + destination: manifestDir, + workspaces: project.workspaces, + report, + }); + await copyCacheMarkedFiles({ destination: manifestDir, project, diff --git a/packages/docker-build/src/utils/copyPatchFiles.ts b/packages/docker-build/src/utils/copyPatchFiles.ts new file mode 100644 index 0000000..e6e5350 --- /dev/null +++ b/packages/docker-build/src/utils/copyPatchFiles.ts @@ -0,0 +1,39 @@ +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 { + for (const ws of workspaces) { + for (const descriptor of ws.dependencies.values()) { + if (!descriptor.range.startsWith('patch:')) continue; + + const { 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; + + const src = ppath.join(ws.relativeCwd, path); + const dest = ppath.join(destination, src); + + report.reportInfo(null, src); + await xfs.mkdirpPromise(ppath.dirname(dest)); + await xfs.copyFilePromise(src, dest); + } + } + } +} diff --git a/yarn.lock b/yarn.lock index 9c729a2..3eec5a2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 From 15b25490f9febe43d7d19cca62af7c4e41d93979 Mon Sep 17 00:00:00 2001 From: Tommy Chen Date: Fri, 11 Sep 2020 15:40:58 +0800 Subject: [PATCH 2/3] feat(docker-build): Resolve patch path with parentLocator --- packages/docker-build/src/utils/copyPatchFiles.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/docker-build/src/utils/copyPatchFiles.ts b/packages/docker-build/src/utils/copyPatchFiles.ts index e6e5350..0048b11 100644 --- a/packages/docker-build/src/utils/copyPatchFiles.ts +++ b/packages/docker-build/src/utils/copyPatchFiles.ts @@ -18,7 +18,9 @@ export default async function copyPatchFiles({ for (const descriptor of ws.dependencies.values()) { if (!descriptor.range.startsWith('patch:')) continue; - const { patchPaths } = patchUtils.parseDescriptor(descriptor); + const { parentLocator, patchPaths } = patchUtils.parseDescriptor( + descriptor, + ); for (const path of patchPaths) { // Ignore builtin modules @@ -27,7 +29,10 @@ export default async function copyPatchFiles({ // TODO: Handle absolute path if (ppath.isAbsolute(path)) continue; - const src = ppath.join(ws.relativeCwd, path); + if (!parentLocator) continue; + + const parentWorkspace = ws.project.getWorkspaceByLocator(parentLocator); + const src = ppath.join(parentWorkspace.relativeCwd, path); const dest = ppath.join(destination, src); report.reportInfo(null, src); From f87be021dc289976606c29b980559107b8d06af7 Mon Sep 17 00:00:00 2001 From: Tommy Chen Date: Mon, 14 Sep 2020 13:30:23 +0800 Subject: [PATCH 3/3] fix(docker-build): Resolve relative patch paths --- .../docker-build/src/utils/copyPatchFiles.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/docker-build/src/utils/copyPatchFiles.ts b/packages/docker-build/src/utils/copyPatchFiles.ts index 0048b11..970a034 100644 --- a/packages/docker-build/src/utils/copyPatchFiles.ts +++ b/packages/docker-build/src/utils/copyPatchFiles.ts @@ -14,6 +14,8 @@ export default async function copyPatchFiles({ workspaces: Workspace[]; report: Report; }): Promise { + const copiedPaths = new Set(); + for (const ws of workspaces) { for (const descriptor of ws.dependencies.values()) { if (!descriptor.range.startsWith('patch:')) continue; @@ -31,11 +33,21 @@ export default async function copyPatchFiles({ if (!parentLocator) continue; + // Get the workspace by parentLocator const parentWorkspace = ws.project.getWorkspaceByLocator(parentLocator); - const src = ppath.join(parentWorkspace.relativeCwd, path); - const dest = ppath.join(destination, src); - report.reportInfo(null, src); + // 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); }