UNPKG

sheetspeare

Version:

Import locales from Google Sheet into Json files.

75 lines (74 loc) 3.82 kB
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 chalk from 'chalk'; import { PackageName } from '../constants.js'; import { initializeSpreadsheet, loadSpreadsheet } from '../core/sheets.js'; import { createConfigFile } from '../core/files.js'; import { logger } from '../utils/logger.js'; export const initCommand = () => __awaiter(void 0, void 0, void 0, function* () { // Ensure that the user has set up the required environment variables before running the command if (!process.env.SERVICE_ACCOUNT_EMAIL || !process.env.SERVICE_ACCOUNT_PRIVATE_KEY) { logger.error('Missing credentials. Make sure Your Google Service Account credentials are properly setup as environment variables. See README for more info: https://github.com/mogharsallah/sheetspeare#setup-environment-variables'); return; } logger.info(`Hey there! Let's set up your ${PackageName} configuration file 💪.`); const answers = yield inquirer.prompt([ { type: 'input', name: 'spreadsheetId', message: 'What is your Google Spreadsheet ID? If you have not created one yet, create an empty spreadsheet and paste the ID here.', validate: (value) => value.length > 0 || 'Please enter your Google Spreadsheet ID: ', }, { type: 'input', name: 'path', message: 'Where do you want to save your localization files?', default: 'src/locales', }, { type: 'input', name: 'locales', message: `Which locales do you need? ${chalk.italic('(comma separated)')}`, default: 'en,es', filter(val) { return val.toLowerCase().replace(/ /g, '').split(','); }, }, { type: 'confirm', name: 'initializeSpreadsheet', message: `Would you like to initialize your spreadsheet? ${chalk.yellow('This removes all data in the spreadsheet!')}`, default: true, }, ]); logger.info('Checking access to Google Spreadsheet...'); const { data: spreadsheet, error } = yield loadSpreadsheet({ serviceAccountEmail: process.env.SERVICE_ACCOUNT_EMAIL, serviceAccountPrivateKey: process.env.SERVICE_ACCOUNT_PRIVATE_KEY, spreadsheetId: answers.spreadsheetId, }); if (error) { logger.error('Could not access to Google Spreadsheet. Please check your credentials and try again.', error.message); return; } logger.info('✅ Access to Google Spreadsheet granted!'); if (answers.initializeSpreadsheet) { initializeSpreadsheet({ spreadsheet, locales: answers.locales }); } logger.info('Creating configuration file...'); createConfigFile(`.${PackageName}rc.json`, { locales: answers.locales, spreadsheetId: answers.spreadsheetId, path: answers.path, }); logger.info(chalk.bold(`Great! We are ready to go 🚀. Run ${chalk.gray(`${PackageName} pull`)}`)); console.info(`To update the Google Spreadsheet with your local locales: Run ${chalk.gray(`${PackageName} push`)}`); });