feat: Partial import in CLI
parent
7af07bfcf0
commit
962952f662
|
@ -65,22 +65,46 @@ const getConfigState = (diff, configName, syncType) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleAction = async (type, skipConfirm) => {
|
const handleAction = async (syncType, skipConfirm, configType, partials) => {
|
||||||
const app = await strapi().load();
|
const app = await strapi().load();
|
||||||
const diff = await app.plugin('config-sync').service('main').getFormattedDiff();
|
const diff = await app.plugin('config-sync').service('main').getFormattedDiff();
|
||||||
|
|
||||||
// No changes.
|
// No changes.
|
||||||
if (isEmpty(diff.diff)) {
|
if (isEmpty(diff.diff)) {
|
||||||
console.log(`${chalk.bgCyan.bold('[notice]')} There are no changes to ${type}.`);
|
console.log(`${chalk.bgCyan.bold('[notice]')} There are no changes to ${syncType}.`);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init table.
|
// Init table.
|
||||||
const table = initTable('Action');
|
const table = initTable('Action');
|
||||||
|
const configNames = partials && partials.split(',');
|
||||||
|
const partialDiff = {};
|
||||||
|
|
||||||
|
// Fill partialDiff with arguments.
|
||||||
|
if (configNames) {
|
||||||
|
configNames.map((name) => {
|
||||||
|
if (diff.diff[name]) partialDiff[name] = diff.diff[name];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (configType) {
|
||||||
|
Object.keys(diff.diff).map((name) => {
|
||||||
|
if (configType === name.split('.')[0]) {
|
||||||
|
partialDiff[name] = diff.diff[name];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// No changes for partial diff.
|
||||||
|
if ((partials || configType) && isEmpty(partialDiff)) {
|
||||||
|
console.log(`${chalk.bgCyan.bold('[notice]')} There are no changes for the specified config.`);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const finalDiff = (partials || configType) && partialDiff ? partialDiff : diff.diff;
|
||||||
|
|
||||||
// Add diff to table.
|
// Add diff to table.
|
||||||
Object.keys(diff.diff).map((configName) => {
|
Object.keys(finalDiff).map((configName) => {
|
||||||
table.push([configName, getConfigState(diff, configName, type)]);
|
table.push([configName, getConfigState(diff, configName, syncType)]);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Print table.
|
// Print table.
|
||||||
|
@ -92,23 +116,25 @@ const handleAction = async (type, skipConfirm) => {
|
||||||
answer = await inquirer.prompt([{
|
answer = await inquirer.prompt([{
|
||||||
type: 'confirm',
|
type: 'confirm',
|
||||||
name: 'confirm',
|
name: 'confirm',
|
||||||
message: `Are you sure you want to ${type} the config changes?`,
|
message: `Are you sure you want to ${syncType} the config changes?`,
|
||||||
}]);
|
}]);
|
||||||
console.log('');
|
console.log('');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Preform the action.
|
// Preform the action.
|
||||||
if (skipConfirm || answer.confirm) {
|
if (skipConfirm || answer.confirm) {
|
||||||
if (type === 'import') {
|
if (syncType === 'import') {
|
||||||
const onSuccess = (name) => console.log(`${chalk.bgGreen.bold('[success]')} Imported ${name}`);
|
const onSuccess = (name) => console.log(`${chalk.bgGreen.bold('[success]')} Imported ${name}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await app.plugin('config-sync').service('main').importAllConfig(null, onSuccess);
|
await Promise.all(Object.keys(finalDiff).map(async (name) => {
|
||||||
|
await app.plugin('config-sync').service('main').importSingleConfig(name, onSuccess);
|
||||||
|
}));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(`${chalk.bgRed.bold('[error]')} Something went wrong during the import. ${e}`);
|
console.log(`${chalk.bgRed.bold('[error]')} Something went wrong during the import. ${e}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (type === 'export') {
|
if (syncType === 'export') {
|
||||||
try {
|
try {
|
||||||
await app.plugin('config-sync').service('main').exportAllConfig();
|
await app.plugin('config-sync').service('main').exportAllConfig();
|
||||||
console.log(`${chalk.bgGreen.bold('[success]')} Config was exported`);
|
console.log(`${chalk.bgGreen.bold('[success]')} Config was exported`);
|
||||||
|
@ -141,22 +167,24 @@ program
|
||||||
program
|
program
|
||||||
.command('import')
|
.command('import')
|
||||||
.alias('i')
|
.alias('i')
|
||||||
// .option('-t, --type <type>', 'The type of config') // TODO: partial import
|
.option('-t, --type <type>', 'The type of config') // TODO: partial import
|
||||||
|
.option('-p, --partial <partials>', 'A comma separated string of configs') // TODO: partial import
|
||||||
.option('-y', 'Skip the confirm prompt')
|
.option('-y', 'Skip the confirm prompt')
|
||||||
.description('Import the config')
|
.description('Import the config')
|
||||||
.action(async ({ y, type }) => {
|
.action(async ({ y, type, partial }) => {
|
||||||
return handleAction('import', y, type);
|
return handleAction('import', y, type, partial);
|
||||||
});
|
});
|
||||||
|
|
||||||
// `$ config-sync export`
|
// `$ config-sync export`
|
||||||
program
|
program
|
||||||
.command('export')
|
.command('export')
|
||||||
.alias('e')
|
.alias('e')
|
||||||
// .option('-t, --type <type>', 'The type of config') // TODO: partial export
|
.option('-t, --type <type>', 'The type of config') // TODO: partial export
|
||||||
|
.option('-p, --partial <partials>', 'A comma separated string of configs') // TODO: partial import
|
||||||
.option('-y', 'Skip the confirm prompt')
|
.option('-y', 'Skip the confirm prompt')
|
||||||
.description('Export the config')
|
.description('Export the config')
|
||||||
.action(async ({ y, type }) => {
|
.action(async ({ y, type, partial }) => {
|
||||||
return handleAction('export', y, type);
|
return handleAction('export', y, type, partial);
|
||||||
});
|
});
|
||||||
|
|
||||||
// `$ config-sync diff`
|
// `$ config-sync diff`
|
||||||
|
|
|
@ -161,14 +161,7 @@ module.exports = () => ({
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
await strapi.plugin('config-sync').service('main').importSingleConfig(`${type}.${name}`, onSuccess);
|
||||||
await strapi.plugin('config-sync').service('main').importSingleConfig(type, name);
|
|
||||||
if (onSuccess) {
|
|
||||||
onSuccess(`${type}.${name}`);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error(e);
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -191,28 +184,35 @@ module.exports = () => ({
|
||||||
/**
|
/**
|
||||||
* Import a single config file into the db.
|
* Import a single config file into the db.
|
||||||
*
|
*
|
||||||
* @param {string} configType - The type of config.
|
|
||||||
* @param {string} configName - The name of the config file.
|
* @param {string} configName - The name of the config file.
|
||||||
|
* @param {object} onSuccess - Success callback to run on each single successfull import.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
importSingleConfig: async (configType, configName) => {
|
importSingleConfig: async (configName, onSuccess) => {
|
||||||
// Check if the config should be excluded.
|
// Check if the config should be excluded.
|
||||||
const shouldExclude = strapi.config.get('plugin.config-sync.exclude').includes(`${configType}.${configName}`);
|
const shouldExclude = strapi.config.get('plugin.config-sync.exclude').includes(configName);
|
||||||
if (shouldExclude) return;
|
if (shouldExclude) return;
|
||||||
|
|
||||||
const fileContents = await strapi.plugin('config-sync').service('main').readConfigFile(configType, configName);
|
const [type, name] = configName.split('.'); // Split the configName.
|
||||||
|
const fileContents = await strapi.plugin('config-sync').service('main').readConfigFile(type, name);
|
||||||
|
|
||||||
await types[configType].importSingle(configName, fileContents);
|
try {
|
||||||
|
await types[type].importSingle(name, fileContents);
|
||||||
|
if (onSuccess) {
|
||||||
|
onSuccess(`${type}.${name}`);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export a single config file.
|
* Export a single config file.
|
||||||
*
|
*
|
||||||
* @param {string} configType - The type of config.
|
|
||||||
* @param {string} configName - The name of the config file.
|
* @param {string} configName - The name of the config file.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
exportSingleConfig: async (configType, configName) => {
|
exportSingleConfig: async (configName) => {
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue