11import { generateSVG } from "./svg" ;
22import * as fs from "fs" ;
33import * as path from "path" ;
4- import { Command } from "commander" ;
54
6- // Helper to get input from environment variables (for GitHub Actions compatibility)
75function 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
4048if ( ! 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
5057const outDir = path . join ( process . cwd ( ) , "output" ) ;
5158if ( ! fs . existsSync ( outDir ) ) {
5259 fs . mkdirSync ( outDir , { recursive : true } ) ;
5360}
5461
55- // Set empty days option
5662const 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).
6063const isGitHubActions = process . env . GITHUB_ACTIONS === "true" ;
6164
6265if ( 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