1
- import os from "os" ;
2
- import fs from "fs-extra" ;
3
1
import path from "path" ;
4
- import inquirer from "inquirer" ;
5
2
import { Command } from "commander" ;
6
- import logger from "./utils/logger" ; // Assuming you have a logger utility
7
- import open from "open"
3
+ import logger from "./utils/logger" ;
8
4
import Invoice from "./utils/invoice" ;
5
+ import minimist from "minimist"
6
+ import { initConfig , openConfigDirectory , openConfigurationFile , timeout } from "./utils" ;
7
+ import * as os from "os"
8
+ import inquirer from "inquirer" ;
9
+
10
+ const args = minimist ( process . argv . slice ( 2 ) )
11
+ const isDebug = args . debug || false
12
+
13
+ logger . setDebugMode ( isDebug )
9
14
10
15
const program = new Command ( ) ;
11
16
12
17
export const DEFAULT_DIR = path . join ( os . homedir ( ) , ".config" , "icli/" ) ;
18
+ export const DEFAULT_INVOICES_DIR = path . join ( os . homedir ( ) , "Documents" , "Invoices/" )
13
19
export const DEFAULT_PATH = path . join ( os . homedir ( ) , ".config" , "icli/icli.json" ) ;
14
20
export const INVOICE_PATH = path . join ( path . dirname ( DEFAULT_PATH ) , "invoices.json" ) ;
15
21
16
- export function configSetup ( configPath : string ) {
17
- return fs . existsSync ( configPath ) ;
18
- }
19
-
20
- async function initConfig ( ) {
21
- if ( ! configSetup ( DEFAULT_PATH ) ) {
22
- const config = {
23
- appName : "Invoice CLI Tool" ,
24
- version : "1.0.0" ,
25
- createdAt : new Date ( ) . toISOString ( ) ,
26
- } ;
27
-
28
- try {
29
- fs . ensureFileSync ( DEFAULT_PATH ) ;
30
- fs . writeJsonSync ( DEFAULT_PATH , config , { spaces : 2 } ) ;
31
- logger . info ( `Your configuration file has been set up.` ) ;
32
- } catch ( error ) {
33
- console . log ( error )
34
- logger . error ( 'Error setting up configuration file' ) ;
35
- }
36
- }
37
- }
38
-
39
-
40
- async function openConfigDirectory ( ) {
41
- const configDir = path . dirname ( DEFAULT_PATH ) ;
42
- try {
43
- await open ( configDir ) ; // Open the directory
44
- logger . info ( `Opened configuration directory: ${ configDir } ` ) ;
45
- } catch ( error ) {
46
- console . log ( error )
47
- logger . error ( "Error opening configuration directory" )
48
- }
49
- }
50
-
51
-
52
-
53
- program
54
- . version ( "1.0.0" )
55
- . description ( "Invoice CLI Tool" ) ;
56
-
57
- program
58
- . command ( "setup" )
59
- . description ( "Setup configuration file" )
60
- . action ( async ( ) => {
61
- await initConfig ( ) ;
62
- mainMenu ( ) ;
63
- } ) ;
64
-
65
- program
66
- . command ( "history" )
67
- . description ( "View invoice history" )
68
- . action ( ( ) => {
69
- new Invoice ( ) . viewHistory ( ) ;
70
- mainMenu ( ) ;
71
- } ) ;
72
-
73
- program
74
- . command ( "create" )
75
- . description ( "Create a new invoice" )
76
- . action ( async ( ) => {
77
- await new Invoice ( ) . createInvoice ( ) ;
78
- mainMenu ( ) ;
79
- } ) ;
80
-
81
- program
82
- . command ( 'config' )
83
- . description ( 'Open the configuration file.' )
84
- . action ( openConfigDirectory )
85
-
86
- program
87
- . command ( "exit" )
88
- . description ( "Exit the CLI" )
89
- . action ( ( ) => {
90
- logger . info ( "Exiting CLI..." ) ;
91
- process . exit ( 0 ) ;
92
- } ) ;
93
-
94
- // Main menu to loop back to choices
95
- async function mainMenu ( ) {
22
+ export async function mainMenu ( ) {
96
23
const choices = await inquirer . prompt ( [
97
24
{
98
25
type : "list" ,
@@ -117,4 +44,80 @@ async function mainMenu() {
117
44
mainMenu ( ) ;
118
45
}
119
46
47
+
48
+ const commands = [
49
+ {
50
+ name : "setup" ,
51
+ description : "Setup configuration file" ,
52
+ action : async ( ) => {
53
+ await initConfig ( ) ;
54
+ await mainMenu ( ) ;
55
+ }
56
+ } ,
57
+ {
58
+ name : "history" ,
59
+ description : "View invoice history" ,
60
+ action : async ( ) => {
61
+ new Invoice ( ) . viewHistory ( )
62
+ await mainMenu ( )
63
+ }
64
+ } ,
65
+ {
66
+ name : "create" ,
67
+ description : "Create a new invoice" ,
68
+ action : async ( ) => {
69
+ await new Invoice ( ) . createInvoice ( args . explicit ? false : true )
70
+ await mainMenu ( )
71
+ } ,
72
+ option : [ {
73
+ value : 'explicit' ,
74
+ description : "Explicitly select the values for your personal details"
75
+ } ]
76
+ } ,
77
+ {
78
+ name : "config" ,
79
+ description : "Open the configuration file" ,
80
+ action : openConfigurationFile
81
+ } ,
82
+ {
83
+ name : "cd" ,
84
+ description : "Open the configuration directory" ,
85
+ action : openConfigDirectory
86
+ } ,
87
+ {
88
+ name : "defaults" ,
89
+ description : "Setup your default details for creating invoices." ,
90
+ // options: [Object.keys(Invoice.prototype.defaultOptionPrompts).map(k => ({value: k, description: `Change default value for: ${k}`}))],
91
+ options :
92
+ Object . keys ( new Invoice ( ) . defaultOptionPrompts )
93
+ . map ( o => ( { value : o . toLowerCase ( ) , description : `Change default for: ${ o } ` } ) ) ,
94
+ action : async ( ) => {
95
+ await new Invoice ( ) . setupDefaultValues ( Array . from ( new Set ( Object . keys ( args ) . filter ( o => o !== "_" && o !== "debug" ) ) ) )
96
+ } ,
97
+ } ,
98
+ {
99
+ name : "exit" ,
100
+ description : "Exit the CLI tool" ,
101
+ action : ( ) => {
102
+ logger . info ( 'Exiting CLI...' )
103
+ process . exit ( 0 )
104
+ }
105
+ } ,
106
+ ]
107
+ program
108
+ . version ( "1.0.0" )
109
+ . description ( "Invoice CLI Tool" )
110
+ . option ( '--debug' , "Display external logs" )
111
+
112
+ commands . forEach ( ( c ) => {
113
+ const cmd = program
114
+ . command ( c . name )
115
+ . description ( c . description )
116
+ . action ( c . action )
117
+
118
+ c . options ?. forEach ( c => {
119
+ cmd . option ( `--${ c . value } ` , c . description )
120
+ } )
121
+ } )
122
+
120
123
program . parse ( process . argv ) ;
0 commit comments