11const fs = require ( 'fs' )
2- const { existsSync, readFileSync } = require ( 'fs' )
2+ const { promisify } = require ( 'util' )
3+ const exists = promisify ( fs . exists )
34const path = require ( 'path' )
4- const { appendFile, readdir } = require ( 'fs' ) . promises
5- const { hasFramework } = require ( '@netlify/framework-info' )
65const nextOnNetlify = require ( 'next-on-netlify' )
76const { PHASE_PRODUCTION_BUILD } = require ( 'next/constants' )
87const { default : loadConfig } = require ( 'next/dist/next-server/server/config' )
8+ const makef = require ( 'makef' )
99const makeDir = require ( 'make-dir' )
1010const cpx = require ( 'cpx' )
1111const isStaticExportProject = require ( './helpers/isStaticExportProject' )
@@ -15,14 +15,22 @@ const isStaticExportProject = require('./helpers/isStaticExportProject')
1515// - Between the build and postbuild steps, any functions are bundled
1616
1717module . exports = {
18- async onPreBuild ( { netlifyConfig, packageJson : { scripts = { } , dependencies = { } } , utils } ) {
18+ async onPreBuild ( { netlifyConfig, packageJson, utils } ) {
1919 const { failBuild } = utils . build
2020
21- if ( ! ( await hasFramework ( 'next' ) ) ) {
22- return failBuild ( `This application does not use Next.js.` )
21+ if ( ! packageJson ) {
22+ failBuild ( `Could not find a package.json for this project` )
23+ return
24+ }
25+
26+ if ( ! netlifyConfig ) {
27+ failBuild ( `Could not find a Netlify configuration for this project` )
28+ return
2329 }
2430
2531 const { build } = netlifyConfig
32+ const { scripts = { } , dependencies = { } } = packageJson
33+
2634 // TO-DO: Post alpha, try to remove this workaround for missing deps in
2735 // the next-on-netlify function template
2836 await utils . run . command ( 'npm install next-on-netlify@latest' )
@@ -46,7 +54,8 @@ module.exports = {
4654 )
4755 }
4856
49- if ( existsSync ( 'next.config.js' ) ) {
57+ const hasNextConfig = await exists ( 'next.config.js' )
58+ if ( hasNextConfig ) {
5059 // If the next config exists, fail build if target isnt in acceptableTargets
5160 const acceptableTargets = [ 'serverless' , 'experimental-serverless-trace' ]
5261 const nextConfig = loadConfig ( PHASE_PRODUCTION_BUILD , path . resolve ( '.' ) )
@@ -61,7 +70,7 @@ module.exports = {
6170 target: 'serverless'
6271 }
6372 `
64- await appendFile ( 'next.config.js' , nextConfig )
73+ makef . createFile ( { 'next.config.js' : nextConfig } )
6574 console . log ( `** Adding next.config.js with target set to 'serverless' **` )
6675 }
6776 } ,
@@ -77,7 +86,8 @@ module.exports = {
7786 // if (!existsSync(FUNCTIONS_DIST)) {
7887 // await makeDir(FUNCTIONS_DIST);
7988 // }
80- if ( ! existsSync ( PUBLISH_DIR ) ) {
89+ const hasPublishDir = await exists ( PUBLISH_DIR )
90+ if ( ! hasPublishDir ) {
8191 await makeDir ( PUBLISH_DIR )
8292 }
8393
0 commit comments