@@ -5,17 +5,18 @@ import inquirer from "inquirer";
5
5
import { Command } from "commander" ;
6
6
import logger from "./utils/logger" ; // Assuming you have a logger utility
7
7
import open from "open"
8
+ import Invoice from "./utils/invoice" ;
8
9
9
10
const program = new Command ( ) ;
10
- const DEFAULT_PATH = path . join ( os . homedir ( ) , ".config" , "icli/icli.json" ) ;
11
- const INVOICE_PATH = path . join ( path . dirname ( DEFAULT_PATH ) , "invoices.json" ) ;
12
11
13
- // Utility function to check if config exists
12
+ export const DEFAULT_DIR = path . join ( os . homedir ( ) , ".config" , "icli/" ) ;
13
+ export const DEFAULT_PATH = path . join ( os . homedir ( ) , ".config" , "icli/icli.json" ) ;
14
+ export const INVOICE_PATH = path . join ( path . dirname ( DEFAULT_PATH ) , "invoices.json" ) ;
15
+
14
16
export function configSetup ( configPath : string ) {
15
17
return fs . existsSync ( configPath ) ;
16
18
}
17
19
18
- // Initialize configuration setup if not already present
19
20
async function initConfig ( ) {
20
21
if ( ! configSetup ( DEFAULT_PATH ) ) {
21
22
const config = {
@@ -35,16 +36,6 @@ async function initConfig() {
35
36
}
36
37
}
37
38
38
- // Function to handle viewing invoice history
39
- function viewInvoiceHistory ( ) {
40
- if ( ! fs . existsSync ( INVOICE_PATH ) ) {
41
- logger . info ( "No invoice history found." ) ;
42
- } else {
43
- const invoiceHistory = fs . readJsonSync ( INVOICE_PATH ) ;
44
- logger . info ( "Invoice History:" ) ;
45
- console . log ( invoiceHistory ) ; // Display the history to the user
46
- }
47
- }
48
39
49
40
async function openConfigDirectory ( ) {
50
41
const configDir = path . dirname ( DEFAULT_PATH ) ;
@@ -58,73 +49,7 @@ async function openConfigDirectory() {
58
49
}
59
50
60
51
61
- async function createInvoice ( ) {
62
- const templatePath = path . dirname ( DEFAULT_PATH ) ;
63
- const templates = fs . readdirSync ( templatePath ) . filter ( file => file . endsWith ( ".html" ) ) ;
64
-
65
- // Check if templates are available
66
- if ( templates . length === 0 ) {
67
- logger . error ( "No invoice templates found. Please ensure there are .html files in the templates directory." ) ;
68
- return ; // Exit the function early
69
- }
70
-
71
- const answers = await inquirer . prompt ( [
72
- {
73
- type : "list" ,
74
- name : "template" ,
75
- message : "Choose an invoice template:" ,
76
- choices : templates ,
77
- } ,
78
- {
79
- type : "input" ,
80
- name : "companyName" ,
81
- message : "Enter your company name:" ,
82
- } ,
83
- {
84
- type : "number" ,
85
- name : "numItems" ,
86
- message : "How many items do you want to add?" ,
87
- validate : value => value && value > 0 || "Must be at least 1 item" ,
88
- } ,
89
- ] ) ;
90
-
91
- const items = [ ] ;
92
- for ( let i = 0 ; i < answers . numItems ; i ++ ) {
93
- const itemDetails = await inquirer . prompt ( [
94
- {
95
- type : "input" ,
96
- name : "itemName" ,
97
- message : `Enter the name for item ${ i + 1 } :` ,
98
- } ,
99
- {
100
- type : "number" ,
101
- name : "price" ,
102
- message : `Enter the price for item ${ i + 1 } :` ,
103
- validate : value => value && value > 0 || "Price must be greater than zero" ,
104
- } ,
105
- ] ) ;
106
- items . push ( itemDetails ) ;
107
- }
108
-
109
- const invoice = {
110
- template : answers . template ,
111
- companyName : answers . companyName ,
112
- items,
113
- createdAt : new Date ( ) . toISOString ( ) ,
114
- } ;
115
-
116
- let invoiceHistory = [ ] ;
117
- if ( fs . existsSync ( INVOICE_PATH ) ) {
118
- invoiceHistory = fs . readJsonSync ( INVOICE_PATH ) ;
119
- }
120
-
121
- invoiceHistory . push ( invoice ) ;
122
- fs . writeJsonSync ( INVOICE_PATH , invoiceHistory , { spaces : 2 } ) ;
123
-
124
- logger . info ( "Invoice successfully created!" ) ;
125
- }
126
52
127
- // CLI setup with commander
128
53
program
129
54
. version ( "1.0.0" )
130
55
. description ( "Invoice CLI Tool" ) ;
@@ -141,15 +66,15 @@ program
141
66
. command ( "history" )
142
67
. description ( "View invoice history" )
143
68
. action ( ( ) => {
144
- viewInvoiceHistory ( ) ;
69
+ new Invoice ( ) . viewHistory ( ) ;
145
70
mainMenu ( ) ;
146
71
} ) ;
147
72
148
73
program
149
74
. command ( "create" )
150
75
. description ( "Create a new invoice" )
151
76
. action ( async ( ) => {
152
- await createInvoice ( ) ;
77
+ await new Invoice ( ) . createInvoice ( ) ;
153
78
mainMenu ( ) ;
154
79
} ) ;
155
80
@@ -179,19 +104,17 @@ async function mainMenu() {
179
104
180
105
switch ( choices . action ) {
181
106
case "View Invoice History" :
182
- viewInvoiceHistory ( ) ;
107
+ new Invoice ( ) . viewHistory ( ) ;
183
108
break ;
184
109
case "Create a New Invoice" :
185
- await createInvoice ( ) ;
110
+ await new Invoice ( ) . createInvoice ( )
186
111
break ;
187
112
case "Exit CLI" :
188
113
logger . info ( "Exiting CLI..." ) ;
189
114
process . exit ( 0 ) ;
190
115
}
191
116
192
- // Loop back to the main menu after completing an action
193
117
mainMenu ( ) ;
194
118
}
195
119
196
- // Initialize the CLI program
197
120
program . parse ( process . argv ) ;
0 commit comments