Ich habe diese Aufgabe Schluck bekam:Warten Sie eine Funktion vor dem Brennen die nächste in Knoten zu beenden JS
// default task, runs through all primary tasks
gulp.task("default", ["media", "scripts", "styles", "html"], function() {
// notify that task is complete
gulp.src("gulpfile.js")
.pipe(plugins.gulpif(ran_tasks.length, plugins.notify({title: "Success!", message: ran_tasks.length + " task" + (ran_tasks.length > 1 ? "s" : "") + " complete! [" + ran_tasks.join(", ") + "]", onLast: true})));
// trigger FTP task if FTP flag is passed
if (plugins.argv.ftp) {
config_module.config(gulp, plugins, settings);
ftp_module.upload(gulp, plugins, settings, ran_tasks, on_error);
}
// reset ran_tasks array
ran_tasks.length = 0;
});
die großen Werke, mit Ausnahme der Bit-FTP. Ich brauche config_module.config()
zu beenden, bevor ftp_module.upload()
ausgelöst werden kann. Ich habe versucht, Versprechen und anonyme Funktionen mit Rückrufen einzurichten, aber keine dieser Lösungen funktionierte; Die FTP-Funktion feuert vor der Konfiguration.
Wie kann ich die ftp_module.upload()
Funktion warten auf config_module.config()
zu beenden, bevor feuern?
EDIT: Hier ist das Versprechen, das ich versuchte, die immer noch nicht richtig funktioniert:
new Promise(function (resolve) {
config_module.config(gulp, plugins, settings);
resolve();
}).then(function() {
ftp_module.upload(gulp, plugins, settings, ran_tasks, on_error);
});
EDIT: Ich hatte gehofft, nicht zu haben, die modules_config.config()
Code zu schreiben, da es ziemlich ist lang, aber ich denke, dass es notwendig ist, sich zu bewegen:
module.exports = {
// config task, generate configuration file for FTP & BrowserSync and prompt dev for input
config(gulp, plugins, settings) {
// generate settings.json and start other functions
const generate_config = function (callback) {
return plugins.fs.stat("./settings.json", function (err) {
if (err !== null) {
const json_data =
`{
"ftp": {
"dev": {
"hostname": "",
"port": "21",
"mode": "ftp",
"username": "",
"password": "",
"path": ""
},
"dist": {
"hostname": "",
"port": "21",
"mode": "ftp",
"username": "",
"password": "",
"path": ""
}
},
"browsersync": {
"dev": {
"proxy": "",
"port": "",
"open": "",
"notify": ""
},
"dist": {
"proxy": "",
"port": "",
"open": "",
"notify": ""
}
}
}`;
plugins.fs.writeFile("./settings.json", json_data, "utf8", function() {
callback();
});
} else if (typeof callback === "function") {
callback();
}
});
};
// configue JSON data
const configure_json = function (namespace, options, env, callback) {
const prompts = [];
// construct the prompts
Object.keys(options).forEach(option => {
const properties = options[option];
// construct the prompt
const prompt = {
name: option,
message: namespace + " " + option + ": ",
};
// construct the prompt
Object.keys(properties).forEach(property => {
prompt[property] = properties[property];
});
// put the prompt in the array
prompts.push(prompt);
});
// prompt the user
return gulp.src("./settings.json")
.pipe(plugins.prompt.prompt(prompts, function (res) {
// open settings.json
const file = plugins.json.read("./settings.json");
// update data in JSON
Object.keys(options).forEach(option => {
file.set(namespace + "." + env + "." + option, res[option]);
settings[namespace][option] = res[option];
});
// write updated file contents
file.writeSync();
if (typeof callback === "function") {
callback();
}
}));
};
return new Promise (function (resolve) {
// get the target environment
const env = plugins.argv.dist ? "dist" : "dev";
// generate settings.json
generate_config(function() {
// read browsersync settings from settings.json
settings.browsersync.proxy = plugins.json.read("./settings.json").get("browsersync." + env + ".proxy");
settings.browsersync.port = plugins.json.read("./settings.json").get("browsersync." + env + ".port");
settings.browsersync.open = plugins.json.read("./settings.json").get("browsersync." + env + ".open");
settings.browsersync.notify = plugins.json.read("./settings.json").get("browsersync." + env + ".notify");
// read FTP settingss from settings.json
settings.ftp.host = plugins.json.read("./settings.json").get("ftp." + env + ".hostname");
settings.ftp.port = plugins.json.read("./settings.json").get("ftp." + env + ".port");
settings.ftp.mode = plugins.json.read("./settings.json").get("ftp." + env + ".mode");
settings.ftp.user = plugins.json.read("./settings.json").get("ftp." + env + ".username");
settings.ftp.pass = plugins.json.read("./settings.json").get("ftp." + env + ".password");
settings.ftp.path = plugins.json.read("./settings.json").get("ftp." + env + ".path");
// configure FTP credentials
configure_json("ftp", {
hostname: {
default: settings.ftp.host,
type: "input",
},
port: {
default: settings.ftp.port,
type: "input",
},
mode: {
default: settings.ftp.mode === "ftp" ? 0 : settings.ftp.mode === "tls" ? 1 : settings.ftp.mode === "sftp" ? 2 : 0,
type: "list",
choices: ["ftp", "tls", "sftp"],
},
username: {
default: settings.ftp.user,
type: "input",
},
password: {
default: settings.ftp.pass,
type: "password",
},
path: {
default: settings.ftp.path,
type: "input",
},
}, env, function() {
// configure BrowserSync settings
configure_json("browsersync", {
proxy: {
default: settings.browsersync.proxy === "" ? "localhost" : settings.browsersync.proxy,
type: "input",
},
port: {
default: settings.browsersync.port === "" ? "8080" : settings.browsersync.port,
type: "input",
},
open: {
default: settings.browsersync.open === "" ? "external" : settings.browsersync.open,
type: "input",
},
notify: {
default: settings.browsersync.open === "" ? "false" : settings.browsersync.open,
type: "input",
},
}, env, function() {
// resolve the promise
resolve();
});
});
});
});
}
};
Wie Sie sehen können, ist es ein Versprechen zurückkehrt, aber fo Aus irgendeinem Grund kann ich die FTP-Aufgabe immer noch nicht auslösen.
Sie sagten es bereits - Versprechen der Umsetzung –
@JohnVandivier mit dem Versprechen, aktualisiert richtig habe ich versucht. Es tut immer noch das Gleiche wie zuvor, mache ich etwas falsch? – JacobTheDev
Die Art, wie Sie das Versprechen geschrieben haben, ist in Ordnung, wenn config_module.config() synchron ist, aber wenn es asynchron ist, haben Sie ein Problem bc resolve() wird passieren, bevor es fertig ist –