mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 16:25:21 +00:00
Edited getModelFolderContents to return just one object of buffers (to be tested yet)
This commit is contained in:
166
src/parser.ts
166
src/parser.ts
@@ -9,11 +9,10 @@ import {
|
|||||||
hasFilesWithName,
|
hasFilesWithName,
|
||||||
deletePersonalization,
|
deletePersonalization,
|
||||||
} from "./utils";
|
} from "./utils";
|
||||||
import fs from "fs";
|
import { promises as fs } from "fs";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
|
|
||||||
const prsDebug = debug("Personalization");
|
const prsDebug = debug("Personalization");
|
||||||
const { readdir: readDir, readFile } = fs.promises;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs checks on the passed model to
|
* Performs checks on the passed model to
|
||||||
@@ -93,26 +92,28 @@ export async function getModelContents(model: Schemas.FactoryOptions["model"]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads and model contents and creates a splitted
|
* Reads the model folder contents
|
||||||
* bundles-object.
|
*
|
||||||
* @param model
|
* @param model
|
||||||
|
* @returns A promise of an object containing all
|
||||||
|
* filePaths and the relative buffer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export async function getModelFolderContents(
|
export async function getModelFolderContents(
|
||||||
model: string,
|
model: string,
|
||||||
): Promise<Schemas.PartitionedBundle> {
|
): Promise<{ [filePath: string]: Buffer }> {
|
||||||
try {
|
try {
|
||||||
const modelPath = `${model}${(!path.extname(model) && ".pass") || ""}`;
|
const modelPath = `${model}${(!path.extname(model) && ".pass") || ""}`;
|
||||||
const modelFilesList = await readDir(modelPath);
|
const modelFilesList = await fs.readdir(modelPath);
|
||||||
|
|
||||||
// No dot-starting files, manifest and signature
|
// No dot-starting files, manifest and signature
|
||||||
const filteredFiles = removeHidden(modelFilesList).filter(
|
const filteredModelRecords = removeHidden(modelFilesList).filter(
|
||||||
(f) =>
|
(f) =>
|
||||||
!/(manifest|signature)/i.test(f) &&
|
!/(manifest|signature)/i.test(f) &&
|
||||||
/.+$/.test(path.parse(f).ext),
|
/.+$/.test(path.parse(f).ext),
|
||||||
);
|
);
|
||||||
|
|
||||||
const isModelInitialized =
|
/* const isModelInitialized =
|
||||||
filteredFiles.length &&
|
filteredFiles.length &&
|
||||||
hasFilesWithName("icon", filteredFiles, "startsWith");
|
hasFilesWithName("icon", filteredFiles, "startsWith");
|
||||||
|
|
||||||
@@ -124,85 +125,32 @@ export async function getModelFolderContents(
|
|||||||
path.parse(model).name,
|
path.parse(model).name,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
} */
|
||||||
|
|
||||||
// Splitting files from localization folders
|
const modelRecords = (
|
||||||
const rawBundleFiles = filteredFiles.filter(
|
|
||||||
(entry) => !entry.includes(".lproj"),
|
|
||||||
);
|
|
||||||
const l10nFolders = filteredFiles.filter((entry) =>
|
|
||||||
entry.includes(".lproj"),
|
|
||||||
);
|
|
||||||
|
|
||||||
const rawBundleBuffers = await Promise.all(
|
|
||||||
rawBundleFiles.map((file) =>
|
|
||||||
readFile(path.resolve(modelPath, file)),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
const bundle: Schemas.BundleUnit = Object.assign(
|
|
||||||
{},
|
|
||||||
...rawBundleFiles.map((fileName, index) => ({
|
|
||||||
[fileName]: rawBundleBuffers[index],
|
|
||||||
})),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Reading concurrently localizations folder
|
|
||||||
// and their files and their buffers
|
|
||||||
const L10N_FilesListByFolder: Array<Schemas.BundleUnit> =
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
l10nFolders.map(async (folderPath) => {
|
/**
|
||||||
// Reading current folder
|
* Obtaining flattened array of buffer records
|
||||||
const currentLangPath = path.join(modelPath, folderPath);
|
* containing file name and the buffer itself.
|
||||||
|
*
|
||||||
|
* This goes also to read every nested l10n
|
||||||
|
* subfolder.
|
||||||
|
*/
|
||||||
|
|
||||||
const files = await readDir(currentLangPath);
|
filteredModelRecords.map((fileOrDirectoryPath) => {
|
||||||
// Transforming files path to a model-relative path
|
const fullPath = path.resolve(
|
||||||
const validFiles = removeHidden(files).map((file) =>
|
modelPath,
|
||||||
path.join(currentLangPath, file),
|
fileOrDirectoryPath,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Getting all the buffers from file paths
|
return readFileOrDirectory(fullPath);
|
||||||
const buffers = await Promise.all(
|
|
||||||
validFiles.map((file) =>
|
|
||||||
readFile(file).catch(() => Buffer.alloc(0)),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Assigning each file path to its buffer
|
|
||||||
// and discarding the empty ones
|
|
||||||
|
|
||||||
return validFiles.reduce<Schemas.BundleUnit>(
|
|
||||||
(acc, file, index) => {
|
|
||||||
if (!buffers[index].length) {
|
|
||||||
return acc;
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileComponents = file.split(path.sep);
|
|
||||||
const fileName =
|
|
||||||
fileComponents[fileComponents.length - 1];
|
|
||||||
|
|
||||||
return {
|
|
||||||
...acc,
|
|
||||||
[fileName]: buffers[index],
|
|
||||||
};
|
|
||||||
},
|
|
||||||
{},
|
|
||||||
);
|
|
||||||
}),
|
}),
|
||||||
);
|
)
|
||||||
|
)
|
||||||
|
.flat(1)
|
||||||
|
.reduce((acc, current) => ({ ...acc, ...current }), {});
|
||||||
|
|
||||||
const l10nBundle: Schemas.PartitionedBundle["l10nBundle"] =
|
return modelRecords;
|
||||||
Object.assign(
|
|
||||||
{},
|
|
||||||
...L10N_FilesListByFolder.map((folder, index) => ({
|
|
||||||
[l10nFolders[index]]: folder,
|
|
||||||
})),
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
bundle,
|
|
||||||
l10nBundle,
|
|
||||||
};
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err?.code === "ENOENT") {
|
if (err?.code === "ENOENT") {
|
||||||
if (err.syscall === "open") {
|
if (err.syscall === "open") {
|
||||||
@@ -226,6 +174,62 @@ export async function getModelFolderContents(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads sequentially
|
||||||
|
* @param filePath
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
|
||||||
|
async function readFileOrDirectory(filePath: string) {
|
||||||
|
if ((await fs.lstat(filePath)).isDirectory()) {
|
||||||
|
return Promise.all(await readDirectory(filePath));
|
||||||
|
} else {
|
||||||
|
return fs
|
||||||
|
.readFile(filePath)
|
||||||
|
.then((content) => getObjectFromModelFile(filePath, content, 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an object containing the parsed fileName
|
||||||
|
* from a path along with its content.
|
||||||
|
*
|
||||||
|
* @param filePath
|
||||||
|
* @param content
|
||||||
|
* @param depthFromEnd - used to preserve localization lproj content
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
|
||||||
|
function getObjectFromModelFile(
|
||||||
|
filePath: string,
|
||||||
|
content: Buffer,
|
||||||
|
depthFromEnd: number,
|
||||||
|
) {
|
||||||
|
const fileComponents = filePath.split(path.sep);
|
||||||
|
const fileName = fileComponents
|
||||||
|
.slice(fileComponents.length - depthFromEnd)
|
||||||
|
.join(path.sep);
|
||||||
|
|
||||||
|
return { [fileName]: content };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads a directory and returns all the files in it
|
||||||
|
* as an Array<Promise>
|
||||||
|
*
|
||||||
|
* @param filePath
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
|
||||||
|
async function readDirectory(filePath: string) {
|
||||||
|
const dirContent = await fs.readdir(filePath).then(removeHidden);
|
||||||
|
|
||||||
|
return dirContent.map(async (fileName) => {
|
||||||
|
const content = await fs.readFile(path.resolve(filePath, fileName));
|
||||||
|
return getObjectFromModelFile(filePath, content, 1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Analyzes the passed buffer model and splits it to
|
* Analyzes the passed buffer model and splits it to
|
||||||
* return buffers and localization files buffers.
|
* return buffers and localization files buffers.
|
||||||
@@ -310,7 +314,7 @@ export async function readCertificatesFromOptions(
|
|||||||
|
|
||||||
if (!!path.parse(content).ext) {
|
if (!!path.parse(content).ext) {
|
||||||
// The content is a path to the document
|
// The content is a path to the document
|
||||||
return readFile(path.resolve(content), { encoding: "utf8" });
|
return fs.readFile(path.resolve(content), { encoding: "utf8" });
|
||||||
} else {
|
} else {
|
||||||
// Content is the real document content
|
// Content is the real document content
|
||||||
return Promise.resolve(content);
|
return Promise.resolve(content);
|
||||||
|
|||||||
Reference in New Issue
Block a user