UNPKG

@itxch/contentful-import

Version:

This tool allows you to import JSON dump exported by contentful-export

80 lines (79 loc) 2.31 kB
import fs from "fs"; import { join } from "path"; import { promisify } from "util"; import { getEntityName } from "contentful-batch-libs/dist/get-entity-name.js"; import { logEmitter } from "contentful-batch-libs/dist/logging.js"; import { ContentfulAssetError, ContentfulEntityError } from "../../utils/errors.js"; const stat = promisify(fs.stat); async function getAssetStreamForURL(url, assetsDirectory) { const [, assetPath] = url.split("//"); const filePath = join(assetsDirectory, assetPath); try { await stat(filePath); return fs.createReadStream(filePath); } catch (err) { const error = new ContentfulAssetError( "Cannot open asset from filesystem", filePath ); throw error; } } async function processAssetForLocale(locale, asset, processingOptions) { try { return await asset.processForLocale(locale, processingOptions); } catch (err) { if (err instanceof ContentfulEntityError) { err.entity = asset; } logEmitter.emit("error", err); throw err; } } async function lastResult(promises) { if (!promises.length) throw new RangeError("No last result from no promises"); const results = []; await Promise.all( promises.map( (p) => p.then((v) => { results.push(v); }) ) ); return results[results.length - 1]; } async function processAssets({ assets, timeout, retryLimit, requestQueue }) { const processingOptions = Object.assign( {}, timeout && { processingCheckWait: timeout }, retryLimit && { processingCheckRetry: retryLimit } ); const pendingProcessingAssets = assets.map(async (asset) => { logEmitter.emit("info", `Processing Asset ${getEntityName(asset)}`); const locales = Object.keys(asset.fields.file || {}); let latestAssetVersion = asset; try { latestAssetVersion = await lastResult( locales.map((locale) => { return requestQueue.add( () => processAssetForLocale(locale, asset, processingOptions) ); }) ); } catch (err) { return null; } return latestAssetVersion; }); const potentiallyProcessedAssets = await Promise.all(pendingProcessingAssets); return potentiallyProcessedAssets.filter((asset) => asset); } export { getAssetStreamForURL, processAssets };