2016-01-21 15 views
0

我試圖生成依賴於包含連接信息的ftp.json文件的FTP一飲而盡任務的其餘部分。該文件包含在.gitignore中,因此當開發人員克隆回購時,該文件不存在。我運行一個檢查來查看他們是否設置了ftp.json,如果不存在,它會下載一個空白的文件。等功能來完成,然後轉移到一個咕嘟咕嘟任務

我到運行的問題是,雖然它下載的文件,它也拋出一個錯誤,結束任務。我想要說的是「等待此功能完成,然後繼續執行其餘任務」,以防止發生錯誤。這是我目前的FTP任務:

// upload to FTP environment 
gulp.task("ftp", function(callback) { 
    // development FTP directory 
    var ftpDirectory = dev; 

    // production FTP directory (if --dist is passed) 
    if (argv.dist) { 
     ftpDirectory = dist; 
    } 

    // check if the FTP.json exists 
    fs.exists("./ftp.json", function(exists) { 
     // if it doesn't, download it 
     if (!exists) { 
      download("https://gist.githubusercontent.com/revxx14/a04f5ba7e00b267e71e2/raw/197fcf2478cc7ee77afaa4d200f908ccdccbc822/ftp.json") 
       .pipe(gulp.dest("./")); 
     } 
    }); // should wait here, and only continue once the download completes. 

    // read FTP credentials fromt ftp.json 
    var host = json.read("./ftp.json").get("dev.host"), 
     user = json.read("./ftp.json").get("dev.user"), 
     pass = json.read("./ftp.json").get("dev.pass"), 
     path = json.read("./ftp.json").get("dev.path"); 

    // read dist FTP credentials from ftp.json (if --dist is passed) 
    if (argv.dist) { 
     host = json.read("./ftp.json").get("dist.host"), 
     user = json.read("./ftp.json").get("dist.user"), 
     pass = json.read("./ftp.json").get("dist.pass"), 
     path = json.read("./ftp.json").get("dist.path"); 
    } 

    // reconfigure ftp.json if a field is empty or if --config is passed 
    if (host === "" || user === "" || pass === "" || argv.config) { 
     gulp.src("./ftp.json") 
      .pipe(prompt.prompt([{ 
       // prompt for the hostname 
       type: "input", 
       name: "host", 
       message: "host:", 
       default: host, 
      }, 
      { 
       // prompt for the username 
       type: "input", 
       name: "user", 
       message: "username:", 
       default: user, 
      }, 
      { 
       // prompt for the password 
       type: "password", 
       name: "pass", 
       message: "password:", 
       default: pass, 
      }, 
      { 
       // prompt for the remote path 
       type: "input", 
       name: "path", 
       message: "remote path:", 
       default: path, 
      }], function(res) { 
       // open the ftp.json 
       var file = json.read("./ftp.json"); 

       // set connection to dev 
       var connection = "dev"; 

       // set connection to dist (if --dist is passed) 
       if (argv.dist) { 
        connection = "dist"; 
       } 

       // update the file contents 
       file.set(connection + ".host", res.host); 
       file.set(connection + ".user", res.user); 
       file.set(connection + ".pass", res.pass); 
       file.set(connection + ".path", res.path); 

       // write the updated file contents 
       file.writeSync(); 
      })); 
    } 

    // create the FTP connection 
    var conn = ftp.create({ 
     host: host, 
     user: user, 
     pass: pass, 
     path: path, 
    }) 

    // upload the changed files 
    return gulp.src(ftpDirectory + "/**/*") 
     .pipe(conn.newer(path)) 
     .pipe(conn.dest(path)); 
}) 

我使用gulp-download的下載功能,如果該事項。如果這意味着我可以得到這個工作,我會很樂意轉移到別的東西上。

+1

