1
- import { pathIsMetaCallJson } from '@metacall/protocol/package ' ;
1
+ import { MetaCallJSON , pathIsMetaCallJson } from '@metacall/protocol' ;
2
2
import { spawn } from 'child_process' ;
3
- import * as fs from 'fs' ;
3
+ import * as fs from 'fs/promises ' ;
4
4
import * as path from 'path' ;
5
5
import {
6
6
Deployment ,
@@ -13,57 +13,80 @@ import {
13
13
} from '../constants' ;
14
14
import { isIAllApps , logProcessOutput } from './utils' ;
15
15
16
- // TODO: Refactor this
17
- export const findJsonFilesRecursively = async (
18
- appsDir : string
19
- ) : Promise < void > => {
20
- // TODO: Avoid sync commands
21
- const files = fs . readdirSync ( appsDir , { withFileTypes : true } ) ;
22
- for ( const file of files ) {
23
- if ( file . isDirectory ( ) ) {
24
- await findJsonFilesRecursively ( path . join ( appsDir , file . name ) ) ;
25
- } else if ( pathIsMetaCallJson ( file . name ) ) {
26
- const filePath = path . join ( appsDir , file . name ) ;
27
- const desiredPath = path . join (
28
- path . resolve ( __dirname , '..' ) ,
29
- 'worker' ,
30
- 'index.js'
31
- ) ;
32
- const id = path . basename ( appsDir ) ;
16
+ export const autoDeployApps = async ( appsDir : string ) : Promise < void > => {
17
+ const allDirectories = await fs . readdir ( appsDir , { withFileTypes : true } ) ;
33
18
34
- const deployment : Deployment = {
35
- id,
36
- type : 'application/x-zip-compressed' ,
37
- path : appsDir ,
38
- jsons : [ ]
39
- } ;
19
+ let directoryProcessed = false ;
40
20
41
- const proc = spawn ( 'metacall' , [ desiredPath , filePath ] , {
42
- stdio : [ 'pipe' , 'pipe' , 'pipe' , 'ipc' ]
21
+ for ( const directory of allDirectories ) {
22
+ if ( directory . isDirectory ( ) ) {
23
+ const directoryPath = path . join ( appsDir , directory . name ) ;
24
+ const directoryFiles = await fs . readdir ( directoryPath , {
25
+ withFileTypes : true
43
26
} ) ;
44
27
45
- const message : WorkerMessage < Deployment > = {
46
- type : ProtocolMessageType . Load ,
47
- data : deployment
48
- } ;
28
+ const jsonFiles = directoryFiles
29
+ . filter ( file => file . isFile ( ) && pathIsMetaCallJson ( file . name ) )
30
+ . map ( file => path . join ( directoryPath , file . name ) ) ;
49
31
50
- proc . send ( message ) ;
32
+ if ( jsonFiles . length > 0 ) {
33
+ directoryProcessed = true ;
51
34
52
- logProcessOutput ( proc . stdout , proc . pid , deployment . id ) ;
53
- logProcessOutput ( proc . stderr , proc . pid , deployment . id ) ;
35
+ const desiredPath = path . join (
36
+ path . resolve ( __dirname , '..' ) ,
37
+ 'worker' ,
38
+ 'index.js'
39
+ ) ;
54
40
55
- proc . on ( 'message' , ( payload : WorkerMessageUnknown ) => {
56
- if ( payload . type === ProtocolMessageType . MetaData ) {
57
- const message = payload as WorkerMessage <
58
- Record < string , IAppWithFunctions >
59
- > ;
60
- if ( isIAllApps ( message . data ) ) {
61
- const appName = Object . keys ( message . data ) [ 0 ] ;
62
- childProcesses [ appName ] = proc ;
63
- allApplications [ appName ] = message . data [ appName ] ;
41
+ const jsonContent : MetaCallJSON [ ] = await Promise . all (
42
+ jsonFiles . map ( async file => {
43
+ const content = await fs . readFile ( file , 'utf-8' ) ;
44
+
45
+ // map method returns array.That's why didn't passed MetaCallJSON[]
46
+ return JSON . parse ( content ) as MetaCallJSON ;
47
+ } )
48
+ ) ;
49
+
50
+ const deployment : Deployment = {
51
+ id : directory . name ,
52
+ type : 'application/x-zip-compressed' ,
53
+ path : directoryPath ,
54
+ jsons : jsonContent
55
+ } ;
56
+
57
+ const proc = spawn ( 'metacall' , [ desiredPath , ...jsonFiles ] , {
58
+ stdio : [ 'pipe' , 'pipe' , 'pipe' , 'ipc' ]
59
+ } ) ;
60
+
61
+ const message : WorkerMessage < Deployment > = {
62
+ type : ProtocolMessageType . Load ,
63
+ data : deployment
64
+ } ;
65
+
66
+ proc . send ( message ) ;
67
+
68
+ logProcessOutput ( proc . stdout , proc . pid , deployment . id ) ;
69
+ logProcessOutput ( proc . stderr , proc . pid , deployment . id ) ;
70
+
71
+ proc . on ( 'message' , ( payload : WorkerMessageUnknown ) => {
72
+ if ( payload . type === ProtocolMessageType . MetaData ) {
73
+ const message = payload as WorkerMessage <
74
+ Record < string , IAppWithFunctions >
75
+ > ;
76
+ if ( isIAllApps ( message . data ) ) {
77
+ const appName = Object . keys ( message . data ) [ 0 ] ;
78
+ childProcesses [ appName ] = proc ;
79
+ allApplications [ appName ] = message . data [ appName ] ;
80
+ }
64
81
}
65
- }
66
- } ) ;
82
+ } ) ;
83
+ }
67
84
}
68
85
}
86
+
87
+ if ( directoryProcessed ) {
88
+ console . log (
89
+ 'Previously deployed applications deployed successfully' . green
90
+ ) ;
91
+ }
69
92
} ;
0 commit comments