From 980b6d240d78711baf6d0c8887bf287fe6f7fcb7 Mon Sep 17 00:00:00 2001 From: Alberto Maturano Date: Tue, 23 Oct 2018 16:23:34 -0500 Subject: [PATCH] Improve code style and code quality Just some little improvements that I found looking around the code. Related 2410d5a --- packages/strapi-generate-admin/lib/before.js | 7 ------- .../scripts/loadAdminConfigurations.js | 3 +-- .../internals/webpack/webpack.base.babel.js | 11 ++--------- .../lib/internals/webpack/webpack.dev.babel.js | 12 ++---------- .../lib/internals/webpack/webpack.dll.babel.js | 3 +-- .../internals/webpack/webpack.prod.babel.js | 14 +++----------- packages/strapi/lib/core/plugins.js | 18 +++++++++++------- 7 files changed, 20 insertions(+), 48 deletions(-) diff --git a/packages/strapi-generate-admin/lib/before.js b/packages/strapi-generate-admin/lib/before.js index 2cfcd11d72..77f28df621 100644 --- a/packages/strapi-generate-admin/lib/before.js +++ b/packages/strapi-generate-admin/lib/before.js @@ -1,13 +1,7 @@ 'use strict'; -/** - * Module dependencies - */ - -// Node.js core. const path = require('path'); -// Public node modules. const _ = require('lodash'); const fs = require('fs-extra'); @@ -18,7 +12,6 @@ const fs = require('fs-extra'); * @param {Object} scope * @param {Function} cb */ - module.exports = function (scope, cb) { if (scope.developerMode) { fs.mkdirsSync(path.resolve(scope.rootPath)); diff --git a/packages/strapi-helper-plugin/lib/internals/scripts/loadAdminConfigurations.js b/packages/strapi-helper-plugin/lib/internals/scripts/loadAdminConfigurations.js index 4e53f933da..e99c4a4d0f 100644 --- a/packages/strapi-helper-plugin/lib/internals/scripts/loadAdminConfigurations.js +++ b/packages/strapi-helper-plugin/lib/internals/scripts/loadAdminConfigurations.js @@ -3,9 +3,8 @@ const shell = require('shelljs'); const pwd = shell.pwd(); const isDevelopmentMode = path.resolve(pwd.stdout).indexOf('strapi-admin') !== -1; +const isSetup = process.env.IS_MONOREPO || false; const appPath = isDevelopmentMode ? path.resolve(process.env.PWD, '..') : path.resolve(pwd.stdout, '..'); -// const isSetup = path.resolve(process.env.PWD, '..', '..') === path.resolve(process.env.INIT_CWD); -const isSetup = process.env.IS_MONOREPO; // Load the app configurations only when : // - starting the app in dev mode diff --git a/packages/strapi-helper-plugin/lib/internals/webpack/webpack.base.babel.js b/packages/strapi-helper-plugin/lib/internals/webpack/webpack.base.babel.js index 3e446c207e..588528cbf5 100644 --- a/packages/strapi-helper-plugin/lib/internals/webpack/webpack.base.babel.js +++ b/packages/strapi-helper-plugin/lib/internals/webpack/webpack.base.babel.js @@ -14,15 +14,8 @@ const isAdmin = process.env.IS_ADMIN === 'true'; const ExtractTextPlugin = require('extract-text-webpack-plugin'); -const appPath = (() => { - if (process.env.APP_PATH) { - return process.env.APP_PATH; - } - - return isAdmin ? path.resolve(process.env.PWD, '..') : path.resolve(process.env.PWD, '..', '..'); -})(); -// const isSetup = path.resolve(process.env.PWD, '..', '..') === path.resolve(process.env.INIT_CWD); -const isSetup = process.env.IS_MONOREPO; +const isSetup = process.env.IS_MONOREPO || false; +const appPath = process.env.APP_PATH || path.resolve(process.env.PWD, '..', ( isAdmin ? '' : '..' )); const adminPath = (() => { if (isAdmin && isSetup) { diff --git a/packages/strapi-helper-plugin/lib/internals/webpack/webpack.dev.babel.js b/packages/strapi-helper-plugin/lib/internals/webpack/webpack.dev.babel.js index 74fae158b6..89ffbf6af4 100644 --- a/packages/strapi-helper-plugin/lib/internals/webpack/webpack.dev.babel.js +++ b/packages/strapi-helper-plugin/lib/internals/webpack/webpack.dev.babel.js @@ -16,19 +16,11 @@ const LodashModuleReplacementPlugin = require('lodash-webpack-plugin'); const isAdmin = process.env.IS_ADMIN === 'true'; const isSetup = path.resolve(process.env.PWD, '..', '..') === path.resolve(process.env.INIT_CWD); -const appPath = (() => { - if (process.env.APP_PATH) { - return process.env.APP_PATH; - } - - return isAdmin ? path.resolve(process.env.PWD, '..') : path.resolve(process.env.PWD, '..', '..'); -})(); +const appPath = process.env.APP_PATH || path.resolve(process.env.PWD, '..', ( isAdmin ? '' : '..' )); const rootAdminpath = (() => { if (isSetup) { - return isAdmin - ? path.resolve(appPath, 'strapi-admin') - : path.resolve(appPath, 'packages', 'strapi-admin'); + return isAdmin ? path.resolve(appPath, 'strapi-admin') : path.resolve(appPath, 'packages', 'strapi-admin'); } return path.resolve(appPath, 'admin'); })(); diff --git a/packages/strapi-helper-plugin/lib/internals/webpack/webpack.dll.babel.js b/packages/strapi-helper-plugin/lib/internals/webpack/webpack.dll.babel.js index 9cf7b23f22..22a28cf889 100644 --- a/packages/strapi-helper-plugin/lib/internals/webpack/webpack.dll.babel.js +++ b/packages/strapi-helper-plugin/lib/internals/webpack/webpack.dll.babel.js @@ -9,8 +9,7 @@ const path = require('path'); const webpack = require('webpack'); const isAdmin = process.env.IS_ADMIN === 'true'; -// const isSetup = path.resolve(process.env.PWD, '..', '..') === path.resolve(process.env.INIT_CWD); -const isSetup = process.env.IS_MONOREPO; +const isSetup = process.env.IS_MONOREPO || false; const appPath = process.env.APP_PATH || path.resolve(process.env.PWD, '..', ( isAdmin ? '' : '..' )); const rootAdminpath = (() => { diff --git a/packages/strapi-helper-plugin/lib/internals/webpack/webpack.prod.babel.js b/packages/strapi-helper-plugin/lib/internals/webpack/webpack.prod.babel.js index 975b48f54b..ea6f0e29de 100644 --- a/packages/strapi-helper-plugin/lib/internals/webpack/webpack.prod.babel.js +++ b/packages/strapi-helper-plugin/lib/internals/webpack/webpack.prod.babel.js @@ -15,14 +15,8 @@ const base = require('./webpack.base.babel'); const isAdmin = process.env.IS_ADMIN === 'true'; // const isSetup = path.resolve(process.env.PWD, '..', '..') === path.resolve(process.env.INIT_CWD); -const isSetup = process.env.IS_MONOREPO; -const appPath = (() => { - if (process.env.APP_PATH) { - return process.env.APP_PATH; - } - - return isAdmin ? path.resolve(process.env.PWD, '..') : path.resolve(process.env.PWD, '..', '..'); -})(); +const isSetup = process.env.IS_MONOREPO || false; +const appPath = process.env.APP_PATH || path.resolve(process.env.PWD, '..', ( isAdmin ? '' : '..' )); const adminPath = (() => { if (isSetup) { return isAdmin ? path.resolve(appPath, 'strapi-admin') : path.resolve(process.env.PWD, '..'); @@ -33,9 +27,7 @@ const adminPath = (() => { const rootAdminpath = (() => { if (isSetup) { - return isAdmin - ? path.resolve(appPath, 'strapi-admin') - : path.resolve(appPath, 'packages', 'strapi-admin'); + return isAdmin ? path.resolve(appPath, 'strapi-admin') : path.resolve(appPath, 'packages', 'strapi-admin'); } return path.resolve(appPath, 'admin'); })(); diff --git a/packages/strapi/lib/core/plugins.js b/packages/strapi/lib/core/plugins.js index 5c303dabfd..da69a4cf87 100644 --- a/packages/strapi/lib/core/plugins.js +++ b/packages/strapi/lib/core/plugins.js @@ -6,17 +6,16 @@ const fs = require('fs-extra'); const _ = require('lodash'); module.exports = async function() { - const folder = ((url = _.get(strapi.config.currentEnvironment.server, 'admin.path', 'admin')) => - url[0] === '/' ? url.substring(1) : url)().replace(/\/$/, ''); + // The regex removes possible slashes from the beginning and end of the value + const folder = _.get(strapi.config.currentEnvironment.server, 'admin.path', 'admin').replace(/^\/|\/$/g, ''); const configuratePlugin = (acc, current, source, name) => { switch (source) { case 'host': { - const host = - _.get(this.config.environments[current].server, 'admin.build.host').replace(/\/$/, '') || '/'; + const host = _.get(this.config.environments[current].server, 'admin.build.host').replace(/\/$/, '') || '/'; if (!host) { - throw new Error(`You can't use \`remote\` as a source without set the \`host\` configuration.`); + throw new Error("You can't use `remote` as a source without set the `host` configuration."); } const folder = _.get(this.config.environments[current].server, 'admin.build.plugins.folder', null); @@ -29,14 +28,16 @@ module.exports = async function() { return `/${host}/${name}/main.js`.replace('//', '/'); } + case 'custom': if (!_.isEmpty(_.get(this.plugins[name].config, `sources.${current}`, {}))) { return (acc[current] = this.plugins[name].config.sources[current]); } throw new Error( - `You have to define the source URL for each environment in \`./plugins/**/config/sources.json\``, + "You have to define the source URL for each environment in `./plugins/**/config/sources.json`" ); + case 'backend': { const backend = _.get( this.config.environments[current], @@ -48,6 +49,7 @@ module.exports = async function() { return `${backend}/${folder.replace(/\/$/, '')}/${name}/main.js`; } + default: return `/${name}/main.js`; } @@ -79,7 +81,9 @@ module.exports = async function() { ); const isAdmin = await fs.pathExists(path.resolve(this.config.appPath, 'admin', 'admin')); - if (!isAdmin) return; + if (!isAdmin) { + return; + } // arrange system directories await Promise.all([