22const figlet = require ( 'figlet' ) ;
33const pkg = require ( '../package.json' ) ;
44const os = require ( "os" ) ;
5+ const fs = require ( "fs" ) ;
56const { exec } = require ( "child_process" )
6- var configdir = require ( "path" ) . join ( os . homedir ( ) + "/.scripty" ) ;
7+ var configdir = require ( "path" ) . join ( process . cwd ( ) + "/.scripty/ " ) ;
78const path = require ( 'path' ) ;
89const program = require ( 'commander' ) ;
10+ const { join } = require ( 'path' ) ;
911program . version ( pkg . version )
1012
1113class app {
@@ -17,103 +19,68 @@ class app {
1719
1820 constructor ( args ) {
1921 this . args = args ;
22+ if ( ! fs . existsSync ( join ( configdir , "scripts.json" ) ) ) {
23+ this . configurate ( ) . then ( ( ) => {
24+ this . config = require ( join ( configdir + 'scripts.json' ) )
25+ } )
26+ } else {
27+ this . config = require ( join ( configdir + 'scripts.json' ) )
28+ }
2029 }
2130
2231 async start ( ) {
2332 console . log ( figlet . textSync ( "exscript" , "Big Money-ne" ) ) ;
24- console . log ( `\n\n\tversion: ${ pkg . version } ` ) ;
25- await this . configurate ( ) ;
26- var config = this . config
27- program . option ( "-r, --run" , "run a script" , "default" ) . action ( ( name ) => {
28- if ( config . scripts [ name . run ] ) {
29- var x = config . scripts [ name . run ]
33+ console . log ( `\n\nversion: ${ pkg . version } ` ) ;
34+ process . on ( "beforeExit" , ( ) => {
35+ fs . writeFile ( join ( configdir , "scripts.json" ) , JSON . stringify ( require ( "./templates/scripts.json" ) , null , 4 ) , ( ) => {
36+ this . config = require ( join ( configdir + 'scripts.json' ) )
37+ } )
38+ } )
39+ program . command ( "run <name>" ) . description ( "run a script" ) . action ( ( name ) => {
40+ if ( ! name ) return ;
41+ console . log ( `running` , name )
42+ if ( this . config . scripts [ name ] ) {
43+ var x = this . config . scripts [ name ]
3044 this . sh ( x . run . command ) . then ( ( put ) => {
31- if ( put . stderr != '' ) {
32- console . log ( stderr )
45+ if ( put . stderr != '' ) {
46+ console . log ( stderr )
47+ process . exit ( )
3348 } else {
3449 console . log ( put . stdout )
50+ process . exit ( )
3551 }
3652 } )
53+ } else {
54+ console . log ( "Not Found" )
55+ process . exit ( )
3756 }
3857 } )
3958
40- program . command ( "config" ) . option ( "-e, --explorer" , "open in explorer" ) . description ( "opens config" ) . action ( ( opts ) => {
41- if ( ! opts . explorer ) {
42- this . sh ( `notepad ${ path . join ( configdir , "scripts.json" ) } ` ) . then ( console . log ( "done" ) )
59+ program . command ( "this. config" ) . option ( "-e, --explorer" , "open in explorer" ) . description ( "opens this. config" ) . action ( ( opts ) => {
60+ if ( ! opts . explorer ) {
61+ this . sh ( `notepad ${ path . join ( this . configdir , "scripts.json" ) } ` ) . then ( console . log ( "done" ) )
4362 } else {
44- this . sh ( `explorer ${ path . join ( configdir ) } ` ) . then ( console . log ( "done" ) )
63+ this . sh ( `explorer ${ path . join ( this . configdir ) } ` ) . then ( console . log ( "done" ) )
4564 }
4665 } )
4766
48- program . command ( "reload" ) . description ( "reload config" ) . action ( ( ) => {
49- this . configurate ( )
67+ program . command ( "reload" ) . description ( "reload this.config" ) . action ( ( ) => {
68+ this . configurate ( ) . then ( ( ) => {
69+ process . exit ( ) ;
70+ } )
5071 } )
5172
5273 program . parse ( this . args ) ;
5374 }
5475
5576 async configurate ( ) {
56- var config ;
57- console . log ( 'owo' ) ;
58- const fs = require ( "fs" ) ;
59- if ( ! fs . existsSync ( configdir ) ) {
60- fs . mkdirSync ( configdir , ( err ) => {
61- if ( err ) {
62- throw err ;
63- } else {
64- if ( ! fs . existsSync ( path . join ( configdir , 'scripts.json' ) ) ) {
65- fs . writeFileSync ( path . join ( configdir , 'scripts.json' ) , JSON . stringify ( {
66- "version" : 1 ,
67- "scripts" : {
68- "default" : {
69- "name" : "default" ,
70- "run" : {
71- "type" : "default" ,
72- "command" : "echo \"hello\""
73- }
74- }
75- }
76- } ) , ( err ) => {
77- if ( err ) {
78- throw err ;
79- } else {
80- console . log ( "Saved first Config!" )
81- }
82- } )
83- }
84- }
77+ try {
78+ this . config = require ( join ( configdir + 'scripts.json' ) )
79+ } catch ( e ) {
80+ fs . writeFile ( join ( configdir , "scripts.json" ) , JSON . stringify ( require ( "./templates/scripts.json" ) , null , 4 ) , ( ) => {
81+ this . config = require ( join ( configdir + 'scripts.json' ) )
8582 } )
86- } else {
87- if ( ! fs . existsSync ( path . join ( configdir , 'scripts.json' ) ) ) {
88- fs . writeFileSync ( path . join ( configdir , 'scripts.json' ) , JSON . stringify ( {
89- "version" : 1 ,
90- "scripts" : {
91- "default" : {
92- "name" : "default" ,
93- "run" : {
94- "type" : "default" ,
95- "command" : "echo \"hello\""
96- }
97- }
98- }
99- } ) , ( err ) => {
100- if ( err ) {
101- throw err ;
102- } else {
103- console . log ( "Saved first Config!" )
104- }
105- } )
106- } else {
107- fs . readFileSync ( path . join ( configdir , 'scripts.json' ) , ( err , data ) => {
108- if ( err ) {
109- throw err
110- } else {
111- config = JSON . parse ( data )
112- }
113- } )
114- }
11583 }
116- this . config = config ;
11784 }
11885
11986 async sh ( cmd ) {
0 commit comments