-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
190 lines (166 loc) · 5.28 KB
/
gulpfile.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* eslint-disable no-console */
const { Transform } = require('stream');
const del = require('del');
const flowRemoveTypes = require('flow-remove-types');
const fs = require('fs');
const gulp = require('gulp');
const mirror = require('gulp-mirror');
const path = require('path');
const rename = require('gulp-rename');
const source = require('vinyl-source-stream');
const Vinyl = require('vinyl');
const {
CombineIntoArray,
getLocalPath,
HttpsReadable,
streamToBuffer,
} = require('./gulp-utils');
gulp.task('clean-spec-source', () => del([
'spec-source/',
]));
gulp.task('download-spec-source', ['clean-spec-source'], () =>
new HttpsReadable('https://encoding.spec.whatwg.org/encodings.json')
.pipe(source('encodings.json'))
.pipe(new Transform({
objectMode: true,
transform(file, encoding, cb) {
streamToBuffer(file.contents)
.then((data) => {
const items = JSON.parse(data.toString());
const item = items.find(({ heading }) => heading === 'Legacy single-byte encodings');
if (!item) {
throw new Error('Invalid encodings');
}
const singleByteEncodings = item.encodings;
// add Mazovia
singleByteEncodings.push({
name: 'mazovia',
labels: ['cp790', 'mazovia'],
custom: true,
});
this.push(new Vinyl({
path: getLocalPath('single-byte-encodings.json'),
contents: Buffer.from(JSON.stringify(singleByteEncodings)),
}));
item.encodings.forEach(({ name, custom }) => {
const filename = `index-${name.toLowerCase()}.txt`;
let contents;
if (custom) {
contents = fs.createReadStream(`./spec-custom/${filename}`);
} else {
contents = new HttpsReadable(`https://encoding.spec.whatwg.org/${filename}`);
}
this.push(new Vinyl({
path: getLocalPath(filename),
contents,
}));
});
})
.catch((err) => {
console.error(err);
})
.then(() => cb(null));
},
}))
.pipe(gulp.dest('spec-source'))
);
gulp.task('clean-spec', () => del([
'spec/',
]));
gulp.task('copy-encodings', ['clean-spec'], () =>
gulp.src('spec-source/single-byte-encodings.json')
.pipe(gulp.dest('spec'))
);
gulp.task('single-byte-indexes', ['copy-encodings'], () =>
gulp.src('spec-source/index-*.txt', { buffer: true })
.pipe(new Transform({
objectMode: true,
transform(file, encoding, cb) {
const filename = path.basename(file.path);
const name = filename.slice(6, -4);
const input = file.contents.toString();
const regex = /$\s+(\d+)\s0x([0-9A-F]{4})/img;
let codes = '';
let matches;
for (let i = 0; i < 128; i += 1) {
matches = regex.exec(input);
if (matches) {
codes += String.fromCharCode(parseInt(matches[2], 16));
} else {
codes += '\u0000';
}
}
this.push([name, codes]);
cb(null);
},
}))
.pipe(new CombineIntoArray())
.pipe(new Transform({
writableObjectMode: true,
transform(arr, encoding, cb) {
const indexes = {};
arr.forEach(([name, codes]) => {
indexes[name] = codes;
});
// no separate file for iso-8859-8-i
indexes['iso-8859-8-i'] = indexes['iso-8859-8'];
this.push(Buffer.from(JSON.stringify(indexes)));
cb(null);
},
}))
.pipe(source('single-byte-indexes.json'))
.pipe(gulp.dest('spec'))
);
gulp.task('docs', () =>
gulp.src('spec/single-byte-encodings.json', { buffer: true })
.pipe(new Transform({
objectMode: true,
transform(file, encoding, cb) {
const encodings = JSON.parse(file.contents.toString());
this.push('Encoding | Labels\n');
this.push('-------- | ------\n');
encodings.forEach(({ name, custom, labels }) => {
const joinedLabels = labels.reduce((a, x, i) => {
let prefix;
if (i === 0) {
prefix = '';
} else if (i % 4 === 0) {
prefix = ',<br>';
} else {
prefix = ', ';
}
return a + prefix + x;
}, '');
let enc;
if (!custom) {
enc = `[${name}](https://encoding.spec.whatwg.org/index-${name.toLowerCase()}.txt)`;
} else {
enc = `\`${name}\``;
}
this.push(`${enc} | ${joinedLabels}\n`);
});
cb(null);
},
}))
.pipe(source('Encodings.md'))
.pipe(gulp.dest('.'))
);
gulp.task('clean', () => del([
'lib/',
]));
gulp.task('flow-remove-types', ['clean'], () =>
gulp.src(['src/**/*.js', '!src/**/*.test.js'])
.pipe(mirror(
rename({ extname: '.js.flow' }),
new Transform({
objectMode: true,
transform: (file, enc, cb) => {
// eslint-disable-next-line no-param-reassign
file.contents = new Buffer(flowRemoveTypes(file.contents.toString()).toString());
cb(null, file);
},
})
))
.pipe(gulp.dest('lib/'))
);
gulp.task('default', ['flow-remove-types']);