我正在尝试构建一个依赖于包含连接信息的ftp.json文件的FTP gulp任务。此文件包含在.gitignore中,因此当开发人员克隆repo时,它不存在。我运行一个检查,看看他们是否设置了一个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
来获取下载功能,如果这很重要的话。如果这意味着我可以让它发挥作用,我会很乐意转向别的东西。
答案 0 :(得分: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"]);
看起来有点荒谬复杂,但它确实有效,所以我现在很高兴。