@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
74 lines (70 loc) • 3.5 kB
JavaScript
;
var fs = require('fs');
var path = require('path');
var commander = require('commander');
var dataTransfer = require('../../utils/data-transfer.js');
var commander$1 = require('../../utils/commander.js');
var helpers = require('../../utils/helpers.js');
var getInquirer = require('../../utils/get-inquirer.js');
var action = require('./action.js');
/**
* `$ strapi import`
*/ const command = ()=>{
return commander.createCommand('import').description('Import data from file to Strapi').allowExcessArguments(false).requiredOption('-f, --file <file>', 'path to a Strapi export (.tar[.gz][.enc]) or to an unpacked export directory').addOption(new commander.Option('-k, --key <string>', 'Provide encryption key in command instead of using the prompt')).addOption(new commander.Option('--verbose', 'Enable verbose logs')).addOption(commander$1.forceOption).addOption(dataTransfer.excludeOption).addOption(dataTransfer.onlyOption).addOption(dataTransfer.throttleOption).hook('preAction', dataTransfer.validateExcludeOnly).hook('preAction', async (thisCommand)=>{
const opts = thisCommand.opts();
const ext = path.extname(String(opts.file));
// check extension to guess if we should prompt for key
if (ext === '.enc') {
if (!opts.key) {
const inquirer = await getInquirer.getInquirer();
const answers = await inquirer.prompt([
{
type: 'password',
message: 'Please enter your decryption key',
name: 'key'
}
]);
if (!answers.key?.length) {
helpers.exitWith(1, 'No key entered, aborting import.');
}
opts.key = answers.key;
}
}
})// set decrypt and decompress options based on filename (archive only)
.hook('preAction', (thisCommand)=>{
const opts = thisCommand.opts();
const filePath = String(opts.file);
let isDirectory = false;
try {
isDirectory = fs.statSync(filePath).isDirectory();
} catch {
// missing path or unreadable — let the transfer fail later with a clear error
}
if (isDirectory) {
thisCommand.opts().decrypt = false;
thisCommand.opts().decompress = false;
return;
}
const { extname, parse } = path;
let file = filePath;
if (extname(file) === '.enc') {
file = parse(file).name; // trim the .enc extension
thisCommand.opts().decrypt = true;
} else {
thisCommand.opts().decrypt = false;
}
if (extname(file) === '.gz') {
file = parse(file).name; // trim the .gz extension
thisCommand.opts().decompress = true;
} else {
thisCommand.opts().decompress = false;
}
if (extname(file) !== '.tar') {
helpers.exitWith(1, `The file '${opts.file}' does not appear to be a valid Strapi data file. Use a path ending in .tar[.gz][.enc], or an existing directory that contains an unpacked export (e.g. metadata.json).`);
}
}).hook('preAction', commander$1.getCommanderConfirmMessage('The import will delete your existing data! Are you sure you want to proceed?', {
failMessage: 'Import process aborted'
})).action(action);
};
module.exports = command;
//# sourceMappingURL=command.js.map