1
- const { existsSync, readFileSync, appendFileSync } = require ( 'fs' )
1
+ const fs = require ( 'fs' )
2
+ const { promisify } = require ( 'util' )
3
+ const exists = promisify ( fs . exists )
2
4
const path = require ( 'path' )
3
5
const nextOnNetlify = require ( 'next-on-netlify' )
4
6
const { PHASE_PRODUCTION_BUILD } = require ( 'next/constants' )
5
7
const { default : loadConfig } = require ( 'next/dist/next-server/server/config' )
8
+ const makef = require ( 'makef' )
6
9
const makeDir = require ( 'make-dir' )
7
10
const cpx = require ( 'cpx' )
8
11
const isStaticExportProject = require ( './helpers/isStaticExportProject' )
@@ -12,9 +15,23 @@ const isStaticExportProject = require('./helpers/isStaticExportProject')
12
15
// - Between the build and postbuild steps, any functions are bundled
13
16
14
17
module . exports = {
15
- async onPreBuild ( { netlifyConfig = { } , packageJson : { scripts = { } , dependencies = { } } , utils } ) {
18
+ // TO-DO: remove default packageJson once CLI issue is diagnosed
19
+ async onPreBuild ( { netlifyConfig, packageJson = { } , utils } ) {
16
20
const { failBuild } = utils . build
21
+
22
+ if ( ! packageJson ) {
23
+ failBuild ( `Could not find a package.json for this project` )
24
+ return
25
+ }
26
+
27
+ if ( ! netlifyConfig ) {
28
+ failBuild ( `Could not find a Netlify configuration for this project` )
29
+ return
30
+ }
31
+
17
32
const { build } = netlifyConfig
33
+ const { scripts = { } , dependencies = { } } = packageJson
34
+
18
35
// TO-DO: Post alpha, try to remove this workaround for missing deps in
19
36
// the next-on-netlify function template
20
37
await utils . run . command ( 'npm install next-on-netlify@latest' )
@@ -38,7 +55,8 @@ module.exports = {
38
55
)
39
56
}
40
57
41
- if ( existsSync ( 'next.config.js' ) ) {
58
+ const hasNextConfig = await exists ( 'next.config.js' )
59
+ if ( hasNextConfig ) {
42
60
// If the next config exists, fail build if target isnt in acceptableTargets
43
61
const acceptableTargets = [ 'serverless' , 'experimental-serverless-trace' ]
44
62
const nextConfig = loadConfig ( PHASE_PRODUCTION_BUILD , path . resolve ( '.' ) )
@@ -53,7 +71,7 @@ module.exports = {
53
71
target: 'serverless'
54
72
}
55
73
`
56
- appendFileSync ( 'next.config.js' , nextConfig )
74
+ makef . createFile ( { 'next.config.js' : nextConfig } )
57
75
console . log ( `** Adding next.config.js with target set to 'serverless' **` )
58
76
}
59
77
} ,
@@ -69,7 +87,8 @@ module.exports = {
69
87
// if (!existsSync(FUNCTIONS_DIST)) {
70
88
// await makeDir(FUNCTIONS_DIST);
71
89
// }
72
- if ( ! existsSync ( PUBLISH_DIR ) ) {
90
+ const hasPublishDir = await exists ( PUBLISH_DIR )
91
+ if ( ! hasPublishDir ) {
73
92
await makeDir ( PUBLISH_DIR )
74
93
}
75
94
0 commit comments