Skip to content
This repository has been archived by the owner on Feb 15, 2019. It is now read-only.

Commit

Permalink
babel
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden committed Jan 11, 2016
1 parent ea34bbe commit ed7ecf0
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 146 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["es2015", "stage-2"],
"plugins": []
}
7 changes: 7 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
ecmaFeatures:
modules: true
blockBindings: true
arrowFunctions: true
destructuring: true
templateStrings: true
rules:
indent:
- 2
Expand All @@ -9,6 +15,7 @@ rules:
semi:
- 2
- always
no-var: 2
env:
node: true
mocha: true
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
dist
node_modules
tmp
6 changes: 3 additions & 3 deletions bin/ember-i18n-csv.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

var emberI18nCsv = require('../lib/ember-i18n-csv');
var emberI18nCsv = require('../dist/ember-i18n-csv').default;

var argv = require('yargs')
.options({
Expand All @@ -13,10 +13,10 @@ var argv = require('yargs')
var direction = argv._[0];
var localesPath = argv['locales-path'];
var csvPath = argv['csv-path'];
var jshintIgnore = argv['jshint-ignore'];
var ignoreJshint = argv['jshint-ignore'];

var options = {
jshintIgnore: jshintIgnore
ignoreJshint: ignoreJshint
};

emberI18nCsv(direction, localesPath, csvPath, options);
16 changes: 8 additions & 8 deletions lib/ember-i18n-csv.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var toCsv = require('./to-csv');
var toJs = require('./to-js');
import toCsv from './to-csv';
import toJs from './to-js';

module.exports = function(direction, localesPath, csvPath, options) {
export default function(direction, localesPath, csvPath, options) {
switch (direction) {
case 'to-csv':
toCsv(localesPath, csvPath, options);
break;
return toCsv(localesPath, csvPath, options);
case 'to-js':
toJs(csvPath, localesPath, options);
break;
return toJs(csvPath, localesPath, options);
default:
return Promise.reject();
}
};
}
106 changes: 56 additions & 50 deletions lib/to-csv.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,69 @@
var fs = require('fs');
var path = require('path');
var stringify = require('csv-stringify');
var eol = require('eol');
import fs from 'fs';
import path from 'path';
import csvStringify from 'csv-stringify';
import eol from 'eol';
import denodeify from 'denodeify';
require('6to5/register');

module.exports = function(localesPath, csvPath) {
var locales = fs.readdirSync(localesPath);
const readdir = denodeify(fs.readdir);
const writeFile = denodeify(fs.writeFile);
const stringify = denodeify(csvStringify);

var keys = [];
var rows = [];
export default function(localesPath, csvPath) {
return readdir(localesPath).then(locales => {
let keys = [];
let rows = [];

function recurse(json, columnIndex, rowIndex, currentKey) {
for (var key in json) {
var value = json[key];
var newKey = currentKey;
if (newKey) {
newKey += '.';
}
newKey += key;
if (typeof value === 'object') {
rowIndex = recurse(value, columnIndex, rowIndex, newKey);
} else {
var row;
var index = keys.indexOf(newKey);
if (index !== -1) {
row = rows[index];
} else {
row = [];
rows.splice(rowIndex, 0, row);
keys.splice(rowIndex, 0, newKey);
function recurse(json, columnIndex, rowIndex, currentKey) {
for (let key in json) {
let value = json[key];
let newKey = currentKey;
if (newKey) {
newKey += '.';
}
for (var i in locales) {
if (i == columnIndex) {
row[i] = value;
} else if (!row[i]) {
row[i] = '';
newKey += key;
// debugger;
if (typeof value === 'object') {
rowIndex = recurse(value, columnIndex, rowIndex, newKey);
} else {
let row;
let index = keys.indexOf(newKey);
if (index !== -1) {
row = rows[index];
} else {
row = [];
rows.splice(rowIndex, 0, row);
keys.splice(rowIndex, 0, newKey);
}
for (let i in locales) {
if (i == columnIndex) {
row[i] = value;
} else if (!row[i]) {
row[i] = '';
}
}
rowIndex++;
}
rowIndex++;
}
return rowIndex;
}
return rowIndex;
}

for (var columnIndex in locales) {
var locale = locales[columnIndex];
var filePath = path.resolve(localesPath, locale, 'translations.js');
var json = require(filePath);
recurse(json, columnIndex, 0, '');
}
for (let columnIndex in locales) {
let locale = locales[columnIndex];
let filePath = path.resolve(localesPath, locale, 'translations.js');
let json = require(filePath);
recurse(json, columnIndex, 0, '');
}

var lines = [];
lines.push(['key'].concat(locales.map(function(locale) { return locale.replace('.js', ''); })));
for (var i in keys) {
lines.push([keys[i]].concat(rows[i]));
}
let lines = [];
lines.push(['key'].concat(locales.map(locale => locale.replace('.js', ''))));
for (let i in keys) {
lines.push([keys[i]].concat(rows[i]));
}

stringify(lines, function(err, csv) {
var normalized = eol.auto(csv);
fs.writeFileSync(csvPath, normalized);
return stringify(lines);
}).then(csv => {
let normalized = eol.auto(csv);
return writeFile(csvPath, normalized);
});
};
}
54 changes: 31 additions & 23 deletions lib/to-js.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
var fs = require('fs');
var path = require('path');
var parse = require('csv-parse');
var eol = require('eol');
import fs from 'fs';
import path from 'path';
import csvParse from 'csv-parse';
import eol from 'eol';
import denodeify from 'denodeify';
import Promise from 'promise';

module.exports = function(csvPath, localesPath, ignoreJshint) {
var csv = fs.readFileSync(csvPath, 'utf8');
const readFile = denodeify(fs.readFile);
const writeFile = denodeify(fs.writeFile);
const parse = denodeify(csvParse);

parse(csv, function(err, lines) {
var i, columnIndex;
var locales = lines.shift().slice(1);
export default function(csvPath, localesPath, { ignoreJshint } = {}) {
return readFile(csvPath, 'utf8').then(csv => {
return parse(csv);
}).then(lines => {
let i, columnIndex;
let locales = lines.shift().slice(1);

var objs = [];
let objs = [];
for (i in locales) {
objs.push({});
}

function recurse(keySections, obj, value) {
var key = keySections[0];
let key = keySections[0];
if (keySections.length > 1) {
if (!obj[key]) {
obj[key] = {};
Expand All @@ -28,30 +34,32 @@ module.exports = function(csvPath, localesPath, ignoreJshint) {
}

for (i in lines) {
var line = lines[i];
var key = line.shift();
var keySections = key.split('.');
let line = lines[i];
let key = line.shift();
let keySections = key.split('.');
for (columnIndex in line) {
var obj = objs[columnIndex];
var value = line[columnIndex];
let obj = objs[columnIndex];
let value = line[columnIndex];
recurse(keySections, obj, value);
}
}

let promises = [];
for (columnIndex in locales) {
var locale = locales[columnIndex];
var localePath = path.join(localesPath, locale);
let locale = locales[columnIndex];
let localePath = path.join(localesPath, locale);
if (!fs.existsSync(localePath)) {
fs.mkdirSync(localePath);
}
var filePath = path.join(localePath, 'translations.js');
var jsonString = JSON.stringify(objs[columnIndex], null, 2);
var string = 'export default ' + jsonString + ';\n';
let filePath = path.join(localePath, 'translations.js');
let jsonString = JSON.stringify(objs[columnIndex], null, 2);
let string = 'export default ' + jsonString + ';\n';
if (ignoreJshint) {
string = '/* jshint ignore:start */\n\n' + string + '\n/* jshint ignore:end */\n';
}
string = eol.auto(string);
fs.writeFileSync(filePath, string);
promises.push(writeFile(filePath, string));
}
return Promise.all(promises);
});
};
}
24 changes: 17 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
"name": "ember-cli-csv",
"version": "0.0.1",
"description": "JSON to CSV and vice-versa for ember-i18n",
"main": "lib/ember-i18n-csv.js",
"main": "dist/ember-i18n-csv.js",
"bin": {
"ember-i18n-csv": "bin/ember-i18n-csv.js"
},
"scripts": {
"test": "eslint-mocha --eslint-args=\"lib/**/*.js test/acceptance/**/*.js\" --mocha-args=\"test/**/*-test.js\"",
"cover": "istanbul cover node_modules/eslint-mocha/bin/eslint-mocha.js --include-all-sources true -- --eslint-args=\"lib/**/*.js test/acceptance/**/*.js\" --mocha-args=\"test/**/*-test.js\""
"build": "babel lib -d dist",
"pretest": "npm run build",
"precover": "npm run build",
"test": "eslint-mocha --eslint-args=\"lib/**/*.js test/acceptance/**/*.js\" --mocha-args=\"--compilers js:babel-register test/**/*-test.js\"",
"cover": "babel-node node_modules/isparta/bin/isparta cover --include-all-sources --report html --report lcov node_modules/eslint-mocha/bin/eslint-mocha.js -- --eslint-args=\"lib/**/*.js test/acceptance/**/*.js\" --mocha-args=\"--compilers js:babel-register test/**/*-test.js\"",
"debug": "node debug node_modules/mocha/bin/_mocha --no-timeouts --compilers js:babel-register \"test/acceptance/**/*-test.js\"",
"lint": "eslint lib/**/*.js test/acceptance/**/*.js"
},
"repository": {
"type": "git",
Expand All @@ -20,15 +25,20 @@
"6to5": "^3.5.3",
"csv-parse": "0.0.6",
"csv-stringify": "0.0.6",
"denodeify": "^1.0.0",
"eol": "0.2.0",
"promise": "^7.0.0",
"yargs": "^3.0.0"
},
"devDependencies": {
"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",
"dir-compare": "0.0.2",
"eslint-mocha": "0.0.3",
"fs-extra": "0.26.4",
"istanbul": "0.4.1"
"eslint-mocha": "0.0.4",
"fs-equal": "0.0.1",
"isparta": "^4.0.0"
}
}
33 changes: 21 additions & 12 deletions test/acceptance/to-csv-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var expect = require('chai').expect;
var test = require('../../lib/test');
var spawn = require('child_process').spawn;
var fs = require('fs-extra');
var compareFiles = require('../helpers/compare-files');
import { expect } from 'chai';
import test from '../../lib/test';
import { spawn } from 'child_process';
import fs from 'fs-extra';
import { areFilesEqual } from 'fs-equal';
// import toCsv from '../../lib/to-csv';

describe('Array', function() {
describe('#indexOf()', function() {
Expand All @@ -20,25 +21,33 @@ describe('to-csv', function() {
});

it('works', function(done) {
var ps = spawn(process.execPath, [
let ps = spawn(process.execPath, [
'bin/ember-i18n-csv.js',
'to-csv',
'--locales-path=test/fixtures/locales',
'--csv-path=tmp/i18n.csv'
]);

var out = '';
var err = '';
ps.stdout.on('data', function(buffer) { out += buffer; });
ps.stderr.on('data', function(buffer) { err += buffer; });
let out = '';
let err = '';
ps.stdout.on('data', buffer => out += buffer);
ps.stderr.on('data', buffer => err += buffer);

ps.on('exit', function() {
ps.on('exit', () => {
expect(out).to.equal('');
expect(err).to.equal('');
compareFiles('tmp/i18n.csv', 'test/fixtures/i18n.csv').then(function(areSame) {
areFilesEqual('tmp/i18n.csv', 'test/fixtures/i18n.csv').then(areSame => {
expect(areSame).to.be.true;
done();
}).catch(done);
});
});

// it.only('works2', function() {
// return toCsv('test/fixtures/locales', 'tmp/i18n.csv').then(() => {
// return areFilesEqual('tmp/i18n.csv', 'test/fixtures/i18n.csv').then(areSame => {
// expect(areSame).to.be.true;
// });
// });
// });
});

0 comments on commit ed7ecf0

Please sign in to comment.