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(remix-dev): Prioritize remix templates in CLI create #2733

Merged
merged 6 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
43 changes: 43 additions & 0 deletions packages/remix-dev/__tests__/cli-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,49 @@ describe("remix CLI", () => {
);
expect(Object.keys(pkgJSON.devDependencies)).not.toContain("typescript");
});

it("prioritizes built-in templates when validating input", async () => {
let projectDir = path.join(TEMP_DIR, "my-js-app");

// create a local directory with the same name as our chosen template and
// give it a package.json so we can check it against the one in our
// template
let dupedDir = path.join(projectDir, "express");
await fse.mkdir(dupedDir);
await fse.writeFile(
path.join(dupedDir, "package.json"),
'{ "name": "dummy" }'
);

let proc = childProcess.spawn(
"node",
[
"--require",
require.resolve("esbuild-register"),
"--require",
path.join(__dirname, "./msw.ts"),
path.resolve(__dirname, "../cli.ts"),
"create",
],
{ stdio: [null, null, null] }
);

await interactWithShell(proc, [
{ question: /Where.*create.*app/i, type: [projectDir, ENTER] },
{ question: /What type of app/i, answer: /basics/i },
{ question: /Where.*deploy/i, answer: /express/i },
{ question: /install/i, type: ["n", ENTER] },
{ question: /typescript or javascript/i, answer: /javascript/i },
]);

expect(
fse.existsSync(path.join(projectDir, "package.json"))
).toBeTruthy();
let pkgJSON = JSON.parse(
fse.readFileSync(path.join(projectDir, "package.json"), "utf-8")
);
expect(pkgJSON.name).not.toBe("dummy");
});
});
});

Expand Down
34 changes: 19 additions & 15 deletions packages/remix-dev/cli/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,14 +555,29 @@ export type TemplateType =
| "local";

export function detectTemplateType(template: string): TemplateType | null {
// 1. Check if the user passed a local file. If they hand us an explicit file
// 1. Prioritize Remix templates and stacks first. This ensures that inputs
// like `--template remix` always pull from our templates, which is almost
// always the desired behavior. If users maintain a fork either locally or
// in another repo they can pass the repo shorthand, URL or path instead.
// This also ensures that our interactive CLI always works as expected even
// if the user has another directory with the same name.
// https://github.com/remix-run/remix/issues/2491
if (isRemixTemplate(template)) {
return "template";
}

if (isRemixStack(template)) {
return "repoTemplate";
}

// 2. Check if the user passed a local file. If they hand us an explicit file
// URL, we'll validate it first. Otherwise we just ping the filesystem to
// see if the string references a filepath and, if not, move on.
if (template.startsWith("file://")) {
return "local";
}

// 2. Check if it's a path to a local directory.
// 3. Check if it's a path to a local directory.
try {
if (
fse.existsSync(
Expand All @@ -577,28 +592,17 @@ export function detectTemplateType(template: string): TemplateType | null {
// ignore FS errors and move on
}

// 3. check if it's one of the pre-built remix stacks
if (isRemixStack(template)) {
return "repoTemplate";
}

// 4. examples/<template> will use an example folder in the Remix repo
if (/^examples?\/[\w-]+$/.test(template)) {
return "example";
}

// 5. If the string contains no slashes, spaces, or special chars, we assume
// it is one of our remix-run/remix/templates.
if (/^[\w-]+$/.test(template)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to do this check since we are verifying our templates statically. I think this is better anyway since we don't have too many and they're easy to verify without fetching.

return "template";
}

// 6. Handle GitHub repos (URLs or :org/:repo shorthand)
// 5. Handle GitHub repos (URLs or :org/:repo shorthand)
if (isValidGithubUrl(template) || isGithubRepoShorthand(template)) {
return "repo";
}

// 7. Any other valid URL should be treated as a tarball.
// 6. Any other valid URL should be treated as a tarball.
if (isUrl(template)) {
return "remoteTarball";
}
Expand Down