feat: Partial import in CLI

pull/28/head
Boaz Poolman 2021-11-18 22:45:20 +01:00
parent 7af07bfcf0
commit 962952f662
2 changed files with 57 additions and 29 deletions

View File

@ -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 diff = await app.plugin('config-sync').service('main').getFormattedDiff();
// No changes.
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);
}
// Init table.
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.
Object.keys(diff.diff).map((configName) => {
table.push([configName, getConfigState(diff, configName, type)]);
Object.keys(finalDiff).map((configName) => {
table.push([configName, getConfigState(diff, configName, syncType)]);
});
// Print table.
@ -92,23 +116,25 @@ const handleAction = async (type, skipConfirm) => {
answer = await inquirer.prompt([{
type: '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('');
}
// Preform the action.
if (skipConfirm || answer.confirm) {
if (type === 'import') {
if (syncType === 'import') {
const onSuccess = (name) => console.log(`${chalk.bgGreen.bold('[success]')} Imported ${name}`);
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) {
console.log(`${chalk.bgRed.bold('[error]')} Something went wrong during the import. ${e}`);
}
}
if (type === 'export') {
if (syncType === 'export') {
try {
await app.plugin('config-sync').service('main').exportAllConfig();
console.log(`${chalk.bgGreen.bold('[success]')} Config was exported`);
@ -141,22 +167,24 @@ program
program
.command('import')
.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')
.description('Import the config')
.action(async ({ y, type }) => {
return handleAction('import', y, type);
.action(async ({ y, type, partial }) => {
return handleAction('import', y, type, partial);
});
// `$ config-sync export`
program
.command('export')
.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')
.description('Export the config')
.action(async ({ y, type }) => {
return handleAction('export', y, type);
.action(async ({ y, type, partial }) => {
return handleAction('export', y, type, partial);
});
// `$ config-sync diff`

View File

@ -161,14 +161,7 @@ module.exports = () => ({
return;
}
try {
await strapi.plugin('config-sync').service('main').importSingleConfig(type, name);
if (onSuccess) {
onSuccess(`${type}.${name}`);
}
} catch (e) {
throw new Error(e);
}
await strapi.plugin('config-sync').service('main').importSingleConfig(`${type}.${name}`, onSuccess);
}));
},
@ -191,28 +184,35 @@ module.exports = () => ({
/**
* 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 {object} onSuccess - Success callback to run on each single successfull import.
* @returns {void}
*/
importSingleConfig: async (configType, configName) => {
importSingleConfig: async (configName, onSuccess) => {
// 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;
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.
*
* @param {string} configType - The type of config.
* @param {string} configName - The name of the config file.
* @returns {void}
*/
exportSingleConfig: async (configType, configName) => {
exportSingleConfig: async (configName) => {
},