Skip to content

Commit

Permalink
add cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
omermecitoglu committed Jul 30, 2024
1 parent ecd0f19 commit b4e70cc
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"license": "MIT",
"main": "bin/index.js",
"bin": {
"buttler": "./bin/index.js"
"buttler": "./bin/cli.js"
},
"files": [
"bin/"
Expand All @@ -48,6 +48,7 @@
},
"dependencies": {
"@figma/nodegit": "^0.28.0-figma.6",
"commander": "^12.1.0",
"dockerode": "^4.0.2",
"dotenv": "^16.4.5",
"express": "^4.19.2",
Expand Down
66 changes: 66 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env node
import { spawn } from "node:child_process";
import fs from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { Command } from "commander";

const program = new Command();

async function checkFile(filePath: string) {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
}

function spawnChildProcess(detached: boolean) {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const appPath = resolve(__dirname, "index.js");
const child = spawn("node", [appPath], {
detached,
stdio: detached ? "ignore" : "inherit",
env: {
PORT: "2083",
},
});
if (detached) child.unref();
return child.pid;
}

program.command("start").description("starts the application").action(async () => {
const pidFilePath = resolve(process.cwd(), "PID");
const exists = await checkFile(pidFilePath);
if (exists) {
return console.warn("the application is already running");
}
const pid = spawnChildProcess(true);
await fs.writeFile(pidFilePath, `${pid}`, "utf8");
console.log("the application has started");
});

program.command("stop").description("stops the application").action(async () => {
const pidFilePath = resolve(process.cwd(), "PID");
const exists = await checkFile(pidFilePath);
if (!exists) {
return console.warn("the application was not running");
}
const content = await fs.readFile(pidFilePath, "utf-8");
const pid = parseInt(content);
if (isNaN(pid)) {
return console.error("Invalid PID");
}
try {
process.kill(pid);
console.log("the application is stopped");
} catch {
console.warn("the application was crashed");
} finally {
await fs.unlink(pidFilePath);
}
});

program.parse(process.argv);
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env node
import http from "node:http";
import readline from "node:readline";
import dotenv from "dotenv";
Expand Down

0 comments on commit b4e70cc

Please sign in to comment.