From f8f06411e1adf65675df711873fc4c87075f1348 Mon Sep 17 00:00:00 2001 From: Lindsay Levine Date: Wed, 18 Nov 2020 06:10:28 -0500 Subject: [PATCH] fail build if app is using NoN already --- index.js | 15 +++++++++------ test/index.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 2ba25c3a70..2e7e37dca2 100644 --- a/index.js +++ b/index.js @@ -31,12 +31,15 @@ module.exports = { return failBuild(`** Static HTML export next.js projects do not require this plugin **`) } - // TO-DO: check scripts to make sure the app isn't manually running NoN - // For now, we'll make it clear in the README - // const isAlreadyUsingNextOnNetlify = Object.keys(dependencies).find((dep) => dep === 'next-on-netlify'); - // if (isAlreadyUsingNextOnNetlify) { - // return failBuild(`This plugin cannot support apps that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`); - // } + const hasNextOnNetlifyInstalled = dependencies['next-on-netlify'] !== undefined + const hasNextOnNetlifyPostbuildScript = + typeof scripts.postbuild === 'string' && scripts.postbuild.includes('next-on-netlify') + const isAlreadyUsingNextOnNetlify = hasNextOnNetlifyInstalled || hasNextOnNetlifyPostbuildScript + if (isAlreadyUsingNextOnNetlify) { + return failBuild( + `This plugin does not support sites that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`, + ) + } const nextConfigPath = await findUp('next.config.js') if (nextConfigPath !== undefined) { diff --git a/test/index.js b/test/index.js index a5bccf3330..101cc3ab3d 100644 --- a/test/index.js +++ b/test/index.js @@ -83,6 +83,36 @@ describe('preBuild()', () => { ).rejects.toThrow('** Static HTML export next.js projects do not require this plugin **') }) + test('fail build if app has next-on-netlify installed', async () => { + const packageJson = { + dependencies: { 'next-on-netlify': '123' }, + } + await expect( + plugin.onPreBuild({ + netlifyConfig: {}, + packageJson, + utils, + }), + ).rejects.toThrow( + `This plugin does not support sites that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`, + ) + }) + + test('fail build if app has next-on-netlify postbuild script', async () => { + const packageJson = { + scripts: { postbuild: 'next-on-netlify' }, + } + await expect( + plugin.onPreBuild({ + netlifyConfig: {}, + packageJson, + utils, + }), + ).rejects.toThrow( + `This plugin does not support sites that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`, + ) + }) + test('fail build if the app has no package.json', async () => { await expect( plugin.onPreBuild({