-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
49 lines (44 loc) · 1.51 KB
/
index.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
import { callbackify } from 'node:util';
import arrify from 'arrify';
import through from 'through2';
import vinylToString from 'vinyl-contents-tostring';
import PluginError from 'plugin-error';
import { parseXmlAsync } from 'libxmljs';
// https://github.com/import-js/eslint-plugin-import/issues/2104
import { PLUGIN_NAME } from './lib/const.js'; // eslint-disable-line import/extensions
import {
func as functionTransformer,
obj as objectTransformer,
} from './lib/transformers.js'; // eslint-disable-line import/extensions
const clone = (fn) => (file, enc) => fn(file.clone(), enc);
const update = (file) => (xml) => Object.assign(file, {
contents: file.isBuffer()
? Buffer.from(xml)
: through().end(xml),
});
const getTransformStream = (transformer) => through.obj(
callbackify(clone((file, enc) => (
file.isNull()
? Promise.resolve(file)
: vinylToString(file, enc)
.then(parseXmlAsync)
.then(transformer)
.then(update(file))))),
);
export default (transformations, nsUri) => {
// check options
switch (typeof transformations) {
case 'function': {
return getTransformStream(functionTransformer(transformations));
}
case 'object': {
return getTransformStream(objectTransformer(arrify(transformations), nsUri));
}
case 'undefined': {
throw new PluginError(PLUGIN_NAME, 'transformations option is required');
}
default: {
throw new PluginError(PLUGIN_NAME, 'transformations option must be a function or an object');
}
}
};