Skip to content

Commit 7db9ce5

Browse files
authored
fix(command-deploy): handle deploy error when there are no files to deploy (#1274)
1 parent 0aec789 commit 7db9ce5

File tree

6 files changed

+37
-14
lines changed

6 files changed

+37
-14
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
"make-dir": "^3.0.0",
123123
"minimist": "^1.2.5",
124124
"multiparty": "^4.2.1",
125-
"netlify": "^4.3.10",
125+
"netlify": "^4.5.2",
126126
"netlify-redirect-parser": "^2.5.0",
127127
"netlify-redirector": "^0.2.0",
128128
"node-fetch": "^2.6.0",

src/commands/deploy.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const { getBuildOptions, runBuild } = require('../lib/build')
1616
const LinkCommand = require('./link')
1717
const { NETLIFYDEV, NETLIFYDEVLOG, NETLIFYDEVERR } = require('../utils/logo')
1818
const { statAsync } = require('../lib/fs')
19+
const { cancelDeploy } = require('../lib/api')
1920
const { deployEdgeHandlers } = require('../utils/edge-handlers')
2021

2122
const DEFAULT_DEPLOY_TIMEOUT = 1.2e6
@@ -174,6 +175,7 @@ const runDeploy = async ({
174175
exit,
175176
}) => {
176177
let results
178+
let deployId
177179
try {
178180
if (deployToProduction) {
179181
if (isObject(siteData.published_deploy) && siteData.published_deploy.locked) {
@@ -198,7 +200,7 @@ const runDeploy = async ({
198200
const draft = !deployToProduction && !alias
199201
const title = flags.message
200202
results = await api.createSiteDeploy({ siteId, title, body: { draft, branch: alias } })
201-
const deployId = results.id
203+
deployId = results.id
202204

203205
const silent = flags.json || flags.silent
204206
await deployEdgeHandlers({
@@ -220,6 +222,9 @@ const runDeploy = async ({
220222
filter: getDeployFilesFilter({ site, deployFolder }),
221223
})
222224
} catch (e) {
225+
if (deployId) {
226+
await cancelDeploy({ api, deployId, warn })
227+
}
223228
switch (true) {
224229
case e.name === 'JSONHTTPError': {
225230
warn(`JSONHTTPError: ${e.json.message} ${e.status}`)

src/lib/api.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// This file should be used to wrap API methods that are not part of our open API spec yet
2-
// Once they become part of the spec, js-client should be used
31
const fetch = require('node-fetch')
42

53
const getHeaders = ({ token }) => {
@@ -47,6 +45,7 @@ const apiPost = async ({ api, path, data }) => {
4745
}
4846

4947
const uploadEdgeHandlers = async ({ api, deployId, bundleBuffer, manifest }) => {
48+
// TODO: use open-api spec via api when it is exposed
5049
const response = await apiPost({ api, path: `deploys/${deployId}/edge_handlers`, data: manifest })
5150
const { error, exists, upload_url: uploadUrl } = await response.json()
5251
if (error) {
@@ -75,4 +74,12 @@ const uploadEdgeHandlers = async ({ api, deployId, bundleBuffer, manifest }) =>
7574
return true
7675
}
7776

78-
module.exports = { uploadEdgeHandlers }
77+
const cancelDeploy = async ({ api, deployId, warn }) => {
78+
try {
79+
await api.cancelSiteDeploy({ deploy_id: deployId })
80+
} catch (e) {
81+
warn(`Failed canceling deploy with id ${deployId}: ${e.message}`)
82+
}
83+
}
84+
85+
module.exports = { uploadEdgeHandlers, cancelDeploy }

src/utils/edge-handlers.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const path = require('path')
22
const { statAsync, readFileAsyncCatchError } = require('../lib/fs')
3-
const { uploadEdgeHandlers } = require('../lib/api')
3+
const { uploadEdgeHandlers, cancelDeploy } = require('../lib/api')
44
const { startSpinner, stopSpinner } = require('../lib/spinner')
55

66
const MANIFEST_FILENAME = 'manifest.json'
@@ -74,11 +74,7 @@ const deployEdgeHandlers = async ({ site, deployId, api, silent, error, warn })
7474
} catch (e) {
7575
const text = `Failed deploying Edge Handlers: ${e.message}`
7676
stopSpinner({ spinner, text, error: true })
77-
try {
78-
await api.cancelSiteDeploy({ deploy_id: deployId })
79-
} catch (e) {
80-
warn(`Failed canceling deploy with id ${deployId}: ${e.message}`)
81-
}
77+
await cancelDeploy({ api, deployId, warn })
8278
// no need to report the error again
8379
error('')
8480
}

tests/command.deploy.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,21 @@ if (process.env.IS_FORK !== 'true') {
291291
})
292292
})
293293

294+
test('should exit with error when deploying an empty directory', async t => {
295+
await withSiteBuilder('site-with-an-empty-directory', async builder => {
296+
await builder.buildAsync()
297+
298+
try {
299+
await callCli(['deploy', '--dir', '.'], {
300+
cwd: builder.directory,
301+
env: { NETLIFY_SITE_ID: t.context.siteId },
302+
})
303+
} catch (e) {
304+
t.is(e.stderr.includes('Error: No files or functions to deploy'), true)
305+
}
306+
})
307+
})
308+
294309
test.after('cleanup', async t => {
295310
const { siteId } = t.context
296311
console.log(`deleting test site "${siteName}". ${siteId}`)

0 commit comments

Comments
 (0)