sheetspeare
Version:
Import locales from Google Sheet into Json files.
57 lines (56 loc) • 2.61 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import * as fs from 'fs';
import * as path from 'path';
import chalk from 'chalk';
import { parseMap, stringifyLocaleKey, stringifyMap } from '../utils/json.js';
import { logger } from '../utils/logger.js';
import { translationMapToLocalizedMap } from './transformers.js';
export const resolveLocalePath = (p, locale) => {
return path.resolve(`${p}/${locale}.json`);
};
export const ensureDirectoryExistence = (p) => {
const dirname = path.resolve(p);
if (fs.existsSync(dirname)) {
return true;
}
fs.mkdirSync(dirname, { recursive: true });
};
export const createConfigFile = (p, config) => {
fs.writeFileSync(path.resolve(p), JSON.stringify(config, null, 2), {
encoding: 'utf-8',
});
logger.info('✅ Configuration file successfully created.');
};
export const loadLocales = (options) => __awaiter(void 0, void 0, void 0, function* () {
const map = {};
options.locales.forEach((locale) => {
const localePath = resolveLocalePath(options.path, locale);
if (fs.existsSync(localePath)) {
Object.entries(parseMap(fs.readFileSync(localePath, { encoding: 'utf-8' }))).forEach(([key, value]) => {
map[stringifyLocaleKey(locale, key)] = value;
});
}
else if (!options.ignoreMissingLocaleFiles) {
logger.warn(chalk.yellow(`⚠️ Locale file for ${locale} not found!`));
}
});
return map;
});
export const saveLocales = (options) => __awaiter(void 0, void 0, void 0, function* () {
ensureDirectoryExistence(options.path);
const localizedMap = translationMapToLocalizedMap(options.translationMap);
Object.entries(localizedMap).forEach(([locale, content]) => {
fs.writeFileSync(resolveLocalePath(options.path, locale), stringifyMap(content), {
encoding: 'utf-8',
});
});
logger.info('✅ Localization files successfully pulled.');
});