Skip to content

Commit

Permalink
fix env issue in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
omermecitoglu committed Jul 30, 2024
1 parent 6dd037a commit a42bf63
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v4
with:
node-version: '20.x'
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
# - run: npm test
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@omer-x/buttler",
"version": "0.1.1",
"version": "0.1.2",
"description": "automates CI/CD workflows using Docker and GitHub webhooks",
"keywords": [
"automation",
Expand Down
25 changes: 16 additions & 9 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ 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);
Expand All @@ -24,14 +22,16 @@ function spawnChildProcess(detached: boolean) {
detached,
stdio: detached ? "ignore" : "inherit",
env: {
...process.env,
PORT: "2083",
WEBHOOK_SECRET: "buttler",
},
});
if (detached) child.unref();
return child.pid;
}

program.command("start").description("starts the application").action(async () => {
async function start() {
const pidFilePath = resolve(process.cwd(), "PID");
const exists = await checkFile(pidFilePath);
if (exists) {
Expand All @@ -40,27 +40,34 @@ program.command("start").description("starts the application").action(async () =
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 () => {
async function stop() {
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 {
if (isNaN(pid)) throw new Error("Invalid PID");
process.kill(pid);
console.log("the application is stopped");
} catch {
console.warn("the application was crashed");
} finally {
await fs.unlink(pidFilePath);
}
});
}

async function restart() {
await stop();
await start();
}

const program = new Command();
program.command("start").description("starts the application").action(start);
program.command("stop").description("stops the application").action(stop);
program.command("restart").description("restarts the application").action(restart);
program.parse(process.argv);

0 comments on commit a42bf63

Please sign in to comment.