1
1
const { existsSync} = require ( 'fs' ) ;
2
2
const { appendFile, readFile, readdir} = require ( 'fs' ) . promises ;
3
3
const { join} = require ( 'path' ) ;
4
+
4
5
const toml = require ( '@iarna/toml' ) ;
5
6
const cpx = require ( 'cpx' ) ;
6
7
const build = require ( 'netlify-lambda/lib/build' ) ;
7
8
8
9
const functionsBuildDir = `functions-build-${ Date . now ( ) } ` ;
10
+ const nimConfig = join ( require ( 'os' ) . homedir ( ) , '.nimbella' ) ;
9
11
let netlifyToml = { } ;
10
12
let isProject = false ;
11
13
let isActions = false ;
@@ -54,39 +56,51 @@ async function deployActions({run, functionsDir, timeout, memory}) {
54
56
module . exports = {
55
57
// Execute before build starts.
56
58
onPreBuild : async ( { utils, constants, inputs} ) => {
57
- try {
58
- if ( ! process . env . NIMBELLA_LOGIN_TOKEN ) {
59
+ if (
60
+ ! process . env . NIMBELLA_LOGIN_TOKEN &&
61
+ ! ( await utils . cache . has ( nimConfig ) )
62
+ ) {
63
+ utils . build . failBuild (
64
+ 'Nimbella login token is not available. Please run `netlify addons:create nimbella` at the base of your local project directory linked to your Netlify site.'
65
+ ) ;
66
+ }
67
+
68
+ await utils . cache . restore ( nimConfig ) ;
69
+
70
+ const loggedIn = existsSync ( nimConfig ) ;
71
+ // Login if not logged in before.
72
+ if ( loggedIn ) {
73
+ try {
74
+ const { stdout} = await utils . run . command ( 'nim auth current' , {
75
+ stdout : 'pipe'
76
+ } ) ;
77
+ console . log ( `Using the following namespace: ${ stdout } ` ) ;
78
+ } catch ( error ) {
59
79
utils . build . failBuild (
60
- 'Nimbella login token not available. Please run `netlify addons:create nimbella` at the base of your local project directory linked to your Netlify site.'
80
+ 'Failed to retrieve the current namespace from cache' ,
81
+ { error}
61
82
) ;
62
83
}
63
-
64
- const nimConfig = join ( require ( 'os' ) . homedir ( ) , '.nimbella' ) ;
65
- await utils . cache . restore ( nimConfig ) ;
66
-
67
- const loggedIn = existsSync ( nimConfig ) ;
68
- // Login if not logged in before.
69
- if ( loggedIn ) {
70
- console . log ( '\nUsing the following namespace.' ) ;
71
- await utils . run . command ( 'nim auth current' ) ;
72
- } else {
84
+ } else {
85
+ try {
73
86
await utils . run . command (
74
87
`nim auth login ${ process . env . NIMBELLA_LOGIN_TOKEN } `
75
88
) ;
76
-
77
89
// Cache the nimbella config to avoid logging in for consecutive builds.
78
90
await utils . cache . save ( nimConfig ) ;
91
+ } catch ( error ) {
92
+ utils . build . failBuild ( 'Failed to login using the provided token' , {
93
+ error
94
+ } ) ;
79
95
}
96
+ }
80
97
81
- if ( constants . CONFIG_PATH && existsSync ( constants . CONFIG_PATH ) ) {
82
- netlifyToml = toml . parse ( await readFile ( constants . CONFIG_PATH ) ) ;
83
- }
84
-
85
- isActions = inputs . functions ? existsSync ( inputs . functions ) : false ;
86
- isProject = existsSync ( 'packages' ) ;
87
- } catch ( error ) {
88
- utils . build . failBuild ( error . message ) ;
98
+ if ( constants . CONFIG_PATH && existsSync ( constants . CONFIG_PATH ) ) {
99
+ netlifyToml = toml . parse ( await readFile ( constants . CONFIG_PATH ) ) ;
89
100
}
101
+
102
+ isActions = inputs . functions ? existsSync ( inputs . functions ) : false ;
103
+ isProject = existsSync ( 'packages' ) ;
90
104
} ,
91
105
// Build the functions
92
106
onBuild : async ( { utils, inputs} ) => {
@@ -99,88 +113,94 @@ module.exports = {
99
113
cpx . copy ( inputs . functions + '/**/*.!(js)' , functionsBuildDir ) ;
100
114
}
101
115
} catch ( error ) {
102
- utils . build . failBuild ( error . message ) ;
116
+ utils . build . failBuild ( 'Failed to build the functions' , { error} ) ;
103
117
}
104
118
} ,
105
119
// Execute after build is done.
106
120
onPostBuild : async ( { constants, utils, inputs} ) => {
107
- try {
108
- const { stdout : namespace } = await utils . run . command ( `nim auth current` ) ;
121
+ const { stdout : namespace } = await utils . run . command ( `nim auth current` ) ;
109
122
110
- if ( process . env . CONTEXT === 'production' ) {
111
- if ( isProject ) {
123
+ if ( process . env . CONTEXT === 'production' ) {
124
+ if ( isProject ) {
125
+ try {
112
126
await deployProject ( utils . run ) ;
127
+ } catch ( error ) {
128
+ utils . build . failBuild ( 'Failed to deploy the project' , { error} ) ;
113
129
}
130
+ }
114
131
115
- if ( isActions ) {
116
- console . log ( '\n------------------Functions------------------\n' ) ;
132
+ if ( isActions ) {
133
+ console . log ( '\n------------------Functions------------------\n' ) ;
134
+ try {
117
135
await deployActions ( {
118
136
run : utils . run ,
119
137
functionsDir : functionsBuildDir ,
120
138
timeout : inputs . timeout , // Default is 6 seconds
121
139
memory : inputs . memory // Default is 256MB (max for free tier)
122
140
} ) ;
141
+ } catch ( error ) {
142
+ utils . build . failBuild ( 'Failed to deploy the functions' , { error} ) ;
123
143
}
124
- } else {
144
+ }
145
+ } else {
146
+ console . log (
147
+ `Skipping the deployment to Nimbella as the context (${ process . env . CONTEXT } ) is not production.`
148
+ ) ;
149
+ }
150
+
151
+ const redirectRules = [ ] ;
152
+ const redirects = [ ] ;
153
+ const redirectsFile = join ( constants . PUBLISH_DIR , '_redirects' ) ;
154
+
155
+ if ( isActions ) {
156
+ if ( existsSync ( redirectsFile ) ) {
125
157
console . log (
126
- `Skipping the deployment to Nimbella as the context ( ${ process . env . CONTEXT } ) is not production.`
158
+ "Found _redirects file. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
127
159
) ;
160
+ const { parseRedirectsFormat} = require ( 'netlify-redirect-parser' ) ;
161
+ const { success} = await parseRedirectsFormat ( redirectsFile ) ;
162
+ redirects . push ( ...success ) ;
128
163
}
129
164
130
- const redirectRules = [ ] ;
131
- const redirects = [ ] ;
132
- const redirectsFile = join ( constants . PUBLISH_DIR , '_redirects' ) ;
133
-
134
- if ( isActions ) {
135
- if ( existsSync ( redirectsFile ) ) {
136
- console . log (
137
- "Found _redirects file. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
138
- ) ;
139
- const { parseRedirectsFormat} = require ( 'netlify-redirect-parser' ) ;
140
- const { success} = await parseRedirectsFormat ( redirectsFile ) ;
141
- redirects . push ( ...success ) ;
142
- }
165
+ if ( netlifyToml . redirects ) {
166
+ console . log (
167
+ "Found redirect rules in netlify.toml. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
168
+ ) ;
169
+ redirects . push ( ...netlifyToml . redirects ) ;
170
+ }
143
171
144
- if ( netlifyToml . redirects ) {
145
- console . log (
146
- "Found redirect rules in netlify.toml. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
172
+ for ( const redirect of redirects ) {
173
+ if (
174
+ redirect . status === 200 &&
175
+ redirect . to . startsWith ( '/.netlify/functions/' )
176
+ ) {
177
+ const redirectPath = redirect . to . split ( '/.netlify/functions/' ) [ 1 ] ;
178
+ redirectRules . push (
179
+ `${ redirect . from } https://apigcp.nimbella.io/api/v1/web/${ namespace } /default/${ redirectPath } 200!`
147
180
) ;
148
- redirects . push ( ...netlifyToml . redirects ) ;
149
- }
150
-
151
- for ( const redirect of redirects ) {
152
- if (
153
- redirect . status === 200 &&
154
- redirect . to . startsWith ( '/.netlify/functions/' )
155
- ) {
156
- const redirectPath = redirect . to . split ( '/.netlify/functions/' ) [ 1 ] ;
157
- redirectRules . push (
158
- `${ redirect . from } https://apigcp.nimbella.io/api/v1/web/${ namespace } /default/${ redirectPath } 200!`
159
- ) ;
160
- }
161
181
}
162
182
}
183
+ }
163
184
164
- let { path : redirectPath } = inputs ;
165
- redirectPath = redirectPath . endsWith ( '/' )
166
- ? redirectPath
167
- : redirectPath + '/' ;
185
+ let { path : redirectPath } = inputs ;
186
+ redirectPath = redirectPath . endsWith ( '/' )
187
+ ? redirectPath
188
+ : redirectPath + '/' ;
168
189
169
- if ( isProject ) {
170
- redirectRules . push (
171
- `${ redirectPath } * https://apigcp.nimbella.io/api/v1/web/${ namespace } /:splat 200!`
172
- ) ;
173
- }
190
+ if ( isProject ) {
191
+ redirectRules . push (
192
+ `${ redirectPath } * https://apigcp.nimbella.io/api/v1/web/${ namespace } /:splat 200!`
193
+ ) ;
194
+ }
174
195
175
- if ( isActions && ! isProject ) {
176
- redirectRules . push (
177
- `${ redirectPath } * https://apigcp.nimbella.io/api/v1/web/${ namespace } /default/:splat 200!`
178
- ) ;
179
- }
196
+ if ( isActions && ! isProject ) {
197
+ redirectRules . push (
198
+ `${ redirectPath } * https://apigcp.nimbella.io/api/v1/web/${ namespace } /default/:splat 200!`
199
+ ) ;
200
+ }
180
201
202
+ if ( redirectRules . length > 0 ) {
181
203
await appendFile ( redirectsFile , redirectRules . join ( '\n' ) ) ;
182
- } catch ( error ) {
183
- utils . build . failBuild ( error . message ) ;
184
204
}
185
205
}
186
206
} ;
0 commit comments