Skip to content

Commit

Permalink
Add allow-non-empty flag to create-remix CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Aug 17, 2023
1 parent fc3be8c commit 51525f2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/allow-non-empty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-remix": minor
---

Add `--allow-non-empty` flag to `create-remix` CLI to allow creating projects in non-empty directories
4 changes: 4 additions & 0 deletions docs/other-api/create-remix.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ To create a new project from a template in a private GitHub repo, pass the `--to
</docs-info>
</aside>

### `create-remix --allow-non-empty`

By default, `create-remix` requires that the directory you are creating your app into is empty. You can disable this requirement with `--allow-non-empty`, but beware that if you have files/folders that match files/folders in the template, the template versions will **overwrite** the version in your local directory when this flag is used.

[templates]: ../pages/templates
23 changes: 22 additions & 1 deletion packages/create-remix/__tests__/create-remix-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,34 @@ describe("create-remix CLI", () => {
expect(
stderr.trim().replace("<TEMP_DIR>\\", "<TEMP_DIR>/") // Normalize Windows path
).toMatchInlineSnapshot(
`"▲ Oh no! Project directory \\"<TEMP_DIR>/non-interactive-not-empty-dir\\" is not empty"`
`"▲ Oh no! Project directory \\"<TEMP_DIR>/non-interactive-not-empty-dir\\" is not empty. Please use the \`--allow-non-empty\` flag if you wish to write your app into this directory."`
);
expect(status).toBe(1);
expect(fse.existsSync(path.join(notEmptyDir, "package.json"))).toBeFalsy();
expect(fse.existsSync(path.join(notEmptyDir, "app/root.tsx"))).toBeFalsy();
});

it("allows non-empty directories when --allow-non-empty is specified", async () => {
let notEmptyDir = getProjectDir("non-interactive-not-empty-dir-allowed");
fse.mkdirSync(notEmptyDir);
fse.createFileSync(path.join(notEmptyDir, "some-file.txt"));

let { status, stderr, stdout } = await execCreateRemix({
args: [notEmptyDir, "--no-install", "--no-git-init", "--allow-non-empty"],
interactive: false,
});

expect(
stdout.trim().replace("<TEMP_DIR>\\", "<TEMP_DIR>/") // Normalize Windows path
).toMatch(
'Directory: Using non-empty directory "<TEMP_DIR>/non-interactive-not-empty-dir-allowed" as a project directory due to --allow-non-empty'
);
expect(stderr).toBe("");
expect(status).toBe(0);
expect(fse.existsSync(path.join(notEmptyDir, "package.json"))).toBeTruthy();
expect(fse.existsSync(path.join(notEmptyDir, "app/root.tsx"))).toBeTruthy();
});

it("works for GitHub username/repo combo", async () => {
let projectDir = getProjectDir("github-username-repo");

Expand Down
23 changes: 19 additions & 4 deletions packages/create-remix/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ async function getContext(argv: string[]): Promise<Context> {
"--V": "--version",
"--no-color": Boolean,
"--no-motion": Boolean,
"--allow-non-empty": Boolean,
},
{ argv, permissive: true }
);
Expand All @@ -110,6 +111,7 @@ async function getContext(argv: string[]): Promise<Context> {
"--no-motion": noMotion,
"--yes": yes,
"--version": versionRequested,
"--allow-non-empty": allowNonEmpty = false,
} = flags;

let cwd = flags["_"][0] as string;
Expand Down Expand Up @@ -159,6 +161,7 @@ async function getContext(argv: string[]): Promise<Context> {
template,
token,
versionRequested,
allowNonEmpty,
};

return context;
Expand All @@ -184,6 +187,7 @@ interface Context {
template?: string;
token?: string;
versionRequested?: boolean;
allowNonEmpty: boolean;
}

async function introStep(ctx: Context) {
Expand Down Expand Up @@ -213,10 +217,12 @@ async function projectNameStep(ctx: Context) {
throw new Error("No project directory provided");
}

if (!cwdIsEmpty) {
if (!cwdIsEmpty && !ctx.allowNonEmpty) {
error(
"Oh no!",
`Project directory "${color.reset(ctx.cwd)}" is not empty`
`Project directory "${color.reset(ctx.cwd)}" is not empty. ` +
"Please use the `--allow-non-empty` flag if you wish to write " +
"your app into this directory."
);
throw new Error("Project directory is not empty");
}
Expand All @@ -231,12 +237,21 @@ async function projectNameStep(ctx: Context) {
color.reset(ctx.cwd),
" as project directory",
]);
} else if (ctx.allowNonEmpty) {
info("Directory:", [
"Using non-empty directory ",
color.reset(`"${ctx.cwd}"`),
" as a project directory due to --allow-non-empty",
]);
} else {
info("Hmm...", [color.reset(`"${ctx.cwd}"`), " is not empty!"]);
info("Hmm...", [
color.reset(`"${ctx.cwd}"`),
" is not empty! Did you mean to use the `--allow-non-empty` flag?",
]);
}
}

if (!ctx.cwd || !cwdIsEmpty) {
if (!ctx.cwd || (!cwdIsEmpty && !ctx.allowNonEmpty)) {
let { name } = await ctx.prompt({
name: "name",
type: "text",
Expand Down

0 comments on commit 51525f2

Please sign in to comment.