Skip to content

Commit 90a6e46

Browse files
committed
feat: get rid of commander
1 parent 6a6ea3d commit 90a6e46

File tree

4 files changed

+79
-83
lines changed

4 files changed

+79
-83
lines changed

dist/cli.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,53 +36,61 @@ Object.defineProperty(exports, "__esModule", { value: true });
3636
const svg_1 = require("./svg");
3737
const fs = __importStar(require("fs"));
3838
const path = __importStar(require("path"));
39-
const commander_1 = require("commander");
40-
// Helper to get input from environment variables (for GitHub Actions compatibility)
4139
function getInput(name, fallback) {
42-
// GitHub Actions sets INPUT_<name> (uppercase, underscores)
4340
const envName = `INPUT_${name.replace(/-/g, "_").toUpperCase()}`;
4441
return process.env[envName] || fallback;
4542
}
46-
// Create program
47-
const program = new commander_1.Command();
48-
program
49-
.name("github-breakout-cli")
50-
.description("Generate a GitHub Breakout SVG")
51-
.option("--username <github-username>", "GitHub username (or set GITHUB_USERNAME or INPUT_GITHUB_USERNAME env var)", getInput("GITHUB_USERNAME", process.env.GITHUB_USERNAME))
52-
.option("--token <github-token>", "GitHub token (or set GITHUB_TOKEN or INPUT_GITHUB_TOKEN env var)", getInput("GITHUB_TOKEN", process.env.GITHUB_TOKEN))
53-
.option("--dark", "Generate dark mode SVG", false)
54-
.option("--enable-empty-days", "Empty days be used as bricks", !!getInput("ENABLE_EMPTY_DAYS"));
55-
// Parse arguments
56-
program.parse(process.argv);
57-
const options = program.opts();
58-
// Check that we have username and token
43+
function parseArgs(argv) {
44+
const parsed = {};
45+
for (let i = 0; i < argv.length; i++) {
46+
const arg = argv[i];
47+
if (arg === "--username" && i + 1 < argv.length) {
48+
parsed.username = argv[++i];
49+
}
50+
else if (arg === "--token" && i + 1 < argv.length) {
51+
parsed.token = argv[++i];
52+
}
53+
else if (arg === "--dark") {
54+
parsed.dark = true;
55+
}
56+
else if (arg === "--enable-empty-days") {
57+
parsed.enableEmptyDays = true;
58+
}
59+
}
60+
return parsed;
61+
}
62+
const cliArgs = parseArgs(process.argv.slice(2));
63+
const options = {
64+
username: cliArgs.username ||
65+
getInput("GITHUB_USERNAME", process.env.GITHUB_USERNAME),
66+
token: cliArgs.token || getInput("GITHUB_TOKEN", process.env.GITHUB_TOKEN),
67+
dark: !!cliArgs.dark,
68+
enableEmptyDays: typeof cliArgs.enableEmptyDays !== "undefined"
69+
? cliArgs.enableEmptyDays
70+
: !!getInput("ENABLE_EMPTY_DAYS"),
71+
};
5972
if (!options.username || !options.token) {
6073
console.error("Error: Both a GitHub username and token are required.\n" +
6174
"Provide via --username/--token or set GITHUB_USERNAME and GITHUB_TOKEN as environment variables.\n" +
6275
"Or use in GitHub Actions with 'github_username' and 'github_token' inputs.");
6376
process.exit(1);
6477
}
65-
// Create output directory
6678
const outDir = path.join(process.cwd(), "output");
6779
if (!fs.existsSync(outDir)) {
6880
fs.mkdirSync(outDir, { recursive: true });
6981
}
70-
// Set empty days option
7182
const ignoreEmptyDays = !options.enableEmptyDays;
72-
// Behavior: If running in GitHub Actions, always generate both light and dark SVGs.
73-
// Otherwise, generate the single requested mode (light by default, dark if --dark is passed).
7483
const isGitHubActions = process.env.GITHUB_ACTIONS === "true";
7584
if (isGitHubActions) {
76-
// Generate both light and dark SVGs for GitHub Actions
7785
const variants = [
7886
{ darkMode: false, name: "light" },
7987
{ darkMode: true, name: "dark" },
8088
];
81-
Promise.all(variants.map(({ darkMode, name }) => (0, svg_1.generateSVG)(options.username, options.token, {
82-
darkMode,
83-
ignoreEmptyDays,
89+
Promise.all(variants.map((variant) => (0, svg_1.generateSVG)(options.username, options.token, {
90+
darkMode: variant.darkMode,
91+
ignoreEmptyDays: ignoreEmptyDays,
8492
}).then((svg) => {
85-
const outputFile = path.join(outDir, `${name}.svg`);
93+
const outputFile = path.join(outDir, `${variant.name}.svg`);
8694
fs.writeFileSync(outputFile, svg);
8795
console.log(`SVG generated: ${outputFile}`);
8896
}))).catch((err) => {
@@ -91,12 +99,11 @@ if (isGitHubActions) {
9199
});
92100
}
93101
else {
94-
// Generate a single SVG (default: light, or dark if --dark)
95102
const darkMode = !!options.dark;
96103
const outputFile = path.join(outDir, `${darkMode ? "dark" : "light"}.svg`);
97104
(0, svg_1.generateSVG)(options.username, options.token, {
98-
darkMode,
99-
ignoreEmptyDays,
105+
darkMode: darkMode,
106+
ignoreEmptyDays: ignoreEmptyDays,
100107
})
101108
.then((svg) => {
102109
fs.writeFileSync(outputFile, svg);

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"@types/node": "^24.0.14",
1414
"@typescript-eslint/eslint-plugin": "^8.37.0",
1515
"@typescript-eslint/parser": "^8.37.0",
16-
"commander": "^14.0.0",
1716
"eslint": "^9.31.0",
1817
"eslint-config-prettier": "^10.1.5",
1918
"eslint-plugin-prettier": "^5.5.1",

src/cli.ts

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
11
import { generateSVG } from "./svg";
22
import * as fs from "fs";
33
import * as path from "path";
4-
import { Command } from "commander";
54

6-
// Helper to get input from environment variables (for GitHub Actions compatibility)
75
function getInput(name: string, fallback?: string) {
8-
// GitHub Actions sets INPUT_<name> (uppercase, underscores)
96
const envName = `INPUT_${name.replace(/-/g, "_").toUpperCase()}`;
107
return process.env[envName] || fallback;
118
}
129

13-
// Create program
14-
const program = new Command();
15-
program
16-
.name("github-breakout-cli")
17-
.description("Generate a GitHub Breakout SVG")
18-
.option(
19-
"--username <github-username>",
20-
"GitHub username (or set GITHUB_USERNAME or INPUT_GITHUB_USERNAME env var)",
21-
getInput("GITHUB_USERNAME", process.env.GITHUB_USERNAME),
22-
)
23-
.option(
24-
"--token <github-token>",
25-
"GitHub token (or set GITHUB_TOKEN or INPUT_GITHUB_TOKEN env var)",
26-
getInput("GITHUB_TOKEN", process.env.GITHUB_TOKEN),
27-
)
28-
.option("--dark", "Generate dark mode SVG", false)
29-
.option(
30-
"--enable-empty-days",
31-
"Empty days be used as bricks",
32-
!!getInput("ENABLE_EMPTY_DAYS"),
33-
);
10+
interface ParsedArgs {
11+
username?: string;
12+
token?: string;
13+
dark?: boolean;
14+
enableEmptyDays?: boolean;
15+
}
16+
17+
function parseArgs(argv: string[]): ParsedArgs {
18+
const parsed: ParsedArgs = {};
19+
for (let i = 0; i < argv.length; i++) {
20+
const arg = argv[i];
21+
if (arg === "--username" && i + 1 < argv.length) {
22+
parsed.username = argv[++i];
23+
} else if (arg === "--token" && i + 1 < argv.length) {
24+
parsed.token = argv[++i];
25+
} else if (arg === "--dark") {
26+
parsed.dark = true;
27+
} else if (arg === "--enable-empty-days") {
28+
parsed.enableEmptyDays = true;
29+
}
30+
}
31+
return parsed;
32+
}
33+
34+
const cliArgs = parseArgs(process.argv.slice(2));
3435

35-
// Parse arguments
36-
program.parse(process.argv);
37-
const options = program.opts();
36+
const options = {
37+
username:
38+
cliArgs.username ||
39+
getInput("GITHUB_USERNAME", process.env.GITHUB_USERNAME),
40+
token: cliArgs.token || getInput("GITHUB_TOKEN", process.env.GITHUB_TOKEN),
41+
dark: !!cliArgs.dark,
42+
enableEmptyDays:
43+
typeof cliArgs.enableEmptyDays !== "undefined"
44+
? cliArgs.enableEmptyDays
45+
: !!getInput("ENABLE_EMPTY_DAYS"),
46+
};
3847

39-
// Check that we have username and token
4048
if (!options.username || !options.token) {
4149
console.error(
4250
"Error: Both a GitHub username and token are required.\n" +
@@ -46,33 +54,27 @@ if (!options.username || !options.token) {
4654
process.exit(1);
4755
}
4856

49-
// Create output directory
5057
const outDir = path.join(process.cwd(), "output");
5158
if (!fs.existsSync(outDir)) {
5259
fs.mkdirSync(outDir, { recursive: true });
5360
}
5461

55-
// Set empty days option
5662
const ignoreEmptyDays = !options.enableEmptyDays;
57-
58-
// Behavior: If running in GitHub Actions, always generate both light and dark SVGs.
59-
// Otherwise, generate the single requested mode (light by default, dark if --dark is passed).
6063
const isGitHubActions = process.env.GITHUB_ACTIONS === "true";
6164

6265
if (isGitHubActions) {
63-
// Generate both light and dark SVGs for GitHub Actions
6466
const variants = [
6567
{ darkMode: false, name: "light" },
6668
{ darkMode: true, name: "dark" },
6769
];
6870

6971
Promise.all(
70-
variants.map(({ darkMode, name }) =>
71-
generateSVG(options.username, options.token, {
72-
darkMode,
73-
ignoreEmptyDays,
72+
variants.map((variant) =>
73+
generateSVG(options.username!, options.token!, {
74+
darkMode: variant.darkMode,
75+
ignoreEmptyDays: ignoreEmptyDays,
7476
}).then((svg) => {
75-
const outputFile = path.join(outDir, `${name}.svg`);
77+
const outputFile = path.join(outDir, `${variant.name}.svg`);
7678
fs.writeFileSync(outputFile, svg);
7779
console.log(`SVG generated: ${outputFile}`);
7880
}),
@@ -82,12 +84,11 @@ if (isGitHubActions) {
8284
process.exit(1);
8385
});
8486
} else {
85-
// Generate a single SVG (default: light, or dark if --dark)
8687
const darkMode = !!options.dark;
8788
const outputFile = path.join(outDir, `${darkMode ? "dark" : "light"}.svg`);
88-
generateSVG(options.username, options.token, {
89-
darkMode,
90-
ignoreEmptyDays,
89+
generateSVG(options.username!, options.token!, {
90+
darkMode: darkMode,
91+
ignoreEmptyDays: ignoreEmptyDays,
9192
})
9293
.then((svg) => {
9394
fs.writeFileSync(outputFile, svg);

0 commit comments

Comments
 (0)