sample/gulp/gulpfile.js
function downloadFile(url, outputPath) {
	const dir = path.dirname(outputPath);
	if (!fs.existsSync(dir)) {
		fs.mkdirSync(dir);
	}
	return new Promise((resolve, reject) => {
		const file = fs.createWriteStream(outputPath);
		const sendReq = request.get(url);
		sendReq.pipe(file);

		sendReq
		.on("response", (response) => {
			if (response.statusCode !== 200) {
				fs.unlink(outputPath);
				const error = new Error("Response status code was " + response.statusCode + ".");
				console.trace(error);
				reject(error);
			}
		})
		.on("error", (error) => {
			fs.unlink(outputPath);
			console.trace(error);
			reject(error);
		});

		file
		.on("finish", () => {
			file.close(() => {
				resolve();
			});
		})
		.on("error", (error) => {
			fs.unlink(outputPath);
			console.trace(error);
			reject(error);
		});
	});
}
function downloadFile(url, outputPath) {
	const dir = path.dirname(outputPath);
	if (!fs.existsSync(dir)) {
		fs.mkdirSync(dir);
	}
	return new Promise((resolve, reject) => {
		const file = fs.createWriteStream(outputPath);
		const sendReq = request.get(url);
		sendReq.pipe(file);

		sendReq
		.on("response", (response) => {
			if (response.statusCode !== 200) {
				fs.unlink(outputPath);
				const error = new Error("Response status code was " + response.statusCode + ".");
				console.trace(error);
				reject(error);
			}
		})
		.on("error", (error) => {
			fs.unlink(outputPath);
			console.trace(error);
			reject(error);
		});

		file
		.on("finish", () => {
			file.close(() => {
				resolve();
			});
		})
		.on("error", (error) => {
			fs.unlink(outputPath);
			console.trace(error);
			reject(error);
		});
	});
}
function downloadFile ( url , outputPath ) { const dir = path . dirname ( outputPath ) ; if ( ! fs . existsSync ( dir ) ) { fs . mkdirSync ( dir ) ; } return new Promise ( ( resolve , reject ) => { const file = fs . createWriteStream ( outputPath ) ; const sendReq = request . get ( url ) ; sendReq . pipe ( file ) ; sendReq . on ( "response" , ( response ) => { if ( response . statusCode !== 200 ) { fs . unlink ( outputPath ) ; const error = new Error ( "Response status code was " + response . statusCode + "." ) ; console . trace ( error ) ; reject ( error ) ; } } ) . on ( "error" , ( error ) => { fs . unlink ( outputPath ) ; console . trace ( error ) ; reject ( error ) ; } ) ; file . on ( "finish" , ( ) => { file . close ( ( ) => { resolve ( ) ; } ) ; } ) . on ( "error" , ( error ) => { fs . unlink ( outputPath ) ; console . trace ( error ) ; reject ( error ) ; } ) ; } ) ; }
Downloads the URL as a file
@param {string} url
@param {string} outputPath
@return {Promise<void>}
Downloads the URL as a file