Skip to content

Commit

Permalink
Don't copy node_modules either
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Aug 18, 2023
1 parent 1524833 commit ed45485
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
28 changes: 22 additions & 6 deletions packages/create-remix/__tests__/create-remix-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -897,7 +911,7 @@ describe("create-remix CLI", () => {
args: [
projectDir,
"--template",
path.join(__dirname, "fixtures", "with-git-dir"),
templateWithIgnoredDirs,
"--no-git-init",
"--no-install",
],
Expand All @@ -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"));
}
});

Expand Down
15 changes: 9 additions & 6 deletions packages/create-remix/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 7 additions & 13 deletions packages/create-remix/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

0 comments on commit ed45485

Please sign in to comment.