Skip to content

Commit

Permalink
fix: unique check on create-resource with refine-cli (refinedev#3063)
Browse files Browse the repository at this point in the history
* feat: add unique check

* chore: changeset

* fix: remove unuse lib

* refactor: move getResourcePath method to utils folder
  • Loading branch information
yildirayunlu authored Nov 25, 2022
1 parent 27df262 commit 949b8bd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-cooks-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pankod/refine-cli": patch
---

Added unique check on `create-resource` with `refine-cli`.
46 changes: 17 additions & 29 deletions packages/cli/src/commands/create-resource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import temp from "temp";
import { plural } from "pluralize";
import execa from "execa";
import inquirer from "inquirer";
import { join } from "path";

import { getProjectType, getUIFramework } from "@utils/project";
import { compileDir } from "@utils/compile";
import { uppercaseFirstChar } from "@utils/text";
import { ProjectTypes } from "@definitions/projectTypes";
import { getResourcePath } from "@utils/resource";

const defaultActions = ["list", "create", "edit", "show"];
const load = (program: Command) => {
Expand Down Expand Up @@ -45,8 +46,6 @@ const action = async (
let { args } = options;
let actions = params.actions;

// check actions

if (!args.length) {
// TODO: Show inquirer
const { name, selectedActions } = await inquirer.prompt<{
Expand Down Expand Up @@ -80,6 +79,21 @@ const action = async (

args.forEach((resourceName) => {
const customActions = actions ? actions.split(",") : undefined;
const { path, alias } = getResourcePath(getProjectType());
const resourceFolderName = plural(resourceName).toLowerCase();

// check exist resource
const resourcePath = join(process.cwd(), path, resourceFolderName);

if (pathExistsSync(resourcePath)) {
console.error(
`Resource (${join(
path,
resourceFolderName,
)}) already exist! ❌`,
);
return;
}

// uppercase first letter
const resource = uppercaseFirstChar(resourceName);
Expand Down Expand Up @@ -116,13 +130,10 @@ const action = async (
});
}

const { alias } = getResourcePath(projectType);

// create desctination dir
mkdirSync(params.path, { recursive: true });

// copy to destination
const resourceFolderName = plural(resourceName).toLowerCase();
const destinationResourcePath = `${params.path}/${resourceFolderName}`;

let moveSyncOptions = {};
Expand Down Expand Up @@ -173,27 +184,4 @@ const generateTempDir = (): string => {
return temp.mkdirSync("resource");
};

const getResourcePath = (
projectType: ProjectTypes,
): { path: string; alias: string } => {
switch (projectType) {
case ProjectTypes.NEXTJS:
return {
path: "src/components",
alias: "../src/components",
};
case ProjectTypes.REMIX:
return {
path: "app/components",
alias: "~/components",
};
}

// vite and react
return {
path: "src/pages",
alias: "pages",
};
};

export default load;
24 changes: 24 additions & 0 deletions packages/cli/src/utils/resource/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ProjectTypes } from "@definitions/projectTypes";

export const getResourcePath = (
projectType: ProjectTypes,
): { path: string; alias: string } => {
switch (projectType) {
case ProjectTypes.NEXTJS:
return {
path: "src/components",
alias: "../src/components",
};
case ProjectTypes.REMIX:
return {
path: "app/components",
alias: "~/components",
};
}

// vite and react
return {
path: "src/pages",
alias: "pages",
};
};

0 comments on commit 949b8bd

Please sign in to comment.