mirror of
https://github.com/strapi/strapi.git
synced 2025-11-08 06:07:41 +00:00
clean up transfer command
This commit is contained in:
parent
e03eaa3808
commit
89b165bfc6
@ -17,8 +17,7 @@ const {
|
|||||||
DEFAULT_IGNORED_CONTENT_TYPES,
|
DEFAULT_IGNORED_CONTENT_TYPES,
|
||||||
formatDiagnostic,
|
formatDiagnostic,
|
||||||
} = require('./utils');
|
} = require('./utils');
|
||||||
|
const { exitWith } = require('../utils/helpers');
|
||||||
const logger = console;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef TransferCommandOptions Options given to the CLI transfer command
|
* @typedef TransferCommandOptions Options given to the CLI transfer command
|
||||||
@ -39,8 +38,7 @@ const logger = console;
|
|||||||
module.exports = async (opts) => {
|
module.exports = async (opts) => {
|
||||||
// Validate inputs from Commander
|
// Validate inputs from Commander
|
||||||
if (!isObject(opts)) {
|
if (!isObject(opts)) {
|
||||||
logger.error('Could not parse command arguments');
|
exitWith(1, 'Could not parse command arguments');
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const strapi = await createStrapiInstance();
|
const strapi = await createStrapiInstance();
|
||||||
@ -49,8 +47,7 @@ module.exports = async (opts) => {
|
|||||||
let destination;
|
let destination;
|
||||||
|
|
||||||
if (!opts.from && !opts.to) {
|
if (!opts.from && !opts.to) {
|
||||||
logger.error('At least one source (from) or destination (to) option must be provided');
|
exitWith(1, 'At least one source (from) or destination (to) option must be provided');
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if no URL provided, use local Strapi
|
// if no URL provided, use local Strapi
|
||||||
@ -61,8 +58,7 @@ module.exports = async (opts) => {
|
|||||||
}
|
}
|
||||||
// if URL provided, set up a remote source provider
|
// if URL provided, set up a remote source provider
|
||||||
else {
|
else {
|
||||||
logger.error(`Remote Strapi source provider not yet implemented`);
|
exitWith(1, `Remote Strapi source provider not yet implemented`);
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if no URL provided, use local Strapi
|
// if no URL provided, use local Strapi
|
||||||
@ -73,6 +69,10 @@ module.exports = async (opts) => {
|
|||||||
}
|
}
|
||||||
// if URL provided, set up a remote destination provider
|
// if URL provided, set up a remote destination provider
|
||||||
else {
|
else {
|
||||||
|
if (!opts.toToken) {
|
||||||
|
exitWith(1, 'Missing token for remote destination');
|
||||||
|
}
|
||||||
|
|
||||||
destination = createRemoteStrapiDestinationProvider({
|
destination = createRemoteStrapiDestinationProvider({
|
||||||
url: opts.to,
|
url: opts.to,
|
||||||
auth: {
|
auth: {
|
||||||
@ -87,8 +87,7 @@ module.exports = async (opts) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!source || !destination) {
|
if (!source || !destination) {
|
||||||
logger.error('Could not create providers');
|
exitWith(1, 'Could not create providers');
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const engine = createTransferEngine(source, destination, {
|
const engine = createTransferEngine(source, destination, {
|
||||||
@ -117,18 +116,15 @@ module.exports = async (opts) => {
|
|||||||
|
|
||||||
engine.diagnostics.onDiagnostic(formatDiagnostic('transfer'));
|
engine.diagnostics.onDiagnostic(formatDiagnostic('transfer'));
|
||||||
|
|
||||||
|
let results;
|
||||||
try {
|
try {
|
||||||
logger.log(`Starting transfer...`);
|
console.log(`Starting transfer...`);
|
||||||
|
results = await engine.transfer();
|
||||||
const results = await engine.transfer();
|
|
||||||
|
|
||||||
const table = buildTransferTable(results.engine);
|
|
||||||
logger.log(table.toString());
|
|
||||||
|
|
||||||
logger.log(`${chalk.bold('Transfer process has been completed successfully!')}`);
|
|
||||||
process.exit(0);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('Transfer process failed.');
|
exitWith(1, 'Transfer process failed.');
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const table = buildTransferTable(results.engine);
|
||||||
|
console.log(table.toString());
|
||||||
|
exitWith(0, `${chalk.bold('Transfer process has been completed successfully!')}`);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -36,22 +36,29 @@ const readableBytes = (bytes, decimals = 1, padStart = 0) => {
|
|||||||
*
|
*
|
||||||
* @param {number} code Code to exit process with
|
* @param {number} code Code to exit process with
|
||||||
* @param {string | Array} message Message(s) to display before exiting
|
* @param {string | Array} message Message(s) to display before exiting
|
||||||
|
* @param {Object} options
|
||||||
|
* @param {console} options.logger - logger object, defaults to console
|
||||||
|
* @param {process} options.prc - process object, defaults to process
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
const exitWith = (code, message = undefined) => {
|
const exitWith = (code, message = undefined, options = {}) => {
|
||||||
const logger = (message) => {
|
const { logger = console, prc = process } = options;
|
||||||
|
|
||||||
|
const log = (message) => {
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
console.log(chalk.green(message));
|
logger.log(chalk.green(message));
|
||||||
} else {
|
} else {
|
||||||
console.error(chalk.red(message));
|
logger.error(chalk.red(message));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isString(message)) {
|
if (isString(message)) {
|
||||||
logger(message);
|
log(message);
|
||||||
} else if (isArray(message)) {
|
} else if (isArray(message)) {
|
||||||
message.forEach((msg) => logger(msg));
|
message.forEach((msg) => log(msg));
|
||||||
}
|
}
|
||||||
process.exit(code);
|
|
||||||
|
prc.exit(code);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user