Skip to content

Commit 2be76ce

Browse files
authored
fix: try/catch individual statements where failure is likely (#43)
1 parent 9223630 commit 2be76ce

File tree

1 file changed

+96
-76
lines changed

1 file changed

+96
-76
lines changed

index.js

Lines changed: 96 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const {existsSync} = require('fs');
22
const {appendFile, readFile, readdir} = require('fs').promises;
33
const {join} = require('path');
4+
45
const toml = require('@iarna/toml');
56
const cpx = require('cpx');
67
const build = require('netlify-lambda/lib/build');
78

89
const functionsBuildDir = `functions-build-${Date.now()}`;
10+
const nimConfig = join(require('os').homedir(), '.nimbella');
911
let netlifyToml = {};
1012
let isProject = false;
1113
let isActions = false;
@@ -54,39 +56,51 @@ async function deployActions({run, functionsDir, timeout, memory}) {
5456
module.exports = {
5557
// Execute before build starts.
5658
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) {
5979
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}
6182
);
6283
}
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 {
7386
await utils.run.command(
7487
`nim auth login ${process.env.NIMBELLA_LOGIN_TOKEN}`
7588
);
76-
7789
// Cache the nimbella config to avoid logging in for consecutive builds.
7890
await utils.cache.save(nimConfig);
91+
} catch (error) {
92+
utils.build.failBuild('Failed to login using the provided token', {
93+
error
94+
});
7995
}
96+
}
8097

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));
89100
}
101+
102+
isActions = inputs.functions ? existsSync(inputs.functions) : false;
103+
isProject = existsSync('packages');
90104
},
91105
// Build the functions
92106
onBuild: async ({utils, inputs}) => {
@@ -99,88 +113,94 @@ module.exports = {
99113
cpx.copy(inputs.functions + '/**/*.!(js)', functionsBuildDir);
100114
}
101115
} catch (error) {
102-
utils.build.failBuild(error.message);
116+
utils.build.failBuild('Failed to build the functions', {error});
103117
}
104118
},
105119
// Execute after build is done.
106120
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`);
109122

110-
if (process.env.CONTEXT === 'production') {
111-
if (isProject) {
123+
if (process.env.CONTEXT === 'production') {
124+
if (isProject) {
125+
try {
112126
await deployProject(utils.run);
127+
} catch (error) {
128+
utils.build.failBuild('Failed to deploy the project', {error});
113129
}
130+
}
114131

115-
if (isActions) {
116-
console.log('\n------------------Functions------------------\n');
132+
if (isActions) {
133+
console.log('\n------------------Functions------------------\n');
134+
try {
117135
await deployActions({
118136
run: utils.run,
119137
functionsDir: functionsBuildDir,
120138
timeout: inputs.timeout, // Default is 6 seconds
121139
memory: inputs.memory // Default is 256MB (max for free tier)
122140
});
141+
} catch (error) {
142+
utils.build.failBuild('Failed to deploy the functions', {error});
123143
}
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)) {
125157
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/*'."
127159
);
160+
const {parseRedirectsFormat} = require('netlify-redirect-parser');
161+
const {success} = await parseRedirectsFormat(redirectsFile);
162+
redirects.push(...success);
128163
}
129164

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+
}
143171

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!`
147180
);
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-
}
161181
}
162182
}
183+
}
163184

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 + '/';
168189

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+
}
174195

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+
}
180201

202+
if (redirectRules.length > 0) {
181203
await appendFile(redirectsFile, redirectRules.join('\n'));
182-
} catch (error) {
183-
utils.build.failBuild(error.message);
184204
}
185205
}
186206
};

0 commit comments

Comments
 (0)