'use strict'

const fs = require('fs')
const path = require('path')

const _ = require('lodash')

const mkdirp = require('mkdirp')

const Directory = require('./directory')
const File = require('./file')

const ITEM_TYPE_DIRECTORY = 0
const ITEM_TYPE_FILE = 1
const ITEM_TYPE_SYMLINK = 2

class Item {

    constructor(basePath, options) {
        let absPath = (false === path.isAbsolute(basePath)) ? path.join(process.cwd(), basePath) : basePath
        this.root = path.normalize(absPath)

        let opts = _.defaults(options || {}, {
            type: this.getType()
        })

        const { type } = opts
        try {
            fs.accessSync(this.root)

            if (_.isEqual(type, ITEM_TYPE_DIRECTORY)) {
                return new Directory(this.root)
            } else if (_.isEqual(type, ITEM_TYPE_FILE)) {
                return new File(this.root)
            } else {
                return
            }
        } catch (e) {
            if (_.isEqual(type, ITEM_TYPE_FILE)) {
                fs.openSync(path.dirname(this.root), 'w')
                return new File(this.root)
            }
            return
        }
    }

    getType() {
        let match = /^(.*[\/\\])([^/]*)$/g.exec(this.root)
        let isDirPath = !/.*\.(\w{1,})$/gi.test(match[2])
        let directory = isDirPath ? path.join(match[1], match[2]) : match[1]
        let filename = !isDirPath ? match[2] : null

        if (!isDirPath) {
            return ITEM_TYPE_FILE
        }

        mkdirp.sync(directory)
        return ITEM_TYPE_DIRECTORY
    }
}

Item.TYPE_DIRECTORY = ITEM_TYPE_DIRECTORY
Item.TYPE_FILE = ITEM_TYPE_FILE
Item.TYPE_SYMLINK = ITEM_TYPE_SYMLINK

module.exports = Item

@@ -659,16 +659,37 @@
 s.root)%0A
+        %7D catch (e) %7B
 %0A       
@@ -723,25 +723,20 @@
 EM_TYPE_
-DIRECTORY
+FILE
 )) %7B%0A   
@@ -752,28 +752,34 @@
     
-return new Directory
+let fileDir = path.dirname
 (thi
@@ -778,32 +778,33 @@
 name(this.root)%0A
+%0A
             %7D el
@@ -803,53 +803,40 @@
     
-%7D else if (_.isEqual(type, ITEM_TYPE_FILE)) %7B
+    mkdirp.sync(fileDir, '0770')
 %0A   
@@ -844,39 +844,35 @@
             
-return new File
+fs.openSync
 (this.root)%0A
@@ -861,32 +861,37 @@
 enSync(this.root
+, 'w'
 )%0A            %7D 
@@ -893,79 +893,20 @@
    %7D
- else %7B%0A                return%0A            %7D%0A        %7D catch (e) %7B%0A    
+%0A        %7D%0A%0A
     
@@ -931,36 +931,41 @@
 type, ITEM_TYPE_
-FILE
+DIRECTORY
 )) %7B%0A           
@@ -969,36 +969,28 @@
     
-    fs.openSync(path.dirname
+return new Directory
 (thi
@@ -996,27 +996,71 @@
 is.root)
-, 'w')
 %0A    
+    %7D else if (_.isEqual(type, ITEM_TYPE_FILE)) %7B%0A
         
@@ -1094,34 +1094,27 @@
 ot)%0A        
-    %7D%0A    
+%7D%0A%0A
         retu
@@ -1116,26 +1116,16 @@
  return%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1459,40 +1459,8 @@
    %7D
-%0A%0A        mkdirp.sync(directory)
 %0A   

Update item.js