UNPKG

@strapi/strapi

Version:

An open source headless CMS solution to create and manage your own API. It provides a powerful dashboard and features to make your life easier. Databases supported: MySQL, MariaDB, PostgreSQL, SQLite

146 lines (142 loc) 5.76 kB
'use strict'; var path = require('path'); var fp = require('lodash/fp'); var fse = require('fs-extra'); var chalk = require('chalk'); var dataTransfer = require('@strapi/data-transfer'); var dataTransfer$1 = require('../../utils/data-transfer.js'); var helpers = require('../../utils/helpers.js'); var validateDirFormat = require('./validate-dir-format.js'); const { providers: { createLocalFileDestinationProvider } } = dataTransfer.file; const { providers: { createLocalStrapiSourceProvider } } = dataTransfer.strapi; const BYTES_IN_MB = 1024 * 1024; /** * Export command. * * It transfers data from a local Strapi instance to a file * * @param {ExportCommandOptions} opts */ var action = (async (opts)=>{ // Validate inputs from Commander if (!fp.isObject(opts)) { helpers.exitWith(1, 'Could not parse command arguments'); } validateDirFormat.normalizeExportDirFormatOpts(opts); const strapi = await dataTransfer$1.createStrapiInstance(); const source = createSourceProvider(strapi); const destination = createDestinationProvider(opts); const engine = dataTransfer.engine.createTransferEngine(source, destination, { versionStrategy: 'ignore', schemaStrategy: 'ignore', exclude: opts.exclude, only: opts.only, throttle: opts.throttle, transforms: { links: [ { filter (link) { return !dataTransfer$1.isIgnoredContentType(link.left.type) && !dataTransfer$1.isIgnoredContentType(link.right.type); } } ], entities: [ { filter (entity) { return !dataTransfer$1.isIgnoredContentType(entity.type); } } ] } }); engine.diagnostics.onDiagnostic(dataTransfer$1.formatDiagnostic('export', opts.verbose)); const progress = engine.progress.stream; const { updateLoader } = dataTransfer$1.loadersFactory(); progress.on(`stage::start`, ({ stage, data })=>{ updateLoader(stage, data).start(); }); progress.on('stage::finish', ({ stage, data })=>{ updateLoader(stage, data).succeed(); }); progress.on('stage::progress', ({ stage, data })=>{ updateLoader(stage, data); }); progress.on('transfer::start', async ()=>{ console.log(`Starting export...`); await strapi.telemetry.send('didDEITSProcessStart', dataTransfer$1.getTransferTelemetryPayload(engine)); }); let results; let outFile; try { // Abort transfer if user interrupts process dataTransfer$1.setSignalHandler(()=>dataTransfer$1.abortTransfer({ engine, strapi })); results = await engine.transfer(); outFile = results.destination?.file?.path ?? ''; if ((opts.format ?? 'tar') === 'dir') { const metadataPath = path.join(outFile, 'metadata.json'); if (!await fse.pathExists(metadataPath)) { throw new dataTransfer.engine.errors.TransferEngineTransferError(`Export directory was not created correctly "${outFile}"`); } } else if (!await fse.pathExists(outFile)) { throw new dataTransfer.engine.errors.TransferEngineTransferError(`Export file not created "${outFile}"`); } // Note: we need to await telemetry or else the process ends before it is sent await strapi.telemetry.send('didDEITSProcessFinish', dataTransfer$1.getTransferTelemetryPayload(engine)); try { const table = dataTransfer$1.buildTransferTable(results.engine); console.log(table?.toString()); } catch (e) { console.error('There was an error displaying the results of the transfer.'); } console.log(`Export archive is in ${chalk.green(outFile)}`); helpers.exitWith(0, dataTransfer$1.exitMessageText('export')); } catch { await strapi.telemetry.send('didDEITSProcessFail', dataTransfer$1.getTransferTelemetryPayload(engine)); helpers.exitWith(1, dataTransfer$1.exitMessageText('export', true)); } }); /** * It creates a local strapi destination provider */ const createSourceProvider = (strapi)=>{ return createLocalStrapiSourceProvider({ async getStrapi () { return strapi; } }); }; /** * It creates a local file or directory destination provider based on the given options */ const createDestinationProvider = (opts)=>{ const { file, compress, encrypt, key, maxSizeJsonl, format = 'tar' } = opts; const filepath = fp.isString(file) && file.length > 0 ? file : dataTransfer$1.getDefaultExportName(); const maxSizeJsonlInMb = fp.isFinite(fp.toNumber(maxSizeJsonl)) ? fp.toNumber(maxSizeJsonl) * BYTES_IN_MB : undefined; if (format === 'dir') { const { createLocalDirectoryDestinationProvider } = dataTransfer.directory.providers; const dirPath = path.isAbsolute(filepath) ? filepath : path.resolve(process.cwd(), filepath); return createLocalDirectoryDestinationProvider({ directory: { path: dirPath }, file: { maxSizeJsonl: maxSizeJsonlInMb } }); } return createLocalFileDestinationProvider({ file: { path: filepath, maxSizeJsonl: maxSizeJsonlInMb }, encryption: { enabled: encrypt ?? false, key: encrypt ? key : undefined }, compression: { enabled: compress ?? false } }); }; module.exports = action; //# sourceMappingURL=action.js.map