From c53b165f119925525ed48a93c6736d723bbc8bbf Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Thu, 15 Jul 2021 10:01:43 +0100 Subject: [PATCH] fix: catch more export cases --- helpers/usesBuildCommand.js | 18 ++++++++++++++++-- test/fixtures/not-static.json | 7 +++++++ test/fixtures/static.json | 13 +++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/helpers/usesBuildCommand.js b/helpers/usesBuildCommand.js index 5f1af420ab..3b86f04b8c 100644 --- a/helpers/usesBuildCommand.js +++ b/helpers/usesBuildCommand.js @@ -1,4 +1,7 @@ const parseNpmScript = require('@netlify/parse-npm-script') + +const COMMAND_PLACEHOLDER = '___netlifybuildcommand' + // Does the build command include this value, either directly or via an npm script? const usesBuildCommand = ({ build, scripts, command }) => { if (!build.command) return false @@ -10,12 +13,23 @@ const usesBuildCommand = ({ build, scripts, command }) => { if (!build.command.includes('npm run') && !build.command.includes('yarn')) { return false } + + // Insert a fake script to represent the build command + + const commands = { ...scripts, [COMMAND_PLACEHOLDER]: build.command } + // This resolves the npm script that is actually being run try { - const { raw } = parseNpmScript({ scripts }, build.command) + const { raw } = parseNpmScript({ scripts: commands }, COMMAND_PLACEHOLDER) return raw.some((script) => script.includes(command)) } catch (error) { - console.error('There was an error parsing your build command', error) + console.error( + 'There was an error parsing your build command:', + error.message, + `\n\nThe build command is "${build.command}" and the available npm scripts are: ${Object.keys(scripts) + .map((script) => `"${script}"`) + .join(', ')}`, + ) } } diff --git a/test/fixtures/not-static.json b/test/fixtures/not-static.json index 841a5ace97..48ce0761b0 100644 --- a/test/fixtures/not-static.json +++ b/test/fixtures/not-static.json @@ -53,5 +53,12 @@ "build:types": "tsc", "build": "run-p build:types" } + }, + { + "command": "npm run build && npm run export", + "scripts": { + "export": "echo export", + "build": "next build" + } } ] \ No newline at end of file diff --git a/test/fixtures/static.json b/test/fixtures/static.json index 90b0a280a0..b6838123dc 100644 --- a/test/fixtures/static.json +++ b/test/fixtures/static.json @@ -12,6 +12,19 @@ "build": "npm run build:export" } }, + { + "command": "npm run build && npm run export", + "scripts": { + "export": "next export", + "build": "next build" + } + }, + { + "command": "npm run build && next export", + "scripts": { + "build": "next build" + } + }, { "command": "npm run build", "scripts": {