Skip to content
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

refactor: cleanup createSiteAddon function #1336

Merged
merged 2 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
57 changes: 27 additions & 30 deletions src/commands/functions/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ async function scaffoldFromTemplate(flags, args, functionsDir) {
spinner.succeed(`installed dependencies for ${name}`)
}

installAddons.call(this, addons, path.resolve(functionPath))
await installAddons.call(this, addons, path.resolve(functionPath))
if (onComplete) {
await addEnvVariables(this.netlify.api, this.netlify.site)
await onComplete.call(this)
Expand All @@ -336,7 +336,7 @@ async function scaffoldFromTemplate(flags, args, functionsDir) {
}
}

function installAddons(addons = [], fnPath) {
async function installAddons(addons = [], fnPath) {
if (addons.length > 0) {
const { api, site } = this.netlify
const siteId = site.id
Expand All @@ -346,36 +346,33 @@ function installAddons(addons = [], fnPath) {
}
this.log(`${NETLIFYDEVLOG} checking Netlify APIs...`)

return api.getSite({ siteId }).then(siteData => {
const accessToken = api.accessToken
const arr = addons.map(({ addonName, addonDidInstall }) => {
this.log(`${NETLIFYDEVLOG} installing addon: ` + chalk.yellow.inverse(addonName))
// will prompt for configs if not supplied - we do not yet allow for addon configs supplied by `netlify functions:create` command and may never do so
return createSiteAddon(accessToken, addonName, siteId, siteData, this.log)
.then(async addonCreateMsg => {
if (addonCreateMsg) {
// spinner.success("installed addon: " + addonName);
if (addonDidInstall) {
const { addEnvVariables } = require('../../utils/dev')
await addEnvVariables(api, site)
const { confirmPostInstall } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirmPostInstall',
message: `This template has an optional setup script that runs after addon install. This can be helpful for first time users to try out templates. Run the script?`,
default: false,
},
])
if (confirmPostInstall) addonDidInstall(fnPath)
}
const siteData = await api.getSite({ siteId })
const arr = addons.map(async ({ addonName, addonDidInstall }) => {
this.log(`${NETLIFYDEVLOG} installing addon: ` + chalk.yellow.inverse(addonName))
try {
const addonCreated = await createSiteAddon(api.accessToken, addonName, siteId, siteData, this.log)
if (addonCreated) {
if (addonDidInstall) {
const { addEnvVariables } = require('../../utils/dev')
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a good reason to dynamically load require() here (I think not)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely not

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm actually not sure why this is there. Looks it's only relevant when calling addonDidInstall a bit after and addEnvVariables is also called after installAddons.

await addEnvVariables(api, site)
const { confirmPostInstall } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirmPostInstall',
message: `This template has an optional setup script that runs after addon install. This can be helpful for first time users to try out templates. Run the script?`,
default: false,
},
])
if (confirmPostInstall) {
addonDidInstall(fnPath)
}
})
.catch(error => {
this.error(`${NETLIFYDEVERR} Error installing addon: `, error)
})
})
return Promise.all(arr)
}
}
} catch (error) {
this.error(`${NETLIFYDEVERR} Error installing addon: `, error)
}
})
return Promise.all(arr)
}
}

Expand Down
Loading