-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command-env): add migrate subcommand (#3876)
* feat: add env transfer subcommand * feat: add test for env transfer * chore: update docs * chore: fix format * chore: fix test * refactor: user options instead of arguments, add error handling * fix: update docs * test: fix tests in CI * feat: rename to migrate Co-authored-by: erezrokah <erezrokah@users.noreply.github.com>
- Loading branch information
1 parent
a8ae1c5
commit 290f9a8
Showing
9 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// @ts-check | ||
|
||
const { isEmpty } = require('lodash') | ||
|
||
const { chalk, error: logError, log } = require('../../utils') | ||
|
||
const safeGetSite = async (api, siteId) => { | ||
try { | ||
const data = await api.getSite({ siteId }) | ||
return { data } | ||
} catch (error) { | ||
return { error } | ||
} | ||
} | ||
|
||
/** | ||
* The env:migrate command | ||
* @param {string} siteIdA Site (From) | ||
* @param {string} siteIdB Site (To) | ||
* @param {import('commander').OptionValues} options | ||
* @param {import('../base-command').BaseCommand} command | ||
* @returns {Promise<boolean>} | ||
*/ | ||
const envMigrate = async (options, command) => { | ||
const { api, site } = command.netlify | ||
|
||
if (!site.id && !options.from) { | ||
log( | ||
'Please include the source site Id as the `--from` option, or run `netlify link` to link this folder to a Netlify site', | ||
) | ||
return false | ||
} | ||
|
||
const siteId = { | ||
from: options.from || site.id, | ||
to: options.to, | ||
} | ||
|
||
const [{ data: siteFrom, error: errorFrom }, { data: siteTo, error: errorTo }] = await Promise.all([ | ||
safeGetSite(api, siteId.from), | ||
safeGetSite(api, siteId.to), | ||
]) | ||
|
||
if (errorFrom) { | ||
logError(`Can't find site with id ${chalk.bold(siteId.from)}. Please make sure the site exists.`) | ||
return false | ||
} | ||
|
||
if (errorTo) { | ||
logError(`Can't find site with id ${chalk.bold(siteId.to)}. Please make sure the site exists.`) | ||
return false | ||
} | ||
|
||
const [ | ||
{ | ||
build_settings: { env: envFrom = {} }, | ||
}, | ||
{ | ||
build_settings: { env: envTo = {} }, | ||
}, | ||
] = [siteFrom, siteTo] | ||
|
||
if (isEmpty(envFrom)) { | ||
log(`${chalk.greenBright(siteFrom.name)} has no environment variables, nothing to migrate`) | ||
return false | ||
} | ||
|
||
// Merge from site A to site B | ||
const mergedEnv = { | ||
...envTo, | ||
...envFrom, | ||
} | ||
|
||
// Apply environment variable updates | ||
await api.updateSite({ | ||
siteId: siteId.to, | ||
body: { | ||
build_settings: { | ||
env: mergedEnv, | ||
}, | ||
}, | ||
}) | ||
|
||
log( | ||
`Successfully migrated environment variables from ${chalk.greenBright(siteFrom.name)} to ${chalk.greenBright( | ||
siteTo.name, | ||
)}`, | ||
) | ||
} | ||
|
||
/** | ||
* Creates the `netlify env:migrate` command | ||
* @param {import('../base-command').BaseCommand} program | ||
* @returns | ||
*/ | ||
const createEnvMigrateCommand = (program) => | ||
program | ||
.command('env:migrate') | ||
.option('-f, --from <from>', 'Site ID (From)') | ||
.requiredOption('-t, --to <to>', 'Site ID (To)') | ||
.description(`Migrate environment variables from one site to another`) | ||
.addExamples([ | ||
'netlify env:migrate --to <to-site-id>', | ||
'netlify env:migrate --to <to-site-id> --from <from-site-id>', | ||
]) | ||
.action(envMigrate) | ||
|
||
module.exports = { createEnvMigrateCommand } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
290f9a8
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.
📊 Benchmark results
Package size: 359 MB