Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display version from package.json in left menu #5478

Merged
merged 3 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/strapi-admin/admin/src/containers/App/reducer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Shared constants
import { fromJS } from 'immutable';
import packageJSON from '../../../../package.json';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will break because we do not copy the pkg.json file in the .cache folder :/


import {
DISABLE_GLOBAL_OVERLAY_BLOCKER,
Expand All @@ -13,6 +14,7 @@ import {
UPDATE_PLUGIN,
} from './constants';

const packageVersion = packageJSON.version;
const initialState = fromJS({
autoReload: false,
blockApp: false,
Expand All @@ -23,7 +25,7 @@ const initialState = fromJS({
overlayBlockerData: null,
plugins: {},
showGlobalAppBlocker: true,
strapiVersion: '3',
strapiVersion: packageVersion,
uuid: false,
});

Expand All @@ -47,13 +49,19 @@ function appReducer(state = initialState, action) {
data: { uuid, currentEnvironment, autoReload, strapiVersion },
} = action;

if (strapiVersion !== state.get('strapiVersion')) {
console.error(
`It seems that the built version ${packageVersion} is different than your project's one (${strapiVersion})`
);
console.error('Please delete your `.cache` and `build` folders and restart your app');
}

return state
.update('isLoading', () => false)
.update('hasAdminUser', () => hasAdminUser)
.update('uuid', () => uuid)
.update('autoReload', () => autoReload)
.update('currentEnvironment', () => currentEnvironment)
.update('strapiVersion', () => strapiVersion);
.update('currentEnvironment', () => currentEnvironment);
}
case PLUGIN_LOADED:
return state.setIn(['plugins', action.plugin.id], fromJS(action.plugin));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fromJS } from 'immutable';
import packageJSON from '../../../../../package.json';
import {
disableGlobalOverlayBlocker,
enableGlobalOverlayBlocker,
Expand All @@ -25,7 +26,7 @@ describe('<App /> reducer', () => {
overlayBlockerData: null,
plugins: {},
showGlobalAppBlocker: true,
strapiVersion: '3',
strapiVersion: packageJSON.version,
uuid: false,
});
});
Expand All @@ -37,26 +38,20 @@ describe('<App /> reducer', () => {

it('should handle the disableGlobalOverlayBlocker action correctly', () => {
const expectedResult = state.set('showGlobalAppBlocker', false);
expect(appReducer(state, disableGlobalOverlayBlocker())).toEqual(
expectedResult
);
expect(appReducer(state, disableGlobalOverlayBlocker())).toEqual(expectedResult);
});

it('should handle the enableGlobalOverlayBlocker action correctly', () => {
state = state.set('showGlobalAppBlocker', false);
const expectedResult = state.set('showGlobalAppBlocker', true);
expect(appReducer(state, enableGlobalOverlayBlocker())).toEqual(
expectedResult
);
expect(appReducer(state, enableGlobalOverlayBlocker())).toEqual(expectedResult);
});

it('should handle the freezeApp action correctly and set the overlayBlockerData key if passed data', () => {
const expectedResult = state
.set('blockApp', true)
.set('overlayBlockerData', { title: 'A title' });
expect(appReducer(state, freezeApp({ title: 'A title' }))).toEqual(
expectedResult
);
expect(appReducer(state, freezeApp({ title: 'A title' }))).toEqual(expectedResult);
});

it('should handle the freezeApp action correctly and NOT set the overlayBlockerData key if no passed data', () => {
Expand All @@ -70,10 +65,7 @@ describe('<App /> reducer', () => {
id: 'content-manager',
description: 'Manage your content',
};
const expectedResult = state.setIn(
['plugins', 'content-manager'],
fromJS(plugin)
);
const expectedResult = state.setIn(['plugins', 'content-manager'], fromJS(plugin));

expect(appReducer(state, pluginLoaded(plugin))).toEqual(expectedResult);
});
Expand All @@ -82,33 +74,24 @@ describe('<App /> reducer', () => {
const plugin = { id: 'content-manager', isReady: false };
state = state.setIn(['plugins', 'content-manager'], fromJS(plugin));

const expectedResult = state.setIn(
['plugins', 'content-manager', 'isReady'],
true
);
const expectedResult = state.setIn(['plugins', 'content-manager', 'isReady'], true);

expect(
appReducer(state, updatePlugin('content-manager', 'isReady', true))
).toEqual(expectedResult);
expect(appReducer(state, updatePlugin('content-manager', 'isReady', true))).toEqual(
expectedResult
);
});

it('should handle the pluginDeleted action correclty', () => {
const plugin = { id: 'content-manager', isReady: false };
state = state.setIn(['plugins', 'content-manager'], fromJS(plugin));
const expectedResult = state.deleteIn(['plugins', 'content-manager']);

expect(appReducer(state, pluginDeleted('content-manager'))).toEqual(
expectedResult
);
expect(appReducer(state, pluginDeleted('content-manager'))).toEqual(expectedResult);
});

it('should handle the unfreezeApp action correclty', () => {
state = state
.set('blockApp', true)
.set('overlayBlockerData', { foo: 'bar' });
const expectedResult = state
.set('blockApp', false)
.set('overlayBlockerData', null);
state = state.set('blockApp', true).set('overlayBlockerData', { foo: 'bar' });
const expectedResult = state.set('blockApp', false).set('overlayBlockerData', null);

expect(appReducer(state, unfreezeApp())).toEqual(expectedResult);
});
Expand Down
37 changes: 11 additions & 26 deletions packages/strapi-admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const WebpackDevServer = require('webpack-dev-server');
const chalk = require('chalk');
const chokidar = require('chokidar');

const getPkgPath = name =>
path.dirname(require.resolve(`${name}/package.json`));
const getPkgPath = name => path.dirname(require.resolve(`${name}/package.json`));

async function createPluginsJs(plugins, localPlugins, dest) {
const content = `
Expand Down Expand Up @@ -51,10 +50,7 @@ module.exports = {
}
`;

return fs.writeFile(
path.resolve(dest, 'admin', 'src', 'plugins.js'),
content
);
return fs.writeFile(path.resolve(dest, 'admin', 'src', 'plugins.js'), content);
}

async function copyPlugin(name, dest) {
Expand Down Expand Up @@ -88,6 +84,9 @@ async function copyAdmin(dest) {
path.resolve(adminPath, 'config', 'layout.js'),
path.resolve(dest, 'config', 'layout.js')
);

// Copy package.json
await fs.copy(path.resolve(adminPath, 'package.json'), path.resolve(dest, 'package.json'));
}

async function copyCustomAdmin(src, dest) {
Expand All @@ -110,9 +109,7 @@ async function createCacheDir(dir) {
localPluginsToCopy = fs
.readdirSync(path.join(dir, 'plugins'))
.filter(plugin =>
fs.existsSync(
path.resolve(dir, 'plugins', plugin, 'admin', 'src', 'index.js')
)
fs.existsSync(path.resolve(dir, 'plugins', plugin, 'admin', 'src', 'index.js'))
);
}

Expand Down Expand Up @@ -235,11 +232,7 @@ async function watchAdmin({ dir, host, port, options }) {

console.log(chalk.green('Starting the development server...'));
console.log();
console.log(
chalk.green(
`Admin development at http://${host}:${port}${opts.publicPath}`
)
);
console.log(chalk.green(`Admin development at http://${host}:${port}${opts.publicPath}`));
});

watchFiles(dir, options.watchIgnoreFiles);
Expand Down Expand Up @@ -269,18 +262,12 @@ async function watchFiles(dir, ignoreFiles = []) {

watcher.on('all', async (event, filePath) => {
const isExtension = filePath.includes(extensionsPath);
const pluginName = isExtension
? filePath.replace(extensionsPath, '').split(path.sep)[1]
: '';
const pluginName = isExtension ? filePath.replace(extensionsPath, '').split(path.sep)[1] : '';

const packageName = isExtension
? `strapi-plugin-${pluginName}`
: 'strapi-admin';
const packageName = isExtension ? `strapi-plugin-${pluginName}` : 'strapi-admin';

const targetPath = isExtension
? path.normalize(
filePath.split(extensionsPath)[1].replace(pluginName, '')
)
? path.normalize(filePath.split(extensionsPath)[1].replace(pluginName, ''))
: path.normalize(filePath.split(admin)[1]);

const destFolder = isExtension
Expand Down Expand Up @@ -320,9 +307,7 @@ async function watchFiles(dir, ignoreFiles = []) {
filePath.split('/admin/src').filter(p => !!p).length === 1;

if (
(event === 'unlinkDir' &&
!isExtension &&
shouldCopyPluginsJSFile) ||
(event === 'unlinkDir' && !isExtension && shouldCopyPluginsJSFile) ||
(!isExtension && filePath.includes('plugins.js'))
) {
await createPluginsJs(appPlugins, path.join(cacheDir));
Expand Down