-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin.js
More file actions
52 lines (44 loc) · 1.5 KB
/
Copy pathplugin.js
File metadata and controls
52 lines (44 loc) · 1.5 KB
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
/* eslint-disable camelcase */
const gettextParser = require('gettext-parser')
const fs = require('fs')
const path = require('path')
const PLUGIN_KEY = process.env.NODE_ENV === 'test'
? 'plugin.js' : 'extract-text'
const DEFAULT_OUTPUT_FILE = 'strings.po'
const {
buildCallExpressionEntry,
buildJSXElementEntry,
mergeEntries,
} = require('./src/builders')
module.exports = ({ types }) => {
return {
pre (state) {
this.entries = []
},
visitor: {
CallExpression (path, state) {
const entry = buildCallExpressionEntry(types, path, state)
if (entry) this.entries.push(entry)
},
JSXElement (path, state) {
const entry = buildJSXElementEntry(types, path, state)
if (entry) this.entries.push(entry)
},
},
post (state) {
if (this.entries.length === 0) return
const thisPlugin = state.opts.plugins.find(plugin => plugin.key.includes(PLUGIN_KEY))
const args = (thisPlugin || {}).options
const filename = this.file.opts.filename || 'unknown'
const currentFileName = path.basename(filename)
const currentFileDir = path.dirname(filename)
const outputFile = args.outputFile ||
(filename !== 'unknown' && `${currentFileName}.po`) ||
DEFAULT_OUTPUT_FILE
const outputDir = args.outputDir || currentFileDir
const poRawData = mergeEntries(args, this.entries)
const po = gettextParser.po.compile(poRawData)
fs.writeFileSync(path.join(outputDir, outputFile), po)
},
}
}