UNPKG

@fugitivesclub/nft-cli

Version:
47 lines (46 loc) 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.readNFTs = void 0; const tslib_1 = require("tslib"); const core_1 = require("@oclif/core"); const fs = (0, tslib_1.__importStar)(require("fs")); const path_1 = (0, tslib_1.__importDefault)(require("path")); const constants_1 = require("../../utils/constants"); const not_empty_1 = require("../../utils/not-empty"); const read_nft_1 = require("./read-nft"); const readNFTs = async (directoryPath) => { const filePaths = await fs.promises.readdir(directoryPath); // Extract file by pair json and image // eslint-disable-next-line unicorn/no-array-reduce const pairedFiles = filePaths.reduce((acc, filePath) => { const filename = path_1.default.basename(filePath); const extension = path_1.default.extname(filePath).toLocaleLowerCase(); const basename = filename.replace(extension, ''); if (constants_1.allowedExtensions.has(extension)) { if (!acc[basename]) { acc[basename] = { metadataPath: null, imagePath: null }; } if (extension === '.json') { acc[basename].metadataPath = filePath; } else { acc[basename].imagePath = filePath; } } return acc; }, {}); // Create promises to read all files const promises = Promise.all(Object.values(pairedFiles).map(async (file) => { var _a; if (!file.imagePath || !file.metadataPath) { core_1.Errors.warn(`Corresponding ${file.imagePath === null ? '`image`' : '`json`'} file is missing for ${(_a = file.imagePath) !== null && _a !== void 0 ? _a : file.metadataPath}, this one will be ignored.`); return null; } return (0, read_nft_1.readNFT)(`${directoryPath}/${file.metadataPath}`, `${directoryPath}/${file.imagePath}`); })); // Read content of all files const result = await promises; // Filter null paired return result.filter(not_empty_1.notEmpty); }; exports.readNFTs = readNFTs;