-
Notifications
You must be signed in to change notification settings - Fork 402
refactor: extract deploy command logic into separate functions #1240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -16,20 +16,263 @@ const SitesCreateCommand = require('./sites/create') | |||
const LinkCommand = require('./link') | |||
const { NETLIFYDEV, NETLIFYDEVLOG, NETLIFYDEVERR } = require('../utils/logo') | |||
|
|||
const triggerDeploy = async ({ api, siteId, siteData, log, error }) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted from
Line 89 in 39da808
try { |
} | ||
|
||
const getDeployFolder = async ({ flags, config, site, siteData, log }) => { | ||
let deployFolder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return deployFolder | ||
} | ||
|
||
const validateDeployFolder = async ({ deployFolder, error }) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old version was:
Line 463 in 39da808
function ensureDirectory(resolvedDeployPath, error) { |
Which was broken in multiple places:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂️
return stat | ||
} | ||
|
||
const getFunctionsFolder = ({ flags, config, site, siteData }) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted from
Line 114 in 39da808
let functionsFolder |
return functionsFolder | ||
} | ||
|
||
const validateFunctionsFolder = async ({ functionsFolder, log, error }) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const validateFolders = async ({ deployFolder, functionsFolder, error, log }) => { | ||
const deployFolderStat = await validateDeployFolder({ deployFolder, error }) | ||
const functionsFolderStat = await validateFunctionsFolder({ functionsFolder, error, log }) | ||
return { deployFolderStat, functionsFolderStat } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need the return value of validateFunctionsFolder
. If it's undefined (since the folder doesn't exist) we don't pass functionsFolder
to the js-client
.
This was done here:
Line 167 in 39da808
functionsFolder = undefined |
return { deployFolderStat, functionsFolderStat } | ||
} | ||
|
||
const runDeploy = async ({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted from here:
Line 181 in 39da808
let results |
const deployUrl = get(results, 'deploy.deploy_ssl_url') || get(results, 'deploy.deploy_url') | ||
const logsUrl = `${get(results, 'deploy.admin_url')}/deploys/${get(results, 'deploy.id')}` | ||
|
||
return { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | ||
} | ||
|
||
const printResults = ({ flags, results, deployToProduction, log, logJson, exit }) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted from
Line 245 in 39da808
const msgData = { |
branch: alias, | ||
log( | ||
prettyjson.render({ | ||
'Deploy path': deployFolder, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | ||
|
||
const deployToProduction = flags.prod | ||
if (deployToProduction && alias) { | ||
this.error(`--prod and --alias flags cannot be used at the same time`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced this with exclusive: ['alias', 'branch']
on the prod
flag: https://github.com/netlify/cli/pull/1240/files#diff-e9001bf37e31e33db8a717da3ac59a69R474
} | ||
|
||
const validateFolders = async ({ deployFolder, functionsFolder, error, log }) => { | ||
const deployFolderStat = await validateDeployFolder({ deployFolder, error }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we run those two async
functions in parallel (Promise.all()
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validateFunctionsFolder
prints a warning message when the folder doesn't exist, while validateDeployFolder
exits.
Didn't want to run into a case where we print a warning then exit with another error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense 👍
This is micro-optimization anyway since those I/O calls are quite fast.
src/commands/deploy.js
Outdated
statusCb: flags.json || flags.silent ? () => {} : deployProgressCb(), | ||
draft: !deployToProduction && !alias, | ||
message: flags.message, | ||
deployTimeout: flags.timeout * 1000 || 1.2e6, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we extract that 1.2e6
into a top-level constant variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure going to update it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @erezrokah, this is much cleaner! 🎉
- Summary
The PR refactors the
deploy
command code by extracting most of the logic into functions.Some logic mutates objects so I didn't extract it yet since I wanted the functions to be pure.
- Test plan
Tested manually the different deploy flags, and actually exposed old bugs in the process (which I fixed).
- Description for the changelog
Should not change behaviour
- A picture of a cute animal (not mandatory but encouraged)
🐼
Related to #1236 and #1227 - I initially started looking into those issues and found the code very hard to follow.