Skip to content

Commit

Permalink
use package name to set keys and use json
Browse files Browse the repository at this point in the history
  • Loading branch information
nicmosc committed Mar 27, 2018
1 parent 4ee6cab commit b7d24c6
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
npm-debug.log
.config
yarn-error.log
12 changes: 6 additions & 6 deletions bin/louki
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ program

program
.command('init')
.description('To initialise louki with the localeapp secret key. Required arguent: secret key.');
.description('To initialise louki with the localeapp secret key. Required argument: secret_key.');

program
.command('update')
Expand All @@ -43,12 +43,12 @@ program.parse(process.argv);

if (typeof cmdValue === 'undefined') {
console.error('no command given!');
process.exit(1);
process.exit();
}

else if (! ['update', 'push', 'pull', 'init'].includes(cmdValue)) {
console.error('Command not supported or recognised');
process.exit(1);
process.exit();
}
else {
if (typeof appCfg.config === 'undefined') {
Expand All @@ -63,15 +63,15 @@ else {


if (cmdValue === 'init' && ! optionsValue) {
console.error('Localeapp key not specified');
process.exit(1);
console.error('Localeapp key not specified.');
process.exit();
}

if (typeof localeTarget === 'undefined'
|| typeof localeSource === 'undefined'
|| typeof defaultLocale === 'undefined') {
console.error('Missing config options! Provide root, source, default');
process.exit(1);
process.exit();
}

var targetPath = rootPath + '/' + localeTarget;
Expand Down
22 changes: 17 additions & 5 deletions dist/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Object.defineProperty(exports, "__esModule", {
value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

exports.default = init;

var _fs = require('fs');
Expand All @@ -17,23 +20,32 @@ var _utils = require('./utils');

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function init(key) {
var value = 'LOCALEAPP_KEY="' + key + '"';
var configPath = (0, _utils.getConfigPath)(true);
var projectName = (0, _utils.getProjectName)();
var existingKeys = {};
try {
existingKeys = JSON.parse(_fs2.default.readFileSync(configPath, 'utf8'));
} catch (e) {
console.log('No previous keys');
}
var combined = _extends({}, existingKeys, _defineProperty({}, projectName, key));
_fs2.default.open(configPath, 'w', function (err, fd) {
if (err) {
_fs2.default.writeFile(configPath, '', function (err) {
if (err) throw err;
writeConfig(fd, value);
writeConfig(fd, combined);
});
} else {
writeConfig(fd, value);
writeConfig(fd, combined);
}
});
}

function writeConfig(fd, value) {
_fs2.default.write(fd, value, function (err, written) {
function writeConfig(fd, keys) {
_fs2.default.write(fd, JSON.stringify(keys), function (err, written) {
if (err) {
console.error('Could not save key');
throw err;
Expand Down
5 changes: 3 additions & 2 deletions dist/pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function pull(rootFolder, targetPath, locale) {

try {
var configPath = (0, _utils.getConfigPath)();
var localeappKey = _fs2.default.readFileSync(configPath, 'utf8').split('=')[1].replace(/"/g, '');
var projectName = (0, _utils.getProjectName)();
var localeappKey = JSON.parse(_fs2.default.readFileSync(configPath, 'utf8'))[projectName];
return (0, _utils.localeappPull)(localeappKey).then(function (_ref) {
var response = _ref.response,
body = _ref.body;
Expand All @@ -48,6 +49,6 @@ function pull(rootFolder, targetPath, locale) {
return console.error(err);
});
} catch (err) {
console.error('No localeapp project key found in! Please specify one with the init command');
console.error('No localeapp project key found! Please specify one with the init command');
}
}
5 changes: 3 additions & 2 deletions dist/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ function push(rootFolder, targetPath, locale, pushDefault) {
}
try {
var configPath = (0, _utils.getConfigPath)();
var localeappKey = _fs2.default.readFileSync(configPath, 'utf8').split('=')[1].replace(/"/g, '');
var projectName = (0, _utils.getProjectName)();
var localeappKey = JSON.parse(_fs2.default.readFileSync(configPath, 'utf8'))[projectName];
var filePath = targetPath + '/' + locale + '.yml';
var data = _fs2.default.createReadStream(filePath);
return (0, _utils.localeappPush)(localeappKey, data).then(function (_ref) {
Expand All @@ -45,6 +46,6 @@ function push(rootFolder, targetPath, locale, pushDefault) {
return console.error(err);
});
} catch (err) {
console.log('No localeapp project key found in! Please specify one with the init command');
console.log('No localeapp project key found! Please specify one with the init command');
}
}
17 changes: 16 additions & 1 deletion dist/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ Object.keys(_conversion).forEach(function (key) {
});
});
exports.getConfigPath = getConfigPath;
exports.getProjectName = getProjectName;

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

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

var _fromFolders3 = _interopRequireDefault(_fromFolders2);
Expand All @@ -54,5 +59,15 @@ function getConfigPath() {
if (!_fs2.default.existsSync(directory) && create) {
_fs2.default.mkdirSync(directory);
}
return directory + '/config';
return directory + '/config.json';
}

function getProjectName() {
var pathToPackage = _path2.default.resolve('package.json');
var name = JSON.parse(_fs2.default.readFileSync(pathToPackage, 'utf8')).name;
if (!name) {
console.log('Missing required "name" in package.json');
process.exit();
}
return name;
}
22 changes: 15 additions & 7 deletions src/init.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
import fs from 'fs';
import path from 'path';

import { getConfigPath } from './utils';
import { getConfigPath, getProjectName } from './utils';


export default function init(key) {
const value = `LOCALEAPP_KEY="${key}"`;
const configPath = getConfigPath(true);
const projectName = getProjectName();
let existingKeys = {};
try {
existingKeys = JSON.parse(fs.readFileSync(configPath, 'utf8'));
}
catch (e) {
console.log('No previous keys');
}
const combined = { ...existingKeys, [projectName]: key };
fs.open(configPath, 'w', (err, fd) => {
if (err) {
fs.writeFile(configPath, '', (err) => {
if(err) throw err;
writeConfig(fd, value);
writeConfig(fd, combined);
});
}
else {
writeConfig(fd, value);
writeConfig(fd, combined);
}
});
}


function writeConfig(fd, value) {
fs.write(fd, value, (err, written) => {
function writeConfig(fd, keys) {
fs.write(fd, JSON.stringify(keys), (err, written) => {
if (err) {
console.error('Could not save key');
throw err;
}
console.log('Successfully saved localeapp key');
})
});
}
6 changes: 4 additions & 2 deletions src/pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {
jsonToYml,
toFolders,
getConfigPath,
getProjectName,
} from './utils';


export default function pull(rootFolder, targetPath, locale, raw=false) {
try {
const configPath = getConfigPath();
const localeappKey = fs.readFileSync(configPath, 'utf8').split('=')[1].replace(/"/g, '');
const projectName = getProjectName();
const localeappKey = JSON.parse(fs.readFileSync(configPath, 'utf8'))[projectName];
return localeappPull(localeappKey).then(({ response, body }) => {
const localesArray = ymlToJson(body);
console.log(`Successfully pulled locales ${Object.keys(localesArray).join(', ')} from Localeapp`);
Expand All @@ -30,6 +32,6 @@ export default function pull(rootFolder, targetPath, locale, raw=false) {
}).catch((err) => console.error(err));
}
catch (err) {
console.error('No localeapp project key found in! Please specify one with the init command');
console.error('No localeapp project key found! Please specify one with the init command');
}
}
7 changes: 4 additions & 3 deletions src/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import get from 'lodash/get';
import path from 'path';

import { localeappPush, getConfigPath } from './utils';
import { localeappPush, getConfigPath, getProjectName } from './utils';
import update from './update';


Expand All @@ -12,14 +12,15 @@ export default function push(rootFolder, targetPath, locale, pushDefault, raw=fa
}
try {
const configPath = getConfigPath();
const localeappKey = fs.readFileSync(configPath, 'utf8').split('=')[1].replace(/"/g, '');
const projectName = getProjectName();
const localeappKey = JSON.parse(fs.readFileSync(configPath, 'utf8'))[projectName];
const filePath = `${targetPath}/${locale}.yml`;
const data = fs.createReadStream(filePath);
return localeappPush(localeappKey, data).then(({ response, body }) => {
console.log(`Successfully pushed ${locale}.yml to Localeapp`);
}).catch((err) => console.error(err));
}
catch (err) {
console.log('No localeapp project key found in! Please specify one with the init command');
console.log('No localeapp project key found! Please specify one with the init command');
}
}
14 changes: 13 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import path from 'path';

export * from './api';
export * from './conversion';
Expand All @@ -12,5 +13,16 @@ export function getConfigPath(create=false) {
if (!fs.existsSync(directory) && create) {
fs.mkdirSync(directory);
}
return `${directory}/config`;
return `${directory}/config.json`;
}


export function getProjectName() {
const pathToPackage = path.resolve('package.json');
const name = JSON.parse(fs.readFileSync(pathToPackage, 'utf8')).name;
if (! name) {
console.log('Missing required "name" in package.json');
process.exit();
}
return name;
}

0 comments on commit b7d24c6

Please sign in to comment.