Skip to content

Commit eb1b08c

Browse files
authored
chore: prerelease cleanup (#74)
1 parent 109eec4 commit eb1b08c

File tree

4 files changed

+56
-52
lines changed

4 files changed

+56
-52
lines changed

Diff for: dist/index.js

+24-20
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,13 @@ export default function convertEncoding(options) {
2020
throw new PluginError(pluginName, ErrorBindings.missingEncoding);
2121
}
2222
const { from = UTF8, to = UTF8 } = options;
23-
if (!iconv.encodingExists(from)) {
24-
throw new PluginError(
25-
pluginName,
26-
`${ErrorBindings.unsupportedEncoding}: ${from}`,
27-
);
28-
}
29-
if (!iconv.encodingExists(to)) {
30-
throw new PluginError(
31-
pluginName,
32-
`${ErrorBindings.unsupportedEncoding}: ${to}`,
33-
);
23+
for (const option of [from, to]) {
24+
if (!iconv.encodingExists(option)) {
25+
throw new PluginError(
26+
pluginName,
27+
`${ErrorBindings.unsupportedEncoding}: ${option}`,
28+
);
29+
}
3430
}
3531
if (from === to) {
3632
console.warn(`${pluginName}: ${ErrorBindings.sameEncoding}`);
@@ -49,17 +45,25 @@ export default function convertEncoding(options) {
4945
return file;
5046
}
5147
if (file.isBuffer()) {
52-
const decodedContent = iconv.decode(
53-
file.contents,
54-
from,
55-
iconvOptions.decode,
56-
);
57-
file.contents = iconv.encode(decodedContent, to, iconvOptions.encode);
48+
try {
49+
const decodedContent = iconv.decode(
50+
file.contents,
51+
from,
52+
iconvOptions.decode,
53+
);
54+
file.contents = iconv.encode(decodedContent, to, iconvOptions.encode);
55+
} catch (err) {
56+
throw new PluginError(pluginName, err);
57+
}
5858
}
5959
if (file.isStream()) {
60-
file.contents = file.contents
61-
.pipe(iconv.decodeStream(from, iconvOptions.decode))
62-
.pipe(iconv.encodeStream(to, iconvOptions.encode));
60+
try {
61+
file.contents = file.contents
62+
.pipe(iconv.decodeStream(from, iconvOptions.decode))
63+
.pipe(iconv.encodeStream(to, iconvOptions.encode));
64+
} catch (err) {
65+
throw new PluginError(pluginName, err);
66+
}
6367
}
6468
return file;
6569
},

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"vinyl": "3.0.0"
7575
},
7676
"peerDependencies": {
77-
"gulp": ">=5"
77+
"gulp": ">=4 <5"
7878
},
7979
"peerDependenciesMeta": {
8080
"gulp": {

Diff for: src/index.ts

+24-21
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,13 @@ export default function convertEncoding(options: Options) {
3939

4040
const { from = UTF8, to = UTF8 } = options;
4141

42-
if (!iconv.encodingExists(from)) {
43-
throw new PluginError(
44-
pluginName,
45-
`${ErrorBindings.unsupportedEncoding}: ${from}`,
46-
);
47-
}
48-
49-
if (!iconv.encodingExists(to)) {
50-
throw new PluginError(
51-
pluginName,
52-
`${ErrorBindings.unsupportedEncoding}: ${to}`,
53-
);
42+
for (const option of [from, to]) {
43+
if (!iconv.encodingExists(option)) {
44+
throw new PluginError(
45+
pluginName,
46+
`${ErrorBindings.unsupportedEncoding}: ${option}`,
47+
);
48+
}
5449
}
5550

5651
if (from === to) {
@@ -74,18 +69,26 @@ export default function convertEncoding(options: Options) {
7469
}
7570

7671
if (file.isBuffer()) {
77-
const decodedContent = iconv.decode(
78-
file.contents,
79-
from,
80-
iconvOptions.decode,
81-
);
82-
file.contents = iconv.encode(decodedContent, to, iconvOptions.encode);
72+
try {
73+
const decodedContent = iconv.decode(
74+
file.contents,
75+
from,
76+
iconvOptions.decode,
77+
);
78+
file.contents = iconv.encode(decodedContent, to, iconvOptions.encode);
79+
} catch (err: unknown) {
80+
throw new PluginError(pluginName, err as Error);
81+
}
8382
}
8483

8584
if (file.isStream()) {
86-
file.contents = file.contents
87-
.pipe(iconv.decodeStream(from, iconvOptions.decode))
88-
.pipe(iconv.encodeStream(to, iconvOptions.encode));
85+
try {
86+
file.contents = file.contents
87+
.pipe(iconv.decodeStream(from, iconvOptions.decode))
88+
.pipe(iconv.encodeStream(to, iconvOptions.encode));
89+
} catch (err: unknown) {
90+
throw new PluginError(pluginName, err as Error);
91+
}
8992
}
9093

9194
return file;

Diff for: test/_helper.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,28 @@ import { Readable } from 'node:stream';
33

44
import Vinyl from 'vinyl';
55

6-
const testString = 'äöüß';
7-
const UTF8 = 'utf8';
86
const LATIN1 = 'latin1';
9-
const LATIN1_ISO = 'iso-8859-1';
7+
const TEST_STRING = 'äöüß';
8+
const UTF8 = 'utf8';
109

1110
export function createFile({ contents }) {
12-
const file = new Vinyl({
11+
return new Vinyl({
1312
contents,
1413
});
15-
16-
return file;
1714
}
1815

1916
const setUp = ({ isFromLatin1 = false, isStream = false } = {}) => {
2017
let from = UTF8;
2118
let to = LATIN1;
22-
let pluginOptions = { to: LATIN1_ISO };
19+
let pluginOptions = { to: LATIN1 };
2320

2421
if (isFromLatin1) {
2522
from = LATIN1;
2623
to = UTF8;
27-
pluginOptions = { from: LATIN1_ISO };
24+
pluginOptions = { from: LATIN1 };
2825
}
2926

30-
const defaultBuffer = Buffer.from(testString);
27+
const defaultBuffer = Buffer.from(TEST_STRING);
3128
const fileContentsBuffer = isFromLatin1
3229
? transcode(defaultBuffer, to, from)
3330
: defaultBuffer;
@@ -41,7 +38,7 @@ const setUp = ({ isFromLatin1 = false, isStream = false } = {}) => {
4138
to,
4239
pluginOptions,
4340
vinylFile: createFile({ contents: fileContents }),
44-
expected: testString,
41+
expected: TEST_STRING,
4542
isStream,
4643
};
4744
};

0 commit comments

Comments
 (0)