1- const dotenv = require ( 'dotenv' )
2- dotenv . config ( )
3- const fastify = require ( 'fastify' ) ( { logger : true } )
1+
42const { exec } = require ( "child_process" )
53const gatsbyProcesses = require ( "./hook_commands/gatsby" )
4+ const gatsbyFtpProcesses = require ( "./hook_commands/gatsby_ftp" )
5+ const FtpDeploy = require ( "ftp-deploy" ) ;
6+ const ftpDeploy = new FtpDeploy ( ) ;
7+ const dotenv = require ( 'dotenv' )
8+ dotenv . config ( )
69
710let isPublishing = false
811let results = ''
12+
13+ const ftpConfig = {
14+ user : process . env . FTP_USERNAME ,
15+ password : process . env . FTP_PASSWORD ,
16+ host : process . env . FTP_HOST ,
17+ port : parseInt ( process . env . FTP_PORT ) ,
18+ include : [ '*' , '**/*' ] ,
19+ localRoot : __dirname + "/hook_commands" ,
20+ remoteRoot : "/backup/" ,
21+ deleteRemote : false ,
22+ forcePasv : true ,
23+ sftp : false
24+ }
25+
926let processes = [ ]
1027
1128const writeResult = ( value ) => {
@@ -25,6 +42,17 @@ function ejectRecursive(index) {
2542
2643 writeResult ( `=====> command is started [ ${ processes [ index ] . command } ] : ${ new Date ( ) } : <=====` )
2744
45+ //ftp commond check
46+ if ( processes [ index ] . ftp ) {
47+ ftpDeploy
48+ . deploy ( ftpConfig )
49+ . then ( res => {
50+ writeResult ( `=====> FTP Upload Finished <=====` )
51+ ejectRecursive ( index + 1 )
52+ } )
53+ return
54+ }
55+
2856 exec ( processes [ index ] . command , processes [ index ] . option , ( error , stdout , stderr ) => {
2957 if ( error ) {
3058 writeResult ( `=====> error command: [ ${ processes [ index ] . command } ] : ${ new Date ( ) } : <=====` )
@@ -56,7 +84,7 @@ function ejectRecursive(index) {
5684 } )
5785}
5886
59- fastify . post ( '/build/:hash' , async ( request , reply ) => {
87+ module . exports . startBuild = ( request , reply ) => {
6088 const receivedHash = request . params . hash
6189
6290 if ( isPublishing )
@@ -67,26 +95,12 @@ fastify.post('/build/:hash', async (request, reply) => {
6795 }
6896
6997 results = ''
70- processes = gatsbyProcesses ;
98+ processes = gatsbyFtpProcesses ;
7199 writeResult ( `Process is starting... : ${ new Date ( ) } :` )
72100 ejectRecursive ( 0 )
73101 isPublishing = true
74102
75103 return response ( true , "build is started" )
76- } )
77-
78- fastify . get ( '/build_result' , async ( ) => {
79- return results
80- } )
81-
82- const start = async ( ) => {
83- try {
84- await fastify . listen ( process . env . SERVER_PORT , process . env . SERVER_IP )
85- fastify . log . info ( `server listening on ${ fastify . server . address ( ) . port } ` )
86- } catch ( err ) {
87- fastify . log . error ( err )
88- process . exit ( 1 )
89- }
90104}
91105
92- start ( )
106+ module . exports . results = ( ) => results
0 commit comments