151 lines
4.6 KiB
JavaScript
Raw Normal View History

2016-08-26 14:19:03 +02:00
'use strict';
2022-02-18 20:15:46 +01:00
const path = require('path');
2022-12-28 18:27:07 +01:00
const { map, values, sumBy, pipe, flatMap, propEq } = require('lodash/fp');
2018-03-23 12:44:17 +01:00
const _ = require('lodash');
2022-02-18 20:15:46 +01:00
const { exists } = require('fs-extra');
const { env } = require('@strapi/utils');
const { isUsingTypeScript } = require('@strapi/typescript-utils');
// eslint-disable-next-line node/no-extraneous-require
2023-09-15 11:09:37 +02:00
const ee = require('@strapi/strapi/dist/utils/ee').default;
const {
validateUpdateProjectSettings,
validateUpdateProjectSettingsFiles,
validateUpdateProjectSettingsImagesDimensions,
} = require('../validation/project-settings');
2021-08-04 19:39:40 +02:00
const { getService } = require('../utils');
2016-08-26 14:19:03 +02:00
/**
* A set of functions called "actions" for `Admin`
*/
module.exports = {
// TODO very temporary to check the switch ee/ce
// When removing this we need to update the /admin/src/index.js file
// where we set the strapi.window.isEE value
async getProjectType() {
// FIXME
try {
return { data: { isEE: strapi.EE, features: ee.features.list() } };
} catch (err) {
return { data: { isEE: false, features: [] } };
}
},
async init() {
let uuid = strapi.config.get('uuid', false);
2021-07-28 15:32:21 +02:00
const hasAdmin = await getService('user').exists();
2022-11-29 15:35:07 +01:00
const { menuLogo, authLogo } = await getService('project-settings').getProjectSettings();
2022-03-30 11:54:06 +03:00
// set to null if telemetryDisabled flag not avaialble in package.json
const telemetryDisabled = strapi.config.get('packageJsonStrapi.telemetryDisabled', null);
if (telemetryDisabled !== null && telemetryDisabled === true) {
uuid = false;
}
return {
data: {
uuid,
hasAdmin,
menuLogo: menuLogo ? menuLogo.url : null,
2022-11-29 15:35:07 +01:00
authLogo: authLogo ? authLogo.url : null,
},
};
},
async getProjectSettings() {
return getService('project-settings').getProjectSettings();
},
async updateProjectSettings(ctx) {
const projectSettingsService = getService('project-settings');
const {
request: { files, body },
} = ctx;
await validateUpdateProjectSettings(body);
await validateUpdateProjectSettingsFiles(files);
const formatedFiles = await projectSettingsService.parseFilesData(files);
await validateUpdateProjectSettingsImagesDimensions(formatedFiles);
2022-04-04 15:09:08 +02:00
return projectSettingsService.updateProjectSettings({ ...body, ...formatedFiles });
},
2022-06-07 16:07:39 +02:00
async telemetryProperties(ctx) {
// If the telemetry is disabled, ignore the request and return early
if (strapi.telemetry.isDisabled) {
2022-06-07 16:19:24 +02:00
ctx.status = 204;
2022-06-07 16:07:39 +02:00
return;
}
const useTypescriptOnServer = await isUsingTypeScript(strapi.dirs.app.root);
const useTypescriptOnAdmin = await isUsingTypeScript(
path.join(strapi.dirs.app.root, 'src', 'admin')
);
2022-09-30 11:40:05 +02:00
const isHostedOnStrapiCloud = env('STRAPI_HOSTING', null) === 'strapi.cloud';
2023-01-09 18:15:38 +01:00
const numberOfAllContentTypes = _.size(strapi.contentTypes);
const numberOfComponents = _.size(strapi.components);
2022-12-28 18:27:07 +01:00
2023-01-30 17:59:37 +01:00
const getNumberOfDynamicZones = () => {
return pipe(
2022-12-28 18:27:07 +01:00
map('attributes'),
flatMap(values),
sumBy(propEq('type', 'dynamiczone'))
)(strapi.contentTypes);
2023-01-30 17:59:37 +01:00
};
return {
data: {
useTypescriptOnServer,
useTypescriptOnAdmin,
2022-09-29 18:12:05 +02:00
isHostedOnStrapiCloud,
2023-01-30 17:59:37 +01:00
numberOfAllContentTypes, // TODO: V5: This event should be renamed numberOfContentTypes in V5 as the name is already taken to describe the number of content types using i18n.
numberOfComponents,
2022-12-28 18:27:07 +01:00
numberOfDynamicZones: getNumberOfDynamicZones(),
},
};
},
async information() {
2021-09-01 19:55:16 +02:00
const currentEnvironment = strapi.config.get('environment');
const autoReload = strapi.config.get('autoReload', false);
const strapiVersion = strapi.config.get('info.strapi', null);
2022-06-23 14:39:48 +02:00
const dependencies = strapi.config.get('info.dependencies', {});
const projectId = strapi.config.get('uuid', null);
const nodeVersion = process.version;
const communityEdition = !strapi.EE;
2022-02-18 20:15:46 +01:00
const useYarn = await exists(path.join(process.cwd(), 'yarn.lock'));
2019-09-09 16:06:54 +02:00
return {
2022-02-18 18:53:03 +01:00
data: {
currentEnvironment,
autoReload,
strapiVersion,
2022-06-23 14:39:48 +02:00
dependencies,
projectId,
2022-02-18 18:53:03 +01:00
nodeVersion,
communityEdition,
2022-02-18 20:15:46 +01:00
useYarn,
2022-02-18 18:53:03 +01:00
},
};
2019-09-09 16:06:54 +02:00
},
2019-07-15 19:33:07 +02:00
async plugins(ctx) {
const enabledPlugins = strapi.config.get('enabledPlugins');
const plugins = Object.entries(enabledPlugins).map(([key, plugin]) => ({
name: plugin.info.name || key,
displayName: plugin.info.displayName || plugin.info.name || key,
description: plugin.info.description || '',
2022-02-21 16:38:15 +01:00
packageName: plugin.info.packageName,
}));
2021-08-20 13:12:37 +02:00
ctx.send({ plugins });
},
2016-08-26 14:19:03 +02:00
};