Skip to content

Commit 25b4275

Browse files
committed
Refactoring. Only generate redirects if actually deploying project.
1 parent 7345d34 commit 25b4275

File tree

1 file changed

+99
-85
lines changed

1 file changed

+99
-85
lines changed

src/index.js

Lines changed: 99 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ let isActions = false
1616
// Disable auto updates of nim.
1717
process.env.NIM_DISABLE_AUTOUPDATE = '1'
1818

19+
/**
20+
* Retrieves API host for current auth.
21+
* Assumes there is a valid auth already.
22+
*/
23+
async function getApiHost(run) {
24+
const {stdout: apihost} = await run.command(`nim auth current --apihost`)
25+
return apihost.replace(/^(https:\/\/)/, '')
26+
}
27+
1928
/**
2029
* Deploy a Nimbella Project.
2130
* @param {*} run - function provided under utils by Netlify to build event functions.
@@ -62,6 +71,75 @@ async function deployActions({run, functionsDir, timeout, memory, envsFile}) {
6271
)
6372
}
6473

74+
// Creates or updates the _redirects file; this file takes precedence
75+
// over other redirect declarations, and is processed from top to bottom
76+
//
77+
// First: if we deployed netlify functions to nimbella, scan the redirects
78+
// for matching rewrites with /.netlify/functions/* as their target
79+
// and remap them to their nimbella api end points.
80+
//
81+
// Second: if there is an API path directive input.api, add a matching rule
82+
// to the _redirects file. The target is either the Nimbella namespace/:splat
83+
// or namespace/default/:splat. The latter is used when deploying Netlify functions
84+
// to Nimbella.
85+
//
86+
// The first set of rewrites is pre-pended to the _redirects file, then the second
87+
// set, if any.
88+
async function processRedirects(redirectsFile, run, inputs, namespace) {
89+
const redirectRules = []
90+
const redirects = []
91+
const apihost = isActions || isProject ? await getApiHost(run) : undefined
92+
93+
if (isActions) {
94+
if (existsSync(redirectsFile)) {
95+
console.log(
96+
"Found _redirects file. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
97+
)
98+
const {success} = await parseRedirectsFormat(redirectsFile)
99+
redirects.push(...success)
100+
}
101+
102+
if (netlifyToml.redirects) {
103+
console.log(
104+
"Found redirect rules in netlify.toml. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
105+
)
106+
redirects.push(...netlifyToml.redirects)
107+
}
108+
109+
for (const redirect of redirects) {
110+
if (
111+
redirect.status === 200 &&
112+
redirect.to &&
113+
redirect.to.startsWith('/.netlify/functions/')
114+
) {
115+
const redirectPath = redirect.to.split('/.netlify/functions/')[1]
116+
redirectRules.push(
117+
`${
118+
redirect.from || redirect.path
119+
} https://${apihost}/api/v1/web/${namespace}/default/${redirectPath} 200!`
120+
)
121+
}
122+
}
123+
}
124+
125+
let {path: redirectPath} = inputs
126+
redirectPath = redirectPath.endsWith('/') ? redirectPath : redirectPath + '/'
127+
128+
if (isProject) {
129+
redirectRules.push(
130+
`${redirectPath}* https://${apihost}/api/v1/web/${namespace}/:splat 200!`
131+
)
132+
}
133+
134+
if (isActions && !isProject) {
135+
redirectRules.push(
136+
`${redirectPath}* https://${apihost}/api/v1/web/${namespace}/default/:splat 200!`
137+
)
138+
}
139+
140+
return redirectRules
141+
}
142+
65143
module.exports = {
66144
// Execute before build starts.
67145
onPreBuild: async ({utils, constants, inputs}) => {
@@ -174,96 +252,32 @@ module.exports = {
174252
utils.build.failBuild('Failed to deploy the functions', {error})
175253
}
176254
}
177-
} else {
178-
console.log(
179-
`Skipping the deployment to Nimbella as the context (${process.env.CONTEXT}) is not production.`
180-
)
181-
}
182-
183-
const redirectRules = []
184-
const redirects = []
185-
const redirectsFile = join(constants.PUBLISH_DIR, '_redirects')
186-
let {stdout: apihost} = await utils.run.command(
187-
`nim auth current --apihost`
188-
)
189-
apihost = apihost.replace(/^(https:\/\/)/, '')
190-
191-
// Creates or updates the _redirects file; this file takes precedence
192-
// over other redirect declarations, and is processed from top to bottom
193-
//
194-
// First: if we deployed netlify functions to nimbella, scan the redirects
195-
// for matching rewrites with /.netlify/functions/* as their target
196-
// and remap them to their nimbella api end points.
197-
//
198-
// Second: if there is an API path directive input.api, add a matching rule
199-
// to the _redirects file. The target is either the Nimbella namespace/:splat
200-
// or namespace/default/:splat. The latter is used when deploying Netlify functions
201-
// to Nimbella.
202-
//
203-
// The first set of rewrites is pre-pended to the _redirects file, then the second
204-
// set, if any.
205-
206-
if (isActions) {
207-
if (existsSync(redirectsFile)) {
208-
console.log(
209-
"Found _redirects file. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
210-
)
211-
const {success} = await parseRedirectsFormat(redirectsFile)
212-
redirects.push(...success)
213-
}
214-
215-
if (netlifyToml.redirects) {
216-
console.log(
217-
"Found redirect rules in netlify.toml. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
218-
)
219-
redirects.push(...netlifyToml.redirects)
220-
}
221-
222-
for (const redirect of redirects) {
223-
if (
224-
redirect.status === 200 &&
225-
redirect.to &&
226-
redirect.to.startsWith('/.netlify/functions/')
227-
) {
228-
const redirectPath = redirect.to.split('/.netlify/functions/')[1]
229-
redirectRules.push(
230-
`${
231-
redirect.from || redirect.path
232-
} https://${apihost}/api/v1/web/${namespace}/default/${redirectPath} 200!`
233-
)
234-
}
235-
}
236-
}
237255

238-
let {path: redirectPath} = inputs
239-
redirectPath = redirectPath.endsWith('/')
240-
? redirectPath
241-
: redirectPath + '/'
242-
243-
if (isProject) {
244-
redirectRules.push(
245-
`${redirectPath}* https://${apihost}/api/v1/web/${namespace}/:splat 200!`
256+
const redirectsFile = join(constants.PUBLISH_DIR, '_redirects')
257+
const redirectRules = await processRedirects(
258+
redirectsFile,
259+
utils.run,
260+
inputs,
261+
namespace
246262
)
247-
}
248263

249-
if (isActions && !isProject) {
250-
redirectRules.push(
251-
`${redirectPath}* https://${apihost}/api/v1/web/${namespace}/default/:splat 200!`
252-
)
253-
}
264+
if (redirectRules.length > 0) {
265+
let content = ''
266+
if (existsSync(redirectsFile)) {
267+
content = await readFile(redirectsFile)
268+
} else if (!existsSync(constants.PUBLISH_DIR)) {
269+
const mkdir = require('make-dir')
270+
await mkdir(constants.PUBLISH_DIR)
271+
}
254272

255-
if (redirectRules.length > 0) {
256-
let content = ''
257-
if (existsSync(redirectsFile)) {
258-
content = await readFile(redirectsFile)
259-
} else if (!existsSync(constants.PUBLISH_DIR)) {
260-
const mkdir = require('make-dir')
261-
await mkdir(constants.PUBLISH_DIR)
273+
// The rewrites take precedence
274+
await writeFile(redirectsFile, redirectRules.join('\n') + '\n')
275+
await appendFile(redirectsFile, content)
262276
}
263-
264-
// The rewrites take precedence
265-
await writeFile(redirectsFile, redirectRules.join('\n') + '\n')
266-
await appendFile(redirectsFile, content)
277+
} else {
278+
console.log(
279+
`Skipping the deployment to Nimbella as the context (${process.env.CONTEXT}) is not production.`
280+
)
267281
}
268282
}
269283
}

0 commit comments

Comments
 (0)