let Promise    = require('bluebird')
let path       = require('path')
let fs         = Promise.promisifyAll(require('fs'))
let mkdirp     = Promise.promisifyAll(require('mkdirp'))
let rimraf     = Promise.promisify(require('rimraf'))
let formidable = require('formidable')

let appRoot = path.join(path.dirname(require.main.filename), '../')

// create a new file and write content in it
let createFile = function (name, context) {
  return new Promise((resolve, reject) => {
    
    fs.writeFileAsync(path.join(appRoot, name), context).then(() => {
      resolve()
    }).catch(error => {
      console.log(error)
      reject('file error')
    })
  })
}

// delete a file
let deleteFile = function (name) {
  return new Promise((resolve, reject) => {
    fs.unlinkAsync(path.join(appRoot, name)).then(() => {
      resolve()
    }).catch(error => {
      console.log(error)
      reject('file error')
    })
  })
}

// read a file
let readFile = function (name, type = undefined) {
  return new Promise((resolve, reject) => {
    let promise
    if (type !== undefined) {
      promise = fs.readFileAsync(path.join(appRoot, name), type)
    } else {
      promise = fs.readFileAsync(path.join(appRoot, name))
    }
    promise.then(context => {
      resolve(context)
    }).catch(error => {
      console.log(error)
      reject('file error')
    })
  })
}

// create a new folder (can be nested)
let createFolder = function (name) {
  return new Promise((resolve, reject) => {
    mkdirp.mkdirpAsync(name).then(() => {
      resolve()
    }).catch(error => {
      console.log(error)
      reject('file error')
    })
  })
}

// delete a folder (can be nested)
let deleteFolder = function (path) {
  return new Promise((resolve, reject) => {
    rimraf(path).then(() => {
      resolve()
    }).catch(error => {
      console.log(error)
      reject('file error')
    })
  })
}

let processFormData = function (data) {
  return new Promise((resolve, reject) => {
    let form = new formidable.IncomingForm()
    form.encoding       = 'utf-8'
    form.keepExtensions = true
    form.multiples      = true

    form.parse(data, (err, fields, files) => {
      if (err) {
        console.log('error')
        reject('file error')
        return
      }
      resolve({fields: fields, files: files})
    })
  })
}

let moveFile = function (from, to) {
  return new Promise((resolve, reject) => {
    fs.rename(from, path.join(appRoot, to), err => {
      if (err) {
        reject('file error')
        return
      }
      resolve()
    })
  })
}

let copyFile = function (from, to) {
  return new Promise((resovle, reject) => {
    fs.copyFile(from, to, err => {
      if (err) {
        reject('file error')
        return
      }
      resovle()
    })
  })
}

module.exports = {
  createFile:      createFile,
  deleteFile:      deleteFile,
  readFile:        readFile,
  createFolder:    createFolder,
  deleteFolder:    deleteFolder,
  processFormData: processFormData,
  moveFile:        moveFile,
  copyFile:        copyFile
}


@@ -4,24 +4,27 @@
  Promise    
+   
 = require('b
@@ -44,24 +44,27 @@
  path       
+   
 = require('p
@@ -80,24 +80,27 @@
  fs         
+   
 = Promise.pr
@@ -136,24 +136,27 @@
  mkdirp     
+   
 = Promise.pr
@@ -196,24 +196,27 @@
  rimraf     
+  
+ 
 = Promise.pr
@@ -257,16 +257,19 @@
 midable 
+   
 = requir
@@ -283,16 +283,69 @@
 idable')
+%0Alet child_process = require('child-process-promise')
 %0A%0Alet ap
@@ -2323,51 +2323,325 @@
   %7D%0A
-      resolve(%7Bfields: fields, files: files
+%0A      let promise = %5B%5D%0A      for (let file of files) %7B%0A        promise.push(child_process.exec('tr -d %5C%5Cr %3C ' + file.path + ' %3E ' + file.path))%0A      %7D%0A      Promise.all(promise).then(() =%3E %7B%0A        resolve(%7Bfields: fields, files: files%7D)%0A      %7D).catch(error =%3E %7B%0A        console.log(error)%0A        reject()%0A      
 %7D)%0A 

test tr