11import * as fs from 'fs' ;
22import * as path from 'path' ;
33import * as readline from 'readline' ;
4+ import Promise from 'bluebird' ;
45import util from '../../../src/util' ;
56import { exportData } from './exportData' ;
67/**
@@ -12,7 +13,7 @@ import { exportData } from './exportData';
1213function runExportData ( filePath , logger ) {
1314 exportData ( filePath , logger )
1415 . then ( ( ) => {
15- logger . log ( 'Successfully exported data' ) ;
16+ logger . info ( 'Successfully exported data' ) ;
1617 process . exit ( 0 ) ;
1718 } )
1819 . catch ( ( err ) => {
@@ -21,38 +22,39 @@ function runExportData(filePath, logger) {
2122 } ) ;
2223}
2324
24- setTimeout ( ( ) => {
25- const logger = console ;
26- const filePath =
27- process . argv [ 2 ] === '--file' && process . argv [ 3 ]
28- ? process . argv [ 3 ]
29- : 'data/demo-data.json' ;
30- logger . log ( '\nScript will export data to file:' , filePath ) ;
31- // check if file exists
32- if ( fs . existsSync ( filePath ) ) {
33- const rl = readline . createInterface ( {
34- input : process . stdin ,
35- output : process . stdout ,
36- } ) ;
37- // confirm overwritting to file
38- rl . question (
39- 'File already exists, Are you sure to overwrite it? [Y] to overwrite: ' ,
40- ( answer ) => {
41- rl . close ( ) ;
42- if ( answer . toLowerCase ( ) === 'y' ) {
43- logger . log ( 'File will be overwritten.' ) ;
44- runExportData ( filePath , logger ) ;
45- } else {
46- logger . log ( 'Exit without exporting any data' ) ;
47- process . exit ( 0 ) ;
48- }
49- } ,
50- ) ; // question()
51- } else {
52- // get base directory of the file
53- const baseDir = path . resolve ( filePath , '..' ) ;
54- // create directory recursively if it does not exist
55- util . mkdirSyncRecursive ( baseDir ) ;
56- runExportData ( filePath , logger ) ;
57- }
25+ const logger = util . getAppLogger ( ) ;
26+ const filePath =
27+ process . argv [ 2 ] === '--file' && process . argv [ 3 ]
28+ ? process . argv [ 3 ]
29+ : 'data/demo-data.json' ;
30+ logger . info ( 'Script will export data to file:' , filePath ) ;
31+ // check if file exists
32+ if ( fs . existsSync ( filePath ) ) {
33+ // We delay question for overwrite file, because the question overlaps with a warning message from sequelize module
34+ Promise . delay ( 1 ) . then ( ( ) => {
35+ const rl = readline . createInterface ( {
36+ input : process . stdin ,
37+ output : process . stdout ,
38+ } ) ;
39+ // confirm overwritting to file
40+ rl . question (
41+ 'File already exists, Are you sure to overwrite it? [Y] to overwrite: ' ,
42+ ( answer ) => {
43+ rl . close ( ) ;
44+ if ( answer . toLowerCase ( ) === 'y' ) {
45+ logger . info ( 'File will be overwritten.' ) ;
46+ runExportData ( filePath , logger ) ;
47+ } else {
48+ logger . info ( 'Exit without exporting any data' ) ;
49+ process . exit ( 0 ) ;
50+ }
51+ } ,
52+ ) ; // question()
5853} ) ;
54+ } else {
55+ // get base directory of the file
56+ const baseDir = path . resolve ( filePath , '..' ) ;
57+ // create directory recursively if it does not exist
58+ util . mkdirSyncRecursive ( baseDir ) ;
59+ runExportData ( filePath , logger ) ;
60+ }
0 commit comments