i18n-patch
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -7,2 +7,6 @@ 'use strict'; | ||
var _getIterator2 = require('babel-runtime/core-js/get-iterator'); | ||
var _getIterator3 = _interopRequireDefault(_getIterator2); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
@@ -24,2 +28,10 @@ | ||
var _glob = require('glob'); | ||
var _glob2 = _interopRequireDefault(_glob); | ||
var _lodash = require('lodash'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _camelizer = require('./camelizer'); | ||
@@ -64,8 +76,54 @@ | ||
var configPath = _path2.default.join(this.options.config, name + '.yml'); | ||
var result = void 0; | ||
if (pathExists.sync(configPath)) { | ||
return yaml.load(_fsExtra2.default.readFileSync(configPath, ENCODING), { filename: configPath }); | ||
result = yaml.load(_fsExtra2.default.readFileSync(configPath, ENCODING), { filename: configPath }); | ||
} else { | ||
configPath = _path2.default.join(this.options.config, name + '.json'); | ||
return JSON.parse(_fsExtra2.default.readFileSync(configPath)); | ||
if (pathExists.sync(configPath)) { | ||
result = JSON.parse(_fsExtra2.default.readFileSync(configPath)); | ||
} else { | ||
result = {}; | ||
} | ||
} | ||
var filenames = _glob2.default.sync(name + '-*.@(yml|json)', { cwd: this.options.config }); | ||
if (filenames) { | ||
filenames = filenames.sort(); | ||
} | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = (0, _getIterator3.default)(filenames), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var filename = _step.value; | ||
var filePath = _path2.default.join(this.options.config, filename); | ||
var obj = void 0; | ||
if (filename.match(/.*\.yml/)) { | ||
obj = yaml.load(_fsExtra2.default.readFileSync(filePath, ENCODING), { filename: filePath }); | ||
} else if (filename.match(/.*\.json/)) { | ||
obj = JSON.parse(_fsExtra2.default.readFileSync(filePath)); | ||
} | ||
_lodash2.default.mergeWith(result, obj, function (objValue, srcValue) { | ||
if (_lodash2.default.isArray(objValue)) { | ||
return objValue.concat(srcValue); | ||
} | ||
}); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
@@ -72,0 +130,0 @@ }, { |
@@ -47,3 +47,3 @@ 'use strict'; | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(Translator).call(this, { objectMode: true })); | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (Translator.__proto__ || (0, _getPrototypeOf2.default)(Translator)).call(this, { objectMode: true })); | ||
@@ -50,0 +50,0 @@ _this.t = translation; |
{ | ||
"name": "i18n-patch", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Replacing codes for i18n with patterns.", | ||
@@ -41,2 +41,3 @@ "license": "MIT", | ||
"js-yaml": "^3.5.5", | ||
"lodash": "^4.17.4", | ||
"meow": "^3.7.0", | ||
@@ -48,3 +49,3 @@ "path-exists": "^2.1.0", | ||
"devDependencies": { | ||
"ava": "^0.13.0", | ||
"ava": "^0.16.0", | ||
"babel-cli": "^6.6.5", | ||
@@ -51,0 +52,0 @@ "babel-plugin-transform-runtime": "^6.7.5", |
@@ -140,2 +140,3 @@ # i18n-patch | ||
* [Parallel groups](#parallel-groups) | ||
* [Split files with suffix](#split-files-with-suffix) | ||
@@ -871,4 +872,49 @@ ### Basic | ||
### Split files with suffix | ||
`i18n.yml` and `ja.yml` can be split into multiple files using suffix: | ||
```yaml | ||
# i18n.yml | ||
translations: | ||
- src: '*.md' | ||
patterns: | ||
- pattern: 'baz' | ||
replace: '${baz}' | ||
# i18n-1.yml | ||
translations: | ||
- src: '*.js' | ||
patterns: | ||
- pattern: 'foo' | ||
replace: '${foo}' | ||
# i18n-2.yml | ||
translations: | ||
- src: '*.html' | ||
patterns: | ||
- pattern: 'bar' | ||
replace: '${bar}' | ||
``` | ||
These files will be merged into a single configuration: | ||
```yaml | ||
translations: | ||
- src: '*.md' | ||
patterns: | ||
- pattern: 'baz' | ||
replace: '${baz}' | ||
- src: '*.js' | ||
patterns: | ||
- pattern: 'foo' | ||
replace: '${foo}' | ||
- src: '*.html' | ||
patterns: | ||
- pattern: 'bar' | ||
replace: '${bar}' | ||
``` | ||
## License | ||
MIT |
@@ -5,2 +5,4 @@ 'use strict'; | ||
import path from 'path'; | ||
import glob from 'glob'; | ||
import _ from 'lodash'; | ||
import Camelizer from './camelizer'; | ||
@@ -35,8 +37,32 @@ const yaml = require('js-yaml'); | ||
let configPath = path.join(this.options.config, `${name}.yml`); | ||
let result; | ||
if (pathExists.sync(configPath)) { | ||
return yaml.load(fs.readFileSync(configPath, ENCODING), {filename: configPath}); | ||
result = yaml.load(fs.readFileSync(configPath, ENCODING), {filename: configPath}); | ||
} else { | ||
configPath = path.join(this.options.config, `${name}.json`); | ||
return JSON.parse(fs.readFileSync(configPath)); | ||
if (pathExists.sync(configPath)) { | ||
result = JSON.parse(fs.readFileSync(configPath)); | ||
} else { | ||
result = {}; | ||
} | ||
} | ||
let filenames = glob.sync(`${name}-*.@(yml|json)`, { cwd: this.options.config }); | ||
if (filenames) { | ||
filenames = filenames.sort(); | ||
} | ||
for (let filename of filenames) { | ||
let filePath = path.join(this.options.config, filename); | ||
let obj; | ||
if (filename.match(/.*\.yml/)) { | ||
obj = yaml.load(fs.readFileSync(filePath, ENCODING), {filename: filePath}); | ||
} else if (filename.match(/.*\.json/)) { | ||
obj = JSON.parse(fs.readFileSync(filePath)); | ||
} | ||
_.mergeWith(result, obj, (objValue, srcValue) => { | ||
if (_.isArray(objValue)) { | ||
return objValue.concat(srcValue); | ||
} | ||
}); | ||
} | ||
return result; | ||
} | ||
@@ -43,0 +69,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
95402
2099
919
0
12
+ Addedlodash@^4.17.4