sheetspeare
Version:
Import locales from Google Sheet into Json files.
78 lines (77 loc) • 3.29 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 inquirer from 'inquirer';
import Config from '../config.js';
import { getLocalizationDiff, logLocalizationDiff, noChanges } from '../core/diff.js';
import { loadLocales } from '../core/files.js';
import { loadSpreadsheet, updateSpreadsheetLocales } from '../core/sheets.js';
import { sheetToTranslationMap } from '../core/transformers.js';
import { logger } from '../utils/logger.js';
export const pushCommand = (options) => __awaiter(void 0, void 0, void 0, function* () {
const { data: spreadsheet } = yield loadSpreadsheet({
serviceAccountEmail: Config.config.serviceAccountEmail,
serviceAccountPrivateKey: Config.config.serviceAccountPrivateKey,
spreadsheetId: Config.config.spreadsheetId,
});
if (!spreadsheet) {
logger.error('Error loading data from Google Spreadsheet');
return;
}
const localMap = yield loadLocales({ path: Config.config.path, locales: Config.config.locales });
const pulledMap = yield sheetToTranslationMap({
locales: Config.config.locales,
worksheet: spreadsheet.sheetsByIndex[0],
});
const diff = getLocalizationDiff(pulledMap, localMap);
logLocalizationDiff(diff);
if (noChanges(diff)) {
logger.info('No changes to push.');
return;
}
let forceDeleteMissingLocales = options.force;
if (options.questions) {
const prompts = [
{
type: 'confirm',
name: 'continue',
message: 'Do you want to continue?',
default: false,
},
];
if (!forceDeleteMissingLocales && Object.keys(diff.deleted).length) {
prompts.unshift({
type: 'confirm',
name: 'forceDelete',
message: 'Some keys are missing from you local files, Do you want to remove these keys from Google spreadsheet?',
default: false,
});
}
const answers = yield inquirer.prompt(prompts);
if (!answers.continue) {
logger.info('Push cancelled');
return;
}
if (answers.forceDelete) {
forceDeleteMissingLocales = true;
}
}
logger.info('Pushing to Google Spreadsheet...');
const { error } = yield updateSpreadsheetLocales({
spreadsheet,
localizationDiff: diff,
deleteMissingLocales: forceDeleteMissingLocales,
});
if (error) {
logger.error('❌ Error updating Google Spreadsheet:', error.message);
}
else {
logger.info('✅ Google Spreadsheet updated.');
}
});