From 13cec4cadb933ec9c3bb4f1f8d8703be0dbd67e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20No=C3=ABl?= Date: Thu, 2 Sep 2021 12:14:44 +0200 Subject: [PATCH 1/3] append strapi.dir to plugin path if relative --- examples/getstarted/config/plugins.js | 2 +- .../loaders/plugins/get-enabled-plugins.js | 23 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/examples/getstarted/config/plugins.js b/examples/getstarted/config/plugins.js index 301ee51da4..5aae0b4a8e 100644 --- a/examples/getstarted/config/plugins.js +++ b/examples/getstarted/config/plugins.js @@ -15,7 +15,7 @@ module.exports = ({ env }) => ({ }, myplugin: { enabled: true, - resolve: `${__dirname}/../plugins/myplugin`, + resolve: `./plugins/myplugin`, // From the root of the project config: { testConf: 3, }, diff --git a/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js b/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js index c28851b478..4234d1c075 100644 --- a/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js +++ b/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js @@ -1,6 +1,6 @@ 'use strict'; -const { dirname, join } = require('path'); +const { dirname, join, isAbsolute } = require('path'); const { statSync, existsSync } = require('fs'); const _ = require('lodash'); const { get, has, pick, pickBy, defaultsDeep, map, prop, pipe } = require('lodash/fp'); @@ -9,6 +9,8 @@ const loadConfigFile = require('../../app-configuration/load-config-file'); const isStrapiPlugin = info => get('strapi.kind', info) === 'plugin'; +const isPath = path => /^(\.\/|\.\.\/|\/|\.\\|\.\.\\|\\)/.test(path); + const validatePluginName = pluginName => { if (!isKebabCase(pluginName)) { throw new Error(`Plugin name "${pluginName}" is not in kebab (an-example-of-kebab-case)`); @@ -26,12 +28,19 @@ const toDetailedDeclaration = declaration => { } if (has('resolve', declaration)) { let pathToPlugin = ''; - try { - pathToPlugin = dirname(require.resolve(declaration.resolve)); - } catch (e) { - if (existsSync(declaration.resolve) && statSync(declaration.resolve).isDirectory()) { - pathToPlugin = declaration.resolve; - } else { + + if (isPath(declaration.resolve)) { + pathToPlugin = isAbsolute(declaration.resolve) + ? declaration.resolve + : join(strapi.dir, declaration.resolve); + + if (!existsSync(pathToPlugin) || !statSync(pathToPlugin).isDirectory()) { + throw new Error(`${declaration.resolve} couldn't be resolved`); + } + } else { + try { + pathToPlugin = dirname(require.resolve(declaration.resolve)); + } catch (e) { throw new Error(`${declaration.resolve} couldn't be resolved`); } } From a31e30170187798d0e5e457b59309810922eeb03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20No=C3=ABl?= Date: Thu, 2 Sep 2021 14:01:13 +0200 Subject: [PATCH 2/3] Shorter regex + direct use of resolve --- .../loaders/plugins/get-enabled-plugins.js | 10 +++------- .../lib/__tests__/string-formatting.test.js | 18 ++++++++++++++++++ packages/core/utils/lib/index.js | 2 ++ packages/core/utils/lib/string-formatting.js | 2 ++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js b/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js index 4234d1c075..70c38e185b 100644 --- a/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js +++ b/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js @@ -1,16 +1,14 @@ 'use strict'; -const { dirname, join, isAbsolute } = require('path'); +const { dirname, join, resolve } = require('path'); const { statSync, existsSync } = require('fs'); const _ = require('lodash'); const { get, has, pick, pickBy, defaultsDeep, map, prop, pipe } = require('lodash/fp'); -const { isKebabCase } = require('@strapi/utils'); +const { isKebabCase, isPath } = require('@strapi/utils'); const loadConfigFile = require('../../app-configuration/load-config-file'); const isStrapiPlugin = info => get('strapi.kind', info) === 'plugin'; -const isPath = path => /^(\.\/|\.\.\/|\/|\.\\|\.\.\\|\\)/.test(path); - const validatePluginName = pluginName => { if (!isKebabCase(pluginName)) { throw new Error(`Plugin name "${pluginName}" is not in kebab (an-example-of-kebab-case)`); @@ -30,9 +28,7 @@ const toDetailedDeclaration = declaration => { let pathToPlugin = ''; if (isPath(declaration.resolve)) { - pathToPlugin = isAbsolute(declaration.resolve) - ? declaration.resolve - : join(strapi.dir, declaration.resolve); + pathToPlugin = resolve(strapi.dir, declaration.resolve); if (!existsSync(pathToPlugin) || !statSync(pathToPlugin).isDirectory()) { throw new Error(`${declaration.resolve} couldn't be resolved`); diff --git a/packages/core/utils/lib/__tests__/string-formatting.test.js b/packages/core/utils/lib/__tests__/string-formatting.test.js index 652583396a..437cf6d81b 100644 --- a/packages/core/utils/lib/__tests__/string-formatting.test.js +++ b/packages/core/utils/lib/__tests__/string-formatting.test.js @@ -6,6 +6,7 @@ const { stringEquals, getCommonBeginning, getCommonPath, + isPath, } = require('../string-formatting'); describe('string-formatting', () => { @@ -97,4 +98,21 @@ describe('string-formatting', () => { expect(result).toBe(expectedResult); }); }); + + describe('isPath', () => { + const tests = [ + ['plugin', false], + ['.plugin', false], + ['./plugin', true], + ['../plugin', true], + ['/plugin', true], + ['.\\plugin', true], + ['..\\plugin', true], + ['\\plugin', true], + ]; + test.each(tests)('%p is a path: %p', (path, expectedResult) => { + const result = isPath(path); + expect(result).toBe(expectedResult); + }); + }); }); diff --git a/packages/core/utils/lib/index.js b/packages/core/utils/lib/index.js index d3e2a31710..39af77b783 100644 --- a/packages/core/utils/lib/index.js +++ b/packages/core/utils/lib/index.js @@ -20,6 +20,7 @@ const { stringEquals, isKebabCase, isCamelCase, + isPath, } = require('./string-formatting'); const { removeUndefined } = require('./object-formatting'); const { getConfigUrls, getAbsoluteAdminUrl, getAbsoluteServerUrl } = require('./config'); @@ -57,6 +58,7 @@ module.exports = { stringEquals, isKebabCase, isCamelCase, + isPath, contentTypes, webhook, env, diff --git a/packages/core/utils/lib/string-formatting.js b/packages/core/utils/lib/string-formatting.js index 882f5d3af3..1be7c5b051 100644 --- a/packages/core/utils/lib/string-formatting.js +++ b/packages/core/utils/lib/string-formatting.js @@ -35,6 +35,7 @@ const stringIncludes = (arr, val) => arr.map(String).includes(String(val)); const stringEquals = (a, b) => String(a) === String(b); const isCamelCase = value => /^[a-z][a-zA-Z0-9]+$/.test(value); const isKebabCase = value => /^([a-z][a-z0-9]*)(-[a-z0-9]+)*$/.test(value); +const isPath = path => /^(\.{0,2}(\/|\\))/.test(path); module.exports = { nameToSlug, @@ -46,4 +47,5 @@ module.exports = { stringEquals, isCamelCase, isKebabCase, + isPath, }; From ce85d3d69d2cbe194113fce557f870a93bda57d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20No=C3=ABl?= Date: Fri, 3 Sep 2021 10:57:46 +0200 Subject: [PATCH 3/3] try to resolve as a module first --- .../loaders/plugins/get-enabled-plugins.js | 13 ++++--------- .../lib/__tests__/string-formatting.test.js | 18 ------------------ packages/core/utils/lib/index.js | 2 -- packages/core/utils/lib/string-formatting.js | 2 -- 4 files changed, 4 insertions(+), 31 deletions(-) diff --git a/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js b/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js index 70c38e185b..67ecf52b5d 100644 --- a/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js +++ b/packages/core/strapi/lib/core/loaders/plugins/get-enabled-plugins.js @@ -4,7 +4,7 @@ const { dirname, join, resolve } = require('path'); const { statSync, existsSync } = require('fs'); const _ = require('lodash'); const { get, has, pick, pickBy, defaultsDeep, map, prop, pipe } = require('lodash/fp'); -const { isKebabCase, isPath } = require('@strapi/utils'); +const { isKebabCase } = require('@strapi/utils'); const loadConfigFile = require('../../app-configuration/load-config-file'); const isStrapiPlugin = info => get('strapi.kind', info) === 'plugin'; @@ -26,19 +26,14 @@ const toDetailedDeclaration = declaration => { } if (has('resolve', declaration)) { let pathToPlugin = ''; - - if (isPath(declaration.resolve)) { + try { + pathToPlugin = dirname(require.resolve(declaration.resolve)); + } catch (e) { pathToPlugin = resolve(strapi.dir, declaration.resolve); if (!existsSync(pathToPlugin) || !statSync(pathToPlugin).isDirectory()) { throw new Error(`${declaration.resolve} couldn't be resolved`); } - } else { - try { - pathToPlugin = dirname(require.resolve(declaration.resolve)); - } catch (e) { - throw new Error(`${declaration.resolve} couldn't be resolved`); - } } detailedDeclaration.pathToPlugin = pathToPlugin; diff --git a/packages/core/utils/lib/__tests__/string-formatting.test.js b/packages/core/utils/lib/__tests__/string-formatting.test.js index 437cf6d81b..652583396a 100644 --- a/packages/core/utils/lib/__tests__/string-formatting.test.js +++ b/packages/core/utils/lib/__tests__/string-formatting.test.js @@ -6,7 +6,6 @@ const { stringEquals, getCommonBeginning, getCommonPath, - isPath, } = require('../string-formatting'); describe('string-formatting', () => { @@ -98,21 +97,4 @@ describe('string-formatting', () => { expect(result).toBe(expectedResult); }); }); - - describe('isPath', () => { - const tests = [ - ['plugin', false], - ['.plugin', false], - ['./plugin', true], - ['../plugin', true], - ['/plugin', true], - ['.\\plugin', true], - ['..\\plugin', true], - ['\\plugin', true], - ]; - test.each(tests)('%p is a path: %p', (path, expectedResult) => { - const result = isPath(path); - expect(result).toBe(expectedResult); - }); - }); }); diff --git a/packages/core/utils/lib/index.js b/packages/core/utils/lib/index.js index 39af77b783..d3e2a31710 100644 --- a/packages/core/utils/lib/index.js +++ b/packages/core/utils/lib/index.js @@ -20,7 +20,6 @@ const { stringEquals, isKebabCase, isCamelCase, - isPath, } = require('./string-formatting'); const { removeUndefined } = require('./object-formatting'); const { getConfigUrls, getAbsoluteAdminUrl, getAbsoluteServerUrl } = require('./config'); @@ -58,7 +57,6 @@ module.exports = { stringEquals, isKebabCase, isCamelCase, - isPath, contentTypes, webhook, env, diff --git a/packages/core/utils/lib/string-formatting.js b/packages/core/utils/lib/string-formatting.js index 1be7c5b051..882f5d3af3 100644 --- a/packages/core/utils/lib/string-formatting.js +++ b/packages/core/utils/lib/string-formatting.js @@ -35,7 +35,6 @@ const stringIncludes = (arr, val) => arr.map(String).includes(String(val)); const stringEquals = (a, b) => String(a) === String(b); const isCamelCase = value => /^[a-z][a-zA-Z0-9]+$/.test(value); const isKebabCase = value => /^([a-z][a-z0-9]*)(-[a-z0-9]+)*$/.test(value); -const isPath = path => /^(\.{0,2}(\/|\\))/.test(path); module.exports = { nameToSlug, @@ -47,5 +46,4 @@ module.exports = { stringEquals, isCamelCase, isKebabCase, - isPath, };