Skip to content

Commit

Permalink
start making the API work
Browse files Browse the repository at this point in the history
  • Loading branch information
nicmosc committed May 4, 2017
1 parent dc3eb2f commit bf98dd6
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015", "stage-0"]
}
81 changes: 81 additions & 0 deletions dist/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.localeappPull = localeappPull;
exports.localeappPush = localeappPush;

var _request = require('request');

var _request2 = _interopRequireDefault(_request);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var localeapp = 'https://api.localeapp.com';

function request(type, url, key) {
var data = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};

return new Promise(function (resolve, reject) {
// console.log(data);
console.log(url);
var fullUrl = localeapp + '/v1/projects/' + key + '/' + url.replace(/^\//, '');
console.log(fullUrl);

_request2.default.get(fullUrl, function (error, response, body) {
if (error) {
reject(error);
} else {
resolve({ response: response, body: body });
}
});

// jquery.ajax({
// url: fullUrl,
// type,
// data: JSON.stringify(data),
// dataType: 'json',
// contentType: 'application/json',
// beforeSend(request) {
// // request.setRequestHeader(
// // 'Authorization',
// // 'Token token=' + window.gon.current_user.authentication_token
// // );
// }
// }).then(resolve, reject).fail(reject);
});
};

function localeappPull(key) {
return request('GET', '/translations/all.yml', key);
};

function localeappPush(key, file) {};

// export const Annotation = {
// create(data) {
// const entity = data.toEntity ? data.toEntity() : data;
// const annotation = annotationToServer(entity);
// let result;
// return request('POST', '/studio/annotations', { annotation });
// },
// update(id, data) {
// const entity = data.toEntity ? data.toEntity() : data;
// const annotation = {
// ...annotationToServer(entity),
// id,
// };
// let result;
// return request('POST', `/studio/annotations`, { annotation });
// },
// delete(id) {
// return request('DELETE', `/studio/annotations/${id}`);
// },
// };


// export default {
// localeappPush,
// localeappPull,
// };
53 changes: 48 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ var _shelljs = require('shelljs');

var _shelljs2 = _interopRequireDefault(_shelljs);

var _dotenv = require('dotenv');

var _dotenv2 = _interopRequireDefault(_dotenv);

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _fromFolders = require('./from-folders');

var _fromFolders2 = _interopRequireDefault(_fromFolders);
Expand All @@ -21,6 +29,8 @@ var _toFolders = require('./to-folders');

var _toFolders2 = _interopRequireDefault(_toFolders);

var _api = require('./api');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
Expand All @@ -44,14 +54,47 @@ function update(rootFolder, targetPath, locale) {
}

function push(rootFolder, targetPath, locale) {
var result = update(rootFolder, targetPath, locale);
// do some promise thing and then...
// command to push to localeapp (only the target file)
_shelljs2.default.exec('localeapp push ' + targetPath + '/' + locale + '.yml').stdout;
update(rootFolder, targetPath, locale);

var localeappKey = (0, _get2.default)(_dotenv2.default.config(), 'parsed.LOCALEAPP_KEY', null);

if (_shelljs2.default.exec('localeapp push ' + targetPath + '/' + locale + '.yml').code !== 0) {
_shelljs2.default.echo('Not a rails project, trying with env variable');

if (!localeappKey) {
console.error('No localeapp project key found in .env! Please specify one');
} else {
(0, _api.localeappPush)(localeappKey, 'file');
}
}
// else {
// shell.exec(
// `localeapp push ${targetPath}/${locale}.yml`,
// ).stdout;
// }
}

function pull(rootFolder, targetPath, locale) {
var result = _shelljs2.default.exec('localeapp pull').stdout;

var localeappKey = (0, _get2.default)(_dotenv2.default.config(), 'parsed.LOCALEAPP_KEY', null);

if (_shelljs2.default.exec('localeapp pull').code !== 0) {
_shelljs2.default.echo('Not a rails project, trying with env variable');

if (!localeappKey) {
console.error('No localeapp project key found in .env! Please specify one');
} else {
var allTrans = (0, _api.localeappPull)(localeappKey).then(function (_ref) {
var response = _ref.response,
body = _ref.body;

console.log(body);
});
}

return;
}

var compiledLocale = _fs2.default.readFileSync(targetPath + '/' + locale + '.yml', 'utf8');
var updatedFolders = (0, _toFolders2.default)(rootFolder, compiledLocale, locale);
return updatedFolders;
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
"babel-polyfill": "^6.23.0",
"commander": "^2.9.0",
"directory-tree": "^1.2.0",
"dotenv": "^4.0.0",
"fs": "^0.0.1-security",
"js-yaml": "^3.8.2",
"lodash": "^4.17.4",
"rc": "^1.1.7",
"request": "^2.81.0",
"rimraf": "^2.6.1",
"shelljs": "^0.7.7"
},
Expand Down
75 changes: 75 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import rq from 'request';


