Skip to content

Commit

Permalink
feat(@refinedev/cli): add "--platform" option to specify project type (
Browse files Browse the repository at this point in the history
…#4833)

* feat(@refinedev/cli): add "--platform" option to specify project type

* Update .changeset/shiny-cooks-push.md

Co-authored-by: Mahir Mahdi <[email protected]>

---------

Co-authored-by: Mahir Mahdi <[email protected]>
  • Loading branch information
mikeyfarina and MahirMahdi authored Aug 22, 2023
1 parent e462b28 commit 9a24e46
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-cooks-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@refinedev/cli": minor
---

add "--platform" option on cli to start, dev, and build to specify project type
22 changes: 18 additions & 4 deletions packages/cli/src/commands/runner/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import { Command } from "commander";
import { Command, Option } from "commander";
import { getProjectType } from "@utils/project";
import { projectScripts } from "../projectScripts";
import { runScript } from "../runScript";
import { updateNotifier } from "src/update-notifier";
import { getRunnerDescription } from "../utils";
import { getPlatformOptionDescription, getRunnerDescription } from "../utils";
import { ProjectTypes } from "@definitions/projectTypes";

const build = (program: Command) => {
return program
.command("build")
.description(getRunnerDescription("build"))
.allowUnknownOption(true)
.addOption(
new Option(
"-p, --platform <platform>",
getPlatformOptionDescription(),
).choices(
Object.values(ProjectTypes).filter(
(type) => type !== ProjectTypes.UNKNOWN,
),
),
)
.argument("[args...]")
.action(action);
};

const action = async (args: string[]) => {
const projectType = getProjectType();
const action = async (
args: string[],
{ platform }: { platform: ProjectTypes },
) => {
const projectType = getProjectType(platform);

const binPath = projectScripts[projectType].getBin("build");
const script = projectScripts[projectType].build;
Expand Down
24 changes: 19 additions & 5 deletions packages/cli/src/commands/runner/dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import { Command } from "commander";
import { ProjectTypes } from "@definitions/projectTypes";
import { getProjectType } from "@utils/project";
import { Command, Option } from "commander";
import { updateNotifier } from "src/update-notifier";
import { projectScripts } from "../projectScripts";
import { runScript } from "../runScript";
import { updateNotifier } from "src/update-notifier";
import { getRunnerDescription } from "../utils";
import { getPlatformOptionDescription, getRunnerDescription } from "../utils";

const dev = (program: Command) => {
return program
.command("dev")
.description(getRunnerDescription("dev"))
.allowUnknownOption(true)
.addOption(
new Option(
"-p, --platform <platform>",
getPlatformOptionDescription(),
).choices(
Object.values(ProjectTypes).filter(
(type) => type !== ProjectTypes.UNKNOWN,
),
),
)
.argument("[args...]")
.action(action);
};

const action = async (args: string[]) => {
const projectType = getProjectType();
const action = async (
args: string[],
{ platform }: { platform: ProjectTypes },
) => {
const projectType = getProjectType(platform);

const binPath = projectScripts[projectType].getBin("dev");
const script = projectScripts[projectType].dev;
Expand Down
24 changes: 19 additions & 5 deletions packages/cli/src/commands/runner/start/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import { Command } from "commander";
import { ProjectTypes } from "@definitions/projectTypes";
import { getProjectType } from "@utils/project";
import { Command, Option } from "commander";
import { updateNotifier } from "src/update-notifier";
import { projectScripts } from "../projectScripts";
import { runScript } from "../runScript";
import { updateNotifier } from "src/update-notifier";
import { getRunnerDescription } from "../utils";
import { getPlatformOptionDescription, getRunnerDescription } from "../utils";

const start = (program: Command) => {
return program
.command("start")
.description(getRunnerDescription("start"))
.allowUnknownOption(true)
.addOption(
new Option(
"-p, --platform <platform>",
getPlatformOptionDescription(),
).choices(
Object.values(ProjectTypes).filter(
(type) => type !== ProjectTypes.UNKNOWN,
),
),
)
.argument("[args...]")
.action(action);
};

const action = async (args: string[]) => {
const projectType = getProjectType();
const action = async (
args: string[],
{ platform }: { platform: ProjectTypes },
) => {
const projectType = getProjectType(platform);

const binPath = projectScripts[projectType].getBin("start");
const script = projectScripts[projectType].start;
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/commands/runner/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ export const getRunnerDescription = (runner: "dev" | "start" | "build") => {
let projectType = getProjectType();
const command = projectScripts[projectType][runner];

if (projectType === "remix" && runner === "start") {
if (projectType === ProjectTypes.REMIX && runner === "start") {
projectType = "remix-serve" as ProjectTypes;
}

return `It runs: \`${projectType} ${command.join(
" ",
)}\`. Also accepts all the arguments \`${projectType}\` accepts.`;
};

export const getPlatformOptionDescription = () => {
return `Platform to run command on. \nex: ${Object.values(
ProjectTypes,
).join(", ")}`;
};
5 changes: 4 additions & 1 deletion packages/cli/src/utils/project/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ProjectTypes, UIFrameworks } from "@definitions";
import { getDependencies, getDevDependencies } from "@utils/package";

export const getProjectType = (): ProjectTypes => {
export const getProjectType = (platform?: ProjectTypes): ProjectTypes => {
if (platform) {
return platform;
}
// read dependencies from package.json
const dependencies = getDependencies();
const devDependencies = getDevDependencies();
Expand Down

0 comments on commit 9a24e46

Please sign in to comment.