11import path from 'path' ;
2- import { readFile } from 'fs/promises' ;
3- import YAML from 'yaml' ;
2+ // import { readFile } from 'fs/promises';
3+ // import YAML from 'yaml';
44import fetch from 'node-fetch' ;
55// import FormData from 'form-data';
66import { FormData , File } from 'formdata-node' ;
@@ -15,7 +15,7 @@ import { loadConfigFromYAML } from './../utils/loadConfigFromYAML.js';
1515export const serverReload = async ( options , program ) => {
1616 await ( async ( ) => {
1717 const cwd = path . resolve ( process . cwd ( ) ) ;
18- console . log ( 'Executing in cwd:' , cwd ) ;
18+ console . log ( 'Executing in cwd:' . green , ` ${ cwd } ` . yellow ) ;
1919
2020 // Load yaml configuration
2121 const configuration = await loadConfigFromYAML ( options ) ;
@@ -32,7 +32,9 @@ export const serverReload = async (options, program) => {
3232 headers,
3333 } ) ;
3434
35- console . log ( 'Metadata reloaded!' ) ;
35+ if ( response . status === 200 ) {
36+ console . log ( 'Metadata reloaded!' . green ) ;
37+ }
3638 console . log ( response . status ) ;
3739 } ) ( )
3840 . then ( ( ) => { } )
@@ -42,7 +44,7 @@ export const serverReload = async (options, program) => {
4244export const serverClearMetadata = async ( options , program ) => {
4345 await ( async ( ) => {
4446 const cwd = path . resolve ( process . cwd ( ) ) ;
45- console . log ( 'Executing in cwd:' , cwd ) ;
47+ console . log ( 'Executing in cwd:' . green , ` ${ cwd } ` . yellow ) ;
4648
4749 // Load yaml configuration
4850 const configuration = await loadConfigFromYAML ( options ) ;
@@ -59,8 +61,27 @@ export const serverClearMetadata = async (options, program) => {
5961 headers,
6062 } ) ;
6163
62- console . log ( 'Metadata cleared!' ) ;
64+ if ( response . status === 200 ) {
65+ console . log ( 'Metadata cleared!' . green ) ;
66+ }
6367 console . log ( response . status ) ;
68+
69+ if ( response . status !== 200 ) {
70+ throw new Error ( errors . ERROR_INVALID_HLAMBDA_ADMIN_SECRET ) ;
71+ }
72+
73+ // This is magic from commander, the real flag was --no-auto-reload but we get positive logic transformation to autoReload
74+ if ( options ?. autoReload ) {
75+ const responseRestart = await fetch ( `${ endpoint } /console/api/v1/trigger-restart` , {
76+ method : 'GET' ,
77+ // body: formData,
78+ headers,
79+ } ) ;
80+ if ( responseRestart . status === 200 ) {
81+ console . log ( 'Metadata reloaded after clearing!' . green ) ;
82+ }
83+ console . log ( responseRestart . status ) ;
84+ }
6485 } ) ( )
6586 . then ( ( ) => { } )
6687 . catch ( CLIErrorHandler ( program ) ) ;
@@ -69,7 +90,7 @@ export const serverClearMetadata = async (options, program) => {
6990export const metadataApply = async ( options , program ) => {
7091 await ( async ( ) => {
7192 const cwd = path . resolve ( process . cwd ( ) ) ;
72- console . log ( 'Executing in cwd:' , cwd ) ;
93+ console . log ( 'Executing in cwd:' . green , ` ${ cwd } ` . yellow ) ;
7394
7495 // console.log(options);
7596
@@ -83,7 +104,7 @@ export const metadataApply = async (options, program) => {
83104 // ZIP the metadata
84105 const zip = new AdmZip ( ) ;
85106 zip . addLocalFolder ( path . resolve ( cwd , options . config , metadataDirectory ) ) ;
86- console . log ( 'Creating zip from the metadata' ) ;
107+ console . log ( 'Creating zip from the metadata' . yellow ) ;
87108
88109 // POST metadata to the API
89110 const payloadAsBuffer = zip . toBuffer ( ) ;
@@ -115,7 +136,9 @@ export const metadataApply = async (options, program) => {
115136 headers,
116137 } ) ;
117138
118- console . log ( 'Metadata applied!' ) ;
139+ if ( response . status === 200 ) {
140+ console . log ( 'Metadata applied!' . green ) ;
141+ }
119142 console . log ( response . status ) ;
120143
121144 // This is magic from commander, the real flag was --no-auto-reload but we get positive logic transformation to autoReload
@@ -125,7 +148,7 @@ export const metadataApply = async (options, program) => {
125148 // body: formData,
126149 headers,
127150 } ) ;
128- console . log ( 'Metadata reloaded!' ) ;
151+ console . log ( 'Metadata reloaded!' . green ) ;
129152 console . log ( responseRestart . status ) ;
130153 }
131154 } ) ( )
@@ -136,7 +159,7 @@ export const metadataApply = async (options, program) => {
136159export const metadataExport = async ( options , program ) => {
137160 ( async ( ) => {
138161 const cwd = path . resolve ( process . cwd ( ) ) ;
139- console . log ( 'Executing in cwd:' , cwd ) ;
162+ console . log ( 'Executing in cwd:' . green , ` ${ cwd } ` . yellow ) ;
140163
141164 // console.log(options);
142165
@@ -171,14 +194,26 @@ export const metadataExport = async (options, program) => {
171194
172195 const metadataFilePath = path . resolve ( cwd , options . config , metadataDirectory ) ;
173196
174- console . log ( `Removing all the local metadata... ${ metadataFilePath } ` ) ;
175-
176197 // Dangerous when you think about it... small mistake here and it can delete a lot of things, please be careful!
177- rimraf . sync ( metadataFilePath ) ;
198+ if (
199+ typeof metadataFilePath === 'string' &&
200+ metadataFilePath !== '' &&
201+ metadataFilePath !== '/' &&
202+ metadataFilePath !== '/*'
203+ ) {
204+ // Sanity check !!!
205+ console . log ( `Removing all the local metadata... ${ metadataFilePath } ` . yellow ) ;
206+ rimraf . sync ( metadataFilePath ) ; // Please be careful...
207+ } else {
208+ throw new Error ( errors . ERROR_DANGEROUS_SANITY_CHECK_DID_NOT_PASS ) ;
209+ }
178210
179211 zip . extractAllTo ( metadataFilePath , true ) ;
180212
181- console . log ( 'Metadata exported!' ) ;
213+ if ( response . status === 200 ) {
214+ console . log ( 'Metadata exported!' . green ) ;
215+ }
216+ console . log ( response . status ) ;
182217 } ) ( )
183218 . then ( ( ) => { } )
184219 . catch ( CLIErrorHandler ( program ) ) ;
0 commit comments