From ed454852358a56af349a89da84ed90641eedd831 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Fri, 18 Aug 2023 16:04:09 -0400 Subject: [PATCH] Don't copy node_modules either --- .../__tests__/create-remix-test.ts | 28 +++++++++++++++---- .../package.json | 0 packages/create-remix/index.ts | 15 ++++++---- packages/create-remix/utils.ts | 20 +++++-------- 4 files changed, 38 insertions(+), 25 deletions(-) rename packages/create-remix/__tests__/fixtures/{with-git-dir => with-ignored-dir}/package.json (100%) diff --git a/packages/create-remix/__tests__/create-remix-test.ts b/packages/create-remix/__tests__/create-remix-test.ts index d550d9d0a79..bc15019c927 100644 --- a/packages/create-remix/__tests__/create-remix-test.ts +++ b/packages/create-remix/__tests__/create-remix-test.ts @@ -883,12 +883,26 @@ describe("create-remix CLI", () => { expect(fse.existsSync(path.join(emptyDir, "app/root.tsx"))).toBeTruthy(); }); - it("does not copy .git/ directory if it exists in the template", async () => { + it("does not copy .git nor node_modules directories if they exist in the template", async () => { // Can't really commit this file into a git repo, so just create it as // part of the test and then remove it when we're done - let withGitDir = path.join(__dirname, "fixtures", "with-git-dir"); - fse.mkdirSync(path.join(withGitDir, ".git")); - fse.createFileSync(path.join(withGitDir, ".git", "some-git-file.txt")); + let templateWithIgnoredDirs = path.join( + __dirname, + "fixtures", + "with-ignored-dir" + ); + fse.mkdirSync(path.join(templateWithIgnoredDirs, ".git")); + fse.createFileSync( + path.join(templateWithIgnoredDirs, ".git", "some-git-file.txt") + ); + fse.mkdirSync(path.join(templateWithIgnoredDirs, "node_modules")); + fse.createFileSync( + path.join( + templateWithIgnoredDirs, + "node_modules", + "some-node-module-file.txt" + ) + ); let projectDir = getProjectDir("with-git-dir"); @@ -897,7 +911,7 @@ describe("create-remix CLI", () => { args: [ projectDir, "--template", - path.join(__dirname, "fixtures", "with-git-dir"), + templateWithIgnoredDirs, "--no-git-init", "--no-install", ], @@ -906,11 +920,13 @@ describe("create-remix CLI", () => { expect(stderr.trim()).toBeFalsy(); expect(status).toBe(0); expect(fse.existsSync(path.join(projectDir, ".git"))).toBeFalsy(); + expect(fse.existsSync(path.join(projectDir, "node_modules"))).toBeFalsy(); expect( fse.existsSync(path.join(projectDir, "package.json")) ).toBeTruthy(); } finally { - fse.removeSync(path.join(withGitDir, ".git")); + fse.removeSync(path.join(templateWithIgnoredDirs, ".git")); + fse.removeSync(path.join(templateWithIgnoredDirs, "node_modules")); } }); diff --git a/packages/create-remix/__tests__/fixtures/with-git-dir/package.json b/packages/create-remix/__tests__/fixtures/with-ignored-dir/package.json similarity index 100% rename from packages/create-remix/__tests__/fixtures/with-git-dir/package.json rename to packages/create-remix/__tests__/fixtures/with-ignored-dir/package.json diff --git a/packages/create-remix/index.ts b/packages/create-remix/index.ts index 435fa0cbabb..3e2ef3c5f51 100644 --- a/packages/create-remix/index.ts +++ b/packages/create-remix/index.ts @@ -13,6 +13,7 @@ import sortPackageJSON from "sort-package-json"; import { version as thisRemixVersion } from "./package.json"; import { prompt } from "./prompt"; import { + IGNORED_TEMPLATE_DIRECTORIES, color, debug, ensureDirectory, @@ -370,13 +371,15 @@ async function copyTempDirToAppDirStep(ctx: Context) { await fse.copy(ctx.tempDir, ctx.cwd, { filter(src, dest) { - // We never copy .git/ directories since it's highly unlikely we want - // them copied - and because templates are primarily being pulled from - // a git tarball and would not contain .git/ anyway - let isGitDir = stripDirectoryFromPath(ctx.tempDir, src) === ".git"; - if (isGitDir) { + // We never copy .git/ or node_modules/ directories since it's highly + // unlikely we want them copied - and because templates are primarily + // being pulled from git tarballs which won't have .git/ and shouldn't + // have node_modules/ + let file = stripDirectoryFromPath(ctx.tempDir, src); + let isIgnored = IGNORED_TEMPLATE_DIRECTORIES.includes(file); + if (isIgnored) { if (ctx.debug) { - debug("Skipping copy of .git/ directory from template"); + debug(`Skipping copy of ${file} directory from template`); } return false; } diff --git a/packages/create-remix/utils.ts b/packages/create-remix/utils.ts index c5727ce01aa..685147eed30 100644 --- a/packages/create-remix/utils.ts +++ b/packages/create-remix/utils.ts @@ -277,24 +277,18 @@ export function stripDirectoryFromPath(dir: string, filePath: string) { return filePath.replace(new RegExp(`^${dir}${path.sep}+`), ""); } +// We do not copy these folders from templates so we can ignore them for comparisons +export const IGNORED_TEMPLATE_DIRECTORIES = [".git", "node_modules"]; + export async function getDirectoryFilesRecursive(dir: string) { - // Ignore comparing within these directories - but detect a collision - // at the directory level and count it. These get prepended to strippedFiles - // in reverse order below - let ignoreDirs = ["node_modules", ".git"]; let files = await recursiveReaddir(dir, [ (file) => { let strippedFile = stripDirectoryFromPath(dir, file); let parts = strippedFile.split(path.sep); - return parts.length > 1 && ignoreDirs.includes(parts[0]); + return ( + parts.length > 1 && IGNORED_TEMPLATE_DIRECTORIES.includes(parts[0]) + ); }, ]); - let strippedFiles = files.map((f) => stripDirectoryFromPath(dir, f)); - ignoreDirs.forEach((dir) => { - let dirPath = path.join(dir, dir); - if (fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory()) { - strippedFiles.unshift(stripDirectoryFromPath(dir, dirPath)); - } - }); - return strippedFiles; + return files.map((f) => stripDirectoryFromPath(dir, f)); }