i18nexus-cli
Version:
Command line interface (CLI) for accessing the i18nexus API
56 lines (45 loc) • 1.32 kB
JavaScript
const fs = require('fs');
const path = require('path');
const colors = require('colors');
const handleError = require('../handleError');
const handleFetch = require('../handleFetch');
const baseUrl = require('../baseUrl');
const importJson = async opt => {
let url = `${baseUrl}/project_resources/import.json`;
url += `?api_key=${opt.apiKey}`;
if (!fs.existsSync(opt.path)) {
console.log(colors.red(`File not found: ${opt.path}`));
process.exit(1);
}
if (path.extname(opt.path) !== '.json') {
console.log(colors.red(`File is not .json: ${opt.path}`));
process.exit(1);
}
const fileData = fs.readFileSync(opt.path);
let strings;
try {
strings = JSON.parse(fileData);
} catch (e) {
console.log(colors.red('Invalid JSON'));
process.exit(1);
}
const response = await handleFetch(url, {
method: 'POST',
body: JSON.stringify({
languages: {
project_base_language: strings
},
overwrite: opt.overwrite,
namespace: opt.namespace
}),
headers: {
Authorization: `Bearer ${opt.pat}`,
'Content-Type': 'application/json'
}
});
if (response.status !== 200) {
return handleError(response);
}
console.log(colors.green(`Successfully imported strings from ${opt.path}`));
};
module.exports = importJson;