Skip to content

Commit

Permalink
fix(command-deploy): handle deploy error when there are no files to d…
Browse files Browse the repository at this point in the history
…eploy (#1274)
  • Loading branch information
erezrokah committed Sep 24, 2020
1 parent 0aec789 commit 7db9ce5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -122,7 +122,7 @@
"make-dir": "^3.0.0",
"minimist": "^1.2.5",
"multiparty": "^4.2.1",
"netlify": "^4.3.10",
"netlify": "^4.5.2",
"netlify-redirect-parser": "^2.5.0",
"netlify-redirector": "^0.2.0",
"node-fetch": "^2.6.0",
Expand Down
7 changes: 6 additions & 1 deletion src/commands/deploy.js
Expand Up @@ -16,6 +16,7 @@ const { getBuildOptions, runBuild } = require('../lib/build')
const LinkCommand = require('./link')
const { NETLIFYDEV, NETLIFYDEVLOG, NETLIFYDEVERR } = require('../utils/logo')
const { statAsync } = require('../lib/fs')
const { cancelDeploy } = require('../lib/api')
const { deployEdgeHandlers } = require('../utils/edge-handlers')

const DEFAULT_DEPLOY_TIMEOUT = 1.2e6
Expand Down Expand Up @@ -174,6 +175,7 @@ const runDeploy = async ({
exit,
}) => {
let results
let deployId
try {
if (deployToProduction) {
if (isObject(siteData.published_deploy) && siteData.published_deploy.locked) {
Expand All @@ -198,7 +200,7 @@ const runDeploy = async ({
const draft = !deployToProduction && !alias
const title = flags.message
results = await api.createSiteDeploy({ siteId, title, body: { draft, branch: alias } })
const deployId = results.id
deployId = results.id

const silent = flags.json || flags.silent
await deployEdgeHandlers({
Expand All @@ -220,6 +222,9 @@ const runDeploy = async ({
filter: getDeployFilesFilter({ site, deployFolder }),
})
} catch (e) {
if (deployId) {
await cancelDeploy({ api, deployId, warn })
}
switch (true) {
case e.name === 'JSONHTTPError': {
warn(`JSONHTTPError: ${e.json.message} ${e.status}`)
Expand Down
13 changes: 10 additions & 3 deletions src/lib/api.js
@@ -1,5 +1,3 @@
// This file should be used to wrap API methods that are not part of our open API spec yet
// Once they become part of the spec, js-client should be used
const fetch = require('node-fetch')

const getHeaders = ({ token }) => {
Expand Down Expand Up @@ -47,6 +45,7 @@ const apiPost = async ({ api, path, data }) => {
}

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

module.exports = { uploadEdgeHandlers }
const cancelDeploy = async ({ api, deployId, warn }) => {
try {
await api.cancelSiteDeploy({ deploy_id: deployId })
} catch (e) {
warn(`Failed canceling deploy with id ${deployId}: ${e.message}`)
}
}

module.exports = { uploadEdgeHandlers, cancelDeploy }
8 changes: 2 additions & 6 deletions src/utils/edge-handlers.js
@@ -1,6 +1,6 @@
const path = require('path')
const { statAsync, readFileAsyncCatchError } = require('../lib/fs')
const { uploadEdgeHandlers } = require('../lib/api')
const { uploadEdgeHandlers, cancelDeploy } = require('../lib/api')
const { startSpinner, stopSpinner } = require('../lib/spinner')

const MANIFEST_FILENAME = 'manifest.json'
Expand Down Expand Up @@ -74,11 +74,7 @@ const deployEdgeHandlers = async ({ site, deployId, api, silent, error, warn })
} catch (e) {
const text = `Failed deploying Edge Handlers: ${e.message}`
stopSpinner({ spinner, text, error: true })
try {
await api.cancelSiteDeploy({ deploy_id: deployId })
} catch (e) {
warn(`Failed canceling deploy with id ${deployId}: ${e.message}`)
}
await cancelDeploy({ api, deployId, warn })
// no need to report the error again
error('')
}
Expand Down
15 changes: 15 additions & 0 deletions tests/command.deploy.test.js
Expand Up @@ -291,6 +291,21 @@ if (process.env.IS_FORK !== 'true') {
})
})

test('should exit with error when deploying an empty directory', async t => {
await withSiteBuilder('site-with-an-empty-directory', async builder => {
await builder.buildAsync()

try {
await callCli(['deploy', '--dir', '.'], {
cwd: builder.directory,
env: { NETLIFY_SITE_ID: t.context.siteId },
})
} catch (e) {
t.is(e.stderr.includes('Error: No files or functions to deploy'), true)
}
})
})

test.after('cleanup', async t => {
const { siteId } = t.context
console.log(`deleting test site "${siteName}". ${siteId}`)
Expand Down

0 comments on commit 7db9ce5

Please sign in to comment.