99 . option ( "-t, --target-dir <targetDir>" , "target directory to build structure in" , "." ) ;
1010
1111program . parse ( process . argv ) ;
12- const { file, targetDir } = program . opts ( ) ;
12+ const opts = program . opts ( ) ;
13+ const file = opts . file ;
14+ const targetDir = opts . targetDir + "/" ;
1315
14- const dirExists = ( filepath : string ) => {
16+ const pathExists = ( filepath : string ) => {
1517 try {
1618 fs . accessSync ( filepath ) ;
1719 return true ;
@@ -49,24 +51,31 @@ try {
4951 const data = fs . readFileSync ( file ) ;
5052 const tree = treeStringToJson ( data . toString ( ) ) ;
5153 const paths = getPaths ( tree ) ;
52- const targetDirExists = dirExists ( targetDir ) ;
54+ const targetDirExists = pathExists ( targetDir ) ;
5355 if ( ! targetDirExists ) {
56+ console . log ( "Creating target directory:" , targetDir ) ;
5457 fs . mkdirSync ( targetDir , { recursive : true } ) ;
5558 }
56- paths . forEach ( async ( { filepath, type } : any ) => {
59+ paths . forEach ( ( { filepath, type } : any ) => {
60+ const fullPath = targetDir + filepath . replace ( / \/ + / g, "/" ) ;
61+ if ( pathExists ( fullPath ) ) {
62+ console . warn ( "Path already exists:" , fullPath ) ;
63+ return ;
64+ }
5765 if ( type === "dir" ) {
58- await fs . mkdirSync ( targetDir + "/" + filepath , { recursive : true } ) ;
66+ console . log ( "Creating directory:" , fullPath ) ;
67+ fs . mkdirSync ( fullPath , { recursive : true } ) ;
5968 } else {
60- const dirname = path . dirname ( filepath ) ;
61- const exist = await dirExists ( dirname ) ;
69+ const dirname = path . dirname ( fullPath ) ;
70+ const exist = pathExists ( dirname ) ;
6271 if ( ! exist ) {
63- await fs . mkdirSync ( targetDir + "/" + dirname , { recursive : true } ) ;
72+ console . log ( "Creating directory:" , dirname ) ;
73+ fs . mkdirSync ( dirname , { recursive : true } ) ;
6474 }
65- await fs . writeFileSync ( targetDir + "/" + filepath , "" ) ;
75+ console . log ( "Creating file:" , fullPath ) ;
76+ fs . writeFileSync ( fullPath , "" , { flag : "wx" } ) ;
6677 }
6778 } ) ;
68- console . log ( tree ) ;
69- console . log ( paths ) ;
7079} catch ( e : any ) {
7180 console . error ( e . message ) ;
7281}
0 commit comments