【发布时间】:2016-01-21 19:37:25
【问题描述】:
我正在尝试构建一个 FTP gulp 任务,该任务依赖于包含连接信息的 ftp.json 文件。此文件包含在 .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 来提供下载功能,如果这很重要的话。如果这意味着我可以完成这项工作,我会很高兴转向其他事情。
【问题讨论】:
-
我会将这些不同的任务分成单独的任务,然后将它们添加为依赖关系,将它们链接在一起,下一个任务只有在依赖关系完成后才会开始task dependency
-
谢谢,会调查的。
-
好的,我遇到了类似的问题,我的新任务
ftp-init,它只包含下载的内容,在下载完成之前“完成”。嗯。 -
这就是我的意思gulp deps 我使用类似的任务来服务我的应用程序,首先我 lint,然后将 css / js 文件注入 html,然后启动应用程序。我从来没有尝试过像你这样的情况,但是,我认为(我可能错了)原理是一样的
标签: javascript json callback gulp promise