Skip to content

Commit

Permalink
feat(command): add dev and build commands
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoneto committed Oct 9, 2023
1 parent cee5213 commit b86b598
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 6 deletions.
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@
},
"dependencies": {
"@expressots/boost-ts": "1.1.1",
"@swc/cli": "0.1.62",
"@swc/core": "1.3.80",
"@swc/register": "0.1.10",
"chalk-animation": "2.0.3",
"cli-progress": "3.11.2",
"concurrently": "8.2.1",
"degit": "2.8.4",
"glob": "10.2.6",
"inquirer": "8.0.0",
"mustache": "4.2.0",
"nodemon": "3.0.1",
"rimraf": "5.0.1",
"ts-node": "10.9.1",
"ts-node-dev": "2.0.0",
"tsconfig-paths": "4.2.0",
"yargs": "17.6.2"
},
"devDependencies": {
Expand All @@ -67,6 +75,7 @@
"@types/inquirer": "^9.0.3",
"@types/mustache": "^4.2.2",
"@types/node": "^18.11.19",
"@types/nodemon": "^1.19.2",
"@types/yargs": "^17.0.22",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
Expand All @@ -77,8 +86,6 @@
"husky": "^8.0.3",
"prettier": "^2.8.4",
"release-it": "^16.1.5",
"rimraf": "^4.1.2",
"ts-node-dev": "^2.0.0",
"typescript": "^4.9.5",
"vite": "^4.4.9",
"vitest": "^0.34.4"
Expand Down
7 changes: 7 additions & 0 deletions src/@types/command-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface CommandDevArgs {
experimental: boolean;
}

export interface CommandBuildArgs {
experimental: boolean;
}
1 change: 1 addition & 0 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./config";
export * from "./template";
export * from "./command-args";
13 changes: 13 additions & 0 deletions src/build/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CommandModule } from "yargs";
import { projectForm } from "./form";
import { CommandDevArgs } from "../@types";

const buildProject = (): CommandModule<Record<string, never>, CommandDevArgs> => {
return {
command: "build",
describe: "Build project",
handler: projectForm,
};
};

export { buildProject };
45 changes: 45 additions & 0 deletions src/build/form.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import path from "node:path";
import { existsSync } from "node:fs";
import { spawnSync } from "node:child_process";
import { CommandBuildArgs } from "../@types";
import { printError } from "../utils/cli-ui";
import { getPlatformCommand } from "../utils/get-platform-command-bin";

const PATH = `${process.env.PATH}${path.delimiter}${path.resolve(__dirname, "../../node_modules/.bin")}`;
const env = { ...process.env, PATH };

const projectForm = async ({ experimental }: CommandBuildArgs): Promise<void> => {
console.time("Build succeed");

spawnSync("rimraf", ["dist"], { env, stdio: "inherit" });

if (experimental) {
if (!existsSync(".swcrc")) {
printError("Experimental features needs .swcrc file", ".swcrc");
process.exit(1);
}

const { result } = require("concurrently")([
{ name: "types", command: "tsc --noEmit" },
{ name: "lint", command: "eslint src/**/*.ts" },
{ name: "build", command: "swc src -d dist" },
], { raw: true });

result
.then(() => {
console.timeEnd("Build succeed");
})
.catch(() => {
printError("Build failed", "expressots build");
process.exit(1);
});

return;
}

spawnSync(getPlatformCommand("tsc"), ["-p", "tsconfig.build.json"], { env, stdio: "inherit" });
console.timeEnd("Build succeed");
};

export { projectForm };
1 change: 1 addition & 0 deletions src/build/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./cli";
4 changes: 4 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { hideBin } from "yargs/helpers";
import { generateProject } from "./generate";
import { infoProject } from "./info";
import { createProject } from "./new";
import { devProject } from "./dev";
import { buildProject } from "./build";

export const CLI_VERSION = "1.3.4";

Expand All @@ -15,6 +17,8 @@ yargs(hideBin(process.argv))
.command(createProject())
.command(generateProject())
.command(infoProject())
.command(devProject())
.command(buildProject())
.example("$0 new expressots-demo", "Create interactively")
.example("$0 new expressots-demo -d ./", "Create interactively with path")
.example("$0 new expressots-demo -p yarn -t opinionated", "Create silently")
Expand Down
13 changes: 13 additions & 0 deletions src/dev/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CommandModule } from "yargs";
import { projectForm } from "./form";
import { CommandDevArgs } from "../@types";

const devProject = (): CommandModule<Record<string, never>, CommandDevArgs> => {
return {
command: "dev",
describe: "Run project in development mode",
handler: projectForm,
};
};

export { devProject };
38 changes: 38 additions & 0 deletions src/dev/form.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import path from "node:path";
import { existsSync } from "node:fs";
import { spawn, spawnSync } from "node:child_process";
import nodemon from "nodemon";
import { CommandDevArgs } from "../@types";
import { printError } from "../utils/cli-ui";
import { getPlatformCommand } from "../utils/get-platform-command-bin";

const projectForm = async ({ experimental }: CommandDevArgs): Promise<void> => {
if (experimental) {
if (!existsSync(".swcrc")) {
printError("Experimental features needs .swcrc file", ".swcrc");
process.exit(1);
}

require('@swc/register');

function nodemonRestart() {
spawn(getPlatformCommand("eslint"), ["src/**/*.ts"], { stdio: "inherit" });
spawn(getPlatformCommand("tsc"), ["--noEmit"], { stdio: "inherit" });
}

nodemon({
ext: "ts",
exec: `node -r ${require.resolve("@swc/register")} src/main.ts`,
})
.on("start", nodemonRestart)
.on("restart", nodemonRestart);


return;
}

spawnSync(getPlatformCommand("ts-node-dev"), ["src/main.ts"], { stdio: "inherit" });
};

export { projectForm };
1 change: 1 addition & 0 deletions src/dev/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./cli";
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./types";
export * from "./generate";
export * from "./utils";
export * from "./new";
export * from "./dev";
6 changes: 2 additions & 4 deletions src/new/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import path from "node:path";
import { centerText } from "../utils/center-text";
import { printError } from "../utils/cli-ui";
import { TemplateEnum } from "../@types";
import { getPlatformCommand } from "../utils/get-platform-command-bin";
import templateList from "../templates-list";

async function packageManagerInstall({
Expand All @@ -20,10 +21,7 @@ async function packageManagerInstall({
progressBar: SingleBar;
}) {
return new Promise((resolve, reject) => {
const isWindows: boolean = process.platform === "win32";
const command: string = isWindows
? `${packageManager}.cmd`
: packageManager;
const command = getPlatformCommand(packageManager);

const installProcess = spawn(command, ["install"], {
cwd: directory,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/get-platform-command-bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function getPlatformCommand(binName: string) {
const isWindows = process.platform === "win32";
return isWindows ? `${binName}.cmd` : binName;
}

export { getPlatformCommand };

0 comments on commit b86b598

Please sign in to comment.