1
- const { existsSync} = require ( 'fs' ) ;
2
- const { appendFile, readFile, readdir, writeFile} = require ( 'fs' ) . promises ;
3
- const { join} = require ( 'path' ) ;
4
- const { homedir, tmpdir} = require ( 'os' ) ;
1
+ const { existsSync} = require ( 'fs' )
2
+ const { appendFile, readFile, readdir, writeFile} = require ( 'fs' ) . promises
3
+ const { join} = require ( 'path' )
4
+ const { homedir, tmpdir} = require ( 'os' )
5
5
6
- const toml = require ( '@iarna/toml' ) ;
7
- const cpx = require ( 'cpx' ) ;
8
- const build = require ( 'netlify-lambda/lib/build' ) ;
6
+ const toml = require ( '@iarna/toml' )
7
+ const cpx = require ( 'cpx' )
8
+ const build = require ( 'netlify-lambda/lib/build' )
9
9
10
- const functionsBuildDir = `functions-build-${ Date . now ( ) } ` ;
11
- const nimConfig = join ( homedir ( ) , '.nimbella' ) ;
12
- let netlifyToml = { } ;
13
- let isProject = false ;
14
- let isActions = false ;
10
+ const functionsBuildDir = `functions-build-${ Date . now ( ) } `
11
+ const nimConfig = join ( homedir ( ) , '.nimbella' )
12
+ let netlifyToml = { }
13
+ let isProject = false
14
+ let isActions = false
15
15
16
16
// Disable auto updates of nim.
17
- process . env . NIM_DISABLE_AUTOUPDATE = '1' ;
17
+ process . env . NIM_DISABLE_AUTOUPDATE = '1'
18
18
19
19
/**
20
20
* Deploy a Nimbella Project.
21
21
* @param {* } run - function provided under utils by Netlify to build event functions.
22
22
*/
23
23
async function deployProject ( run ) {
24
- await run . command ( `nim project deploy . --exclude=web` ) ;
24
+ await run . command ( `nim project deploy . --exclude=web` )
25
25
}
26
26
27
27
/**
@@ -30,36 +30,36 @@ async function deployProject(run) {
30
30
* @param {string } functionsDir - Path to the actions directory.
31
31
*/
32
32
async function deployActions ( { run, functionsDir, timeout, memory, envsFile} ) {
33
- const files = await readdir ( functionsDir ) ;
33
+ const files = await readdir ( functionsDir )
34
34
if ( ! existsSync ( envsFile ) ) {
35
- envsFile = false ;
35
+ envsFile = false
36
36
}
37
37
38
38
await Promise . all (
39
39
files . map ( async ( file ) => {
40
- const [ actionName , extension ] = file . split ( '.' ) ;
40
+ const [ actionName , extension ] = file . split ( '.' )
41
41
const command = [
42
42
`nim action update ${ actionName } ${ join ( functionsDir , file ) } ` ,
43
43
`--timeout=${ Number ( timeout ) } --memory=${ Number ( memory ) } ` ,
44
44
`--web=raw`
45
- ] ;
45
+ ]
46
46
47
47
if ( extension === 'js' ) {
48
- command . push ( '--kind nodejs-lambda:10 --main handler' ) ;
48
+ command . push ( '--kind nodejs-lambda:10 --main handler' )
49
49
}
50
50
51
51
if ( envsFile ) {
52
- command . push ( `--env-file=${ envsFile } ` ) ;
52
+ command . push ( `--env-file=${ envsFile } ` )
53
53
}
54
54
55
55
const { stderr, exitCode, failed} = await run . command ( command . join ( ' ' ) , {
56
56
reject : false ,
57
57
stdout : 'ignore'
58
- } ) ;
59
- const message = exitCode === 0 && ! failed ? 'done.' : String ( stderr ) ;
60
- console . log ( `Deploying ${ file } : ${ message } ` ) ;
58
+ } )
59
+ const message = exitCode === 0 && ! failed ? 'done.' : String ( stderr )
60
+ console . log ( `Deploying ${ file } : ${ message } ` )
61
61
} )
62
- ) ;
62
+ )
63
63
}
64
64
65
65
module . exports = {
@@ -71,85 +71,85 @@ module.exports = {
71
71
) {
72
72
utils . build . failBuild (
73
73
'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.'
74
- ) ;
74
+ )
75
75
}
76
76
77
- await utils . cache . restore ( nimConfig ) ;
77
+ await utils . cache . restore ( nimConfig )
78
78
79
- const loggedIn = existsSync ( nimConfig ) ;
79
+ const loggedIn = existsSync ( nimConfig )
80
80
// Login if not logged in before.
81
81
if ( loggedIn ) {
82
82
try {
83
83
const { stdout} = await utils . run . command ( 'nim auth current' , {
84
84
stdout : 'pipe'
85
- } ) ;
86
- console . log ( `Using the following namespace: ${ stdout } ` ) ;
85
+ } )
86
+ console . log ( `Using the following namespace: ${ stdout } ` )
87
87
} catch ( error ) {
88
88
utils . build . failBuild (
89
89
'Failed to retrieve the current namespace from cache' ,
90
90
{ error}
91
- ) ;
91
+ )
92
92
}
93
93
} else {
94
94
try {
95
95
await utils . run . command (
96
96
`nim auth login ${ process . env . NIMBELLA_LOGIN_TOKEN } `
97
- ) ;
97
+ )
98
98
// Cache the nimbella config to avoid logging in for consecutive builds.
99
- await utils . cache . save ( nimConfig ) ;
99
+ await utils . cache . save ( nimConfig )
100
100
} catch ( error ) {
101
101
utils . build . failBuild ( 'Failed to login using the provided token' , {
102
102
error
103
- } ) ;
103
+ } )
104
104
}
105
105
}
106
106
107
107
if ( constants . CONFIG_PATH && existsSync ( constants . CONFIG_PATH ) ) {
108
- netlifyToml = toml . parse ( await readFile ( constants . CONFIG_PATH ) ) ;
108
+ netlifyToml = toml . parse ( await readFile ( constants . CONFIG_PATH ) )
109
109
}
110
110
111
- isActions = inputs . functions ? existsSync ( inputs . functions ) : false ;
112
- isProject = existsSync ( 'packages' ) ;
111
+ isActions = inputs . functions ? existsSync ( inputs . functions ) : false
112
+ isProject = existsSync ( 'packages' )
113
113
} ,
114
114
// Build the functions
115
115
onBuild : async ( { utils, inputs} ) => {
116
116
try {
117
117
if ( isActions ) {
118
118
// Here we're passing the build directory instead of source because source is extracted from inputs.functions.
119
- const stats = await build . run ( functionsBuildDir , inputs . functions ) ;
120
- console . log ( stats . toString ( stats . compilation . options . stats ) ) ;
119
+ const stats = await build . run ( functionsBuildDir , inputs . functions )
120
+ console . log ( stats . toString ( stats . compilation . options . stats ) )
121
121
// Copy any files that do not end with .js. T
122
- cpx . copy ( inputs . functions + '/**/*.!(js)' , functionsBuildDir ) ;
122
+ cpx . copy ( inputs . functions + '/**/*.!(js)' , functionsBuildDir )
123
123
}
124
124
} catch ( error ) {
125
- utils . build . failBuild ( 'Failed to build the functions' , { error} ) ;
125
+ utils . build . failBuild ( 'Failed to build the functions' , { error} )
126
126
}
127
127
} ,
128
128
// Execute after build is done.
129
129
onPostBuild : async ( { constants, utils, inputs} ) => {
130
- const { stdout : namespace } = await utils . run . command ( `nim auth current` ) ;
130
+ const { stdout : namespace } = await utils . run . command ( `nim auth current` )
131
131
132
132
if ( process . env . CONTEXT === 'production' ) {
133
133
if ( isProject ) {
134
134
try {
135
- await deployProject ( utils . run ) ;
135
+ await deployProject ( utils . run )
136
136
} catch ( error ) {
137
- utils . build . failBuild ( 'Failed to deploy the project' , { error} ) ;
137
+ utils . build . failBuild ( 'Failed to deploy the project' , { error} )
138
138
}
139
139
}
140
140
141
141
if ( isActions ) {
142
- console . log ( '\n------------------Functions------------------\n' ) ;
142
+ console . log ( '\n------------------Functions------------------\n' )
143
143
try {
144
144
if ( inputs . envs . length > 0 ) {
145
- const envs = { } ;
146
- let envMessage = 'Forwarded the following environment variables: ' ;
145
+ const envs = { }
146
+ let envMessage = 'Forwarded the following environment variables: '
147
147
inputs . envs . forEach ( ( env , index ) => {
148
- envs [ env ] = process . env [ env ] ;
149
- envMessage += index === inputs . envs . length - 1 ? env : env + ', ' ;
150
- } ) ;
151
- await writeFile ( join ( tmpdir ( ) , 'env.json' ) , JSON . stringify ( envs ) ) ;
152
- console . log ( envMessage ) ;
148
+ envs [ env ] = process . env [ env ]
149
+ envMessage += index === inputs . envs . length - 1 ? env : env + ', '
150
+ } )
151
+ await writeFile ( join ( tmpdir ( ) , 'env.json' ) , JSON . stringify ( envs ) )
152
+ console . log ( envMessage )
153
153
}
154
154
155
155
await deployActions ( {
@@ -158,36 +158,36 @@ module.exports = {
158
158
functionsDir : functionsBuildDir ,
159
159
timeout : inputs . timeout , // Default is 6 seconds
160
160
memory : inputs . memory // Default is 256MB (max for free tier)
161
- } ) ;
161
+ } )
162
162
} catch ( error ) {
163
- utils . build . failBuild ( 'Failed to deploy the functions' , { error} ) ;
163
+ utils . build . failBuild ( 'Failed to deploy the functions' , { error} )
164
164
}
165
165
}
166
166
} else {
167
167
console . log (
168
168
`Skipping the deployment to Nimbella as the context (${ process . env . CONTEXT } ) is not production.`
169
- ) ;
169
+ )
170
170
}
171
171
172
- const redirectRules = [ ] ;
173
- const redirects = [ ] ;
174
- const redirectsFile = join ( constants . PUBLISH_DIR , '_redirects' ) ;
172
+ const redirectRules = [ ]
173
+ const redirects = [ ]
174
+ const redirectsFile = join ( constants . PUBLISH_DIR , '_redirects' )
175
175
176
176
if ( isActions ) {
177
177
if ( existsSync ( redirectsFile ) ) {
178
178
console . log (
179
179
"Found _redirects file. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
180
- ) ;
181
- const { parseRedirectsFormat} = require ( 'netlify-redirect-parser' ) ;
182
- const { success} = await parseRedirectsFormat ( redirectsFile ) ;
183
- redirects . push ( ...success ) ;
180
+ )
181
+ const { parseRedirectsFormat} = require ( 'netlify-redirect-parser' )
182
+ const { success} = await parseRedirectsFormat ( redirectsFile )
183
+ redirects . push ( ...success )
184
184
}
185
185
186
186
if ( netlifyToml . redirects ) {
187
187
console . log (
188
188
"Found redirect rules in netlify.toml. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
189
- ) ;
190
- redirects . push ( ...netlifyToml . redirects ) ;
189
+ )
190
+ redirects . push ( ...netlifyToml . redirects )
191
191
}
192
192
193
193
for ( const redirect of redirects ) {
@@ -196,38 +196,38 @@ module.exports = {
196
196
redirect . to &&
197
197
redirect . to . startsWith ( '/.netlify/functions/' )
198
198
) {
199
- const redirectPath = redirect . to . split ( '/.netlify/functions/' ) [ 1 ] ;
199
+ const redirectPath = redirect . to . split ( '/.netlify/functions/' ) [ 1 ]
200
200
redirectRules . push (
201
201
`${ redirect . from } https://apigcp.nimbella.io/api/v1/web/${ namespace } /default/${ redirectPath } 200!`
202
- ) ;
202
+ )
203
203
}
204
204
}
205
205
}
206
206
207
- let { path : redirectPath } = inputs ;
207
+ let { path : redirectPath } = inputs
208
208
redirectPath = redirectPath . endsWith ( '/' )
209
209
? redirectPath
210
- : redirectPath + '/' ;
210
+ : redirectPath + '/'
211
211
212
212
if ( isProject ) {
213
213
redirectRules . push (
214
214
`${ redirectPath } * https://apigcp.nimbella.io/api/v1/web/${ namespace } /:splat 200!`
215
- ) ;
215
+ )
216
216
}
217
217
218
218
if ( isActions && ! isProject ) {
219
219
redirectRules . push (
220
220
`${ redirectPath } * https://apigcp.nimbella.io/api/v1/web/${ namespace } /default/:splat 200!`
221
- ) ;
221
+ )
222
222
}
223
223
224
224
if ( redirectRules . length > 0 ) {
225
225
if ( ! existsSync ( constants . PUBLISH_DIR ) ) {
226
- const mkdir = require ( 'make-dir' ) ;
227
- await mkdir ( constants . PUBLISH_DIR ) ;
226
+ const mkdir = require ( 'make-dir' )
227
+ await mkdir ( constants . PUBLISH_DIR )
228
228
}
229
229
230
- await appendFile ( redirectsFile , redirectRules . join ( '\n' ) ) ;
230
+ await appendFile ( redirectsFile , redirectRules . join ( '\n' ) )
231
231
}
232
232
}
233
- } ;
233
+ }
0 commit comments