@lokse/core
Advanced tools
Comparing version 1.7.0 to 2.0.0
@@ -1,3 +0,4 @@ | ||
import { CLIError } from "@oclif/errors"; | ||
export declare class MissingAuthError extends CLIError { | ||
export declare class FatalError extends Error { | ||
} | ||
export declare class MissingAuthError extends FatalError { | ||
name: string; | ||
@@ -4,0 +5,0 @@ constructor(); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.LangColumnNotFound = exports.KeyColumnNotFound = exports.MissingAuthError = void 0; | ||
const errors_1 = require("@oclif/errors"); | ||
exports.LangColumnNotFound = exports.KeyColumnNotFound = exports.MissingAuthError = exports.FatalError = void 0; | ||
const dedent = require("dedent"); | ||
class MissingAuthError extends errors_1.CLIError { | ||
class FatalError extends Error { | ||
} | ||
exports.FatalError = FatalError; | ||
class MissingAuthError extends FatalError { | ||
constructor() { | ||
super(dedent ` | ||
Cannot authenticate to fetch Spreadsheet data. | ||
Provide either Service account credentials or API key 🔑 See detail info at https://github.com/AckeeCZ/lokse/tree/master/packages/lokse#authentication | ||
Provide either Service account credentials or API key 🔑 See detail info at https://github.com/AckeeCZ/lokse/tree/master/packages/core/doc/authentication.md | ||
`); | ||
@@ -12,0 +14,0 @@ this.name = "MissingAuthError"; |
export { Reader, WorksheetReader, InvalidFilterError } from "./reader"; | ||
export type { WorksheetLinesByTitle, SheetsFilter } from "./reader"; | ||
export type { Logger } from "./logger"; | ||
export { transformersByFormat } from "./transformer"; | ||
@@ -7,1 +8,4 @@ export { default as FileWriter } from "./writer"; | ||
export { OutputFormat } from "./constants"; | ||
export { FatalError } from "./errors"; | ||
export * from "./plugins"; | ||
export * from "./utils"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
var reader_1 = require("./reader"); | ||
@@ -15,1 +16,5 @@ Object.defineProperty(exports, "Reader", { enumerable: true, get: function () { return reader_1.Reader; } }); | ||
Object.defineProperty(exports, "OutputFormat", { enumerable: true, get: function () { return constants_1.OutputFormat; } }); | ||
var errors_1 = require("./errors"); | ||
Object.defineProperty(exports, "FatalError", { enumerable: true, get: function () { return errors_1.FatalError; } }); | ||
tslib_1.__exportStar(require("./plugins"), exports); | ||
tslib_1.__exportStar(require("./utils"), exports); |
@@ -16,3 +16,5 @@ declare class Line { | ||
getPluralKey(): string; | ||
setKey(nextKey: string | ((key: string) => string)): void; | ||
setValue(nextValue: string | ((value: string) => string)): void; | ||
} | ||
export default Line; |
@@ -73,2 +73,9 @@ "use strict"; | ||
} | ||
setKey(nextKey) { | ||
this.key = typeof nextKey === "function" ? nextKey(this.key) : nextKey; | ||
} | ||
setValue(nextValue) { | ||
this.value = | ||
typeof nextValue === "function" ? nextValue(this.value) : nextValue; | ||
} | ||
} | ||
@@ -75,0 +82,0 @@ Line.checkIsPlural = function (val) { |
@@ -1,1 +0,6 @@ | ||
export default console; | ||
export interface Logger { | ||
log: (...args: any[]) => void; | ||
warn: (...args: any[]) => void; | ||
} | ||
declare const _default: Logger; | ||
export default _default; |
import Line from "../line"; | ||
import WorksheetReader from "./worksheet-reader"; | ||
import Worksheet from "./worksheet"; | ||
import type { Logger } from "../logger"; | ||
import type { PluginsRunner } from "../plugins"; | ||
export declare type WorksheetLinesByTitle = { | ||
[worksheetTitle: string]: Line[]; | ||
}; | ||
interface SpreadsheetReaderOptions { | ||
logger?: Logger; | ||
} | ||
export declare class SpreadsheetReader { | ||
private sheetsReader; | ||
private plugins; | ||
private spreadsheet; | ||
private sheetsReader; | ||
private worksheets; | ||
constructor(spreadsheetId: string, sheetsReader: WorksheetReader); | ||
logger: Logger; | ||
constructor(spreadsheetId: string, sheetsReader: WorksheetReader, plugins: PluginsRunner, options?: SpreadsheetReaderOptions); | ||
authenticate(): Promise<void>; | ||
@@ -13,0 +20,0 @@ fetchSheets(): Promise<Worksheet[]>; |
@@ -5,12 +5,14 @@ "use strict"; | ||
const google_spreadsheet_1 = require("google-spreadsheet"); | ||
const errors_1 = require("@oclif/errors"); | ||
const errors_2 = require("../errors"); | ||
const errors_1 = require("../errors"); | ||
const logger_1 = require("../logger"); | ||
class SpreadsheetReader { | ||
constructor(spreadsheetId, sheetsReader) { | ||
constructor(spreadsheetId, sheetsReader, plugins, options = {}) { | ||
this.sheetsReader = sheetsReader; | ||
this.plugins = plugins; | ||
this.logger = options.logger || logger_1.default; | ||
this.spreadsheet = new google_spreadsheet_1.GoogleSpreadsheet(spreadsheetId); | ||
this.sheetsReader = sheetsReader; | ||
this.worksheets = null; | ||
} | ||
async authenticate() { | ||
const { LOKSE_API_KEY, LOKSE_SERVICE_ACCOUNT_EMAIL, LOKSE_PRIVATE_KEY, } = process.env; | ||
const { LOKSE_API_KEY, LOKSE_SERVICE_ACCOUNT_EMAIL, LOKSE_PRIVATE_KEY } = process.env; | ||
if (LOKSE_SERVICE_ACCOUNT_EMAIL && LOKSE_PRIVATE_KEY) { | ||
@@ -27,3 +29,3 @@ await this.spreadsheet.useServiceAccountAuth({ | ||
else { | ||
throw new errors_2.MissingAuthError(); | ||
throw new errors_1.MissingAuthError(); | ||
} | ||
@@ -38,3 +40,3 @@ } | ||
catch (error) { | ||
throw new errors_1.CLIError(error.message); | ||
throw new errors_1.FatalError(error.message); | ||
} | ||
@@ -47,8 +49,10 @@ this.worksheets = await this.sheetsReader.read(this.spreadsheet); | ||
const worksheets = await this.fetchSheets(); | ||
return worksheets.reduce((worksheetLines, worksheet) => { | ||
const plugins = this.plugins; | ||
const worksheetsLines = await worksheets.reduce(async (worksheetLinesPromise, worksheet) => { | ||
const worksheetLines = await worksheetLinesPromise; | ||
const { title } = worksheet; | ||
try { | ||
const lines = worksheet.extractLines(keyColumn, valueColumn); | ||
const lines = await worksheet.extractLines(keyColumn, valueColumn, plugins); | ||
if (worksheetLines[title]) { | ||
errors_1.warn(`🔀 Found two sheets with same title ${title}. We're gonna concat the data.`); | ||
this.logger.warn(`🔀 Found two sheets with same title ${title}. We're gonna concat the data.`); | ||
worksheetLines[title] = worksheetLines[title].concat(lines); | ||
@@ -61,6 +65,7 @@ } | ||
catch (error) { | ||
errors_1.warn(error.message); | ||
this.logger.warn(error.message); | ||
} | ||
return worksheetLines; | ||
}, {}); | ||
}, Promise.resolve({})); | ||
return worksheetsLines; | ||
} | ||
@@ -67,0 +72,0 @@ } |
@@ -1,3 +0,4 @@ | ||
import { GoogleSpreadsheet, GoogleSpreadsheetWorksheet } from "google-spreadsheet"; | ||
import type { GoogleSpreadsheet, GoogleSpreadsheetWorksheet } from "google-spreadsheet"; | ||
import Worksheet from "./worksheet"; | ||
import type { Logger } from "../logger"; | ||
export declare class InvalidFilterError extends Error { | ||
@@ -17,6 +18,10 @@ filterStringified: string; | ||
}; | ||
interface WorksheetReaderOptions { | ||
logger?: Logger; | ||
} | ||
declare class WorksheetReader { | ||
static ALL_SHEETS_FILTER: string; | ||
filter: SheetFilterComplex; | ||
constructor(filter?: SheetsFilter | null); | ||
logger: Logger; | ||
constructor(filter?: SheetsFilter | null, options?: WorksheetReaderOptions); | ||
static normalizeFilter(filter?: SheetsFilter | null): SheetFilterComplex; | ||
@@ -23,0 +28,0 @@ static isSheetInTheList(worksheet: GoogleSpreadsheetWorksheet, list: SheetIndexOrTitle[]): boolean; |
@@ -5,6 +5,6 @@ "use strict"; | ||
const bluebird_1 = require("bluebird"); | ||
const errors_1 = require("@oclif/errors"); | ||
const lodash_1 = require("lodash"); | ||
const utils_1 = require("../utils"); | ||
const worksheet_1 = require("./worksheet"); | ||
const logger_1 = require("../logger"); | ||
class InvalidFilterError extends Error { | ||
@@ -19,3 +19,4 @@ constructor(filter) { | ||
class WorksheetReader { | ||
constructor(filter) { | ||
constructor(filter, options = {}) { | ||
this.logger = options.logger || logger_1.default; | ||
this.filter = WorksheetReader.normalizeFilter(filter); | ||
@@ -83,3 +84,3 @@ } | ||
message += ` that match the filter ${filterStringified}. Existing sheets are ${existingSheets}`; | ||
errors_1.warn(`${message}. `); | ||
this.logger.warn(`${message}. `); | ||
} | ||
@@ -86,0 +87,0 @@ const sheets = await bluebird_1.all(worksheets.map(this.loadSheet)); |
import { GoogleSpreadsheetRow } from "google-spreadsheet"; | ||
import Line from "../line"; | ||
import type { PluginsRunner } from "../plugins"; | ||
export default class Worksheet { | ||
@@ -8,3 +9,3 @@ title: string; | ||
constructor(title: string, header: string[], rows: GoogleSpreadsheetRow[]); | ||
extractLines(keyColumn: string, langColumn: string): Line[]; | ||
extractLines(keyColumn: string, langColumn: string, plugins: PluginsRunner): Promise<Line[]>; | ||
} |
@@ -12,3 +12,3 @@ "use strict"; | ||
} | ||
extractLines(keyColumn, langColumn) { | ||
async extractLines(keyColumn, langColumn, plugins) { | ||
let keyColumnId = ""; | ||
@@ -30,5 +30,16 @@ let langColumnId = ""; | ||
} | ||
return this.rows.map((row) => new line_1.default(row[keyColumnId], row[langColumnId])); | ||
const linesPromises = this.rows.map(async (row) => { | ||
const key = row[keyColumnId]; | ||
const unprocessedLine = new line_1.default(key, row[langColumnId]); | ||
const line = await plugins.runHook("readTranslation", unprocessedLine, { | ||
key, | ||
language: langColumn, | ||
row, | ||
}); | ||
return line; | ||
}); | ||
const lines = await Promise.all(linesPromises); | ||
return lines; | ||
} | ||
} | ||
exports.default = Worksheet; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const os_1 = require("os"); | ||
const constants_1 = require("../constants"); | ||
function setCharAt(str, index, chr) { | ||
@@ -10,2 +11,3 @@ if (index > str.length - 1) | ||
const androidTransformer = { | ||
outputFormat: constants_1.OutputFormat.ANDROID, | ||
transformComment(comment) { | ||
@@ -12,0 +14,0 @@ return " <!-- " + comment + " -->"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const os_1 = require("os"); | ||
const constants_1 = require("../constants"); | ||
const iOSTransformer = { | ||
outputFormat: constants_1.OutputFormat.IOS, | ||
transformComment(comment) { | ||
@@ -6,0 +8,0 @@ return "// " + comment; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const os_1 = require("os"); | ||
const prettier = require("prettier"); | ||
const logger_1 = require("../logger"); | ||
async function format(output) { | ||
const prettierConfig = await prettier.resolveConfigFile(); | ||
if (!prettierConfig) { | ||
return output; | ||
} | ||
const options = await prettier.resolveConfig(prettierConfig, { | ||
editorconfig: true, | ||
}); | ||
if (options === null) { | ||
return output; | ||
} | ||
try { | ||
return prettier.format(output, Object.assign({ parser: "json" }, options)); | ||
} | ||
catch (error) { | ||
logger_1.default.log("Error when formatting the output", error); | ||
return output; | ||
} | ||
} | ||
const constants_1 = require("../constants"); | ||
const jsonTransformer = { | ||
outputFormat: constants_1.OutputFormat.JSON, | ||
transformComment() { | ||
@@ -40,5 +21,3 @@ return ""; | ||
newValues = newValues.substring(0, newValues.length - 1); | ||
const output = `${os_1.EOL}{${os_1.EOL}${newValues}${os_1.EOL}}`; | ||
const formatted = await format(output); | ||
return formatted; | ||
return `${os_1.EOL}{${os_1.EOL}${newValues}${os_1.EOL}}`; | ||
}, | ||
@@ -45,0 +24,0 @@ getFileName(lang, domain) { |
@@ -0,3 +1,5 @@ | ||
import type { OutputFormat } from "../constants"; | ||
import Line from "../line"; | ||
export interface Transformer { | ||
outputFormat: OutputFormat; | ||
transformComment(comment: string): string; | ||
@@ -4,0 +6,0 @@ transformKeyValue(key: string, value: string): string; |
import Transformer from "./transformer"; | ||
import Line from "./line"; | ||
import { PluginsRunner } from "./plugins"; | ||
import type { TransformLineMeta } from "./plugins"; | ||
interface FileInfo { | ||
language: string; | ||
domain?: string; | ||
outputDir: string; | ||
} | ||
declare class FileWriter { | ||
write(filePath: string, lines: Line[], transformer: Transformer, encoding?: string): Promise<void>; | ||
getTransformedLines(lines: Line[], transformer: Transformer): string; | ||
private plugins; | ||
constructor(plugins: PluginsRunner); | ||
write(fileInfo: FileInfo, lines: Line[], transformer: Transformer, encoding?: string): Promise<string>; | ||
getTransformedLines(lines: Line[], transformer: Transformer, meta: TransformLineMeta): Promise<string>; | ||
} | ||
export default FileWriter; |
@@ -9,4 +9,11 @@ "use strict"; | ||
class FileWriter { | ||
async write(filePath, lines, transformer, encoding = "utf8") { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor(plugins) { | ||
this.plugins = plugins; | ||
} | ||
async write(fileInfo, lines, transformer, encoding = "utf8") { | ||
let fileContent = ""; | ||
const { language, domain, outputDir } = fileInfo; | ||
const fileName = transformer.getFileName(language, domain); | ||
const filePath = path.resolve(outputDir, fileName); | ||
try { | ||
@@ -20,17 +27,28 @@ await fs.accessAsync(filePath, fs.F_OK); | ||
} | ||
const valueToInsert = this.getTransformedLines(lines, transformer); | ||
const output = await transformer.insert(fileContent, valueToInsert); | ||
const valueToInsert = await this.getTransformedLines(lines, transformer, { | ||
language, | ||
domain, | ||
}); | ||
let output = await transformer.insert(fileContent, valueToInsert); | ||
output = await this.plugins.runHook("transformFullOutput", output, { | ||
transformer, | ||
language, | ||
domain, | ||
}); | ||
const dirname = path.dirname(filePath); | ||
await mkdirp(dirname); | ||
await fs.writeFileAsync(filePath, output, encoding); | ||
return fileName; | ||
} | ||
getTransformedLines(lines, transformer) { | ||
async getTransformedLines(lines, transformer, meta) { | ||
let valueToInsert = ""; | ||
const plurals = {}; | ||
for (let i = 0; i < lines.length; i++) { | ||
const line = lines[i]; | ||
const unprocessedLine = lines[i]; | ||
const isLastLine = i === lines.length - 1; | ||
if (line.isEmpty()) { | ||
if (unprocessedLine.isEmpty()) { | ||
continue; | ||
} | ||
// eslint-disable-next-line no-await-in-loop | ||
const line = await this.plugins.runHook("transformLine", unprocessedLine, meta); | ||
if (line.isComment()) { | ||
@@ -37,0 +55,0 @@ valueToInsert += transformer.transformComment(line.getComment()); |
{ | ||
"name": "@lokse/core", | ||
"description": "Core of localization from spreadsheet solution lokse", | ||
"version": "1.7.0", | ||
"version": "2.0.0", | ||
"author": { | ||
@@ -11,3 +11,2 @@ "name": "Lukas Horak", | ||
"dependencies": { | ||
"@oclif/errors": "^1.3.4", | ||
"@types/bluebird": "^3.5.32", | ||
@@ -21,4 +20,3 @@ "@types/dedent": "^0.7.0", | ||
"lodash": "^4.17.20", | ||
"mkdirp": "^1.0.4", | ||
"prettier": "^2.1.2" | ||
"mkdirp": "^1.0.4" | ||
}, | ||
@@ -44,3 +42,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "b471ec7283a567bef8c30e74b8417227678430c6" | ||
"gitHead": "6dd70bf4076eb73f4beb72615b33ea1b1ba58736" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
525767
9
44
945
4
- Removed@oclif/errors@^1.3.4
- Removedprettier@^2.1.2
- Removed@oclif/errors@1.3.6(transitive)
- Removedansi-regex@5.0.1(transitive)
- Removedansi-styles@4.3.0(transitive)
- Removedclean-stack@3.0.1(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedemoji-regex@8.0.0(transitive)
- Removedescape-string-regexp@4.0.0(transitive)
- Removedfs-extra@8.1.0(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedindent-string@4.0.0(transitive)
- Removedis-fullwidth-code-point@3.0.0(transitive)
- Removedjsonfile@4.0.0(transitive)
- Removedprettier@2.8.8(transitive)
- Removedstring-width@4.2.3(transitive)
- Removedstrip-ansi@6.0.1(transitive)
- Removeduniversalify@0.1.2(transitive)
- Removedwrap-ansi@7.0.0(transitive)