-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathindex.js
131 lines (110 loc) · 4.08 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
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
/* eslint-disable no-buffer-constructor */
import fs from 'fs';
import path from 'path';
import eol from 'eol';
import get from 'lodash/get';
import includes from 'lodash/includes';
import VirtualFile from 'vinyl';
import through2 from 'through2';
import Parser from './parser';
const transform = (parser, customTransform) => {
return function _transform(file, enc, done) {
const { options } = parser;
const content = fs.readFileSync(file.path, enc);
const extname = path.extname(file.path);
if (includes(get(options, 'attr.extensions'), extname)) {
// Parse attribute (e.g. data-i18n="key")
parser.parseAttrFromString(content, {
transformOptions: {
filepath: file.path
}
});
}
if (includes(get(options, 'func.extensions'), extname)) {
// Parse translation function (e.g. i18next.t('key'))
parser.parseFuncFromString(content, {
transformOptions: {
filepath: file.path
}
});
}
if (includes(get(options, 'trans.extensions'), extname)) {
// Look for Trans components in JSX
parser.parseTransFromString(content, {
transformOptions: {
filepath: file.path
}
});
}
if (typeof customTransform === 'function') {
this.parser = parser;
customTransform.call(this, file, enc, done);
return;
}
done();
};
};
const flush = (parser, customFlush) => {
return function _flush(done) {
const { options } = parser;
if (typeof customFlush === 'function') {
this.parser = parser;
customFlush.call(this, done);
return;
}
// Flush to resource store
const resStore = parser.get({ sort: options.sort });
const { jsonIndent } = options.resource;
const lineEnding = String(options.resource.lineEnding).toLowerCase();
Object.keys(resStore).forEach((lng) => {
const namespaces = resStore[lng];
Object.keys(namespaces).forEach((ns) => {
const obj = namespaces[ns];
const resPath = parser.formatResourceSavePath(lng, ns);
let text = JSON.stringify(obj, null, jsonIndent) + '\n';
if (lineEnding === 'auto') {
text = eol.auto(text);
} else if (lineEnding === '\r\n' || lineEnding === 'crlf') {
text = eol.crlf(text);
} else if (lineEnding === '\n' || lineEnding === 'lf') {
text = eol.lf(text);
} else if (lineEnding === '\r' || lineEnding === 'cr') {
text = eol.cr(text);
} else { // Defaults to LF
text = eol.lf(text);
}
let contents = null;
try {
// "Buffer.from(string[, encoding])" is added in Node.js v5.10.0
contents = Buffer.from(text);
} catch (e) {
// Fallback to "new Buffer(string[, encoding])" which is deprecated since Node.js v6.0.0
contents = new Buffer(text);
}
this.push(new VirtualFile({
path: resPath,
contents: contents
}));
});
});
done();
};
};
// @param {object} options The options object.
// @param {function} [customTransform]
// @param {function} [customFlush]
// @return {object} Returns a through2.obj().
const createStream = (options, customTransform, customFlush) => {
const parser = new Parser(options);
const stream = through2.obj(
transform(parser, customTransform),
flush(parser, customFlush)
);
return stream;
};
// Convenience API
module.exports = (...args) => module.exports.createStream(...args);
// Basic API
module.exports.createStream = createStream;
// Parser
module.exports.Parser = Parser;