Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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) {
Copy link
Contributor

@ehmicky ehmicky Sep 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[sand] Should this be put inside an additional try/catch inner block around deployEdgeHandlers() + api.deploy() instead? Like this, there would be no need to check if deployId exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would mean we need to re-throw the error after canceling the deploy to reach the other part of the error handling code (we could also refactor that part when we do netlify/js-client#157 to have a dedicated try/catch per operation to have better error reporting).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to merge and release. We can discuss this in netlify/js-client#157

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 })
Copy link
Contributor Author

@erezrokah erezrokah Sep 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can throw an error and let the calling function cancel the deploy, but we would need lift the spinner handling too.

// no need to report the error again
error('')
}
Expand Down
15 changes: 15 additions & 0 deletions tests/command.deploy.test.js
Original file line number Diff line number Diff line change
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