@@ -14,36 +14,34 @@ import * as yaml from "js-yaml";
1414import { cwd } from "process" ;
1515import chalk from "chalk" ;
1616import ora from "ora" ;
17- import { spawn } from "child_process" ;
1817
1918// Load the plugins list from JSON file
20- const pluginsFilePath = path . resolve ( __dirname , "../../../pluginsList.json" ) ;
21- if ( ! fs . existsSync ( pluginsFilePath ) ) {
22- console . error ( "Error: pluginsList.json file not found!" ) ;
23- process . exit ( 1 ) ;
24- }
25- const pluginsData = fs . readFileSync ( pluginsFilePath , { encoding : "utf-8" } ) ;
26- const availablePlugins = JSON . parse ( pluginsData ) ;
27-
28- // or Search the plugins list from npm
29- // const command = "npm search @godspeedsystems/plugins --json";
30- // const stdout = execSync(command, { encoding: "utf-8" });
31- // const availablePlugins = JSON.parse(stdout.trim());
32-
33- // Map to the format expected by the UI
34- const pluginNames = availablePlugins . map (
35- ( plugin : { value : string ; name : string ; description : string } ) => ( {
36- value : plugin . value ,
37- Name : plugin . name . split ( "plugins-" ) [ 1 ] ,
38- Description : plugin . description ,
39- } )
40- ) ;
19+ const pluginsFilePath = path . resolve ( __dirname , '../../../pluginsList.json' ) ;
20+ if ( ! fs . existsSync ( pluginsFilePath ) ) {
21+ console . error ( "Error: pluginsList.json file not found!" ) ;
22+ process . exit ( 1 ) ;
23+ }
24+ const pluginsData = fs . readFileSync ( pluginsFilePath , { encoding : "utf-8" } ) ;
25+ const availablePlugins = JSON . parse ( pluginsData ) ;
26+
27+ // or Search the plugins list from npm
28+ // const command = "npm search @godspeedsystems/plugins --json";
29+ // const stdout = execSync(command, { encoding: "utf-8" });
30+ // const availablePlugins = JSON.parse(stdout.trim());
31+
32+ // Map to the format expected by the UI
33+ const pluginNames = availablePlugins . map ( ( plugin : { value : string ; name : string ; description : string } ) => ( {
34+ value : plugin . value ,
35+ Name : plugin . name . split ( "plugins-" ) [ 1 ] ,
36+ Description : plugin . description ,
37+ } ) ) ;
4138
4239const program = new Command ( ) ;
4340
4441type ModuleType = "DS" | "ES" | "BOTH" ;
4542
4643const addAction = async ( pluginsList : string [ ] ) => {
44+ // install that package
4745 const spinner = ora ( {
4846 text : "Installing plugins... " ,
4947 spinner : {
@@ -56,120 +54,177 @@ const addAction = async (pluginsList: string[]) => {
5654 try {
5755 spinner . start ( ) ;
5856
59- const child = spawn ( "pnpm" , [ "add" , ...pluginsList , "--silent" ] , {
60- stdio : "inherit" ,
61- } ) ;
57+ // Use spawnCommand instead of spawnSync
58+ const child = spawnSync (
59+ "npm" ,
60+ [
61+ "install" ,
62+ ...pluginsList ,
63+ "--quiet" ,
64+ "--no-warnings" ,
65+ "--silent" ,
66+ "--progress=false" ,
67+ ] ,
68+ {
69+ stdio : "inherit" , // Redirect output
70+ }
71+ ) ;
6272
63- await new Promise < void > ( ( resolve , reject ) => {
64- child . on ( "close" , ( code ) => {
65- if ( code === 0 ) resolve ( ) ;
66- else reject ( new Error ( `pnpm install failed with code ${ code } ` ) ) ;
73+ await new Promise < void > ( ( resolve ) => {
74+ child . on ( "close" , ( ) => {
75+ resolve ( ) ;
6776 } ) ;
6877 } ) ;
6978
70- spinner . succeed ( "Plugins installed successfully!" ) ;
79+ spinner . stop ( ) ; // Stop the spinner when the installation is complete
80+ console . log ( "\nPlugins installed successfully!" ) ;
7181 console . log ( chalk . cyan . bold ( "Happy coding with Godspeed! ππ\n" ) ) ;
7282 } catch ( error : any ) {
73- spinner . fail ( "Failed to install plugins." ) ;
83+ spinner . stop ( ) ; // Stop the spinner in case of an error
7484 console . error ( "Error during installation:" , error . message ) ;
7585 }
7686 }
7787
78- // Install plugins before proceeding
88+ // Call the installPlugin function
7989 await installPlugin ( pluginsList ) ;
8090
81- await Promise . all (
82- pluginsList . map ( async ( pluginName ) => {
83- try {
84- const Module = await import (
85- path . join ( process . cwd ( ) , "node_modules" , pluginName )
86- ) ;
91+ pluginsList . map ( async ( pluginName : string ) => {
92+ try {
93+ const Module = await import (
94+ path . join ( process . cwd ( ) , "node_modules" , pluginName )
95+ ) ;
8796
88- let moduleType = Module . SourceType ;
89- let loaderFileName = Module . Type as string ;
90- let yamlFileName = Module . CONFIG_FILE_NAME as string ;
91- let defaultConfig = Module . DEFAULT_CONFIG || { } ;
97+ let moduleType = Module . SourceType as ModuleType ;
98+ let loaderFileName = Module . Type as string ;
99+ let yamlFileName = Module . CONFIG_FILE_NAME as string ;
100+ let defaultConfig = Module . DEFAULT_CONFIG || ( { } as PlainObject ) ;
92101
93- switch ( moduleType ) {
94- case "BOTH" : {
95- const eventSourcePath = path . join (
96- process . cwd ( ) ,
97- "src" ,
98- "eventsources"
99- ) ;
100- const dataSourcePath = path . join (
101- process . cwd ( ) ,
102- "src" ,
103- "datasources"
102+ switch ( moduleType ) {
103+ case "BOTH" :
104+ {
105+ mkdirSync (
106+ path . join ( process . cwd ( ) , "src" , "eventsources" , "types" ) ,
107+ {
108+ recursive : true ,
109+ }
104110 ) ;
105-
106- mkdirSync ( path . join ( eventSourcePath , "types" ) , { recursive : true } ) ;
107- mkdirSync ( path . join ( dataSourcePath , "types" ) , { recursive : true } ) ;
111+ mkdirSync ( path . join ( process . cwd ( ) , "src" , "datasources" , "types" ) , {
112+ recursive : true ,
113+ } ) ;
108114
109115 writeFileSync (
110- path . join ( eventSourcePath , "types" , `${ loaderFileName } .ts` ) ,
111- `import { EventSource } from '${ pluginName } ';\nexport default EventSource;`
116+ path . join (
117+ process . cwd ( ) ,
118+ "src" ,
119+ "eventsources" ,
120+ "types" ,
121+ `${ loaderFileName } .ts`
122+ ) ,
123+ `
124+ import { EventSource } from '${ pluginName } ';
125+ export default EventSource;
126+ `
112127 ) ;
113128 writeFileSync (
114- path . join ( eventSourcePath , `${ yamlFileName } .yaml` ) ,
129+ path . join (
130+ process . cwd ( ) ,
131+ "src" ,
132+ "eventsources" ,
133+ `${ yamlFileName } .yaml`
134+ ) ,
115135 yaml . dump ( { type : loaderFileName , ...defaultConfig } )
116136 ) ;
117137
118138 writeFileSync (
119- path . join ( dataSourcePath , "types" , `${ loaderFileName } .ts` ) ,
120- `import { DataSource } from '${ pluginName } ';\nexport default DataSource;`
139+ path . join (
140+ process . cwd ( ) ,
141+ "src" ,
142+ "datasources" ,
143+ "types" ,
144+ `${ loaderFileName } .ts`
145+ ) ,
146+ `
147+ import { DataSource } from '${ pluginName } ';
148+ export default DataSource;
149+ `
121150 ) ;
122151 writeFileSync (
123- path . join ( dataSourcePath , `${ yamlFileName } .yaml` ) ,
152+ path . join (
153+ process . cwd ( ) ,
154+ "src" ,
155+ "datasources" ,
156+ `${ yamlFileName } .yaml`
157+ ) ,
124158 yaml . dump ( { type : loaderFileName , ...defaultConfig } )
125159 ) ;
126- break ;
127160 }
128- case "DS" : {
129- const dataSourcePath = path . join (
130- process . cwd ( ) ,
131- "src" ,
132- "datasources"
133- ) ;
134- mkdirSync ( path . join ( dataSourcePath , "types" ) , { recursive : true } ) ;
135-
161+ break ;
162+ case "DS" :
163+ {
164+ mkdirSync ( path . join ( process . cwd ( ) , "src" , "datasources" , "types" ) , {
165+ recursive : true ,
166+ } ) ;
136167 writeFileSync (
137- path . join ( dataSourcePath , "types" , `${ loaderFileName } .ts` ) ,
138- `import { DataSource } from '${ pluginName } ';\nexport default DataSource;`
168+ path . join (
169+ process . cwd ( ) ,
170+ "src" ,
171+ "datasources" ,
172+ "types" ,
173+ `${ loaderFileName } .ts`
174+ ) ,
175+ `
176+ import { DataSource } from '${ pluginName } ';
177+ export default DataSource;
178+ `
139179 ) ;
140-
180+ // special case for prisma for now
181+ // @ts -ignore
141182 if ( Module . Type !== "prisma" ) {
142183 writeFileSync (
143- path . join ( dataSourcePath , `${ yamlFileName } .yaml` ) ,
184+ path . join (
185+ process . cwd ( ) ,
186+ "src" ,
187+ "datasources" ,
188+ `${ yamlFileName } .yaml`
189+ ) ,
144190 yaml . dump ( { type : loaderFileName , ...defaultConfig } )
145191 ) ;
146192 }
147- break ;
148193 }
149- case "ES" : {
150- const eventSourcePath = path . join (
194+ break ;
195+ case "ES" : {
196+ mkdirSync ( path . join ( process . cwd ( ) , "src" , "eventsources" , "types" ) , {
197+ recursive : true ,
198+ } ) ;
199+ writeFileSync (
200+ path . join (
151201 process . cwd ( ) ,
152202 "src" ,
153- "eventsources"
154- ) ;
155- mkdirSync ( path . join ( eventSourcePath , "types" ) , { recursive : true } ) ;
156-
157- writeFileSync (
158- path . join ( eventSourcePath , "types" , `${ loaderFileName } .ts` ) ,
159- `import { EventSource } from '${ pluginName } ';\nexport default EventSource;`
160- ) ;
161- writeFileSync (
162- path . join ( eventSourcePath , `${ yamlFileName } .yaml` ) ,
163- yaml . dump ( { type : loaderFileName , ...defaultConfig } )
164- ) ;
165- break ;
166- }
203+ "eventsources" ,
204+ "types" ,
205+ `${ loaderFileName } .ts`
206+ ) ,
207+ `
208+ import { EventSource } from '${ pluginName } ';
209+ export default EventSource;
210+ `
211+ ) ;
212+ writeFileSync (
213+ path . join (
214+ process . cwd ( ) ,
215+ "src" ,
216+ "eventsources" ,
217+ `${ yamlFileName } .yaml`
218+ ) ,
219+ yaml . dump ( { type : loaderFileName , ...defaultConfig } )
220+ ) ;
167221 }
168- } catch ( error ) {
169- console . error ( `Unable to import module '${ pluginName } ':` , error ) ;
170222 }
171- } )
172- ) ;
223+ } catch ( error ) {
224+ console . error ( "unable to import the module." , error ) ;
225+ }
226+ } ) ;
227+ // create folder for eventsource or datasource respective file
173228} ;
174229
175230const add = program
@@ -190,7 +245,7 @@ const add = program
190245 const missingPlugins = pluginNames . filter (
191246 ( plugin : { value : string | number } ) => ! localpluginsList [ plugin . value ]
192247 ) ;
193-
248+
194249 if ( ! givenPluginName ) {
195250 if ( pluginNames . length === 0 ) {
196251 console . error ( "No plugins found." ) ;
@@ -275,26 +330,34 @@ const removeAction = async (pluginsList: string[]) => {
275330 try {
276331 spinner . start ( ) ;
277332
278- const child = spawn ( "pnpm" , [ "remove" , ...pluginsList , "--silent" ] , {
279- stdio : "inherit" , // Ensure proper output handling
280- } ) ;
333+ // Use spawnCommand instead of spawnSync
334+ const child = spawnSync (
335+ "npm" ,
336+ [
337+ "uninstall" ,
338+ ...pluginsList ,
339+ "--quiet" ,
340+ "--no-warnings" ,
341+ "--silent" ,
342+ "--progress=false" ,
343+ ] ,
344+ {
345+ stdio : "inherit" , // Redirect output
346+ }
347+ ) ;
281348
282- await new Promise < void > ( ( resolve , reject ) => {
283- child . on ( "close" , ( code ) => {
284- if ( code === 0 ) {
285- resolve ( ) ;
286- } else {
287- reject ( new Error ( `pnpm remove failed with code ${ code } ` ) ) ;
288- }
349+ await new Promise < void > ( ( resolve ) => {
350+ child . on ( "close" , ( ) => {
351+ resolve ( ) ;
289352 } ) ;
290353 } ) ;
291354
292- spinner . stop ( ) ; // Stop the spinner when uninstallation is complete
355+ spinner . stop ( ) ; // Stop the spinner when the installation is complete
293356 console . log ( "\nPlugins uninstalled successfully!" ) ;
294357 console . log ( chalk . cyan . bold ( "Happy coding with Godspeed! ππ\n" ) ) ;
295358 } catch ( error : any ) {
296359 spinner . stop ( ) ; // Stop the spinner in case of an error
297- console . error ( "Error during uninstallation :" , error . message ) ;
360+ console . error ( "Error during installation :" , error . message ) ;
298361 }
299362 }
300363
@@ -417,7 +480,7 @@ const remove = program
417480 console . error ( "There are no eventsource/datasource plugins installed." ) ;
418481 return ;
419482 }
420-
483+
421484 let pkgPath = path . join ( cwd ( ) , "package.json" ) ;
422485 pluginsList = existsSync ( pkgPath )
423486 ? JSON . parse ( readFileSync ( pkgPath , { encoding : "utf-8" } ) ) . dependencies
@@ -531,6 +594,7 @@ const update = program
531594 }
532595 }
533596
597+
534598 let pkgPath = path . join ( cwd ( ) , "package.json" ) ;
535599 pluginsList = existsSync ( pkgPath )
536600 ? JSON . parse ( readFileSync ( pkgPath , { encoding : "utf-8" } ) ) . dependencies
0 commit comments