Merge pull request #5 from boazpoolman/develop

Develop
pull/7/head 0.1.4
Boaz Poolman 2021-03-27 21:55:54 +01:00 committed by GitHub
commit b9e6a37df8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 320 additions and 79 deletions

View File

@ -2,7 +2,7 @@
A lot of configuration of your Strapi project is stored in the database. Like core_store, user permissions, user roles & webhooks. Things you might want to have the same on all environments. But when you update them locally, you will have to manually update them on all other environments too.
That's where this plugin comes in to play. It allows you to export these configs as individual JSON files for each config, and write them somewhere in your project. With the configs written in your filesystem your can keep track of them through version control (git), and easily pull and import them across environments.
That's where this plugin comes in to play. It allows you to export these configs as individual JSON files for each config, and write them somewhere in your project. With the configs written in your filesystem you can keep track of them through version control (git), and easily pull and import them across environments.
Importing, exporting and keeping track of config changes is done in the admin page of the plugin.
@ -27,44 +27,50 @@ This way your app won't reload when you export the config in development.
admin: {
auth: {
...
// ...
},
watchIgnoreFiles: [
'**/config-sync/files/**',
]
],
},
## Settings
Some settings for the plugin are able to be modified by creating a file `extensions/config-sync/config/config.json` and overwriting the default settings.
The settings of the plugin can be overridden in the `config/plugins.js` file.
In the example below you can see how, and also what the default settings are.
#### Default settings:
{
"destination": "extensions/config-sync/files/",
"minify": false,
"importOnBootstrap": false,
"include": [
"core-store",
"role-permissions",
"webhooks"
],
"exclude": []
}
##### `config/plugins.js`:
module.exports = ({ env }) => ({
// ...
'config-sync': {
destination: "extensions/config-sync/files/",
minify: false,
importOnBootstrap: false,
include: [
"core-store",
"role-permissions"
],
exclude: [
"core-store.plugin_users-permissions_grant"
]
},
// ...
});
| Property | Default | Description |
| -------- | ------- | ----------- |
| destination | extensions/config-sync/files/ | The path for reading and writing the config JSON files. |
| minify | false | Setting to minify the JSON that's being exported. It defaults to false for better readability in git commits. |
| importOnBootstrap | false | Allows you to let the config be imported automaticly when strapi is bootstrapping (on `yarn start`). This setting should only be used in production, and should be handled very carefully as it can unintendedly overwrite the changes in your database. PLEASE USE WITH CARE. |
| include | ["core-store", "role-permissions", "webhooks"] | Configs you want to include. Allowed values: `core-store`, `role-permissions`, `webhooks`. |
| exclude | [] | You might not want all your database config exported and managed in git. This settings allows you to add an array of config names which should not be tracked by the config-sync plugin. *Currently not working* |
| Property | Type | Description |
| -------- | ---- | ----------- |
| destination | string | The path for reading and writing the sync files. |
| minify | bool | When enabled all the exported JSON files will be minified. |
| importOnBootstrap | bool | Allows you to let the config be imported automaticly when strapi is bootstrapping (on `strapi start`). This setting should only be used in production, and should be handled very carefully as it can unintendedly overwrite the changes in your database. PLEASE USE WITH CARE. |
| include | array | Configs types you want to include in the syncing process. Allowed values: `core-store`, `role-permissions`, `webhooks`. |
| exclude | array | Specify the names of configs you want to exclude from the syncing process. By default the API tokens for users-permissions, which are stored in core_store, are excluded. This setting expects the config names to comply with the naming convention. |
## Naming convention
All the config files written in the file destination have the same naming convention. It goes as follows:
[config-type].[config-name].json
- `config-type` - Corresponds to the value in from the config.include setting.
- `config-type` - Corresponds to the value in from the include setting.
- `config-name` - The unique identifier of the config.
- For `core-store` config this is the `key` value.
- For `role-permissions` config this is the `type` value.
@ -78,7 +84,14 @@ All the config files written in the file destination have the same naming conven
- Exporting of EE roles & permissions
- Add partial import/export functionality
- Add CLI commands for importing/exporting
- Track config deletions
- ~~Track config deletions~~
## ⭐️ Show your support
Give a star if this project helped you.
## Credits
Shout out to [@ScottAgirs](https://github.com/ScottAgirs) for making [strapi-plugin-migrate](https://github.com/ijsto/strapi-plugin-migrate) as it was a big help while making the config-sync plugin.
## Resources
@ -88,7 +101,3 @@ All the config files written in the file destination have the same naming conven
- [NPM package](https://www.npmjs.com/package/strapi-plugin-config-sync)
- [GitHub repository](https://github.com/boazpoolman/strapi-plugin-config-sync)
## ⭐️ Show your support
Give a star if this project helped you.

View File

@ -23,8 +23,11 @@ const ActionButtons = ({ diff }) => {
return (
<ActionButtonsStyling>
<Button disabled={isEmpty(diff.fileConfig)} color="primary" label="Import" onClick={() => openModal('import')} />
<Button disabled={isEmpty(diff.fileConfig)} color="primary" label="Export" onClick={() => openModal('export')} />
<Button disabled={isEmpty(diff.diff)} color="primary" label="Import" onClick={() => openModal('import')} />
<Button disabled={isEmpty(diff.diff)} color="primary" label="Export" onClick={() => openModal('export')} />
{!isEmpty(diff.diff) && (
<h4 style={{ display: 'inline' }}>{Object.keys(diff.diff).length} {Object.keys(diff.diff).length === 1 ? "config change" : "config changes"}</h4>
)}
<ConfirmModal
isOpen={modalIsOpen}
onClose={closeModal}

View File

@ -0,0 +1,52 @@
import React from 'react';
import styled from 'styled-components';
const CustomRow = ({ row }) => {
const { config_name, config_type, state, onClick } = row;
return (
<tr onClick={() => onClick(config_type, config_name)}>
<td>
<p>{config_name}</p>
</td>
<td>
<p>{config_type}</p>
</td>
<td>
<p style={stateStyle(state)}>{state}</p>
</td>
</tr>
);
};
const stateStyle = (state) => {
let style = {
display: 'inline-flex',
padding: '0 10px',
borderRadius: '12px',
height: '24px',
alignItems: 'center',
fontWeight: '500',
};
if (state === 'Only in DB') {
style.backgroundColor = '#cbf2d7';
style.color = '#1b522b';
}
if (state === 'Only in sync dir') {
style.backgroundColor = '#f0cac7';
style.color = '#3d302f';
}
if (state === 'Different') {
style.backgroundColor = '#e8e6b7';
style.color = '#4a4934';
}
return style;
};
export default CustomRow

View File

@ -3,6 +3,7 @@ import { Table } from '@buffetjs/core';
import { isEmpty } from 'lodash';
import ConfigDiff from '../ConfigDiff';
import FirstExport from '../FirstExport';
import ConfigListRow from './ConfigListRow';
const headers = [
{
@ -14,8 +15,8 @@ const headers = [
value: 'config_type',
},
{
name: 'Change',
value: 'change_type',
name: 'State',
value: 'state',
},
];
@ -26,6 +27,25 @@ const ConfigList = ({ diff, isLoading }) => {
const [configName, setConfigName] = useState('');
const [rows, setRows] = useState([]);
const getConfigState = (configName) => {
if (
diff.fileConfig[configName] &&
diff.databaseConfig[configName]
) {
return 'Different'
} else if (
diff.fileConfig[configName] &&
!diff.databaseConfig[configName]
) {
return 'Only in sync dir'
} else if (
!diff.fileConfig[configName] &&
diff.databaseConfig[configName]
) {
return 'Only in DB'
}
};
useEffect(() => {
if (isEmpty(diff.diff)) {
setRows([]);
@ -33,14 +53,20 @@ const ConfigList = ({ diff, isLoading }) => {
}
let formattedRows = [];
Object.keys(diff.fileConfig).map((configName) => {
Object.keys(diff.diff).map((configName) => {
const type = configName.split('.')[0]; // Grab the first part of the filename.
const name = configName.split(/\.(.+)/)[1]; // Grab the rest of the filename minus the file extension.
formattedRows.push({
config_name: name,
config_type: type,
change_type: ''
state: getConfigState(configName),
onClick: (config_type, config_name) => {
setOriginalConfig(diff.fileConfig[`${config_type}.${config_name}`]);
setNewConfig(diff.databaseConfig[`${config_type}.${config_name}`]);
setConfigName(`${config_type}.${config_name}`);
setOpenModal(true);
}
});
});
@ -70,12 +96,7 @@ const ConfigList = ({ diff, isLoading }) => {
/>
<Table
headers={headers}
onClickRow={(e, { config_type, config_name }) => {
setOriginalConfig(diff.fileConfig[`${config_type}.${config_name}`]);
setNewConfig(diff.databaseConfig[`${config_type}.${config_name}`]);
setConfigName(`${config_type}.${config_name}`);
setOpenModal(true);
}}
customRow={ConfigListRow}
rows={!isLoading ? rows : []}
isLoading={isLoading}
tableEmptyText="No config changes. You are up to date!"

View File

@ -23,7 +23,6 @@ export default strapi => {
blockerComponent: null,
blockerComponentProps: {},
description: pluginDescription,
icon: pluginPkg.strapi.icon,
id: pluginId,
initializer: Initializer,
injectedComponents: [],

View File

@ -4,8 +4,9 @@
"importOnBootstrap": false,
"include": [
"core-store",
"role-permissions",
"webhooks"
"role-permissions"
],
"exclude": []
"exclude": [
"core-store.plugin_users-permissions_grant"
]
}

View File

@ -70,7 +70,8 @@ module.exports = {
const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles();
const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase();
const diff = difference(fileConfig, databaseConfig);
const diff = difference(databaseConfig, fileConfig);
formattedDiff.diff = diff;
Object.keys(diff).map((changedConfigName) => {

View File

@ -1,10 +1,10 @@
{
"name": "strapi-plugin-config-sync",
"version": "0.1.3",
"version": "0.1.4",
"description": "Manage your Strapi database configuration as partial json files which can be imported/exported across environments. ",
"strapi": {
"name": "config-sync",
"icon": "plug",
"icon": "sync",
"description": "Manage your Strapi database configuration as partial json files which can be imported/exported across environments. "
},
"dependencies": {

View File

@ -2,6 +2,7 @@
const coreStoreQueryString = 'core_store';
const configPrefix = 'core-store'; // Should be the same as the filename.
const difference = require('../utils/getObjectDiff');
/**
* Import/Export for core-store configs.
@ -14,11 +15,38 @@ module.exports = {
* @returns {void}
*/
exportAll: async () => {
const coreStore = await strapi.query(coreStoreQueryString).find({ _limit: -1 });
const formattedDiff = {
fileConfig: {},
databaseConfig: {},
diff: {}
};
const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles(configPrefix);
const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase(configPrefix);
const diff = difference(databaseConfig, fileConfig);
await Promise.all(Object.values(coreStore).map(async ({ id, ...config }) => {
config.value = JSON.parse(config.value);
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, config.key, config);
formattedDiff.diff = diff;
Object.keys(diff).map((changedConfigName) => {
formattedDiff.fileConfig[changedConfigName] = fileConfig[changedConfigName];
formattedDiff.databaseConfig[changedConfigName] = databaseConfig[changedConfigName];
})
await Promise.all(Object.entries(diff).map(async ([configName, config]) => {
// Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configName}`);
if (shouldExclude) return;
const currentConfig = formattedDiff.databaseConfig[configName];
if (
!currentConfig &&
formattedDiff.fileConfig[configName]
) {
await strapi.plugins['config-sync'].services.main.deleteConfigFile(configName);
} else {
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, currentConfig.key, currentConfig);
}
}));
},
@ -30,11 +58,22 @@ module.exports = {
* @returns {void}
*/
importSingle: async (configName, configContent) => {
const { value, ...fileContent } = configContent;
// Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configPrefix}.${configName}`);
if (shouldExclude) return;
const coreStoreAPI = strapi.query(coreStoreQueryString);
const configExists = await coreStoreAPI
.findOne({ key: configName, environment: fileContent.environment });
.findOne({ key: configName });
if (configExists && configContent === null) {
await coreStoreAPI.delete({ key: configName });
return;
}
const { value, ...fileContent } = configContent;
if (!configExists) {
await coreStoreAPI.create({ value: JSON.stringify(value), ...fileContent });
@ -53,6 +92,10 @@ module.exports = {
let configs = {};
Object.values(coreStore).map( ({ id, value, key, ...config }) => {
// Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configPrefix}.${key}`);
if (shouldExclude) return;
configs[`${configPrefix}.${key}`] = { key, value: JSON.parse(value), ...config };
});

View File

@ -2,6 +2,7 @@
const fs = require('fs');
const util = require('util');
const difference = require('../utils/getObjectDiff');
/**
* Main services for config import/export.
@ -43,6 +44,20 @@ module.exports = {
});
},
/**
* Delete config file.
*
* @param {string} configName - The name of the config file.
* @returns {void}
*/
deleteConfigFile: async (configName) => {
// Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configName}`);
if (shouldExclude) return;
fs.unlinkSync(`${strapi.plugins['config-sync'].config.destination}${configName}.json`);
},
/**
* Read from a config file.
*
@ -67,7 +82,11 @@ module.exports = {
*
* @returns {object} Object with key value pairs of configs.
*/
getAllConfigFromFiles: async () => {
getAllConfigFromFiles: async (configType = null) => {
if (!fs.existsSync(strapi.plugins['config-sync'].config.destination)) {
return {};
}
const configFiles = fs.readdirSync(strapi.plugins['config-sync'].config.destination);
const getConfigs = async () => {
@ -76,6 +95,15 @@ module.exports = {
await Promise.all(configFiles.map(async (file) => {
const type = file.split('.')[0]; // Grab the first part of the filename.
const name = file.split(/\.(.+)/)[1].split('.').slice(0, -1).join('.'); // Grab the rest of the filename minus the file extension.
if (
configType && configType !== type ||
!strapi.plugins['config-sync'].config.include.includes(type) ||
strapi.plugins['config-sync'].config.exclude.includes(`${type}.${name}`)
) {
return;
}
const fileContents = await strapi.plugins['config-sync'].services.main.readConfigFile(type, name);
fileConfigs[`${type}.${name}`] = fileContents;
}));
@ -116,11 +144,14 @@ module.exports = {
* @returns {void}
*/
importAllConfig: async (configType = null) => {
const configFiles = fs.readdirSync(strapi.plugins['config-sync'].config.destination);
const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles();
const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase();
configFiles.map((file) => {
const diff = difference(databaseConfig, fileConfig);
Object.keys(diff).map((file) => {
const type = file.split('.')[0]; // Grab the first part of the filename.
const name = file.split(/\.(.+)/)[1].split('.').slice(0, -1).join('.'); // Grab the rest of the filename minus the file extension.
const name = file.split(/\.(.+)/)[1]; // Grab the rest of the filename minus the file extension.
if (configType && configType !== type) {
return;

View File

@ -1,6 +1,7 @@
'use strict';
const { sanitizeEntity } = require('strapi-utils');
const difference = require('../utils/getObjectDiff');
const configPrefix = 'role-permissions'; // Should be the same as the filename.
@ -15,26 +16,39 @@ module.exports = {
* @returns {void}
*/
exportAll: async () => {
const service =
strapi.plugins['users-permissions'].services.userspermissions;
const formattedDiff = {
fileConfig: {},
databaseConfig: {},
diff: {}
};
const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles(configPrefix);
const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase(configPrefix);
const diff = difference(databaseConfig, fileConfig);
const [roles, plugins] = await Promise.all([
service.getRoles(),
service.getPlugins(),
]);
formattedDiff.diff = diff;
const rolesWithPermissions = await Promise.all(
roles.map(async role => service.getRole(role.id, plugins))
);
Object.keys(diff).map((changedConfigName) => {
formattedDiff.fileConfig[changedConfigName] = fileConfig[changedConfigName];
formattedDiff.databaseConfig[changedConfigName] = databaseConfig[changedConfigName];
})
const sanitizedRolesArray = rolesWithPermissions.map(role =>
sanitizeEntity(role, {
model: strapi.plugins['users-permissions'].models.role,
})
);
await Promise.all(Object.entries(diff).map(async ([configName, config]) => {
// Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configName}`);
if (shouldExclude) return;
const currentConfig = formattedDiff.databaseConfig[configName];
if (
!currentConfig &&
formattedDiff.fileConfig[configName]
) {
await strapi.plugins['config-sync'].services.main.deleteConfigFile(configName);
} else {
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, currentConfig.type, currentConfig);
}
await Promise.all(sanitizedRolesArray.map(async (config) => {
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, config.type, config);
}));
},
@ -46,6 +60,10 @@ module.exports = {
* @returns {void}
*/
importSingle: async (configName, configContent) => {
// Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configPrefix}.${configName}`);
if (shouldExclude) return;
const service =
strapi.plugins['users-permissions'].services.userspermissions;
@ -53,6 +71,15 @@ module.exports = {
.query('role', 'users-permissions')
.findOne({ type: configName });
if (role && configContent === null) {
const publicRole = await strapi.query('role', 'users-permissions').findOne({ type: 'public' });
const publicRoleID = publicRole.id;
await service.deleteRole(role.id, publicRoleID);
return;
}
const users = role ? role.users : [];
configContent.users = users;
@ -89,7 +116,11 @@ module.exports = {
let configs = {};
Object.values(sanitizedRolesArray).map((config) => {
Object.values(sanitizedRolesArray).map(({ id, ...config }) => {
// Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configPrefix}.${config.type}`);
if (shouldExclude) return;
configs[`${configPrefix}.${config.type}`] = config;
});

View File

@ -6,6 +6,7 @@
const webhookQueryString = 'strapi_webhooks';
const configPrefix = 'webhooks'; // Should be the same as the filename.
const difference = require('../utils/getObjectDiff');
module.exports = {
/**
@ -14,10 +15,38 @@ module.exports = {
* @returns {void}
*/
exportAll: async () => {
const webhooks = await strapi.query(webhookQueryString).find({ _limit: -1 });
const formattedDiff = {
fileConfig: {},
databaseConfig: {},
diff: {}
};
const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles(configPrefix);
const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase(configPrefix);
const diff = difference(databaseConfig, fileConfig);
await Promise.all(Object.values(webhooks).map(async (config) => {
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, config.id, config);
formattedDiff.diff = diff;
Object.keys(diff).map((changedConfigName) => {
formattedDiff.fileConfig[changedConfigName] = fileConfig[changedConfigName];
formattedDiff.databaseConfig[changedConfigName] = databaseConfig[changedConfigName];
})
await Promise.all(Object.entries(diff).map(async ([configName, config]) => {
// Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configName}`);
if (shouldExclude) return;
const currentConfig = formattedDiff.databaseConfig[configName];
if (
!currentConfig &&
formattedDiff.fileConfig[configName]
) {
await strapi.plugins['config-sync'].services.main.deleteConfigFile(configName);
} else {
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, currentConfig.id, currentConfig);
}
}));
},
@ -29,6 +58,10 @@ module.exports = {
* @returns {void}
*/
importSingle: async (configName, configContent) => {
// Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configPrefix}.${configName}`);
if (shouldExclude) return;
const webhookAPI = strapi.query(webhookQueryString);
const configExists = await webhookAPI.findOne({ id: configName });
@ -36,6 +69,9 @@ module.exports = {
if (!configExists) {
await webhookAPI.create(configContent);
} else {
if (configContent === null) {
await webhookAPI.delete({ id: configName });
}
await webhookAPI.update({ id: configName }, configContent);
}
},
@ -50,6 +86,10 @@ module.exports = {
let configs = {};
Object.values(webhooks).map( (config) => {
// Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configPrefix}.${config.id}`);
if (shouldExclude) return;
configs[`${configPrefix}.${config.id}`] = config;
});

View File

@ -10,13 +10,23 @@ const { transform, isEqual, isArray, isObject } = require('lodash');
const difference = (origObj, newObj) => {
function changes(newObj, origObj) {
let arrayIndexCounter = 0
return transform(newObj, function (result, value, key) {
const newObjChange = transform(newObj, function (result, value, key) {
if (!isEqual(value, origObj[key])) {
let resultKey = isArray(origObj) ? arrayIndexCounter++ : key
result[resultKey] = (isObject(value) && isObject(origObj[key])) ? changes(value, origObj[key]) : value
}
});
const origObjChange = transform(origObj, function (result, value, key) {
if (!isEqual(value, newObj[key])) {
let resultKey = isArray(newObj) ? arrayIndexCounter++ : key
result[resultKey] = (isObject(value) && isObject(newObj[key])) ? changes(value, newObj[key]) : value
}
})
return Object.assign(newObjChange, origObjChange);
}
return changes(newObj, origObj)
}