@8ms/helpers 1.2.10 → 1.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/url/writeUrlContents.d.ts +1 -1
- package/url/writeUrlContents.js +24 -11
package/package.json
CHANGED
|
@@ -7,5 +7,5 @@ type WriteUrlContents = {
|
|
|
7
7
|
*
|
|
8
8
|
* Requires fs-extra
|
|
9
9
|
*/
|
|
10
|
-
declare const saveUrlContents: ({ filePath, url }: WriteUrlContents) => Promise<
|
|
10
|
+
declare const saveUrlContents: ({ filePath, url }: WriteUrlContents) => Promise<void>;
|
|
11
11
|
export default saveUrlContents;
|
package/url/writeUrlContents.js
CHANGED
|
@@ -12,18 +12,31 @@ const createDirectory_1 = __importDefault(require("../file/createDirectory"));
|
|
|
12
12
|
*/
|
|
13
13
|
const saveUrlContents = async ({ filePath, url }) => {
|
|
14
14
|
const fs = require('fs-extra');
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
15
|
+
await (0, createDirectory_1.default)({
|
|
16
|
+
filePath,
|
|
17
|
+
});
|
|
18
|
+
const writer = fs.createWriteStream(filePath);
|
|
19
|
+
await (0, axios_1.default)({
|
|
20
|
+
method: "get",
|
|
21
|
+
url,
|
|
22
|
+
responseType: "stream"
|
|
23
|
+
})
|
|
24
|
+
.then(response => {
|
|
25
|
+
return new Promise(async (resolve, reject) => {
|
|
26
|
+
response.data.pipe(writer);
|
|
27
|
+
let error = null;
|
|
28
|
+
writer.on('error', err => {
|
|
29
|
+
error = err;
|
|
30
|
+
writer.close();
|
|
31
|
+
reject(err);
|
|
32
|
+
});
|
|
33
|
+
writer.on('close', () => {
|
|
34
|
+
if (!error) {
|
|
35
|
+
resolve(true);
|
|
36
|
+
}
|
|
37
|
+
//no need to call the reject here, as it will have been called in the
|
|
38
|
+
//'error' stream;
|
|
24
39
|
});
|
|
25
|
-
response.data.pipe(fs.createWriteStream(filePath));
|
|
26
|
-
resolve(true);
|
|
27
40
|
});
|
|
28
41
|
});
|
|
29
42
|
};
|