const localeapp = 'https://api.localeapp.com';


function request(type, url, key, data={}) {
return new Promise((resolve, reject) => {
// console.log(data);
console.log(url);
const fullUrl = `${localeapp}/v1/projects/${key}/${url.replace(/^\//, '')}`;
console.log(fullUrl);

rq.get(fullUrl, (error, response, body) => {
if (error) {
reject(error);
}
else {
resolve({ response, body });
}
});

// jquery.ajax({
// url: fullUrl,
// type,
// data: JSON.stringify(data),
// dataType: 'json',
// contentType: 'application/json',
// beforeSend(request) {
// // request.setRequestHeader(
// // 'Authorization',
// // 'Token token=' + window.gon.current_user.authentication_token
// // );
// }
// }).then(resolve, reject).fail(reject);
});
};


export function localeappPull(key) {
return request('GET', '/translations/all.yml', key);
};


export function localeappPush(key, file) {

};


// export const Annotation = {
// create(data) {
// const entity = data.toEntity ? data.toEntity() : data;
// const annotation = annotationToServer(entity);
// let result;
// return request('POST', '/studio/annotations', { annotation });
// },
// update(id, data) {
// const entity = data.toEntity ? data.toEntity() : data;
// const annotation = {
// ...annotationToServer(entity),
// id,
// };
// let result;
// return request('POST', `/studio/annotations`, { annotation });
// },
// delete(id) {
// return request('DELETE', `/studio/annotations/${id}`);
// },
// };


// export default {
// localeappPush,
// localeappPull,
// };
52 changes: 45 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import fs from 'fs';
import shell from 'shelljs';
import dotenv from 'dotenv';
import get from 'lodash/get';

import fromFolders from './from-folders';
import toFolders from './to-folders';

import { localeappPull, localeappPush } from './api';


/**
AVAILABLE COMMANDS:
- update : will parse the translations from the root folder provided and replae
Expand All @@ -26,22 +31,55 @@ function update(rootFolder, targetPath, locale) {
return finalTranslation;
}


function push(rootFolder, targetPath, locale) {
const result = update(rootFolder, targetPath, locale);
// do some promise thing and then...
// command to push to localeapp (only the target file)
shell.exec(
`localeapp push ${targetPath}/${locale}.yml`,
).stdout;
update(rootFolder, targetPath, locale);

const localeappKey = get(dotenv.config(), 'parsed.LOCALEAPP_KEY', null);

if (shell.exec(`localeapp push ${targetPath}/${locale}.yml`).code !== 0) {
shell.echo('Not a rails project, trying with env variable');

if (! localeappKey) {
console.error('No localeapp project key found in .env! Please specify one');
}
else {
localeappPush(localeappKey, 'file');
}
}
// else {
// shell.exec(
// `localeapp push ${targetPath}/${locale}.yml`,
// ).stdout;
// }
}


function pull(rootFolder, targetPath, locale) {
const result = shell.exec('localeapp pull').stdout;

const localeappKey = get(dotenv.config(), 'parsed.LOCALEAPP_KEY', null);

if (shell.exec('localeapp pull').code !== 0) {
shell.echo('Not a rails project, trying with env variable');

if (! localeappKey) {
console.error('No localeapp project key found in .env! Please specify one');
}
else {
const allTrans = localeappPull(localeappKey).then(({ response, body }) => {
console.log(body);
});
}

return;
}

const compiledLocale = fs.readFileSync(`${targetPath}/${locale}.yml`, 'utf8');
const updatedFolders = toFolders(rootFolder, compiledLocale, locale);
return updatedFolders;
}


export default function awesome(command, rootFolder, targetPath, defaultLocale='en') { // this must be set somewhere
if (command === 'update') {
return update(rootFolder, targetPath, defaultLocale);
Expand Down
6 changes: 5 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,10 @@ directory-tree@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/directory-tree/-/directory-tree-1.2.0.tgz#b1b87f06e00e46b7a88acd22d8b83d8728e5f758"

dotenv@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-4.0.0.tgz#864ef1379aced55ce6f95debecdce179f7a0cd1d"

ecc-jsbn@~0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.1.tgz#0fc73a9ed5f0d53c38193398523ef7e543777505"
Expand Down Expand Up @@ -1731,7 +1735,7 @@ repeating@^2.0.0:
dependencies:
is-finite "^1.0.0"

request@^2.79.0:
request@^2.79.0, request@^2.81.0:
version "2.81.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.81.0.tgz#c6928946a0e06c5f8d6f8a9333469ffda46298a0"
dependencies:
Expand Down

0 comments on commit bf98dd6

Please sign in to comment.