我會separe這些不同的任務分解成不同的任務,然後,將它們添加爲依賴關係鏈接在一起,接下來的任務將只依賴啓動後完成[任務相關](https://github.com/gulpjs/gulp /blob/master/docs/recipes/running-tasks-in-series.md) – rmjoia

+0

謝謝,將着眼於此。 – JacobTheDev

+0

好吧,我到了類似的問題在我的新任務'FTP-init',其中只包含了下載的「完成」下載完成之前運行英寸嗯。 – JacobTheDev

回答

0

好了,感謝@rmjoia,我想通了,如何建立依賴關係。這是我的最終任務集:

// initialize ftp.json 
gulp.task("ftp-init", function(cb) { 
    // check if the ftp.json exists 
    fs.stat("./ftp.json", function (err, stats) { 
     if (err != null) { 
      // if it doesn't, create it 
      fs.writeFile("./ftp.json", "{\"dev\": {\"host\": \"\",\"user\": \"\",\"pass\": \"\",\"path\": \"\"\},\"dist\": {\"host\": \"\",\"user\": \"\",\"pass\": \"\",\"path\": \"\"\}}", function (err) { 
       cb(err); 
      }); 
     } else { 
      // otherwise return 
      cb(err); 
     } 
    }); 
}); 

// configure FTP credentials in ftp.json, depends on ftp-init 
gulp.task("ftp-config", ["ftp-init"], function(cb) { 
    // read FTP credentials from ftp.json 
    host = json.read("./ftp.json").get("dev.host"), 
    user = json.read("./ftp.json").get("dev.user"), 
    pass = json.read("./ftp.json").get("dev.pass"), 
    path = json.read("./ftp.json").get("dev.path"); 

    // read dist FTP credentials from ftp.json (if --dist is passed) 
    if (argv.dist) { 
     host = json.read("./ftp.json").get("dist.host"), 
     user = json.read("./ftp.json").get("dist.user"), 
     pass = json.read("./ftp.json").get("dist.pass"), 
     path = json.read("./ftp.json").get("dist.path"); 
    } 

    if (host === "" || user === "" || pass === "" || argv.config) { 
     // reconfigure ftp.json if a field is empty or if --config is passed 
     gulp.src("./ftp.json") 
      .pipe(prompt.prompt([{ 
       // prompt for the hostname 
       type: "input", 
       name: "host", 
       message: "host:", 
       default: host, 
      }, 
      { 
       // prompt for the username 
       type: "input", 
       name: "user", 
       message: "username:", 
       default: user, 
      }, 
      { 
       // prompt for the password 
       type: "password", 
       name: "pass", 
       message: "password:", 
       default: pass, 
      }, 
      { 
       // prompt for the remote path 
       type: "input", 
       name: "path", 
       message: "remote path:", 
       default: path, 
      }], function(res) { 
       // open the ftp.json 
       var file = json.read("./ftp.json"); 

       // set connection to dev 
       var connection = "dev"; 

       // set connection to dist (if --dist is passed) 
       if (argv.dist) { 
        connection = "dist"; 
       } 

       // update the file contents 
       file.set(connection + ".host", res.host); 
       file.set(connection + ".user", res.user); 
       file.set(connection + ".pass", res.pass); 
       file.set(connection + ".path", res.path); 

       // write the updated file contents 
       file.writeSync(); 

       cb(null); 
      })); 
    } else { 
     // otherwise return 
     cb(null); 
    } 
}) 

// upload to FTP environment 
gulp.task("ftp-upload", ["ftp-config", "ftp-init"], function(cb) { 
    // development FTP directory 
    var ftpDirectory = dev; 

    // production FTP directory (if --dist is passed) 
    if (argv.dist) { 
     ftpDirectory = dist; 
    } 

    // create the FTP connection 
    var conn = ftp.create({ 
     host: host, 
     user: user, 
     pass: pass, 
     path: path, 
    }) 

    // upload the changed files 
    return gulp.src(ftpDirectory + "/**/*") 
     .pipe(conn.newer(path)) 
     .pipe(conn.dest(path)); 

    // return 
    cb(null); 
}); 

// combine FTP tasks 
gulp.task("ftp", ["ftp-upload", "ftp-config", "ftp-init"]); 

似乎有點荒謬的複雜,但它的工作,所以我現在很高興。