mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 16:25:21 +00:00
Splitted strings utils to its own file
This commit is contained in:
@@ -6,7 +6,7 @@ import { Stream } from "stream";
|
|||||||
import { processDate } from "./processDate";
|
import { processDate } from "./processDate";
|
||||||
import forge from "node-forge";
|
import forge from "node-forge";
|
||||||
import * as Signature from "./signature";
|
import * as Signature from "./signature";
|
||||||
import { EOL } from "os";
|
import * as Strings from "./StringsUtils";
|
||||||
import { isValidRGB } from "./utils";
|
import { isValidRGB } from "./utils";
|
||||||
|
|
||||||
/** Exporting for tests specs */
|
/** Exporting for tests specs */
|
||||||
@@ -430,7 +430,7 @@ export default class PKPass extends Bundle {
|
|||||||
|
|
||||||
Object.assign(
|
Object.assign(
|
||||||
(this[localizationSymbol][lang] ??= {}),
|
(this[localizationSymbol][lang] ??= {}),
|
||||||
Object.fromEntries(parseStringsFile(buffer).translations),
|
Object.fromEntries(Strings.parse(buffer).translations),
|
||||||
);
|
);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -565,7 +565,7 @@ export default class PKPass extends Bundle {
|
|||||||
) {
|
) {
|
||||||
const [lang, translations] = entry;
|
const [lang, translations] = entry;
|
||||||
|
|
||||||
const stringsFile = createStringFile(translations);
|
const stringsFile = Strings.create(translations);
|
||||||
|
|
||||||
if (stringsFile.length) {
|
if (stringsFile.length) {
|
||||||
super.addBuffer(`${lang}.lproj/pass.strings`, stringsFile);
|
super.addBuffer(`${lang}.lproj/pass.strings`, stringsFile);
|
||||||
@@ -946,68 +946,3 @@ function readPassMetadata(buffer: Buffer) {
|
|||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseStringsFile(buffer: Buffer) {
|
|
||||||
const fileAsString = buffer.toString("utf8");
|
|
||||||
const translationRowRegex = /"(?<key>.+)"\s+=\s+"(?<value>.+)";\n?/;
|
|
||||||
const commentRowRegex = /\/\*\s*(.+)\s*\*\//;
|
|
||||||
|
|
||||||
let translations: [placeholder: string, value: string][] = [];
|
|
||||||
let comments: string[] = [];
|
|
||||||
|
|
||||||
let blockStartPoint = 0;
|
|
||||||
let blockEndPoint = 0;
|
|
||||||
|
|
||||||
do {
|
|
||||||
if (
|
|
||||||
/** New Line, new life */
|
|
||||||
/\n/.test(fileAsString[blockEndPoint]) ||
|
|
||||||
/** EOF */
|
|
||||||
blockEndPoint === fileAsString.length
|
|
||||||
) {
|
|
||||||
let match: RegExpMatchArray;
|
|
||||||
|
|
||||||
const section = fileAsString.substring(
|
|
||||||
blockStartPoint,
|
|
||||||
blockEndPoint + 1,
|
|
||||||
);
|
|
||||||
|
|
||||||
if ((match = section.match(translationRowRegex))) {
|
|
||||||
const {
|
|
||||||
groups: { key, value },
|
|
||||||
} = match;
|
|
||||||
|
|
||||||
translations.push([key, value]);
|
|
||||||
} else if ((match = section.match(commentRowRegex))) {
|
|
||||||
const [, content] = match;
|
|
||||||
|
|
||||||
comments.push(content.trimEnd());
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Skipping \n and going to the next block. */
|
|
||||||
blockEndPoint += 2;
|
|
||||||
blockStartPoint = blockEndPoint - 1;
|
|
||||||
} else {
|
|
||||||
blockEndPoint += 1;
|
|
||||||
}
|
|
||||||
} while (blockEndPoint <= fileAsString.length);
|
|
||||||
|
|
||||||
return {
|
|
||||||
translations,
|
|
||||||
comments,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function createStringFile(translations: { [key: string]: string }): Buffer {
|
|
||||||
const stringContents = [];
|
|
||||||
|
|
||||||
const translationsEntries = Object.entries(translations);
|
|
||||||
|
|
||||||
for (let i = 0; i < translationsEntries.length; i++) {
|
|
||||||
const [key, value] = translationsEntries[i];
|
|
||||||
|
|
||||||
stringContents.push(`"${key}" = "${value}";`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Buffer.from(stringContents.join(EOL));
|
|
||||||
}
|
|
||||||
|
|||||||
85
src/StringsUtils.ts
Normal file
85
src/StringsUtils.ts
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
import { EOL } from "os";
|
||||||
|
|
||||||
|
// ************************************ //
|
||||||
|
// *** UTILS FOR PASS.STRINGS FILES *** //
|
||||||
|
// ************************************ //
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a string file to convert it to
|
||||||
|
* an object
|
||||||
|
*
|
||||||
|
* @param buffer
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function parse(buffer: Buffer) {
|
||||||
|
const fileAsString = buffer.toString("utf8");
|
||||||
|
const translationRowRegex = /"(?<key>.+)"\s+=\s+"(?<value>.+)";\n?/;
|
||||||
|
const commentRowRegex = /\/\*\s*(.+)\s*\*\//;
|
||||||
|
|
||||||
|
let translations: [placeholder: string, value: string][] = [];
|
||||||
|
let comments: string[] = [];
|
||||||
|
|
||||||
|
let blockStartPoint = 0;
|
||||||
|
let blockEndPoint = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (
|
||||||
|
/** New Line, new life */
|
||||||
|
/\n/.test(fileAsString[blockEndPoint]) ||
|
||||||
|
/** EOF */
|
||||||
|
blockEndPoint === fileAsString.length
|
||||||
|
) {
|
||||||
|
let match: RegExpMatchArray;
|
||||||
|
|
||||||
|
const section = fileAsString.substring(
|
||||||
|
blockStartPoint,
|
||||||
|
blockEndPoint + 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
if ((match = section.match(translationRowRegex))) {
|
||||||
|
const {
|
||||||
|
groups: { key, value },
|
||||||
|
} = match;
|
||||||
|
|
||||||
|
translations.push([key, value]);
|
||||||
|
} else if ((match = section.match(commentRowRegex))) {
|
||||||
|
const [, content] = match;
|
||||||
|
|
||||||
|
comments.push(content.trimEnd());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Skipping \n and going to the next block. */
|
||||||
|
blockEndPoint += 2;
|
||||||
|
blockStartPoint = blockEndPoint - 1;
|
||||||
|
} else {
|
||||||
|
blockEndPoint += 1;
|
||||||
|
}
|
||||||
|
} while (blockEndPoint <= fileAsString.length);
|
||||||
|
|
||||||
|
return {
|
||||||
|
translations,
|
||||||
|
comments,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a strings file buffer
|
||||||
|
*
|
||||||
|
* @param translations
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function create(translations: { [key: string]: string }): Buffer {
|
||||||
|
const stringContents = [];
|
||||||
|
|
||||||
|
const translationsEntries = Object.entries(translations);
|
||||||
|
|
||||||
|
for (let i = 0; i < translationsEntries.length; i++) {
|
||||||
|
const [key, value] = translationsEntries[i];
|
||||||
|
|
||||||
|
stringContents.push(`"${key}" = "${value}";`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Buffer.from(stringContents.join(EOL));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user