diff --git a/lib/to-csv.js b/lib/to-csv.js index c6c84c6..da393c2 100644 --- a/lib/to-csv.js +++ b/lib/to-csv.js @@ -4,10 +4,7 @@ import csvStringify from 'csv-stringify'; import eol from 'eol'; import denodeify from 'denodeify'; import map from 'lodash/map'; - -// this is needed to `require` the js locale files -// instead of using fs.read -import 'babel-register'; +import parseJsonFromJsFile from './utils/parse-json-from-js-file'; const readdir = denodeify(fs.readdir); const stat = denodeify(fs.stat); @@ -73,8 +70,7 @@ export default function(localesPath, csvPath, { onlyMissing } = {}) { for (let columnIndex in filePaths) { let filePath = filePaths[columnIndex]; - // this is why we need `babel-register` above - let json = require(filePath).default; + let json = parseJsonFromJsFile(filePath); recurse(json, columnIndex, 0, ''); } diff --git a/lib/to-js.js b/lib/to-js.js index 75f4df0..b967e86 100644 --- a/lib/to-js.js +++ b/lib/to-js.js @@ -4,6 +4,7 @@ import csvParse from 'csv-parse'; import eol from 'eol'; import denodeify from 'denodeify'; import Promise from 'promise'; +import parseJsonFromJsFile from './utils/parse-json-from-js-file'; const readFile = denodeify(fs.readFile); const writeFile = denodeify(fs.writeFile); @@ -22,7 +23,7 @@ export default function(csvPath, localesPath, { ignoreJshint, merge } = {}) { if (merge) { let filePath = path.resolve(localesPath, locales[i], 'translations.js'); if (fs.existsSync(filePath)) { - obj = require(filePath).default; + obj = parseJsonFromJsFile(filePath); } } objs.push(obj); diff --git a/lib/utils/parse-json-from-js-file.js b/lib/utils/parse-json-from-js-file.js new file mode 100644 index 0000000..6228627 --- /dev/null +++ b/lib/utils/parse-json-from-js-file.js @@ -0,0 +1,8 @@ +import fs from 'fs'; + +export default filePath => { + let js = fs.readFileSync(filePath, 'utf8'); + let jsonString = js.substring(js.indexOf('{'), js.lastIndexOf('}') + 1); + let json = JSON.parse(jsonString); + return json; +}; diff --git a/package.json b/package.json index af8b43a..86d640a 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ }, "homepage": "https://github.com/kellyselden/ember-i18n-csv#readme", "dependencies": { - "babel-register": "^6.0.0", "csv-parse": "^1.0.0", "csv-stringify": "^1.0.0", "denodeify": "^1.0.0", @@ -41,6 +40,7 @@ "babel-cli": "^6.0.0", "babel-preset-es2015": "^6.0.0", "babel-preset-stage-2": "^6.0.0", + "babel-register": "^6.0.0", "chai": "^3.0.0", "coveralls": "^2.0.0", "eslint": "^2.0.0", diff --git a/test/fixtures/locales-only-missing/nl-nl/translations.js b/test/fixtures/locales-only-missing/nl-nl/translations.js index df90a78..207b3be 100644 --- a/test/fixtures/locales-only-missing/nl-nl/translations.js +++ b/test/fixtures/locales-only-missing/nl-nl/translations.js @@ -14,7 +14,7 @@ export default { "node2-2-1": "test", "node2-2-2": "test" } - }, + } }; /* jshint ignore:end */ diff --git a/test/fixtures/locales-only-missing/pt-br/translations.js b/test/fixtures/locales-only-missing/pt-br/translations.js index 3fa5a85..a5ca348 100644 --- a/test/fixtures/locales-only-missing/pt-br/translations.js +++ b/test/fixtures/locales-only-missing/pt-br/translations.js @@ -10,7 +10,7 @@ export default { }, "node2": { "node2-2": { - "node2-2-1": "test", + "node2-2-1": "test" } }, "weekday": {