This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathgulp-utils.js
132 lines (114 loc) · 2.73 KB
/
gulp-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* This Source Code is subject to the terms of the Mozilla Public License
* version 2.0 (the "License"). You can obtain a copy of the License at
* http://mozilla.org/MPL/2.0/.
*/
"use strict";
import fs from "fs";
import https from "https";
import path from "path";
import {Transform} from "stream";
import chai from "chai";
import Mocha from "mocha";
export function readArg(prefix, defaultValue)
{
for (let arg of process.argv)
if (arg.startsWith(prefix))
return arg.substr(prefix.length);
return defaultValue;
}
export function transform(modifier, opts)
{
if (!opts)
opts = {};
let stream = new Transform({objectMode: true});
stream._transform = function(file, encoding, callback)
{
if (file.isDirectory())
{
callback(null, file);
return;
}
if (!file.isBuffer())
throw new Error("Unexpected file type");
if (opts.files && opts.files.indexOf(path.basename(file.path)) < 0)
{
callback(null, file);
return;
}
Promise.resolve().then(() =>
{
let contents = opts.raw ? file.contents : file.contents.toString("utf-8");
return modifier(file.path, contents);
}).then(([filepath, contents]) =>
{
file.path = filepath;
file.contents = Buffer.from(contents, "utf-8");
callback(null, file);
}).catch(e =>
{
console.error(e);
callback(e);
});
};
return stream;
}
export function download(url)
{
return new Promise((resolve, reject) =>
{
let request = https.get(url, response =>
{
if (response.statusCode != 200)
{
reject(new Error("Unexpected status code: " + response.statusCode));
response.resume();
return;
}
let data = "";
response.on("data", chunk =>
{
data += chunk;
});
response.on("end", () =>
{
resolve(data);
});
});
request.on("error", error => reject(new Error(error.message)));
});
}
export function runTests()
{
let mocha = new Mocha({
timeout: 30000
});
global.expect = chai.expect;
let stream = new Transform({objectMode: true});
stream._transform = function(file, encoding, callback)
{
if (!file.path)
throw new Error("Unexpected file type");
mocha.addFile(file.path);
callback(null);
};
stream._flush = async function(callback)
{
try
{
await mocha.loadFilesAsync();
await new Promise((resolve, reject) =>
{
mocha.run(failures => failures ? reject(new Error(`${failures} test(s) failed`)) : resolve());
});
callback(null);
}
catch (e)
{
callback(e);
}
};
stream.on("close", () => delete global.expect);
stream.on("error", () => delete global.expect);
return stream;
}