var fs = require('fs'); var crypto = require('crypto'); const getParams = query => { if (!query) { return { }; } return (/^[?#]/.test(query) ? query.slice(1) : query) .split('&') .reduce((params, param) => { let [ key, value ] = param.split('='); params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : ''; return params; }, { }); }; module.exports = function(source) { const params = getParams(this.query); const chunk = crypto.createHash('md5').update(this.resource).digest("hex"); if (params.path === undefined) { this.emitError('path parameter must be provided'); } if(this.cacheable) this.cacheable(); const regex = /(.*)<\/translate>/mg; let data = {} // replace all Something into {this.props.p.t('Something')} const result = source.replace(regex, function(match, p1, p2) { const md5 = crypto.createHash('md5').update(p2).digest("hex"); data[md5] = p2; if(p1.trim() == 'span') { return "{this.props.p.t('"+md5+"')}"; } else if (p1.trim() == 'nojsx') { return "this.props.p.t('"+md5+"')"; } else { return "{this.props.p.t('"+md5+"')}"; } }); const path = params.path+"/_"+chunk+".json"; if (Object.keys(data).length !== 0) { fs.writeFile(path, JSON.stringify(data), function(err) { if(err) { return console.log(err); } }); } return result; };