-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract.js
68 lines (63 loc) · 1.91 KB
/
extract.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
/**
* Copyright 2020 - Frederik Ring <[email protected]>
* SPDX-License-Identifier: MIT
*/
var fs = require('fs')
var jscodeshift = require('jscodeshift')
var PO = require('pofile')
var events = require('events')
module.exports = extractStrings
function extractStrings (files, globalFunctionIdentifier) {
if (!files || !files.length) {
throw new Error('No files found using given pattern, exiting')
}
return parse(files, globalFunctionIdentifier)
}
function parse (files, globalFunctionIdentifier) {
var stream = new events.EventEmitter()
var all = files
.filter(function (fileName) {
return !(/node_modules/.test(fileName))
})
.map(function (fileName) {
return new Promise(function (resolve, reject) {
fs.readFile(fileName, 'utf-8', function (err, data) {
if (err) {
return reject(err)
}
var j = jscodeshift(data)
var calls = j.find(jscodeshift.CallExpression, {
callee: {
type: 'Identifier',
name: globalFunctionIdentifier
}
})
var strings = []
calls.forEach(function (node) {
var dupes = strings.filter(function (string) {
return string.msgid === node.value.arguments[0].value
})
if (dupes.length) {
dupes[0].comments.push(fileName + ':' + node.node.loc.start.line)
} else {
var item = new PO.Item()
item.msgid = node.value.arguments[0].value
item.comments = [
fileName + ':' + node.node.loc.start.line
]
stream.emit('data', item.toString() + '\n\n')
}
})
resolve()
})
})
})
Promise.all(all)
.then(function (results) {
stream.emit('end')
})
.catch(function (err) {
stream.emit('error', err)
})
return stream
}