From 3a48664751144c82292a808123766652154e7fdb Mon Sep 17 00:00:00 2001 From: Johann Date: Thu, 27 Jun 2019 14:56:14 +0200 Subject: [PATCH] feat(context): Use middleware to fill context Added middleware function (using yargs middlewares) to build the context by checking if the management-token, space-id, environment-id and host are set in the .contentfulrc.json or passed into the command via flags. --- lib/cli.js | 18 +- lib/cmds/boilerplate.js | 14 +- lib/cmds/config_cmds/add.js | 7 +- lib/cmds/config_cmds/list.js | 10 +- lib/cmds/config_cmds/remove.js | 25 +- lib/cmds/content-type_cmds/get.js | 15 +- lib/cmds/content-type_cmds/list.js | 18 +- lib/cmds/extension_cmds/create.js | 18 +- lib/cmds/extension_cmds/delete.js | 20 +- lib/cmds/extension_cmds/get.js | 20 +- lib/cmds/extension_cmds/list.js | 16 +- lib/cmds/extension_cmds/update.js | 20 +- lib/cmds/guide.js | 10 +- lib/cmds/login.js | 18 +- lib/cmds/logout.js | 6 +- .../space_cmds/accesstoken_cmds/create.js | 27 +- lib/cmds/space_cmds/accesstoken_cmds/list.js | 15 +- lib/cmds/space_cmds/create.js | 17 +- lib/cmds/space_cmds/delete.js | 18 +- .../space_cmds/environment_cmds/create.js | 25 +- .../space_cmds/environment_cmds/delete.js | 14 +- lib/cmds/space_cmds/environment_cmds/list.js | 12 +- lib/cmds/space_cmds/environment_cmds/use.js | 18 +- lib/cmds/space_cmds/export.js | 23 +- .../space_cmds/generate_cmds/migration.js | 26 +- lib/cmds/space_cmds/import.js | 23 +- lib/cmds/space_cmds/list.js | 12 +- lib/cmds/space_cmds/migration.js | 22 +- lib/cmds/space_cmds/seed.js | 29 +- lib/cmds/space_cmds/use.js | 23 +- lib/context.js | 2 +- lib/guide/step-create-space.js | 6 +- lib/guide/step-finish.js | 4 +- lib/guide/step-login.js | 7 +- lib/guide/step-seed.js | 7 +- lib/guide/step-setup.js | 13 +- lib/utils/assertions.js | 18 +- lib/utils/contentful-clients.js | 2 +- lib/utils/middlewares.js | 33 + lib/utils/normalizer.js | 9 - package-lock.json | 653 ++++++++++++++++-- package.json | 2 +- test/e2e/__snapshots__/basics.test.js.snap | 72 +- .../cmds/__snapshots__/main.test.js.snap | 72 +- test/integration/cmds/config/list.test.js | 4 +- .../__snapshots__/create.test.js.snap | 2 +- .../__snapshots__/delete.test.js.snap | 2 +- .../__snapshots__/update.test.js.snap | 2 +- .../space/__snapshots__/delete.test.js.snap | 2 +- .../space/__snapshots__/help.test.js.snap | 84 +-- test/integration/scripts/pre-test.js | 2 +- test/integration/util.js | 6 +- test/unit/cmds/boilerplate.test.js | 29 +- test/unit/cmds/config_cmds/add.test.js | 3 - test/unit/cmds/config_cmds/remove.test.js | 6 +- test/unit/cmds/content-type_cmds/list.test.js | 32 +- test/unit/cmds/extension_cmds/create.test.js | 99 ++- test/unit/cmds/extension_cmds/delete.test.js | 25 +- test/unit/cmds/extension_cmds/get.test.js | 11 +- test/unit/cmds/extension_cmds/list.test.js | 21 +- test/unit/cmds/extension_cmds/update.test.js | 46 +- test/unit/cmds/login.test.js | 15 +- test/unit/cmds/logout.test.js | 4 +- .../accesstoken_cmds/create.test.js | 20 +- test/unit/cmds/space_cmds/create.test.js | 27 +- .../environment_cmds/create.test.js | 23 +- .../environment_cmds/delete.test.js | 8 +- .../space_cmds/environment_cmds/list.test.js | 8 +- .../space_cmds/environment_cmds/use.test.js | 8 +- test/unit/cmds/space_cmds/export.test.js | 20 +- .../generate_cmds/migration.test.js | 11 +- test/unit/cmds/space_cmds/import.test.js | 17 +- test/unit/cmds/space_cmds/migration.test.js | 17 +- test/unit/cmds/space_cmds/use.test.js | 11 +- test/unit/context.test.js | 2 +- test/unit/guide/step-create-space.test.js | 9 +- test/unit/guide/step-login.test.js | 6 +- test/unit/guide/step-seed.test.js | 8 +- test/unit/guide/step-setup.test.js | 4 +- test/unit/utils/assertions.test.js | 4 +- test/unit/utils/middlewares.test.js | 79 +++ 81 files changed, 1371 insertions(+), 745 deletions(-) create mode 100644 lib/utils/middlewares.js delete mode 100644 lib/utils/normalizer.js create mode 100644 test/unit/utils/middlewares.test.js diff --git a/lib/cli.js b/lib/cli.js index b7f5887e2..154dea9f7 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -1,17 +1,14 @@ import yargs from 'yargs' import {log} from './utils/log' +import {buildContext} from './utils/middlewares' import {version} from '../package.json' -// Workaround to remove display of script name in help. See: -// * https://github.com/yargs/yargs/pull/1143 -// * https://github.com/yargs/yargs/issues/1065 -// * https://github.com/yargs/yargs/issues/1048 -function fixHelp (output) { - return output.replace(/( +)contentful\.js/g, '$1') -} - yargs.usage('\nUsage: contentful [args]') .commandDir('cmds') + .middleware([ + buildContext + ]) + .scriptName('') .demandCommand(4, 'Please specify a command.') .help('h') .alias('h', 'help') @@ -28,14 +25,11 @@ yargs.usage('\nUsage: contentful [args]') .epilog('Copyright 2018 Contentful, this is a BETA release') .fail(function (msg, err, yargs) { if (err) throw err - console.error(fixHelp(yargs.help())) + console.error(yargs.help()) console.error(msg) process.exit(1) }) .parse(process.argv.slice(2), (_, argv, output) => { - if (argv.help || argv.h) { - output = fixHelp(output) - } if (argv.version === true && !argv._.length) { log(version) } else { diff --git a/lib/cmds/boilerplate.js b/lib/cmds/boilerplate.js index 84258cce3..e202b6aac 100644 --- a/lib/cmds/boilerplate.js +++ b/lib/cmds/boilerplate.js @@ -9,7 +9,6 @@ import markdown from 'markdown-cli' import { handleAsyncError as handle } from '../utils/async' import { log, success } from '../utils/log' -import normalizer from '../utils/normalizer' import { successEmoji } from '../utils/emojis' import { frame } from '../utils/text' import { accessTokenCreate } from './space_cmds/accesstoken_cmds/create' @@ -46,9 +45,10 @@ async function getBoilerplates () { } export async function downloadBoilerplate (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) - const { spaceId } = await normalizer(argv) + const { context } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) + const { activeSpaceId } = context const boilerplatesResult = await getBoilerplates() @@ -78,7 +78,9 @@ export async function downloadBoilerplate (argv) { ctx.accessToken = await accessTokenCreate({ name: `Boilerplate CDA access token`, description: `This token was generated for the ${boilerplate.name}`, - spaceId, + context: { + activeSpaceId + }, silent: true }) } @@ -87,7 +89,7 @@ export async function downloadBoilerplate (argv) { title: `Downloading ${boilerplate.name} boilerplate`, task: async (ctx) => { const response = await axios({ - url: `https://tools.contentful.com/boilerplates/${boilerplate.sys.id}/download?space_id=${spaceId}&access_token=${ctx.accessToken.accessToken}`, + url: `https://tools.contentful.com/boilerplates/${boilerplate.sys.id}/download?space_id=${activeSpaceId}&access_token=${ctx.accessToken.accessToken}`, responseType: 'stream' }) ctx.data = response.data diff --git a/lib/cmds/config_cmds/add.js b/lib/cmds/config_cmds/add.js index 49fdadd41..a13af403b 100644 --- a/lib/cmds/config_cmds/add.js +++ b/lib/cmds/config_cmds/add.js @@ -1,6 +1,6 @@ import { isEmpty, pickBy } from 'lodash' -import { setContext, getContext, storeRuntimeConfig } from '../../context' +import { setContext, storeRuntimeConfig } from '../../context' import { handleAsyncError as handle } from '../../utils/async' import { InvalidConfigOptionsError } from '../../utils/error' import { successEmoji } from '../../utils/emojis.js' @@ -48,7 +48,6 @@ export const addHandler = async (argv) => { const opts = pickBy(argv, (value, key) => optionsToPick.includes(key) && value !== undefined) validateOptions(opts) const configs = transform(opts) - await getContext() setContext(configs) await storeRuntimeConfig() success(`${successEmoji} config added successfully`) @@ -69,10 +68,6 @@ function validateOptions (opts) { } function transform (opts) { - if (opts.managementToken) { - opts.cmaToken = opts.managementToken - delete opts.managementToken - } if (opts.proxy) { opts.proxy = proxyStringToObject(opts.proxy) } diff --git a/lib/cmds/config_cmds/list.js b/lib/cmds/config_cmds/list.js index 6fda7b3bf..a5d2216ff 100644 --- a/lib/cmds/config_cmds/list.js +++ b/lib/cmds/config_cmds/list.js @@ -1,5 +1,4 @@ import { handleAsyncError as handle } from '../../utils/async' -import { getContext } from '../../context' import emojic from 'emojic' import { frame } from '../../utils/text' import { success } from '../../utils/log' @@ -15,13 +14,12 @@ export const builder = (yargs) => { } export const aliases = ['ls'] -export const listHandler = async (argv) => { - const ctx = await getContext() - const configList = Object.keys(ctx).map((key) => { +export const listHandler = async ({context}) => { + const configList = Object.keys(context).map((key) => { if (key === 'proxy') { - return `${emojic.gear} ${key}: ${proxyObjectToString(ctx[key])}` + return `${emojic.gear} ${key}: ${proxyObjectToString(context[key])}` } else { - return `${emojic.gear} ${key}: ${ctx[key]}` + return `${emojic.gear} ${key}: ${context[key]}` } }) success(frame(configList.join('\n'))) diff --git a/lib/cmds/config_cmds/remove.js b/lib/cmds/config_cmds/remove.js index 47654fb0f..c18b4da3b 100644 --- a/lib/cmds/config_cmds/remove.js +++ b/lib/cmds/config_cmds/remove.js @@ -1,8 +1,7 @@ import { handleAsyncError as handle } from '../../utils/async' import { success } from '../../utils/log' import { successEmoji } from '../../utils/emojis.js' -import { setContext, getContext, emptyContext, storeRuntimeConfig } from '../../context' -import { pick } from 'lodash' +import { setContext, emptyContext, storeRuntimeConfig } from '../../context' export const command = 'remove' export const desc = 'Removes a config from ~/.contentfulrc.json' @@ -49,21 +48,19 @@ export const builder = (yargs) => { } export const removeHandler = async (argv) => { - let context = await getContext() - const optionsToPick = ['cmaToken', 'proxy', 'activeSpaceId'] - let options = pick(context, optionsToPick) + let options = {...argv.context} if (argv.all) { options = {} } else { - if (argv.managementToken) { - delete options['cmaToken'] - } - if (argv.activeSpaceId) { - delete options['activeSpaceId'] - } - if (argv.proxy) { - delete options['proxy'] - } + const contextKeys = [ + 'managementToken', + 'activeSpaceId', + 'activeEnvironmentId', + 'proxy', + 'rawProxy', + 'host' + ] + contextKeys.forEach(key => argv[key] && delete options[key]) } emptyContext() setContext(options) diff --git a/lib/cmds/content-type_cmds/get.js b/lib/cmds/content-type_cmds/get.js index ccad88ccb..45cedf560 100644 --- a/lib/cmds/content-type_cmds/get.js +++ b/lib/cmds/content-type_cmds/get.js @@ -1,6 +1,5 @@ import Table from 'cli-table3' -import { getContext } from '../../context' import { createManagementClient } from '../../utils/contentful-clients' import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions' import { log } from '../../utils/log' @@ -21,22 +20,20 @@ export const builder = (yargs) => { } async function ctShow (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const { context } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) const contentTypeId = getId(argv) - const { cmaToken, activeSpaceId, activeEnvironmentId } = await getContext() - const managementToken = argv.managementToken || cmaToken - const spaceId = argv.spaceId || activeSpaceId - const environmentId = argv.environmentId || activeEnvironmentId + const { managementToken, activeSpaceId, activeEnvironmentId } = context const client = await createManagementClient({ accessToken: managementToken, feature: 'content_type-get' }) - const space = await client.getSpace(spaceId) - const environment = await space.getEnvironment(environmentId) + const space = await client.getSpace(activeSpaceId) + const environment = await space.getEnvironment(activeEnvironmentId) const result = await environment.getContentType(contentTypeId) const { sys, name, displayField, fields } = result diff --git a/lib/cmds/content-type_cmds/list.js b/lib/cmds/content-type_cmds/list.js index b89e0b11c..e98c34e46 100644 --- a/lib/cmds/content-type_cmds/list.js +++ b/lib/cmds/content-type_cmds/list.js @@ -1,6 +1,5 @@ import Table from 'cli-table3' -import { getContext } from '../../context' import { createManagementClient } from '../../utils/contentful-clients' import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions' import { handleAsyncError as handle } from '../../utils/async' @@ -31,24 +30,21 @@ export const builder = yargs => { .epilog('Copyright 2018 Contentful, this is a BETA release') } -async function ctList (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) +async function ctList ({context}) { + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - const { cmaToken, activeSpaceId, activeEnvironmentId } = await getContext() - const managementToken = argv.managementToken || cmaToken - const spaceId = argv.spaceId || activeSpaceId - const environmentId = argv.environmentId || activeEnvironmentId || 'master' + const { managementToken, activeSpaceId, activeEnvironmentId } = context const client = await createManagementClient({ accessToken: managementToken, feature: 'content_type-list' }) - const space = await client.getSpace(spaceId) - const environment = await space.getEnvironment(environmentId) + const space = await client.getSpace(activeSpaceId) + const environment = await space.getEnvironment(activeEnvironmentId) - log(highlightStyle(`Environment: "${environmentId}"`)) + log(highlightStyle(`Environment: "${activeEnvironmentId}"`)) const result = await paginate({ client: environment, diff --git a/lib/cmds/extension_cmds/create.js b/lib/cmds/extension_cmds/create.js index b43ad781b..24307050a 100644 --- a/lib/cmds/extension_cmds/create.js +++ b/lib/cmds/extension_cmds/create.js @@ -1,4 +1,3 @@ -import { getContext } from '../../context' import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions' import { handleAsyncError as handle } from '../../utils/async' import { createManagementClient } from '../../utils/contentful-clients' @@ -54,8 +53,9 @@ export async function createExtension (environment, data) { } export async function createExtensionHandler (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const { context } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) const data = await prepareData(argv) @@ -65,23 +65,21 @@ export async function createExtensionHandler (argv) { await readSrcDocFile(data.extension) } - const { cmaToken, activeSpaceId, activeEnvironmentId } = await getContext() - const spaceId = argv.spaceId || activeSpaceId - const environmentId = argv.environmentId || activeEnvironmentId || 'master' + const { managementToken, activeSpaceId, activeEnvironmentId } = context const client = await createManagementClient({ - accessToken: argv.managementToken || cmaToken, + accessToken: managementToken, feature: 'extension-create' }) - const space = await client.getSpace(spaceId) - const environment = await space.getEnvironment(environmentId) + const space = await client.getSpace(activeSpaceId) + const environment = await space.getEnvironment(activeEnvironmentId) const extension = await createExtension(environment, data) success(`${successEmoji} Successfully created extension:\n`) - logExtension(extension, spaceId, environmentId) + logExtension(extension, activeSpaceId, activeEnvironmentId) } export const handler = handle(createExtensionHandler) diff --git a/lib/cmds/extension_cmds/delete.js b/lib/cmds/extension_cmds/delete.js index a09a9bd9a..cb977d05c 100644 --- a/lib/cmds/extension_cmds/delete.js +++ b/lib/cmds/extension_cmds/delete.js @@ -1,4 +1,3 @@ -import { getContext } from '../../context' import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions' import { handleAsyncError as handle } from '../../utils/async' import { createManagementClient } from '../../utils/contentful-clients' @@ -29,27 +28,26 @@ export const builder = (yargs) => { } export async function deleteExtension (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const {context, id} = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - const { cmaToken, activeSpaceId, activeEnvironmentId } = await getContext() - const spaceId = argv.spaceId || activeSpaceId - const environmentId = argv.environmentId || activeEnvironmentId || 'master' + const { managementToken, activeSpaceId, activeEnvironmentId } = context const client = await createManagementClient({ - accessToken: argv.managementToken || cmaToken, + accessToken: managementToken, feature: 'extension-delete' }) - const space = await client.getSpace(spaceId) - const environment = await space.getEnvironment(environmentId) - const extension = await environment.getUiExtension(argv.id) + const space = await client.getSpace(activeSpaceId) + const environment = await space.getEnvironment(activeEnvironmentId) + const extension = await environment.getUiExtension(id) await assertForceOrCorrectVersionProvided(argv, extension.sys.version) await extension.delete() - success(`${successEmoji} Successfully deleted extension with ID ${argv.id}`) + success(`${successEmoji} Successfully deleted extension with ID ${id}`) } export const handler = handle(deleteExtension) diff --git a/lib/cmds/extension_cmds/get.js b/lib/cmds/extension_cmds/get.js index 4304dade2..666a5d65d 100644 --- a/lib/cmds/extension_cmds/get.js +++ b/lib/cmds/extension_cmds/get.js @@ -1,4 +1,3 @@ -import { getContext } from '../../context' import { createManagementClient } from '../../utils/contentful-clients' import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions' import { handleAsyncError as handle } from '../../utils/async' @@ -18,23 +17,22 @@ export const builder = (yargs) => { } async function getExtension (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const {context, id} = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - const { cmaToken, activeSpaceId, activeEnvironmentId } = await getContext() - const spaceId = argv.spaceId || activeSpaceId - const environmentId = argv.environmentId || activeEnvironmentId || 'master' + const { managementToken, activeSpaceId, activeEnvironmentId } = context const client = await createManagementClient({ - accessToken: argv.managementToken || cmaToken, + accessToken: managementToken, feature: 'extension-get' }) - const space = await client.getSpace(spaceId) - const environment = await space.getEnvironment(environmentId) - const extension = await environment.getUiExtension(argv.id) + const space = await client.getSpace(activeSpaceId) + const environment = await space.getEnvironment(activeEnvironmentId) + const extension = await environment.getUiExtension(id) - logExtension(extension, spaceId, environmentId) + logExtension(extension, activeSpaceId, activeEnvironmentId) } export const handler = handle(getExtension) diff --git a/lib/cmds/extension_cmds/list.js b/lib/cmds/extension_cmds/list.js index ef0f0ac07..5fc81833b 100644 --- a/lib/cmds/extension_cmds/list.js +++ b/lib/cmds/extension_cmds/list.js @@ -1,6 +1,5 @@ import Table from 'cli-table3' -import { getContext } from '../../context' import { createManagementClient } from '../../utils/contentful-clients' import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions' import { handleAsyncError as handle } from '../../utils/async' @@ -20,20 +19,19 @@ export const builder = (yargs) => { } export async function listExtensions (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const { context } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - const { cmaToken, activeSpaceId, activeEnvironmentId } = await getContext() - const spaceId = argv.spaceId || activeSpaceId - const environmentId = argv.environmentId || activeEnvironmentId || 'master' + const { managementToken, activeSpaceId, activeEnvironmentId } = context const client = await createManagementClient({ - accessToken: argv.managementToken || cmaToken, + accessToken: managementToken, feature: 'extension-list' }) - const space = await client.getSpace(spaceId) - const environment = await space.getEnvironment(environmentId) + const space = await client.getSpace(activeSpaceId) + const environment = await space.getEnvironment(activeEnvironmentId) const result = await paginate({ client: environment, diff --git a/lib/cmds/extension_cmds/update.js b/lib/cmds/extension_cmds/update.js index d2bb925c1..54d40ba3d 100644 --- a/lib/cmds/extension_cmds/update.js +++ b/lib/cmds/extension_cmds/update.js @@ -1,4 +1,3 @@ -import { getContext } from '../../context' import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions' import { handleAsyncError as handle } from '../../utils/async' import { createManagementClient } from '../../utils/contentful-clients' @@ -61,8 +60,9 @@ export async function updateExtension (extension, data) { } export async function updateExtensionHandler (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const { context } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) const data = await prepareData(argv) @@ -72,17 +72,15 @@ export async function updateExtensionHandler (argv) { await readSrcDocFile(data.extension) } - const { cmaToken, activeSpaceId, activeEnvironmentId } = await getContext() - const spaceId = argv.spaceId || activeSpaceId - const environmentId = argv.environmentId || activeEnvironmentId || 'master' + const { managementToken, activeSpaceId, activeEnvironmentId } = context const client = await createManagementClient({ - accessToken: argv.managementToken || cmaToken, + accessToken: managementToken, feature: 'extension-update' }) - const space = await client.getSpace(spaceId) - const environment = await space.getEnvironment(environmentId) + const space = await client.getSpace(activeSpaceId) + const environment = await space.getEnvironment(activeEnvironmentId) let extension = null let operation = 'update' @@ -104,13 +102,13 @@ export async function updateExtensionHandler (argv) { success(`${successEmoji} Successfully updated extension:\n`) - logExtension(updated, spaceId, environmentId) + logExtension(updated, activeSpaceId, activeEnvironmentId) } else if (operation === 'create') { const created = await createExtension(environment, data) success(`${successEmoji} Successfully created extension:\n`) - logExtension(created, spaceId, environmentId) + logExtension(created, activeSpaceId, activeEnvironmentId) } } diff --git a/lib/cmds/guide.js b/lib/cmds/guide.js index 19d69a24a..302a0d149 100644 --- a/lib/cmds/guide.js +++ b/lib/cmds/guide.js @@ -27,10 +27,10 @@ const guides = { directoryName: 'contentful-nuxt-blog', repository: 'contentful/blog-in-5-minutes', seed: 'blog', - setupConfig: async ({cmaToken, cdaToken, spaceId, installationDirectory}) => { + setupConfig: async ({managementToken, deliveryToken, spaceId, installationDirectory}) => { const config = await bfj.read(join(installationDirectory, '.contentful.sample.json')) - config.CTF_CDA_ACCESS_TOKEN = cdaToken - config.CTF_CMA_ACCESS_TOKEN = cmaToken + config.CTF_CDA_ACCESS_TOKEN = deliveryToken + config.CTF_CMA_ACCESS_TOKEN = managementToken config.CTF_SPACE_ID = spaceId return bfj.write(join(installationDirectory, '.contentful.json'), config, { space: 2 @@ -46,10 +46,10 @@ const guides = { directoryName: 'contentful-gatsby-blog', repository: 'contentful/starter-gatsby-blog', seed: 'blog', - setupConfig: async ({cdaToken, spaceId, installationDirectory}) => { + setupConfig: async ({deliveryToken, spaceId, installationDirectory}) => { const config = await bfj.read(join(installationDirectory, '.contentful.json.sample')) config.spaceId = spaceId - config.accessToken = cdaToken + config.accessToken = deliveryToken return bfj.write(join(installationDirectory, '.contentful.json'), config, { space: 2 }) diff --git a/lib/cmds/login.js b/lib/cmds/login.js index 417432e74..12f3cca19 100644 --- a/lib/cmds/login.js +++ b/lib/cmds/login.js @@ -2,7 +2,7 @@ import opn from 'opn' import inquirer from 'inquirer' import chalk from 'chalk' -import { getContext, getConfigPath, setContext, storeRuntimeConfig } from '../context' +import { getConfigPath, setContext, storeRuntimeConfig } from '../context' import { confirmation } from '../utils/actions' import { handleAsyncError as handle } from '../utils/async' import { log } from '../utils/log' @@ -17,15 +17,15 @@ export const command = 'login' export const desc = 'Login to Contentful' -export async function login () { - const { cmaToken } = await getContext() +export async function login ({ context }) { + const { managementToken } = context - if (cmaToken) { + if (managementToken) { log() log(`Looks like you already stored a management token on your system. ${chalk.dim(`(Located at ${await getConfigPath()})`)}`) - log(frame(`Your management token: ${cmaToken}`)) + log(frame(`Your management token: ${managementToken}`)) log(`Maybe you want to ${codeStyle('contentful logout')}?`) - return cmaToken + return managementToken } log(`A browser window will open where you will log in (or sign up if you don’t have an account), authorize this CLI tool and paste your ${highlightStyle('CMA token')} here:`) @@ -50,21 +50,21 @@ export async function login () { const tokenAnswer = await inquirer.prompt([ { type: 'input', - name: 'cmaToken', + name: 'managementToken', message: 'Paste your token here:', validate: (val) => /^[a-zA-Z0-9_-]{43,64}$/i.test(val.trim()) // token is 43 to 64 cahracters and accept lower/uppercase caharacter plus `-` and `_` } ]) await setContext({ - cmaToken: tokenAnswer.cmaToken + managementToken: tokenAnswer.managementToken }) await storeRuntimeConfig() log() log(`Great! Your ${highlightStyle('CMA token')} is now stored on your system. ${chalk.dim(`(Located at ${await getConfigPath()})`)}`) log(`You can always run ${codeStyle('contentful logout')} to remove it.`) - return tokenAnswer.cmaToken + return tokenAnswer.managementToken } export const handler = handle(login) diff --git a/lib/cmds/logout.js b/lib/cmds/logout.js index d7049f84e..fa67b4889 100644 --- a/lib/cmds/logout.js +++ b/lib/cmds/logout.js @@ -8,8 +8,8 @@ export const command = 'logout' export const desc = 'Logout from Contentful' -export async function logout (argv) { - await assertLoggedIn(argv) +export async function logout ({ context }) { + await assertLoggedIn(context) warning('This will log you out by deleting the CMA token stored on your system.') const confirmed = await confirmation('Do you want to log out now?') @@ -18,7 +18,7 @@ export async function logout (argv) { log('Log out aborted by user.') return } - await setContext({ cmaToken: null }) + await setContext({ managementToken: null }) await storeRuntimeConfig() success('Successfully logged you out.') diff --git a/lib/cmds/space_cmds/accesstoken_cmds/create.js b/lib/cmds/space_cmds/accesstoken_cmds/create.js index c14ae6baf..6d17debec 100644 --- a/lib/cmds/space_cmds/accesstoken_cmds/create.js +++ b/lib/cmds/space_cmds/accesstoken_cmds/create.js @@ -1,10 +1,8 @@ -import { getContext } from '../../../context' import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions' import { handleAsyncError as handle } from '../../../utils/async' import { createManagementClient } from '../../../utils/contentful-clients' import { successEmoji } from '../../../utils/emojis' import { success } from '../../../utils/log' -import normalizer from '../../../utils/normalizer' import paginate from '../../../utils/pagination' import { highlightStyle } from '../../../utils/styles' @@ -38,34 +36,35 @@ export const builder = (yargs) => { } export async function accessTokenCreate (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) - const { spaceId } = await normalizer(argv) - const { cmaToken } = await getContext() + const { context, name, description, silent, feature = 'space-access_token-create' } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) + + const { managementToken, activeSpaceId } = context const client = await createManagementClient({ - accessToken: argv.managementToken || cmaToken, - feature: argv.feature || 'space-access_token-create' + accessToken: managementToken, + feature }) - const space = await client.getSpace(spaceId) + const space = await client.getSpace(activeSpaceId) const accessTokens = await paginate({ client: space, method: 'getApiKeys' }) - let accessToken = accessTokens.items.find((key) => key.name === argv.name) + let accessToken = accessTokens.items.find((key) => key.name === name) if (accessToken) { - if (!argv.silent) { + if (!silent) { success(`${successEmoji} Successfully returned already existing access token ${highlightStyle(accessToken.name)} (${highlightStyle(accessToken.accessToken)})`) } return accessToken } accessToken = await space.createApiKey({ - name: argv.name, - description: argv.description + name, + description }) - if (!argv.silent) { + if (!silent) { success(`${successEmoji} Successfully created access token ${highlightStyle(accessToken.name)} (${highlightStyle(accessToken.accessToken)})`) } diff --git a/lib/cmds/space_cmds/accesstoken_cmds/list.js b/lib/cmds/space_cmds/accesstoken_cmds/list.js index 15a56c658..1a0b41b3a 100644 --- a/lib/cmds/space_cmds/accesstoken_cmds/list.js +++ b/lib/cmds/space_cmds/accesstoken_cmds/list.js @@ -1,11 +1,9 @@ import Table from 'cli-table3' -import { getContext } from '../../../context' import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions' import { handleAsyncError as handle } from '../../../utils/async' import { createManagementClient } from '../../../utils/contentful-clients' import { log } from '../../../utils/log' -import normalizer from '../../../utils/normalizer' import paginate from '../../../utils/pagination' export const command = 'list' @@ -31,17 +29,18 @@ export const builder = (yargs) => { export const aliases = ['ls'] async function accessTokenList (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) - const { spaceId } = await normalizer(argv) - const { cmaToken } = await getContext() + const { context } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) + + const { managementToken, activeSpaceId } = context const client = await createManagementClient({ - accessToken: argv.managementToken || cmaToken, + accessToken: managementToken, feature: 'space-access_token-list' }) - const space = await client.getSpace(spaceId) + const space = await client.getSpace(activeSpaceId) const result = await paginate({ client: space, method: 'getApiKeys' }) const tokens = result.items.sort((a, b) => a.name.localeCompare(b.name)) diff --git a/lib/cmds/space_cmds/create.js b/lib/cmds/space_cmds/create.js index df80b4c73..2fcc966e3 100644 --- a/lib/cmds/space_cmds/create.js +++ b/lib/cmds/space_cmds/create.js @@ -1,4 +1,3 @@ -import { getContext } from '../../context' import * as logging from '../../utils/log' import { handleAsyncError as handle } from '../../utils/async' import { createManagementClient } from '../../utils/contentful-clients' @@ -56,14 +55,14 @@ export const builder = yargs => { } export async function spaceCreate (argv) { - await assertLoggedIn(argv) + const { context, name, defaultLocale, yes, use, feature = 'space-create' } = argv + await assertLoggedIn(context) - let { name, organizationId, defaultLocale } = argv - const { cmaToken } = await getContext() - const managementToken = argv.managementToken || cmaToken + const {managementToken} = context + let { organizationId } = argv const client = await createManagementClient({ accessToken: managementToken, - feature: argv.feature || 'space-create' + feature }) logging.log( @@ -75,7 +74,7 @@ the Pricing page: https://www.contentful.com/pricing/?faq_category=payments&faq= ) let confirm = false - if (!argv.yes) { + if (!yes) { confirm = await confirmation(`Do you want to confirm the space creation?`) } else { confirm = true @@ -90,7 +89,7 @@ the Pricing page: https://www.contentful.com/pricing/?faq_category=payments&faq= const intentSystem = new IntentSystem() intentSystem.addHandler( createSpaceIntents({ - skipConfirm: argv.yes + skipConfirm: yes }) ) @@ -133,7 +132,7 @@ the Pricing page: https://www.contentful.com/pricing/?faq_category=payments&faq= organizationId ) - if (argv.use) { + if (use) { await spaceUse({spaceId: space.sys.id}) } diff --git a/lib/cmds/space_cmds/delete.js b/lib/cmds/space_cmds/delete.js index 4bb2c4d74..4f4129840 100644 --- a/lib/cmds/space_cmds/delete.js +++ b/lib/cmds/space_cmds/delete.js @@ -1,10 +1,8 @@ -import { getContext } from '../../context' import { confirmation } from '../../utils/actions' import { handleAsyncError as handle } from '../../utils/async' import { createManagementClient } from '../../utils/contentful-clients' import { successEmoji } from '../../utils/emojis' import { log, success } from '../../utils/log' -import normalizer from '../../utils/normalizer' import { highlightStyle, warningStyle } from '../../utils/styles' import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions' @@ -25,25 +23,23 @@ export const builder = (yargs) => { type: 'string' }) .option('yes', { - describe: 'Skip the confirmation question', - default: false + describe: 'Skip the confirmation question' }) .epilog('Copyright 2018 Contentful, this is a BETA release') } export async function spaceDelete (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) - const { spaceId } = await normalizer(argv) - const { cmaToken } = await getContext() - const managementToken = argv.managementToken || cmaToken + const { context, yes } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) + const { managementToken, activeSpaceId } = context const client = await createManagementClient({ accessToken: managementToken }) - const space = await client.getSpace(spaceId) + const space = await client.getSpace(activeSpaceId) - if (!argv.yes) { + if (!yes) { log(`You are about to delete your ${highlightStyle(space.name)} (${highlightStyle(space.sys.id)}) space and all of its content. This cannot be undone.`) log() diff --git a/lib/cmds/space_cmds/environment_cmds/create.js b/lib/cmds/space_cmds/environment_cmds/create.js index 868ea6b41..86d79d923 100644 --- a/lib/cmds/space_cmds/environment_cmds/create.js +++ b/lib/cmds/space_cmds/environment_cmds/create.js @@ -1,5 +1,4 @@ import Promise from 'bluebird' -import { getContext } from '../../../context' import * as logging from '../../../utils/log' import { handleAsyncError as handle } from '../../../utils/async' import { createManagementClient } from '../../../utils/contentful-clients' @@ -51,32 +50,30 @@ export const builder = (yargs) => { } export async function environmentCreate (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const { context, name, source, awaitProcessing, environmentId } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - const { cmaToken, activeSpaceId } = await getContext() - - const managementToken = argv.managementToken || cmaToken - const spaceId = argv.spaceId || activeSpaceId + const { managementToken, activeSpaceId } = context const client = await createManagementClient({ accessToken: managementToken, feature: 'space-environment-create' }) - const space = await client.getSpace(spaceId) + const space = await client.getSpace(activeSpaceId) const options = {} - if (argv.name) { - options.name = argv.name + if (name) { + options.name = name } - const environment = (argv.source) ? await space.createEnvironmentWithId(argv.environmentId, options, argv.source) - : await space.createEnvironmentWithId(argv.environmentId, options) + const environment = (source) ? await space.createEnvironmentWithId(environmentId, options, source) + : await space.createEnvironmentWithId(environmentId, options) - logging.success(`Successfully created environment ${environment.name} (${environment.sys.id}) ${argv.source ? `with source ${argv.source}` : ''}`) + logging.success(`Successfully created environment ${environment.name} (${environment.sys.id}) ${source ? `with source ${source}` : ''}`) - if (argv.awaitProcessing) { + if (awaitProcessing) { const DELAY = 3000 const MAX_NUMBER_OF_TRIES = 10 let count = 0 diff --git a/lib/cmds/space_cmds/environment_cmds/delete.js b/lib/cmds/space_cmds/environment_cmds/delete.js index 5fb21be63..54e4359db 100644 --- a/lib/cmds/space_cmds/environment_cmds/delete.js +++ b/lib/cmds/space_cmds/environment_cmds/delete.js @@ -1,4 +1,3 @@ -import { getContext } from '../../../context' import * as logging from '../../../utils/log' import { handleAsyncError as handle } from '../../../utils/async' import { createManagementClient } from '../../../utils/contentful-clients' @@ -34,21 +33,20 @@ export const builder = (yargs) => { } export async function environmentDelete (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const { context, environmentId } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - const { cmaToken, activeSpaceId } = await getContext() - const spaceId = argv.spaceId || activeSpaceId - const managementToken = argv.managementToken || cmaToken + const { managementToken, activeSpaceId } = context const client = await createManagementClient({ accessToken: managementToken, feature: 'space-environment-delete' }) - const space = await client.getSpace(spaceId) + const space = await client.getSpace(activeSpaceId) - const environment = await space.getEnvironment(argv.environmentId) + const environment = await space.getEnvironment(environmentId) await environment.delete() diff --git a/lib/cmds/space_cmds/environment_cmds/list.js b/lib/cmds/space_cmds/environment_cmds/list.js index 81840e6d1..9a5a9e2a7 100644 --- a/lib/cmds/space_cmds/environment_cmds/list.js +++ b/lib/cmds/space_cmds/environment_cmds/list.js @@ -1,6 +1,5 @@ import Table from 'cli-table3' -import { getContext } from '../../../context' import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions' import { handleAsyncError as handle } from '../../../utils/async' import { createManagementClient } from '../../../utils/contentful-clients' @@ -35,19 +34,18 @@ export const builder = (yargs) => { export const aliases = ['ls'] export async function environmentList (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const { context } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - const { cmaToken, activeSpaceId, activeEnvironmentId } = await getContext() - const spaceId = argv.spaceId || activeSpaceId - const managementToken = argv.managementToken || cmaToken + const { managementToken, activeSpaceId, activeEnvironmentId } = context const client = await createManagementClient({ accessToken: managementToken, feature: 'space-environment-list' }) - const space = await client.getSpace(spaceId) + const space = await client.getSpace(activeSpaceId) const result = await paginate({ client: space, method: 'getEnvironments' }) diff --git a/lib/cmds/space_cmds/environment_cmds/use.js b/lib/cmds/space_cmds/environment_cmds/use.js index bc6289ba5..cb2ea9a74 100644 --- a/lib/cmds/space_cmds/environment_cmds/use.js +++ b/lib/cmds/space_cmds/environment_cmds/use.js @@ -1,7 +1,7 @@ import inquirer from 'inquirer' import { createManagementClient } from '../../../utils/contentful-clients' -import { getContext, setContext, storeRuntimeConfig } from '../../../context' +import { setContext, storeRuntimeConfig } from '../../../context' import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions' import { handleAsyncError as handle } from '../../../utils/async' import { success } from '../../../utils/log' @@ -19,7 +19,8 @@ export const builder = (yargs) => { .usage('Usage: contentful space environment use') .option('environment-id', { alias: 'e', - describe: 'ID of the Environment within the currently active Space to use for other commands' + describe: 'ID of the Environment within the currently active Space to use for other commands', + demandOption: true }) .option('management-token', { alias: 'mt', @@ -35,21 +36,18 @@ function showSuccess (space, environment) { } export async function environmentUse (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const { context, environmentId } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - const { cmaToken, activeSpaceId } = await getContext() - - const spaceId = argv.spaceId || activeSpaceId - const environmentId = argv.environmentId - const managementToken = argv.managementToken || cmaToken + const { managementToken, activeSpaceId } = context const client = await createManagementClient({ accessToken: managementToken, feature: 'space-environment-use' }) - const space = await client.getSpace(spaceId) + const space = await client.getSpace(activeSpaceId) if (environmentId) { const environment = await space.getEnvironment(environmentId) diff --git a/lib/cmds/space_cmds/export.js b/lib/cmds/space_cmds/export.js index 51b92971d..6bdc2275e 100644 --- a/lib/cmds/space_cmds/export.js +++ b/lib/cmds/space_cmds/export.js @@ -1,4 +1,3 @@ -import {getContext} from '../../context' import * as log from '../../utils/log' import runContentfulExport from 'contentful-export' import {handleAsyncError as handle} from '../../utils/async' @@ -109,17 +108,23 @@ export const builder = function (yargs) { } export const exportSpace = async (argv) => { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const { context, feature = 'space-export' } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - const { cmaToken, activeSpaceId, activeEnvironmentId, proxy, rawProxy, host = 'api.contentful.com' } = await getContext() - const spaceId = argv.spaceId || activeSpaceId - const environmentId = argv.environmentId || activeEnvironmentId - const managementToken = argv.managementToken || cmaToken + const { managementToken, activeSpaceId, activeEnvironmentId, host, proxy, rawProxy } = context const managementApplication = `contentful.cli/${version}` - const managementFeature = argv.feature || `space-export` + const managementFeature = feature - const options = { ...argv, spaceId, environmentId, managementApplication, managementFeature, managementToken, host } + const options = { + ...argv, + spaceId: activeSpaceId, + environmentId: activeEnvironmentId, + managementApplication, + managementFeature, + managementToken, + host + } if (proxy) { // contentful-import and contentful-export diff --git a/lib/cmds/space_cmds/generate_cmds/migration.js b/lib/cmds/space_cmds/generate_cmds/migration.js index 257c7a635..8c1696ed5 100644 --- a/lib/cmds/space_cmds/generate_cmds/migration.js +++ b/lib/cmds/space_cmds/generate_cmds/migration.js @@ -1,4 +1,3 @@ -import { getContext } from '../../../context' import { assertLoggedIn, assertSpaceIdProvided } from '../../../utils/assertions' import { handleAsyncError as handle } from '../../../utils/async' import { log, success, warning } from '../../../utils/log' @@ -256,35 +255,32 @@ export const generateFileName = function (spaceId, environmentId, contentTypeId) return `${spaceId}-${environmentId}${contentTypePart}-${Date.now()}.js` } -const createManagementClient = function (cmaToken) { +const createManagementClient = function (managementToken) { return createClient({ - accessToken: cmaToken, + accessToken: managementToken, feature: 'migration-generate' }) } -const getEnvironment = async function (cmaToken, spaceId, environmentId) { - const client = await createManagementClient(cmaToken) +const getEnvironment = async function (managementToken, spaceId, environmentId) { + const client = await createManagementClient(managementToken) const space = await client.getSpace(spaceId) return space.getEnvironment(environmentId) } export async function generateMigration (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const { context, filename: filenameFlag, contentTypeId } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - const { cmaToken, activeSpaceId, activeEnvironmentId } = await getContext() - const spaceId = argv.spaceId || activeSpaceId - const managementToken = argv.managementToken || cmaToken - const environmentId = argv.environmentId || activeEnvironmentId - const contentTypeId = argv.contentTypeId + const { managementToken, activeSpaceId, activeEnvironmentId } = context - const filename = argv.filename || generateFileName(spaceId, environmentId, contentTypeId) + const filename = filenameFlag || generateFileName(activeSpaceId, activeEnvironmentId, contentTypeId) const environment = await getEnvironment( managementToken, - spaceId, - environmentId + activeSpaceId, + activeEnvironmentId ) log('Fetching content model') diff --git a/lib/cmds/space_cmds/import.js b/lib/cmds/space_cmds/import.js index 277e92d8f..b5ec4dd4d 100644 --- a/lib/cmds/space_cmds/import.js +++ b/lib/cmds/space_cmds/import.js @@ -1,4 +1,3 @@ -import {getContext} from '../../context' import runContentfulImport from 'contentful-import' import {handleAsyncError as handle} from '../../utils/async' import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions' @@ -74,17 +73,21 @@ export const builder = (yargs) => { } export const importSpace = async (argv) => { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) + const { context, feature = 'space-import' } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - let { cmaToken, activeSpaceId, proxy, rawProxy, host = 'api.contentful.com' } = await getContext() - const spaceId = argv.spaceId || activeSpaceId - const managementToken = argv.managementToken || cmaToken - const managementApplication = `contentful.cli/${version}` - const managementFeature = argv.feature || `space-import` - host = argv.host || host + const { managementToken, activeSpaceId, activeEnvironmentId, host, proxy, rawProxy } = context - const options = { ...argv, spaceId, managementApplication, managementFeature, managementToken, host } + const options = { + ...argv, + spaceId: activeSpaceId, + environmentId: activeEnvironmentId, + managementApplication: `contentful.cli/${version}`, + managementFeature: feature, + managementToken, + host + } if (proxy) { // contentful-import and contentful-export // expect a string for the proxy config diff --git a/lib/cmds/space_cmds/list.js b/lib/cmds/space_cmds/list.js index 0c1ee00dd..c6e0b3309 100644 --- a/lib/cmds/space_cmds/list.js +++ b/lib/cmds/space_cmds/list.js @@ -1,11 +1,9 @@ import Table from 'cli-table3' -import { getContext } from '../../context' import { assertLoggedIn } from '../../utils/assertions' import { handleAsyncError as handle } from '../../utils/async' import { createManagementClient } from '../../utils/contentful-clients' import { log } from '../../utils/log' -import normalizer from '../../utils/normalizer' import paginate from '../../utils/pagination' import { highlightStyle } from '../../utils/styles' @@ -30,11 +28,9 @@ export const builder = (yargs) => { export const aliases = ['ls'] -async function spaceList (argv) { - await assertLoggedIn(argv) - const { spaceId } = await normalizer(argv) - const { cmaToken } = await getContext() - const managementToken = argv.managementToken || cmaToken +async function spaceList ({context}) { + await assertLoggedIn(context) + const { managementToken, activeSpaceId } = context const client = await createManagementClient({ accessToken: managementToken, @@ -50,7 +46,7 @@ async function spaceList (argv) { }) spaces.forEach((space) => { - if (space.sys.id === spaceId) { + if (space.sys.id === activeSpaceId) { table.push([highlightStyle(`${space.name} [active]`), highlightStyle(space.sys.id)]) return } diff --git a/lib/cmds/space_cmds/migration.js b/lib/cmds/space_cmds/migration.js index 3d1ecb380..c8c0c6be0 100644 --- a/lib/cmds/space_cmds/migration.js +++ b/lib/cmds/space_cmds/migration.js @@ -1,4 +1,3 @@ -import {getContext} from '../../context' import runMigration from 'contentful-migration/built/bin/cli' import {handleAsyncError as handle} from '../../utils/async' import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions' @@ -41,8 +40,7 @@ export const builder = (yargs) => { .option('yes', { alias: 'y', boolean: true, - describe: 'Skips any confirmation before applying the migration script', - default: false + describe: 'Skips any confirmation before applying the migration script' }) .option('quiet', { alias: 'q', @@ -59,16 +57,22 @@ export const builder = (yargs) => { } export const migration = async (argv) => { - await assertLoggedIn({managementToken: argv.managementToken, paramName: '--management-token'}) - await assertSpaceIdProvided(argv) + const { context } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) - const { cmaToken, activeSpaceId, proxy, rawProxy } = await getContext() - const spaceId = argv.spaceId || activeSpaceId - const managementToken = argv.managementToken || cmaToken + const { managementToken, activeSpaceId, activeEnvironmentId, proxy, rawProxy } = context const managementApplication = `contentful.cli/${version}` const managementFeature = `space-migration` - const options = { ...argv, spaceId, managementApplication, managementFeature, accessToken: managementToken } + const options = { + ...argv, + spaceId: activeSpaceId, + managementApplication, + managementFeature, + accessToken: managementToken, + environmentId: activeEnvironmentId + } if (proxy) { // contentful-import and contentful-export // expect a string for the proxy config diff --git a/lib/cmds/space_cmds/seed.js b/lib/cmds/space_cmds/seed.js index 530ff1e8e..7a1a2e8d6 100644 --- a/lib/cmds/space_cmds/seed.js +++ b/lib/cmds/space_cmds/seed.js @@ -4,14 +4,12 @@ import { tmpdir } from 'os' import spaceImport from 'contentful-import' import bfj from 'bfj' -import { getContext } from '../../context' import { confirmation } from '../../utils/actions' import { handleAsyncError as handle } from '../../utils/async' import { createManagementClient } from '../../utils/contentful-clients' import { successEmoji } from '../../utils/emojis' import { getLatestGitHubRelease } from '../../utils/github' import { log, success } from '../../utils/log' -import normalizer from '../../utils/normalizer' import { highlightStyle, warningStyle } from '../../utils/styles' import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions' @@ -38,28 +36,27 @@ export const builder = (yargs) => { type: 'string' }) .option('yes', { - describe: 'Skip the confirmation question', - default: false + describe: 'Skip the confirmation question' }) .epilog('Copyright 2018 Contentful, this is a BETA release') } export async function spaceSeed (argv) { - await assertLoggedIn(argv) - await assertSpaceIdProvided(argv) - const { spaceId } = await normalizer(argv) - const { cmaToken, host = 'api.contentful.com', proxy, rawProxy } = await getContext() - const managementToken = argv.managementToken || cmaToken + const { context, yes, template, feature = 'space-seed' } = argv + await assertLoggedIn(context) + await assertSpaceIdProvided(context) + + const { managementToken, activeSpaceId, host, proxy, rawProxy } = context const client = await createManagementClient({ accessToken: managementToken, - feature: argv.feature || 'space-seed', + feature, proxy, rawProxy }) - const space = await client.getSpace(spaceId) + const space = await client.getSpace(activeSpaceId) - if (!argv.yes) { + if (!yes) { log(`You are about to apply changes to your ${highlightStyle(space.name)} (${highlightStyle(space.sys.id)}) Space. Existing content might be overwritten.`) log() @@ -74,15 +71,15 @@ export async function spaceSeed (argv) { const destination = resolve(tmpdir(), '.contentful', 'content-models') await getLatestGitHubRelease('contentful/content-models', destination).run() - const content = await bfj.read(resolve(destination, argv.template, 'contentful-export.json')) + const content = await bfj.read(resolve(destination, template, 'contentful-export.json')) log() await spaceImport({ content, - spaceId, + spaceId: activeSpaceId, host, - managementToken: cmaToken, - managementFeature: argv.feature || 'space-seed' + managementToken, + managementFeature: feature || 'space-seed' }) log() diff --git a/lib/cmds/space_cmds/use.js b/lib/cmds/space_cmds/use.js index 4eea8dbf2..5a18c0fe5 100644 --- a/lib/cmds/space_cmds/use.js +++ b/lib/cmds/space_cmds/use.js @@ -1,7 +1,7 @@ import inquirer from 'inquirer' import { createManagementClient } from '../../utils/contentful-clients' -import { getContext, setContext, storeRuntimeConfig } from '../../context' +import { setContext, storeRuntimeConfig } from '../../context' import { assertLoggedIn } from '../../utils/assertions' import { handleAsyncError as handle } from '../../utils/async' import { success } from '../../utils/log' @@ -24,19 +24,18 @@ export const builder = (yargs) => { .epilog('Copyright 2018 Contentful, this is a BETA release') } -function showSuccess (space) { - success(`Now using the 'master' Environment of Space ${highlightStyle(space.name)} (${highlightStyle(space.sys.id)}) when the \`--environment-id\` option is missing.`) +function showSuccess (space, env) { + success(`Now using the '${env}' Environment of Space ${highlightStyle(space.name)} (${highlightStyle(space.sys.id)}) when the \`--environment-id\` option is missing.`) } export async function spaceUse (argv) { - await assertLoggedIn(argv) + const { context, spaceId } = argv + await assertLoggedIn(context) - const context = await getContext() - const { cmaToken } = context - const spaceId = argv.spaceId + const { managementToken, activeEnvironmentId } = context const client = await createManagementClient({ - accessToken: cmaToken, + accessToken: managementToken, feature: 'space-use' }) @@ -44,12 +43,12 @@ export async function spaceUse (argv) { const space = await client.getSpace(spaceId) await setContext({ activeSpaceId: space.sys.id, - activeEnvironmentId: 'master' + activeEnvironmentId }) await storeRuntimeConfig() - showSuccess(space) + showSuccess(space, activeEnvironmentId) return space } @@ -76,12 +75,12 @@ export async function spaceUse (argv) { await setContext({ activeSpaceId: space.sys.id, - activeEnvironmentId: 'master' + activeEnvironmentId }) await storeRuntimeConfig() - showSuccess(space) + showSuccess(space, activeEnvironmentId) return space } diff --git a/lib/context.js b/lib/context.js index d16f9dbe3..16272a6d3 100644 --- a/lib/context.js +++ b/lib/context.js @@ -80,7 +80,7 @@ async function loadRuntimeConfig () { export async function storeRuntimeConfig () { const contextToStore = { - cmaToken: context.cmaToken, + managementToken: context.managementToken, activeSpaceId: context.activeSpaceId, activeEnvironmentId: context.activeEnvironmentId, host: context.host, diff --git a/lib/guide/step-create-space.js b/lib/guide/step-create-space.js index ba70f1eb9..81ce2fd5d 100644 --- a/lib/guide/step-create-space.js +++ b/lib/guide/step-create-space.js @@ -2,6 +2,7 @@ import shellescape from 'shell-escape' import { spaceCreate } from '../cmds/space_cmds/create' import { log, wrappedLog } from '../utils/log' +import { getContext } from '../context' import { separator } from '../utils/text' import { highlightStyle, codeStyle } from '../utils/styles' import { generateNumberEmoji } from '../utils/emojis' @@ -38,9 +39,12 @@ export default async function createSpaceStep (guideContext) { throw new AbortedError() } + const context = await getContext() + const space = await spaceCreate({ name: activeGuide.name, - feature: 'guide' + feature: 'guide', + context }) guideContext.spaceId = space.sys.id diff --git a/lib/guide/step-finish.js b/lib/guide/step-finish.js index 2e1b3fb42..78eb51b5f 100644 --- a/lib/guide/step-finish.js +++ b/lib/guide/step-finish.js @@ -16,11 +16,11 @@ import { GUIDE_MAX_WIDTH } from './helpers' export default async function finishStep (guideContext) { const { installationDirectory, activeGuide, spaceId } = guideContext - const { cmaToken, host = 'api.contentful.com' } = await getContext() + const { managementToken, host } = await getContext() const client = await createManagementClient({ host, - accessToken: cmaToken, + accessToken: managementToken, feature: 'guide' }) diff --git a/lib/guide/step-login.js b/lib/guide/step-login.js index dfb778898..89d3cc982 100644 --- a/lib/guide/step-login.js +++ b/lib/guide/step-login.js @@ -8,9 +8,10 @@ import { generateNumberEmoji } from '../utils/emojis' import { GUIDE_MAX_WIDTH } from './helpers' export default async function loginStep (guideContext) { - const { cmaToken } = await getContext() + const context = await getContext() + const { managementToken } = context - if (!cmaToken) { + if (!managementToken) { guideContext.stepCount++ const { stepCount } = guideContext @@ -24,6 +25,6 @@ export default async function loginStep (guideContext) { wrappedLog(`We’ll run the ${codeStyle('contentful login')} command which will open a new browser window. In the browser, you’ll find your ${highlightStyle('CMA token')}. Copy/paste your ${highlightStyle('CMA token')} to authenticate.`, GUIDE_MAX_WIDTH) log() - await login() + await login({ context }) } } diff --git a/lib/guide/step-seed.js b/lib/guide/step-seed.js index 6a6f77117..f7b3d757f 100644 --- a/lib/guide/step-seed.js +++ b/lib/guide/step-seed.js @@ -3,6 +3,7 @@ import shellescape from 'shell-escape' import { spaceSeed } from '../cmds/space_cmds/seed' import { log, wrappedLog } from '../utils/log' import { separator } from '../utils/text' +import { getContext } from '../context' import { highlightStyle, codeStyle } from '../utils/styles' import { generateNumberEmoji } from '../utils/emojis' import { confirmation } from '../utils/actions' @@ -33,10 +34,12 @@ export default async function seedStep (guideContext) { throw new AbortedError() } + const context = await getContext() + await spaceSeed({ template: activeGuide.seed, - spaceId, yes: true, - feature: 'guide' + feature: 'guide', + context: { activeSpaceId: spaceId, ...context } }) } diff --git a/lib/guide/step-setup.js b/lib/guide/step-setup.js index a02338e48..2fec31e23 100644 --- a/lib/guide/step-setup.js +++ b/lib/guide/step-setup.js @@ -69,22 +69,23 @@ export default async function setupStep (guideContext) { { title: 'Creating a Contentful Delivery API token to read content from your Space.', task: async (ctx) => { + const context = await getContext() const { accessToken } = await accessTokenCreate({ name: 'Contentful Get Started Guide', description: 'This access token was created by the Contentful Get Started Guide', silent: true, - spaceId, - feature: 'guide' + feature: 'guide', + context: { activeSpaceId: spaceId, ...context } }) - ctx.cdaToken = accessToken + ctx.deliveryToken = accessToken } }, { title: 'Setting up project configuration file which includes your Contentful Delivery API token', task: async (ctx) => { - const { cmaToken } = await getContext() - const { cdaToken } = ctx - return activeGuide.setupConfig({ cmaToken, cdaToken, spaceId, installationDirectory }) + const { managementToken } = await getContext() + const { deliveryToken } = ctx + return activeGuide.setupConfig({ managementToken, deliveryToken, spaceId, installationDirectory }) } } ], { diff --git a/lib/utils/assertions.js b/lib/utils/assertions.js index b7af233aa..eee60a6f8 100644 --- a/lib/utils/assertions.js +++ b/lib/utils/assertions.js @@ -3,16 +3,24 @@ import { PreconditionFailedError } from './error' import { highlightStyle } from './styles' export async function assertLoggedIn ({managementToken, paramName} = {}) { - const context = await getContext() + let noToken = !managementToken + if (!managementToken) { + const context = await getContext() + noToken = !context.managementToken + } paramName = paramName || '--management-Token' - if (!context.cmaToken && !managementToken) { + if (noToken) { throw new PreconditionFailedError(`You have to be logged in to do this.\nYou can log in via ${highlightStyle('contentful login')}\nOr provide a managementToken via ${paramName} argument`) } } -export async function assertSpaceIdProvided ({spaceId} = {}) { - const context = await getContext() - if (!(spaceId || context.activeSpaceId)) { +export async function assertSpaceIdProvided ({spaceId, activeSpaceId} = {}) { + let noSpaceId = !spaceId && !activeSpaceId + if (noSpaceId) { + const context = await getContext() + noSpaceId = !context.activeSpaceId + } + if (noSpaceId) { throw new PreconditionFailedError(`You need to provide a space id. You can pass it via the ${highlightStyle('--space-id')} parameter or by running ${highlightStyle('contentful space use')}`) } } diff --git a/lib/utils/contentful-clients.js b/lib/utils/contentful-clients.js index 47ed40c8d..ecf4fe6b3 100644 --- a/lib/utils/contentful-clients.js +++ b/lib/utils/contentful-clients.js @@ -7,7 +7,7 @@ export async function createManagementClient (params) { params.application = `contentful.cli/${version}` const context = await getContext() - const { rawProxy, proxy, host = 'api.contentful.com' } = context + const { rawProxy, proxy, host } = context const proxyConfig = {} if (!rawProxy) { diff --git a/lib/utils/middlewares.js b/lib/utils/middlewares.js new file mode 100644 index 000000000..4ac24d871 --- /dev/null +++ b/lib/utils/middlewares.js @@ -0,0 +1,33 @@ +import {getContext} from '../context' + +export const buildContext = async (argv) => { + const { + managementToken, + spaceId, + activeSpaceId, + environmentId, + activeEnvironmentId, + host, + rawProxy, + proxy + } = argv + + const context = await getContext() + + if (managementToken) { context.managementToken = managementToken } + + const space = spaceId || activeSpaceId + if (space) { context.activeSpaceId = space } + + const environment = environmentId || activeEnvironmentId + if (environment) { context.activeEnvironmentId = environment } + if (!context.activeEnvironmentId) { context.activeEnvironmentId = 'master' } + + if (host) { context.host = host } + if (!context.host) { context.host = 'api.contentful.com' } + + if (rawProxy !== undefined) { context.rawProxy = rawProxy } + if (proxy) { context.proxy = proxy } + + return {context} +} diff --git a/lib/utils/normalizer.js b/lib/utils/normalizer.js deleted file mode 100644 index 99ca9670b..000000000 --- a/lib/utils/normalizer.js +++ /dev/null @@ -1,9 +0,0 @@ -import { getContext } from '../context' - -export default async function normalizer ({spaceId}) { - const context = await getContext() - const { activeSpaceId } = context - return { - spaceId: spaceId || activeSpaceId - } -} diff --git a/package-lock.json b/package-lock.json index 38d45d29d..ffee4894d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -582,7 +582,6 @@ "version": "3.2.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.0.tgz", "integrity": "sha512-NnSOmMEYtVR2JVMIGTzynRkkaxtiq1xnFBcdQD/DnNCYPoEPsVJhM98BDyaoNOQIi7p4okdi3E27eN7GQbsUug==", - "dev": true, "requires": { "color-convert": "^1.9.0" } @@ -2353,6 +2352,98 @@ "yargs": "^12.0.1" }, "dependencies": { + "cross-spawn": { + "version": "6.0.5", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", + "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", + "dev": true, + "requires": { + "nice-try": "^1.0.4", + "path-key": "^2.0.1", + "semver": "^5.5.0", + "shebang-command": "^1.2.0", + "which": "^1.2.9" + } + }, + "execa": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/execa/-/execa-1.0.0.tgz", + "integrity": "sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==", + "dev": true, + "requires": { + "cross-spawn": "^6.0.0", + "get-stream": "^4.0.0", + "is-stream": "^1.1.0", + "npm-run-path": "^2.0.0", + "p-finally": "^1.0.0", + "signal-exit": "^3.0.0", + "strip-eof": "^1.0.0" + } + }, + "get-stream": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", + "integrity": "sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==", + "dev": true, + "requires": { + "pump": "^3.0.0" + } + }, + "invert-kv": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-2.0.0.tgz", + "integrity": "sha512-wPVv/y/QQ/Uiirj/vh3oP+1Ww+AWehmi1g5fFWGPF6IpCBCDVrhgHRMvrLfdYcwDh3QJbGXDW4JAuzxElLSqKA==", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", + "dev": true + }, + "lcid": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", + "integrity": "sha512-avPEb8P8EGnwXKClwsNUgryVjllcRqtMYa49NTsbQagYuT1DcXnl1915oxWjoyGrXR6zH/Y0Zc96xWsPcoDKeA==", + "dev": true, + "requires": { + "invert-kv": "^2.0.0" + } + }, + "mem": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/mem/-/mem-4.3.0.tgz", + "integrity": "sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w==", + "dev": true, + "requires": { + "map-age-cleaner": "^0.1.1", + "mimic-fn": "^2.0.0", + "p-is-promise": "^2.0.0" + } + }, + "mimic-fn": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", + "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==", + "dev": true + }, + "os-locale": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-3.1.0.tgz", + "integrity": "sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==", + "dev": true, + "requires": { + "execa": "^1.0.0", + "lcid": "^2.0.0", + "mem": "^4.0.0" + } + }, + "p-is-promise": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-2.1.0.tgz", + "integrity": "sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==", + "dev": true + }, "parse-json": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-4.0.0.tgz", @@ -2389,6 +2480,16 @@ "tslib": "^1.9.0" } }, + "string-width": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "dev": true, + "requires": { + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^4.0.0" + } + }, "supports-color": { "version": "4.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.5.0.tgz", @@ -2397,6 +2498,26 @@ "requires": { "has-flag": "^2.0.0" } + }, + "yargs": { + "version": "12.0.5", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-12.0.5.tgz", + "integrity": "sha512-Lhz8TLaYnxq/2ObqHDql8dX8CJi97oHxrjUcYtzKbbykPtVW9WB+poxI+NM2UIzsMgNCZTIf0AQwsjK5yMAqZw==", + "dev": true, + "requires": { + "cliui": "^4.0.0", + "decamelize": "^1.2.0", + "find-up": "^3.0.0", + "get-caller-file": "^1.0.1", + "os-locale": "^3.0.0", + "require-directory": "^2.1.1", + "require-main-filename": "^1.0.1", + "set-blocking": "^2.0.0", + "string-width": "^2.0.0", + "which-module": "^2.0.0", + "y18n": "^3.2.1 || ^4.0.0", + "yargs-parser": "^11.1.1" + } } } }, @@ -2471,6 +2592,118 @@ "moment": "^2.22.2", "request": "^2.87.0", "yargs": "^12.0.1" + }, + "dependencies": { + "cross-spawn": { + "version": "6.0.5", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", + "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", + "requires": { + "nice-try": "^1.0.4", + "path-key": "^2.0.1", + "semver": "^5.5.0", + "shebang-command": "^1.2.0", + "which": "^1.2.9" + } + }, + "execa": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/execa/-/execa-1.0.0.tgz", + "integrity": "sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==", + "requires": { + "cross-spawn": "^6.0.0", + "get-stream": "^4.0.0", + "is-stream": "^1.1.0", + "npm-run-path": "^2.0.0", + "p-finally": "^1.0.0", + "signal-exit": "^3.0.0", + "strip-eof": "^1.0.0" + } + }, + "get-stream": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", + "integrity": "sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==", + "requires": { + "pump": "^3.0.0" + } + }, + "invert-kv": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-2.0.0.tgz", + "integrity": "sha512-wPVv/y/QQ/Uiirj/vh3oP+1Ww+AWehmi1g5fFWGPF6IpCBCDVrhgHRMvrLfdYcwDh3QJbGXDW4JAuzxElLSqKA==" + }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=" + }, + "lcid": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", + "integrity": "sha512-avPEb8P8EGnwXKClwsNUgryVjllcRqtMYa49NTsbQagYuT1DcXnl1915oxWjoyGrXR6zH/Y0Zc96xWsPcoDKeA==", + "requires": { + "invert-kv": "^2.0.0" + } + }, + "mem": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/mem/-/mem-4.3.0.tgz", + "integrity": "sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w==", + "requires": { + "map-age-cleaner": "^0.1.1", + "mimic-fn": "^2.0.0", + "p-is-promise": "^2.0.0" + } + }, + "mimic-fn": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", + "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==" + }, + "os-locale": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-3.1.0.tgz", + "integrity": "sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==", + "requires": { + "execa": "^1.0.0", + "lcid": "^2.0.0", + "mem": "^4.0.0" + } + }, + "p-is-promise": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-2.1.0.tgz", + "integrity": "sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==" + }, + "string-width": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "requires": { + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^4.0.0" + } + }, + "yargs": { + "version": "12.0.5", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-12.0.5.tgz", + "integrity": "sha512-Lhz8TLaYnxq/2ObqHDql8dX8CJi97oHxrjUcYtzKbbykPtVW9WB+poxI+NM2UIzsMgNCZTIf0AQwsjK5yMAqZw==", + "requires": { + "cliui": "^4.0.0", + "decamelize": "^1.2.0", + "find-up": "^3.0.0", + "get-caller-file": "^1.0.1", + "os-locale": "^3.0.0", + "require-directory": "^2.1.1", + "require-main-filename": "^1.0.1", + "set-blocking": "^2.0.0", + "string-width": "^2.0.0", + "which-module": "^2.0.0", + "y18n": "^3.2.1 || ^4.0.0", + "yargs-parser": "^11.1.1" + } + } } }, "contentful-import": { @@ -2490,6 +2723,118 @@ "lodash": "^4.17.10", "moment": "^2.22.2", "yargs": "^12.0.1" + }, + "dependencies": { + "cross-spawn": { + "version": "6.0.5", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", + "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", + "requires": { + "nice-try": "^1.0.4", + "path-key": "^2.0.1", + "semver": "^5.5.0", + "shebang-command": "^1.2.0", + "which": "^1.2.9" + } + }, + "execa": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/execa/-/execa-1.0.0.tgz", + "integrity": "sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==", + "requires": { + "cross-spawn": "^6.0.0", + "get-stream": "^4.0.0", + "is-stream": "^1.1.0", + "npm-run-path": "^2.0.0", + "p-finally": "^1.0.0", + "signal-exit": "^3.0.0", + "strip-eof": "^1.0.0" + } + }, + "get-stream": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", + "integrity": "sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==", + "requires": { + "pump": "^3.0.0" + } + }, + "invert-kv": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-2.0.0.tgz", + "integrity": "sha512-wPVv/y/QQ/Uiirj/vh3oP+1Ww+AWehmi1g5fFWGPF6IpCBCDVrhgHRMvrLfdYcwDh3QJbGXDW4JAuzxElLSqKA==" + }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=" + }, + "lcid": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", + "integrity": "sha512-avPEb8P8EGnwXKClwsNUgryVjllcRqtMYa49NTsbQagYuT1DcXnl1915oxWjoyGrXR6zH/Y0Zc96xWsPcoDKeA==", + "requires": { + "invert-kv": "^2.0.0" + } + }, + "mem": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/mem/-/mem-4.3.0.tgz", + "integrity": "sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w==", + "requires": { + "map-age-cleaner": "^0.1.1", + "mimic-fn": "^2.0.0", + "p-is-promise": "^2.0.0" + } + }, + "mimic-fn": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", + "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==" + }, + "os-locale": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-3.1.0.tgz", + "integrity": "sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==", + "requires": { + "execa": "^1.0.0", + "lcid": "^2.0.0", + "mem": "^4.0.0" + } + }, + "p-is-promise": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-2.1.0.tgz", + "integrity": "sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==" + }, + "string-width": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "requires": { + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^4.0.0" + } + }, + "yargs": { + "version": "12.0.5", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-12.0.5.tgz", + "integrity": "sha512-Lhz8TLaYnxq/2ObqHDql8dX8CJi97oHxrjUcYtzKbbykPtVW9WB+poxI+NM2UIzsMgNCZTIf0AQwsjK5yMAqZw==", + "requires": { + "cliui": "^4.0.0", + "decamelize": "^1.2.0", + "find-up": "^3.0.0", + "get-caller-file": "^1.0.1", + "os-locale": "^3.0.0", + "require-directory": "^2.1.1", + "require-main-filename": "^1.0.1", + "set-blocking": "^2.0.0", + "string-width": "^2.0.0", + "which-module": "^2.0.0", + "y18n": "^3.2.1 || ^4.0.0", + "yargs-parser": "^11.1.1" + } + } } }, "contentful-management": { @@ -3756,6 +4101,11 @@ "resolved": "https://registry.npmjs.org/elegant-spinner/-/elegant-spinner-1.0.1.tgz", "integrity": "sha1-2wQ1IcldfjA/2PNFvtwzSc+wcp4=" }, + "emoji-regex": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", + "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==" + }, "emojic": { "version": "1.1.14", "resolved": "https://registry.npmjs.org/emojic/-/emojic-1.1.14.tgz", @@ -3776,7 +4126,6 @@ "version": "1.4.1", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.1.tgz", "integrity": "sha512-1MkrZNvWTKCaigbn+W15elq2BB/L22nqrSY5DKlo3X6+vclJm8Bb5djXJBmEX6fS3+zCh/F4VBK5Z2KxJt4s2Q==", - "dev": true, "requires": { "once": "^1.4.0" } @@ -9029,6 +9378,14 @@ "tmpl": "1.0.x" } }, + "map-age-cleaner": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz", + "integrity": "sha512-bJzx6nMoP6PDLPBFmg7+xRKeFZvFboMrGlxmNj9ClvX53KrmvM5bXFXEWjbz4cz1AFn+jWJ9z/DJSz7hrs0w3w==", + "requires": { + "p-defer": "^1.0.0" + } + }, "map-cache": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/map-cache/-/map-cache-0.2.2.tgz", @@ -15938,7 +16295,6 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, "requires": { "wrappy": "1" } @@ -16100,6 +16456,11 @@ "object-assign": "^4.1.0" } }, + "p-defer": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/p-defer/-/p-defer-1.0.0.tgz", + "integrity": "sha1-n26xgvbJqozXQwBKfU+WsZaw+ww=" + }, "p-filter": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/p-filter/-/p-filter-1.0.0.tgz", @@ -16621,7 +16982,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", - "dev": true, "requires": { "end-of-stream": "^1.1.0", "once": "^1.3.1" @@ -17554,6 +17914,27 @@ "integrity": "sha512-7T/BxH19zbcCTa8XkMlbK5lTo1WtgkFi3GvdWEyNuc4Vex7/9Dqbnpsf4JMydcfj9HCg4zUWFTL3Za6lapg5/w==", "dev": true }, + "invert-kv": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-2.0.0.tgz", + "integrity": "sha512-wPVv/y/QQ/Uiirj/vh3oP+1Ww+AWehmi1g5fFWGPF6IpCBCDVrhgHRMvrLfdYcwDh3QJbGXDW4JAuzxElLSqKA==", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", + "dev": true + }, + "lcid": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", + "integrity": "sha512-avPEb8P8EGnwXKClwsNUgryVjllcRqtMYa49NTsbQagYuT1DcXnl1915oxWjoyGrXR6zH/Y0Zc96xWsPcoDKeA==", + "dev": true, + "requires": { + "invert-kv": "^2.0.0" + } + }, "load-json-file": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz", @@ -17572,12 +17953,46 @@ "integrity": "sha512-+H0L3ibcWhAZE02SKMqmvYsErLo4EAVJxu5h3bHBBDvvjeWXtl92rGUSBYHL2++5Y+RSNgl8dYOAXcYe7lp1fA==", "dev": true }, + "mem": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/mem/-/mem-4.3.0.tgz", + "integrity": "sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w==", + "dev": true, + "requires": { + "map-age-cleaner": "^0.1.1", + "mimic-fn": "^2.0.0", + "p-is-promise": "^2.0.0" + } + }, + "mimic-fn": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", + "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==", + "dev": true + }, "ms": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==", "dev": true }, + "os-locale": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-3.1.0.tgz", + "integrity": "sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==", + "dev": true, + "requires": { + "execa": "^1.0.0", + "lcid": "^2.0.0", + "mem": "^4.0.0" + } + }, + "p-is-promise": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-2.1.0.tgz", + "integrity": "sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==", + "dev": true + }, "p-limit": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.1.0.tgz", @@ -17653,6 +18068,36 @@ "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz", "integrity": "sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==", "dev": true + }, + "string-width": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "dev": true, + "requires": { + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^4.0.0" + } + }, + "yargs": { + "version": "12.0.5", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-12.0.5.tgz", + "integrity": "sha512-Lhz8TLaYnxq/2ObqHDql8dX8CJi97oHxrjUcYtzKbbykPtVW9WB+poxI+NM2UIzsMgNCZTIf0AQwsjK5yMAqZw==", + "dev": true, + "requires": { + "cliui": "^4.0.0", + "decamelize": "^1.2.0", + "find-up": "^3.0.0", + "get-caller-file": "^1.0.1", + "os-locale": "^3.0.0", + "require-directory": "^2.1.1", + "require-main-filename": "^1.0.1", + "set-blocking": "^2.0.0", + "string-width": "^2.0.0", + "which-module": "^2.0.0", + "y18n": "^3.2.1 || ^4.0.0", + "yargs-parser": "^11.1.1" + } } } }, @@ -19735,8 +20180,7 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, "write": { "version": "0.2.1", @@ -19777,11 +20221,6 @@ "integrity": "sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==", "dev": true }, - "xregexp": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/xregexp/-/xregexp-4.0.0.tgz", - "integrity": "sha512-PHyM+sQouu7xspQQwELlGwwd05mXUFqwFYfqPO0cC7x4fxyHnnuetmQr6CjJiafIDoH4MogHb9dOoJzR/Y4rFg==" - }, "xtend": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", @@ -19809,99 +20248,181 @@ } }, "yargs": { - "version": "12.0.1", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-12.0.1.tgz", - "integrity": "sha512-B0vRAp1hRX4jgIOWFtjfNjd9OA9RWYZ6tqGA9/I/IrTMsxmKvtWy+ersM+jzpQqbC3YfLzeABPdeTgcJ9eu1qQ==", + "version": "13.2.4", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-13.2.4.tgz", + "integrity": "sha512-HG/DWAJa1PAnHT9JAhNa8AbAv3FPaiLzioSjCcmuXXhP8MlpHO5vwls4g4j6n30Z74GVQj8Xa62dWVx1QCGklg==", "requires": { - "cliui": "^4.0.0", - "decamelize": "^2.0.0", + "cliui": "^5.0.0", "find-up": "^3.0.0", - "get-caller-file": "^1.0.1", - "os-locale": "^2.0.0", + "get-caller-file": "^2.0.1", + "os-locale": "^3.1.0", "require-directory": "^2.1.1", - "require-main-filename": "^1.0.1", + "require-main-filename": "^2.0.0", "set-blocking": "^2.0.0", - "string-width": "^2.0.0", + "string-width": "^3.0.0", "which-module": "^2.0.0", - "y18n": "^3.2.1 || ^4.0.0", - "yargs-parser": "^10.1.0" + "y18n": "^4.0.0", + "yargs-parser": "^13.1.0" }, "dependencies": { - "decamelize": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-2.0.0.tgz", - "integrity": "sha512-Ikpp5scV3MSYxY39ymh45ZLEecsTdv/Xj2CaQfI8RLMuwi7XvjX9H/fhraiSuU+C5w5NTDu4ZU72xNiZnurBPg==", + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==" + }, + "cliui": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-5.0.0.tgz", + "integrity": "sha512-PYeGSEmmHM6zvoef2w8TPzlrnNpXIjTipYK780YswmIP9vjxmd6Y2a3CB2Ks6/AU8NHjZugXvo8w3oWM2qnwXA==", "requires": { - "xregexp": "4.0.0" + "string-width": "^3.1.0", + "strip-ansi": "^5.2.0", + "wrap-ansi": "^5.1.0" } }, - "find-up": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", - "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", + "cross-spawn": { + "version": "6.0.5", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", + "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", + "requires": { + "nice-try": "^1.0.4", + "path-key": "^2.0.1", + "semver": "^5.5.0", + "shebang-command": "^1.2.0", + "which": "^1.2.9" + } + }, + "execa": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/execa/-/execa-1.0.0.tgz", + "integrity": "sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==", + "requires": { + "cross-spawn": "^6.0.0", + "get-stream": "^4.0.0", + "is-stream": "^1.1.0", + "npm-run-path": "^2.0.0", + "p-finally": "^1.0.0", + "signal-exit": "^3.0.0", + "strip-eof": "^1.0.0" + } + }, + "get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==" + }, + "get-stream": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", + "integrity": "sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==", "requires": { - "locate-path": "^3.0.0" + "pump": "^3.0.0" } }, + "invert-kv": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-2.0.0.tgz", + "integrity": "sha512-wPVv/y/QQ/Uiirj/vh3oP+1Ww+AWehmi1g5fFWGPF6IpCBCDVrhgHRMvrLfdYcwDh3QJbGXDW4JAuzxElLSqKA==" + }, "is-fullwidth-code-point": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=" }, - "locate-path": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", - "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", + "lcid": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", + "integrity": "sha512-avPEb8P8EGnwXKClwsNUgryVjllcRqtMYa49NTsbQagYuT1DcXnl1915oxWjoyGrXR6zH/Y0Zc96xWsPcoDKeA==", "requires": { - "p-locate": "^3.0.0", - "path-exists": "^3.0.0" + "invert-kv": "^2.0.0" } }, - "p-limit": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.0.0.tgz", - "integrity": "sha512-fl5s52lI5ahKCernzzIyAP0QAZbGIovtVHGwpcu1Jr/EpzLVDI2myISHwGqK7m8uQFugVWSrbxH7XnhGtvEc+A==", + "mem": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/mem/-/mem-4.3.0.tgz", + "integrity": "sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w==", "requires": { - "p-try": "^2.0.0" + "map-age-cleaner": "^0.1.1", + "mimic-fn": "^2.0.0", + "p-is-promise": "^2.0.0" } }, - "p-locate": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", - "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", + "mimic-fn": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", + "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==" + }, + "os-locale": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-3.1.0.tgz", + "integrity": "sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==", "requires": { - "p-limit": "^2.0.0" + "execa": "^1.0.0", + "lcid": "^2.0.0", + "mem": "^4.0.0" } }, - "p-try": { + "p-is-promise": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-2.1.0.tgz", + "integrity": "sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==" + }, + "require-main-filename": { "version": "2.0.0", - "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.0.0.tgz", - "integrity": "sha512-hMp0onDKIajHfIkdRk3P4CdCmErkYAxxDtP3Wx/4nZ3aGlau2VKh3mZpcuFkH27WQkL/3WBCPOktzA9ZOAnMQQ==" + "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz", + "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==" }, "string-width": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", - "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz", + "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==", "requires": { + "emoji-regex": "^7.0.1", "is-fullwidth-code-point": "^2.0.0", - "strip-ansi": "^4.0.0" + "strip-ansi": "^5.1.0" + } + }, + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "requires": { + "ansi-regex": "^4.1.0" + } + }, + "wrap-ansi": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-5.1.0.tgz", + "integrity": "sha512-QC1/iN/2/RPVJ5jYK8BGttj5z83LmSKmvbvrXPNCLZSEb32KKVDJDl/MOt2N01qU2H/FkzEa9PKto1BqDjtd7Q==", + "requires": { + "ansi-styles": "^3.2.0", + "string-width": "^3.0.0", + "strip-ansi": "^5.0.0" + } + }, + "y18n": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", + "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==" + }, + "yargs-parser": { + "version": "13.1.1", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.1.tgz", + "integrity": "sha512-oVAVsHz6uFrg3XQheFII8ESO2ssAf9luWuAd6Wexsu4F3OtIW0o8IribPXYrD4WC24LWtPrJlGy87y5udK+dxQ==", + "requires": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" } } } }, "yargs-parser": { - "version": "10.1.0", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-10.1.0.tgz", - "integrity": "sha512-VCIyR1wJoEBZUqk5PA+oOBF6ypbwh5aNB3I50guxAL/quggdfs4TtNHQrSazFA3fYZ+tEqfs0zIGlv0c/rgjbQ==", + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-11.1.1.tgz", + "integrity": "sha512-C6kB/WJDiaxONLJQnF8ccx9SEeoTTLek8RVbaOIsrAUS8VrBEXfmeSnCZxygc+XC2sNMBIwOOnfcxiynjHsVSQ==", "requires": { - "camelcase": "^4.1.0" - }, - "dependencies": { - "camelcase": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-4.1.0.tgz", - "integrity": "sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0=" - } + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" } }, "zlib": { diff --git a/package.json b/package.json index deacdf09e..4936c0258 100644 --- a/package.json +++ b/package.json @@ -112,7 +112,7 @@ "to-ast": "^1.0.0", "tree-kill": "^1.1.0", "wrap-ansi": "^3.0.1", - "yargs": "^12.0.1", + "yargs": "^13.2.4", "zlib": "^1.0.5" }, "engines": { diff --git a/test/e2e/__snapshots__/basics.test.js.snap b/test/e2e/__snapshots__/basics.test.js.snap index c68432ba1..5cd944651 100644 --- a/test/e2e/__snapshots__/basics.test.js.snap +++ b/test/e2e/__snapshots__/basics.test.js.snap @@ -5,16 +5,14 @@ exports[`should print help message 1`] = ` Usage: contentful [args] Commands: - boilerplate Download a boilerplate - config Manage and list your configs - content-type Manage and list your space content types - [aliases: ct] - extension Manage and list your extensions - guide A guide introducing basic concepts of working with - Contentful - login Login to Contentful - logout Logout from Contentful - space Manage and list your spaces + boilerplate Download a boilerplate + config Manage and list your configs + content-type Manage and list your space content types [aliases: ct] + extension Manage and list your extensions + guide A guide introducing basic concepts of working with Contentful + login Login to Contentful + logout Logout from Contentful + space Manage and list your spaces Options: -h, --help Show help [boolean] @@ -28,16 +26,14 @@ exports[`should print help message on shortcut 1`] = ` Usage: contentful [args] Commands: - boilerplate Download a boilerplate - config Manage and list your configs - content-type Manage and list your space content types - [aliases: ct] - extension Manage and list your extensions - guide A guide introducing basic concepts of working with - Contentful - login Login to Contentful - logout Logout from Contentful - space Manage and list your spaces + boilerplate Download a boilerplate + config Manage and list your configs + content-type Manage and list your space content types [aliases: ct] + extension Manage and list your extensions + guide A guide introducing basic concepts of working with Contentful + login Login to Contentful + logout Logout from Contentful + space Manage and list your spaces Options: -h, --help Show help [boolean] @@ -51,16 +47,14 @@ exports[`should print help message on wrong subcommand 1`] = ` Usage: contentful [args] Commands: - boilerplate Download a boilerplate - config Manage and list your configs - content-type Manage and list your space content types - [aliases: ct] - extension Manage and list your extensions - guide A guide introducing basic concepts of working with - Contentful - login Login to Contentful - logout Logout from Contentful - space Manage and list your spaces + boilerplate Download a boilerplate + config Manage and list your configs + content-type Manage and list your space content types [aliases: ct] + extension Manage and list your extensions + guide A guide introducing basic concepts of working with Contentful + login Login to Contentful + logout Logout from Contentful + space Manage and list your spaces Options: -h, --help Show help [boolean] @@ -76,16 +70,14 @@ exports[`should return code 1 when errors exist no args 1`] = ` Usage: contentful [args] Commands: - boilerplate Download a boilerplate - config Manage and list your configs - content-type Manage and list your space content types - [aliases: ct] - extension Manage and list your extensions - guide A guide introducing basic concepts of working with - Contentful - login Login to Contentful - logout Logout from Contentful - space Manage and list your spaces + boilerplate Download a boilerplate + config Manage and list your configs + content-type Manage and list your space content types [aliases: ct] + extension Manage and list your extensions + guide A guide introducing basic concepts of working with Contentful + login Login to Contentful + logout Logout from Contentful + space Manage and list your spaces Options: -h, --help Show help [boolean] diff --git a/test/integration/cmds/__snapshots__/main.test.js.snap b/test/integration/cmds/__snapshots__/main.test.js.snap index aac2b946c..393e1b399 100644 --- a/test/integration/cmds/__snapshots__/main.test.js.snap +++ b/test/integration/cmds/__snapshots__/main.test.js.snap @@ -4,16 +4,14 @@ exports[`should print help message on shortcut: help data is incorrect 1`] = ` "Usage: contentful [args] Commands: - boilerplate Download a boilerplate - config Manage and list your configs - content-type Manage and list your space content types - [aliases: ct] - extension Manage and list your extensions - guide A guide introducing basic concepts of working with - Contentful - login Login to Contentful - logout Logout from Contentful - space Manage and list your spaces + boilerplate Download a boilerplate + config Manage and list your configs + content-type Manage and list your space content types [aliases: ct] + extension Manage and list your extensions + guide A guide introducing basic concepts of working with Contentful + login Login to Contentful + logout Logout from Contentful + space Manage and list your spaces Options: -h, --help Show help [boolean] @@ -26,16 +24,14 @@ exports[`should print help message on wrong subcommand: help data is incorrect 1 "Usage: contentful [args] Commands: - boilerplate Download a boilerplate - config Manage and list your configs - content-type Manage and list your space content types - [aliases: ct] - extension Manage and list your extensions - guide A guide introducing basic concepts of working with - Contentful - login Login to Contentful - logout Logout from Contentful - space Manage and list your spaces + boilerplate Download a boilerplate + config Manage and list your configs + content-type Manage and list your space content types [aliases: ct] + extension Manage and list your extensions + guide A guide introducing basic concepts of working with Contentful + login Login to Contentful + logout Logout from Contentful + space Manage and list your spaces Options: -h, --help Show help [boolean] @@ -49,16 +45,14 @@ exports[`should print help message: help data is incorrect 1`] = ` "Usage: contentful [args] Commands: - boilerplate Download a boilerplate - config Manage and list your configs - content-type Manage and list your space content types - [aliases: ct] - extension Manage and list your extensions - guide A guide introducing basic concepts of working with - Contentful - login Login to Contentful - logout Logout from Contentful - space Manage and list your spaces + boilerplate Download a boilerplate + config Manage and list your configs + content-type Manage and list your space content types [aliases: ct] + extension Manage and list your extensions + guide A guide introducing basic concepts of working with Contentful + login Login to Contentful + logout Logout from Contentful + space Manage and list your spaces Options: -h, --help Show help [boolean] @@ -71,16 +65,14 @@ exports[`should return code 1 when errors exist no args: help data is incorrect "Usage: contentful [args] Commands: - boilerplate Download a boilerplate - config Manage and list your configs - content-type Manage and list your space content types - [aliases: ct] - extension Manage and list your extensions - guide A guide introducing basic concepts of working with - Contentful - login Login to Contentful - logout Logout from Contentful - space Manage and list your spaces + boilerplate Download a boilerplate + config Manage and list your configs + content-type Manage and list your space content types [aliases: ct] + extension Manage and list your extensions + guide A guide introducing basic concepts of working with Contentful + login Login to Contentful + logout Logout from Contentful + space Manage and list your spaces Options: -h, --help Show help [boolean] diff --git a/test/integration/cmds/config/list.test.js b/test/integration/cmds/config/list.test.js index d64dcac0b..08602a9c5 100644 --- a/test/integration/cmds/config/list.test.js +++ b/test/integration/cmds/config/list.test.js @@ -12,7 +12,7 @@ const app = () => { let oldConfigContents = null const testConfigPath = process.cwd() + '/.contentfulrc.json' const testConfig = { - cmaToken: 'blahblah12234553', + managementToken: 'blahblah12234553', activeSpaceId: '89898989' } @@ -41,7 +41,7 @@ test('Should list configs from first found config file', done => { .expect(result => { const resultText = result.stdout.trim() expect( - resultText.includes(testConfig.cmaToken) && resultText.includes(testConfig.activeSpaceId) + resultText.includes(testConfig.managementToken) && resultText.includes(testConfig.activeSpaceId) ).toBe(true) }) .after(after) diff --git a/test/integration/cmds/extension/__snapshots__/create.test.js.snap b/test/integration/cmds/extension/__snapshots__/create.test.js.snap index 9dbdba483..666a69e1b 100644 --- a/test/integration/cmds/extension/__snapshots__/create.test.js.snap +++ b/test/integration/cmds/extension/__snapshots__/create.test.js.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`should print help message: help data is incorrect 1`] = ` -"contentful.js extension create +"extension create Create an extension diff --git a/test/integration/cmds/extension/__snapshots__/delete.test.js.snap b/test/integration/cmds/extension/__snapshots__/delete.test.js.snap index cf89b27ac..1199a58d3 100644 --- a/test/integration/cmds/extension/__snapshots__/delete.test.js.snap +++ b/test/integration/cmds/extension/__snapshots__/delete.test.js.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`should print help message: help data is incorrect 1`] = ` -"contentful.js extension delete +"extension delete Delete an extension diff --git a/test/integration/cmds/extension/__snapshots__/update.test.js.snap b/test/integration/cmds/extension/__snapshots__/update.test.js.snap index e3f6354ff..cc20b9cee 100644 --- a/test/integration/cmds/extension/__snapshots__/update.test.js.snap +++ b/test/integration/cmds/extension/__snapshots__/update.test.js.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`should print help message: help data is incorrect 1`] = ` -"contentful.js extension update +"extension update Update an extension diff --git a/test/integration/cmds/space/__snapshots__/delete.test.js.snap b/test/integration/cmds/space/__snapshots__/delete.test.js.snap index 6b84928df..7e1349d32 100644 --- a/test/integration/cmds/space/__snapshots__/delete.test.js.snap +++ b/test/integration/cmds/space/__snapshots__/delete.test.js.snap @@ -12,7 +12,7 @@ Options: -h, --help Show help [boolean] --space-id, -s ID of the space to delete --management-token, --mt Contentful management API token [string] - --yes Skip the confirmation question [default: false] + --yes Skip the confirmation question Copyright 2018 Contentful, this is a BETA release" `; diff --git a/test/integration/cmds/space/__snapshots__/help.test.js.snap b/test/integration/cmds/space/__snapshots__/help.test.js.snap index 36e009abf..68c0782ba 100644 --- a/test/integration/cmds/space/__snapshots__/help.test.js.snap +++ b/test/integration/cmds/space/__snapshots__/help.test.js.snap @@ -2,22 +2,18 @@ exports[`should print help message on shortcut: help data is incorrect 1`] = ` "Commands: - space accesstoken Manage and list your delivery access tokens - [aliases: at] - space create Create a space - space delete Deletes a space - space environment Manage and list environments - space export export a space data to a json file - space generate Generate code for different tasks[aliases: g] - space import import a space - space list List your spaces [aliases: ls] - space migration Parses and runs a migration script on a - Contentful space - space seed Seed a content model and content based on - given templates - space use Sets the default space which every command - will use when the --space-id option is - skipped. [aliases: u] + space accesstoken Manage and list your delivery access tokens [aliases: at] + space create Create a space + space delete Deletes a space + space environment Manage and list environments + space export export a space data to a json file + space generate Generate code for different tasks [aliases: g] + space import import a space + space list List your spaces [aliases: ls] + space migration Parses and runs a migration script on a Contentful space + space seed Seed a content model and content based on given templates + space use Sets the default space which every command will use when + the --space-id option is skipped. [aliases: u] Options: -h, --help Show help [boolean]" @@ -25,22 +21,18 @@ Options: exports[`should print help message when no command provided: wrong response in case of no command provided 1`] = ` "Commands: - space accesstoken Manage and list your delivery access tokens - [aliases: at] - space create Create a space - space delete Deletes a space - space environment Manage and list environments - space export export a space data to a json file - space generate Generate code for different tasks[aliases: g] - space import import a space - space list List your spaces [aliases: ls] - space migration Parses and runs a migration script on a - Contentful space - space seed Seed a content model and content based on - given templates - space use Sets the default space which every command - will use when the --space-id option is - skipped. [aliases: u] + space accesstoken Manage and list your delivery access tokens [aliases: at] + space create Create a space + space delete Deletes a space + space environment Manage and list environments + space export export a space data to a json file + space generate Generate code for different tasks [aliases: g] + space import import a space + space list List your spaces [aliases: ls] + space migration Parses and runs a migration script on a Contentful space + space seed Seed a content model and content based on given templates + space use Sets the default space which every command will use when + the --space-id option is skipped. [aliases: u] Options: -h, --help Show help [boolean] @@ -49,22 +41,18 @@ Please specify a sub command." exports[`should print help message: help data is incorrect 1`] = ` "Commands: - space accesstoken Manage and list your delivery access tokens - [aliases: at] - space create Create a space - space delete Deletes a space - space environment Manage and list environments - space export export a space data to a json file - space generate Generate code for different tasks[aliases: g] - space import import a space - space list List your spaces [aliases: ls] - space migration Parses and runs a migration script on a - Contentful space - space seed Seed a content model and content based on - given templates - space use Sets the default space which every command - will use when the --space-id option is - skipped. [aliases: u] + space accesstoken Manage and list your delivery access tokens [aliases: at] + space create Create a space + space delete Deletes a space + space environment Manage and list environments + space export export a space data to a json file + space generate Generate code for different tasks [aliases: g] + space import import a space + space list List your spaces [aliases: ls] + space migration Parses and runs a migration script on a Contentful space + space seed Seed a content model and content based on given templates + space use Sets the default space which every command will use when + the --space-id option is skipped. [aliases: u] Options: -h, --help Show help [boolean]" diff --git a/test/integration/scripts/pre-test.js b/test/integration/scripts/pre-test.js index 39038f841..116109d6c 100644 --- a/test/integration/scripts/pre-test.js +++ b/test/integration/scripts/pre-test.js @@ -18,7 +18,7 @@ const setup = async () => { try { console.log('Writing test config...') const testConfig = { - cmaToken: process.env.CLI_E2E_CMA_TOKEN, + managementToken: process.env.CLI_E2E_CMA_TOKEN, proxy: { host: 'localhost', port: 3333, diff --git a/test/integration/util.js b/test/integration/util.js index a25df3acd..0a026a57c 100644 --- a/test/integration/util.js +++ b/test/integration/util.js @@ -13,16 +13,16 @@ export async function initConfig () { try { await stat(configFile) } catch (e) { - return writeFile(configFile, JSON.stringify({ cmaToken: process.env.CLI_E2E_CMA_TOKEN }, null, 4)) + return writeFile(configFile, JSON.stringify({ managementToken: process.env.CLI_E2E_CMA_TOKEN }, null, 4)) } const configParams = require(configFile) - if (configParams.cmaToken !== null) { + if (configParams.managementToken !== null) { return configParams } - return writeFile(configFile, JSON.stringify({ cmaToken: process.env.CLI_E2E_CMA_TOKEN }, null, 4)) + return writeFile(configFile, JSON.stringify({ managementToken: process.env.CLI_E2E_CMA_TOKEN }, null, 4)) } export async function deleteSpaces (spacesToDelete) { diff --git a/test/unit/cmds/boilerplate.test.js b/test/unit/cmds/boilerplate.test.js index e09cb2f83..c997cbd0f 100644 --- a/test/unit/cmds/boilerplate.test.js +++ b/test/unit/cmds/boilerplate.test.js @@ -39,6 +39,14 @@ const mockedSpace = { createApiKey: jest.fn().mockImplementation(() => mockedApiKey) } +const defaults = { + context: { + managementToken: 'management-token', + activeSpaceId: 'space', + activeEnvironmentId: 'master' + } +} + createManagementClient.mockImplementation(() => ({ getSpace: jest.fn(() => mockedSpace), getApiKeys: jest.fn(() => [{ @@ -73,10 +81,11 @@ test( 'successfully downloads boilerplate and generates access token', async () => { getContext.mockResolvedValue({ - cmaToken: 'mocked' + managementToken: 'mocked', + spaceId: mockedSpace.sys.id }) await downloadBoilerplate({ - spaceId: mockedSpace.sys.id + context: { ...defaults.context, activeSpaceId: mockedSpace.sys.id } }) expect(axios.mock.calls).toHaveLength(2) expect(createWriteStreamMock.mock.calls).toHaveLength(1) @@ -86,10 +95,10 @@ test( test('requires login', async () => { getContext.mockResolvedValue({ - cmaToken: null + managementToken: null }) try { - await expect(downloadBoilerplate({})).rejects.toThrowError(PreconditionFailedError) + await expect(downloadBoilerplate({context: {}})).rejects.toThrowError(PreconditionFailedError) } catch (error) { expect(error.message.includes('You have to be logged in to do this')).toBeTruthy() } @@ -97,10 +106,10 @@ test('requires login', async () => { test('requires spaceId and fails without', async () => { getContext.mockResolvedValue({ - cmaToken: 'mocked' + managementToken: 'mocked' }) try { - await expect(downloadBoilerplate({})).rejects.toThrowError(PreconditionFailedError) + await expect(downloadBoilerplate({context: {managementToken: 'management-token'}})).rejects.toThrowError(PreconditionFailedError) } catch (error) { expect(error.message.includes('You need to provide a space id')).toBeTruthy() } @@ -108,7 +117,7 @@ test('requires spaceId and fails without', async () => { test('requires spaceId and accepts it from context', async () => { getContext.mockResolvedValue({ - cmaToken: 'mocked', + managementToken: 'mocked', activeSpaceId: 'mocked' }) await expect(downloadBoilerplate).not.toThrowError('works with space id provided via context') @@ -116,9 +125,7 @@ test('requires spaceId and accepts it from context', async () => { test('requires spaceId and accepts it from argv arguments', async () => { getContext.mockResolvedValue({ - cmaToken: 'mocked' + managementToken: 'mocked' }) - await expect(() => downloadBoilerplate({ - spaceId: 'mocked' - })).not.toThrowError('works with space id provided via arguments') + await expect(() => downloadBoilerplate(defaults)).not.toThrowError('works with space id provided via arguments') }) diff --git a/test/unit/cmds/config_cmds/add.test.js b/test/unit/cmds/config_cmds/add.test.js index 6f2d6201c..5e258fd89 100644 --- a/test/unit/cmds/config_cmds/add.test.js +++ b/test/unit/cmds/config_cmds/add.test.js @@ -1,7 +1,6 @@ import { addHandler } from '../../../../lib/cmds/config_cmds/add' import { setContext, - getContext, storeRuntimeConfig } from '../../../../lib/context' import { success } from '../../../../lib/utils/log' @@ -10,12 +9,10 @@ import { successEmoji } from '../../../../lib/utils/emojis' jest.mock('../../../../lib/context') jest.mock('../../../../lib/utils/log') -getContext.mockResolvedValue({}) storeRuntimeConfig.mockResolvedValue() afterEach(() => { setContext.mockClear() - getContext.mockClear() storeRuntimeConfig.mockClear() success.mockClear() }) diff --git a/test/unit/cmds/config_cmds/remove.test.js b/test/unit/cmds/config_cmds/remove.test.js index 3ae556b27..dc4d36b08 100644 --- a/test/unit/cmds/config_cmds/remove.test.js +++ b/test/unit/cmds/config_cmds/remove.test.js @@ -1,7 +1,6 @@ import { removeHandler } from '../../../../lib/cmds/config_cmds/remove' import { setContext, - getContext, storeRuntimeConfig } from '../../../../lib/context' import { success } from '../../../../lib/utils/log' @@ -10,11 +9,10 @@ import { successEmoji } from '../../../../lib/utils/emojis' jest.mock('../../../../lib/context') jest.mock('../../../../lib/utils/log') -getContext.mockResolvedValue({ cmaToken: 'cmaToken', proxy: {} }) storeRuntimeConfig.mockResolvedValue() test('config remove command', async () => { - await removeHandler({proxy: true}) - expect(setContext.mock.calls[0][0]).toEqual({cmaToken: 'cmaToken'}) + await removeHandler({context: {managementToken: 'managementToken'}, proxy: true}) + expect(setContext.mock.calls[0][0]).toEqual({managementToken: 'managementToken'}) expect(success).toHaveBeenCalledWith(`${successEmoji} config removed successfully`) }) diff --git a/test/unit/cmds/content-type_cmds/list.test.js b/test/unit/cmds/content-type_cmds/list.test.js index 3c74508a9..470832c63 100644 --- a/test/unit/cmds/content-type_cmds/list.test.js +++ b/test/unit/cmds/content-type_cmds/list.test.js @@ -1,5 +1,4 @@ import { handler } from '../../../../lib/cmds/content-type_cmds/list' -import { getContext } from '../../../../lib/context' import { log } from '../../../../lib/utils/log' import { createManagementClient } from '../../../../lib/utils/contentful-clients' @@ -56,11 +55,6 @@ const fakeClient = { } createManagementClient.mockResolvedValue(fakeClient) -getContext.mockResolvedValue({ - cmaToken: 'mockedToken', - activeSpaceId: 'someSpaceId' -}) - afterEach(() => { createManagementClient.mockClear() getContentTypesSub.mockClear() @@ -68,7 +62,13 @@ afterEach(() => { }) test('List content types from default environment, "master"', async () => { - await handler({}) + await handler({ + context: { + managementToken: 'mockedToken', + activeSpaceId: 'someSpaceId', + activeEnvironmentId: 'master' + } + }) expect(createManagementClient).toHaveBeenCalledTimes(1) expect(getContentTypesSub).toHaveBeenCalledTimes(1) @@ -88,14 +88,14 @@ test('List content types from default environment, "master"', async () => { }) test('List content types based on active environment if available', async () => { - getContext.mockResolvedValue({ - cmaToken: 'mockedToken', - activeSpaceId: 'someSpaceId', - activeEnvironmentId: 'develop' + await handler({ + context: { + managementToken: 'mockedToken', + activeSpaceId: 'someSpaceId', + activeEnvironmentId: 'develop' + } }) - await handler({}) - expect(createManagementClient).toHaveBeenCalledTimes(1) expect(getContentTypesSub).toHaveBeenCalledTimes(1) @@ -115,7 +115,11 @@ test('List content types based on active environment if available', async () => test('List content types based on environment passed if --environment-id option is used', async () => { const stubArgv = { - environmentId: 'test' + context: { + managementToken: 'mockedToken', + activeSpaceId: 'someSpaceId', + activeEnvironmentId: 'test' + } } await handler(stubArgv) diff --git a/test/unit/cmds/extension_cmds/create.test.js b/test/unit/cmds/extension_cmds/create.test.js index eea988245..c1d57fea0 100644 --- a/test/unit/cmds/extension_cmds/create.test.js +++ b/test/unit/cmds/extension_cmds/create.test.js @@ -2,7 +2,6 @@ import { resolve } from 'path' import { createExtensionHandler } from '../../../../lib/cmds/extension_cmds/create' -import { getContext } from '../../../../lib/context' import { successEmoji } from '../../../../lib/utils/emojis' import { success, log } from '../../../../lib/utils/log' import { createManagementClient } from '../../../../lib/utils/contentful-clients' @@ -26,6 +25,14 @@ const createUiExtensionStub = jest.fn().mockResolvedValue({ sys: { id: '123', version: 3 } }) +const defaults = { + context: { + managementToken: 'management-token', + activeSpaceId: 'space', + activeEnvironmentId: 'master' + } +} + const fakeClient = { getSpace: async () => ({ getEnvironment: async () => ({ @@ -36,11 +43,6 @@ const fakeClient = { createManagementClient.mockResolvedValue(fakeClient) beforeEach(() => { - getContext.mockReset() - getContext.mockResolvedValue({ - cmaToken: 'mockedToken', - activeSpaceId: 'someSpaceId' - }) success.mockClear() log.mockClear() createManagementClient.mockClear() @@ -49,25 +51,32 @@ beforeEach(() => { test('Throws error if name is missing', async () => { await expect( - createExtensionHandler({ spaceId: 'space', fieldTypes: ['Symbol'], src: 'https://awesome.extension' }) + createExtensionHandler({ + fieldTypes: ['Symbol'], + src: 'https://awesome.extension', + ...defaults + }) ).rejects.toThrowErrorMatchingSnapshot() }) test('Throws error if both src and srcdoc are not provided', async () => { await expect( - createExtensionHandler({ spaceId: 'space', environmentId: 'master', name: 'Widget', fieldTypes: ['Symbol'] }) + createExtensionHandler({ + fieldTypes: ['Symbol'], + name: 'Widget', + ...defaults + }) ).rejects.toThrowErrorMatchingSnapshot() }) test('Throws error if both src and srcdoc are at the same time', async () => { await expect( createExtensionHandler({ - spaceId: 'space', name: 'Widget', - environmentId: 'master', fieldTypes: ['Symbol'], src: 'https://awesome.extension', - srcdoc: './awesome-extension.html' + srcdoc: './awesome-extension.html', + ...defaults }) ).rejects.toThrowErrorMatchingSnapshot() }) @@ -75,20 +84,20 @@ test('Throws error if both src and srcdoc are at the same time', async () => { test('Throws an error if installation parameters cannot be parsed', async () => { await expect( createExtensionHandler({ - spaceId: 'space', name: 'Widget', fieldTypes: ['Symbol'], src: 'https://awesome.extension', - installationParameters: '{"test": lol}' + installationParameters: '{"test": lol}', + ...defaults }) ).rejects.toThrowErrorMatchingSnapshot() }) test('Creates extension if field-types is missing', async () => { await createExtensionHandler({ - spaceId: 'space', name: 'Widget', - src: 'https://awesome.extension' + src: 'https://awesome.extension', + ...defaults }) expect(createUiExtensionStub).toHaveBeenCalledWith({ @@ -103,10 +112,10 @@ test('Creates extension if field-types is missing', async () => { test('Creates extension from command line arguments', async () => { await createExtensionHandler({ - spaceId: 'space', name: 'Widget', fieldTypes: ['Symbol'], - src: 'https://awesome.extension' + src: 'https://awesome.extension', + ...defaults }) expect(createUiExtensionStub).toHaveBeenCalledWith({ @@ -122,11 +131,10 @@ test('Creates extension from command line arguments', async () => { test('Logs extension data', async () => { await createExtensionHandler({ - spaceId: 'space', - environmentId: 'master', name: 'Widget', fieldTypes: ['Symbol'], - src: 'https://awesome.extension' + src: 'https://awesome.extension', + ...defaults }) const values = [ '123', 'Widget', 'Symbol', 'https://awesome.extension' ] @@ -141,35 +149,6 @@ test('Logs extension data', async () => { expect(success).toHaveBeenCalledWith(`${successEmoji} Successfully created extension:\n`) }) -test('Uses activeEnvironmentId if environmentId is not specified', async () => { - getContext.mockReset() - getContext.mockResolvedValue({ - cmaToken: 'mockedToken', - activeSpaceId: 'someSpaceId', - activeEnvironmentId: 'test' - }) - - await createExtensionHandler({ - spaceId: 'space', - name: 'Widget', - fieldTypes: ['Symbol'], - src: 'https://awesome.extension' - }) - - const values = [ '123', 'Widget', 'Symbol', 'https://awesome.extension' ] - - expect(log.mock.calls[0][0]).toContain('Space: space') - expect(log.mock.calls[1][0]).toContain('Environment: test') - expect(log.mock.calls[2][0]).toContain( - 'Your extension: https://app.contentful.com/spaces/space/environments/test/settings/extensions/123' - ) - values.forEach(value => { - expect(log.mock.calls[3][0]).toContain(value) - }) - - expect(success).toHaveBeenCalledWith(`${successEmoji} Successfully created extension:\n`) -}) - test('Creates extension with values from descriptor file', async () => { const descriptor = `{ "name": "Test Extension", @@ -179,7 +158,10 @@ test('Creates extension with values from descriptor file', async () => { readFileP.mockResolvedValue(descriptor) - await createExtensionHandler({ descriptor: 'test.json' }) + await createExtensionHandler({ + descriptor: 'test.json', + ...defaults + }) expect(createUiExtensionStub).toHaveBeenCalledWith({ extension: { @@ -207,7 +189,11 @@ test( readFileP.mockResolvedValue(descriptor) - await createExtensionHandler({ descriptor: 'x.json', installationParameters: JSON.stringify({flag: true}) }) + await createExtensionHandler({ + descriptor: 'x.json', + installationParameters: JSON.stringify({flag: true}), + ...defaults + }) expect(createUiExtensionStub).toHaveBeenCalledWith({ extension: { @@ -237,7 +223,11 @@ test( readFileP.mockResolvedValue(descriptor) - await createExtensionHandler({ descriptor: 'test.json', srcdoc: resolve(__dirname, 'sample-extension.html') }) + await createExtensionHandler({ + descriptor: 'test.json', + srcdoc: resolve(__dirname, 'sample-extension.html'), + ...defaults + }) expect(createUiExtensionStub).toHaveBeenCalledWith({ extension: { @@ -253,11 +243,10 @@ test( test('Creates extension and reads srcdoc from disk', async () => { await createExtensionHandler({ - spaceId: 'space', - environmentId: 'master', name: 'Widget', fieldTypes: ['Symbol'], - srcdoc: resolve(__dirname, 'sample-extension.html') + srcdoc: resolve(__dirname, 'sample-extension.html'), + ...defaults }) expect(createUiExtensionStub).toHaveBeenCalledWith({ diff --git a/test/unit/cmds/extension_cmds/delete.test.js b/test/unit/cmds/extension_cmds/delete.test.js index 79a6300d0..392a649e4 100644 --- a/test/unit/cmds/extension_cmds/delete.test.js +++ b/test/unit/cmds/extension_cmds/delete.test.js @@ -1,6 +1,5 @@ import { deleteExtension } from '../../../../lib/cmds/extension_cmds/delete' -import { getContext } from '../../../../lib/context' import { successEmoji } from '../../../../lib/utils/emojis' import { success } from '../../../../lib/utils/log' import { createManagementClient } from '../../../../lib/utils/contentful-clients' @@ -23,26 +22,34 @@ const fakeClient = { } createManagementClient.mockResolvedValue(fakeClient) -getContext.mockResolvedValue({ - cmaToken: 'mockedToken', - activeSpaceId: 'someSpaceId' -}) - beforeEach(() => { success.mockClear() createManagementClient.mockClear() }) test('Throws error if --version and --force are missing', async () => { - await expect(deleteExtension({ spaceId: 'space', id: 'test' })).rejects.toThrowErrorMatchingSnapshot() + await expect(deleteExtension({ + context: { + managementToken: 'managementToken', + activeSpaceId: 'space' + }, + id: 'test' + })).rejects.toThrowErrorMatchingSnapshot() }) test('Throws error if wrong --version value is passed', async () => { - await expect(deleteExtension({ spaceId: 'space', id: 'test', version: 4 })).rejects.toThrowErrorMatchingSnapshot() + await expect(deleteExtension({ + context: { + managementToken: 'managementToken', + activeSpaceId: 'space' + }, + id: 'test', + version: 4 + })).rejects.toThrowErrorMatchingSnapshot() }) test('Logs message if delete is successful', async () => { - await deleteExtension({spaceId: 'space', id: 'test', force: true}) + await deleteExtension({ context: { managementToken: 'managementToken', activeSpaceId: 'space' }, id: 'test', force: true }) expect(deleteStub).toHaveBeenCalledTimes(1) expect(success).toHaveBeenLastCalledWith(`${successEmoji} Successfully deleted extension with ID test`) }) diff --git a/test/unit/cmds/extension_cmds/get.test.js b/test/unit/cmds/extension_cmds/get.test.js index ba74a2a58..5dddeb99b 100644 --- a/test/unit/cmds/extension_cmds/get.test.js +++ b/test/unit/cmds/extension_cmds/get.test.js @@ -1,7 +1,5 @@ import { handler } from '../../../../lib/cmds/extension_cmds/get' -import { getContext } from '../../../../lib/context' - import { log } from '../../../../lib/utils/log' import { createManagementClient } from '../../../../lib/utils/contentful-clients' @@ -27,24 +25,19 @@ const fakeClient = { } createManagementClient.mockResolvedValue(fakeClient) -getContext.mockResolvedValue({ - cmaToken: 'mockedToken', - activeSpaceId: 'someSpaceId' -}) - beforeEach(() => { log.mockClear() createManagementClient.mockClear() }) test('Calls getUiExtension() with ID', async () => { - await handler({ spaceId: 'space1', id: 'widget1' }) + await handler({ context: { activeSpaceId: 'space1', activeEnvironmentId: 'master', managementToken: 'token' }, id: 'widget1' }) expect(getUiExtensionStub).toHaveBeenCalledWith('widget1') }) test('Logs extension data', async () => { - await handler({ spaceId: 'space1', id: 'widget1' }) + await handler({ context: { activeSpaceId: 'space1', activeEnvironmentId: 'master', managementToken: 'token' }, id: 'widget1' }) const outputValues = [ '123', 'Widget', 'Symbol, Symbols', 'https://awesome.extension' ] diff --git a/test/unit/cmds/extension_cmds/list.test.js b/test/unit/cmds/extension_cmds/list.test.js index 81312f398..b0978cee2 100644 --- a/test/unit/cmds/extension_cmds/list.test.js +++ b/test/unit/cmds/extension_cmds/list.test.js @@ -1,7 +1,5 @@ import { handler } from '../../../../lib/cmds/extension_cmds/list' -import { getContext } from '../../../../lib/context' - import { log } from '../../../../lib/utils/log' import { createManagementClient } from '../../../../lib/utils/contentful-clients' @@ -29,7 +27,15 @@ const mockExtensions = { ] } -const getEnvironmentStub = jest.fn().mockImplementation((environmentId) => { +const defaults = { + context: { + managementToken: 'management-token', + activeSpaceId: 'space', + activeEnvironmentId: 'env' + } +} + +const getEnvironmentStub = jest.fn().mockImplementation(environmentId => { if (environmentId === 'env') { return Promise.resolve({ getUiExtensions: () => Promise.resolve(mockExtensions) @@ -47,18 +53,13 @@ const fakeClient = { } createManagementClient.mockResolvedValue(fakeClient) -getContext.mockResolvedValue({ - cmaToken: 'mockedToken', - activeSpaceId: 'someSpaceId' -}) - beforeEach(() => { log.mockClear() createManagementClient.mockClear() }) test('Lists extensions', async () => { - await handler({spaceId: 'space', environmentId: 'env'}) + await handler(defaults) const outputValues = [ 'Widget', '123', '7', 'Widget 2', '456', '8' ] @@ -68,7 +69,7 @@ test('Lists extensions', async () => { }) test('Displays message if list is empty', async () => { - await handler({spaceId: 'space', environmentId: 'empty'}) + await handler({ context: { ...defaults.context, activeEnvironmentId: 'empty' } }) expect(log).toHaveBeenCalledWith('No extensions found') }) diff --git a/test/unit/cmds/extension_cmds/update.test.js b/test/unit/cmds/extension_cmds/update.test.js index 1cb32f1b2..6f8897fc9 100644 --- a/test/unit/cmds/extension_cmds/update.test.js +++ b/test/unit/cmds/extension_cmds/update.test.js @@ -2,8 +2,6 @@ import { resolve } from 'path' import { updateExtensionHandler } from '../../../../lib/cmds/extension_cmds/update' -import { getContext } from '../../../../lib/context' - import { successEmoji } from '../../../../lib/utils/emojis' import { success, log } from '../../../../lib/utils/log' import { createManagementClient } from '../../../../lib/utils/contentful-clients' @@ -26,15 +24,18 @@ const basicExtension = { sys: { id: '123', version: 3 } } +const defaults = { + context: { + managementToken: 'management-token', + activeSpaceId: 'someSpaceId', + activeEnvironmentId: 'someEnvironmentId' + } +} + let updateStub let fakeClient beforeEach(() => { - getContext.mockResolvedValue({ - cmaToken: 'mockedToken', - activeSpaceId: 'someSpaceId' - }) - updateStub = jest.fn().mockImplementation((extension) => extension) fakeClient = { @@ -59,30 +60,35 @@ afterEach(() => { success.mockClear() log.mockClear() createExtension.mockClear() - getContext.mockReset() }) test('Throws error if id is missing', async () => { await expect( - updateExtensionHandler({ spaceId: 'space', fieldTypes: ['Symbol'], src: 'https://awesome.extension', force: true }) + updateExtensionHandler({ ...defaults, fieldTypes: ['Symbol'], src: 'https://awesome.extension', force: true }) ).rejects.toThrowErrorMatchingSnapshot() }) test('Throws error if name is missing', async () => { await expect( - updateExtensionHandler({ id: '123', spaceId: 'space', fieldTypes: ['Symbol'], src: 'https://awesome.extension', force: true }) + updateExtensionHandler({ ...defaults, id: '123', fieldTypes: ['Symbol'], src: 'https://awesome.extension', force: true }) ).rejects.toThrowErrorMatchingSnapshot() }) test('Throws error if --version and --force are missing', async () => { await expect( - updateExtensionHandler({ spaceId: 'space', id: '123', name: 'Widget', fieldTypes: ['Symbol'], src: 'https://awesome.extension' }) + updateExtensionHandler({ + ...defaults, + id: '123', + name: 'Widget', + fieldTypes: ['Symbol'], + src: 'https://awesome.extension' + }) ).rejects.toThrowErrorMatchingSnapshot() }) test('Throws error if wrong --version value is passed', async () => { await expect( - updateExtensionHandler({ id: '123', spaceId: 'space', fieldTypes: ['Symbol'], name: 'New name', src: 'https://new.url', version: 4 }) + updateExtensionHandler({ ...defaults, id: '123', fieldTypes: ['Symbol'], name: 'New name', src: 'https://new.url', version: 4 }) ).rejects.toThrowErrorMatchingSnapshot() }) @@ -105,9 +111,9 @@ test('Creates an extension with there is no one and force flag is present', asyn }) await updateExtensionHandler({ + ...defaults, id: '123', force: true, - spaceId: 'space', name: 'Widget', src: 'https://new.url' }) @@ -116,8 +122,8 @@ test('Creates an extension with there is no one and force flag is present', asyn await expect( updateExtensionHandler({ + ...defaults, id: '123', - spaceId: 'space', name: 'Widget', src: 'https://new.url' }) @@ -130,9 +136,9 @@ test( 'Calls update on extension with no version number but force', async () => { await updateExtensionHandler({ + ...defaults, id: '123', force: true, - spaceId: 'space', name: 'Widget', src: 'https://new.url' }) @@ -144,9 +150,9 @@ test( test('Calls update on extension and reads srcdoc from disk', async () => { await updateExtensionHandler({ + ...defaults, id: '123', version: 3, - spaceId: 'space', name: 'Widget', fieldTypes: ['Symbol'], srcdoc: resolve(__dirname, 'sample-extension.html') @@ -157,13 +163,6 @@ test('Calls update on extension and reads srcdoc from disk', async () => { }) test('Updates an extension with parameter definitions ', async () => { - getContext.mockReset() - getContext.mockResolvedValue({ - cmaToken: 'mockedToken', - activeSpaceId: 'someSpaceId', - activeEnvironmentId: 'someEnvironmentId' - }) - const descriptor = `{ "name": "Test Extension", "fieldTypes": ["Boolean"], @@ -177,6 +176,7 @@ test('Updates an extension with parameter definitions ', async () => { readFileP.mockResolvedValue(descriptor) await updateExtensionHandler({ + ...defaults, id: 'extension-id', descriptor: 'x.json', installationParameters: JSON.stringify({flag: true}), diff --git a/test/unit/cmds/login.test.js b/test/unit/cmds/login.test.js index 5eda948ed..e7dc5a49e 100644 --- a/test/unit/cmds/login.test.js +++ b/test/unit/cmds/login.test.js @@ -11,11 +11,11 @@ jest.mock('../../../lib/utils/actions') jest.mock('../../../lib/context') const mockedRcConfig = { - cmaToken: 'mockedToken' + managementToken: 'mockedToken' } inquirer.prompt.mockResolvedValue(mockedRcConfig) setContext.mockResolvedValue(true) -getContext.mockResolvedValue({ cmaToken: false }) +getContext.mockResolvedValue({ managementToken: false }) confirmation.mockResolvedValue(true) afterEach(() => { @@ -27,7 +27,7 @@ afterEach(() => { }) test('login - without error', async () => { - const result = await loginHandler() + const result = await loginHandler({context: {}}) if (['win32', 'darwin'].includes(process.platform)) { expect(opn).toHaveBeenCalled() @@ -36,15 +36,14 @@ test('login - without error', async () => { expect(inquirer.prompt).toHaveBeenCalledTimes(1) expect(setContext).toHaveBeenCalledTimes(1) expect(setContext.mock.calls[0][0]).toEqual(mockedRcConfig) - expect(result).toBe(mockedRcConfig.cmaToken) + expect(result).toBe(mockedRcConfig.managementToken) }) test('login - user abort', async () => { confirmation.mockResolvedValueOnce(false) - await loginHandler() + await loginHandler({context: {}}) - expect(getContext).toHaveBeenCalled() expect(confirmation).toHaveBeenCalled() if (['win32', 'darwin'].includes(process.platform)) { expect(opn).not.toHaveBeenCalled() @@ -54,9 +53,9 @@ test('login - user abort', async () => { }) test('login - already logged in', async () => { - getContext.mockResolvedValueOnce({ cmaToken: 'alreadyLoggedIn' }) + getContext.mockResolvedValueOnce({ managementToken: 'alreadyLoggedIn' }) - await loginHandler() + await loginHandler({context: {managementToken: 'token'}}) expect(opn).not.toHaveBeenCalled() expect(setContext).not.toHaveBeenCalled() diff --git a/test/unit/cmds/logout.test.js b/test/unit/cmds/logout.test.js index cb7947dd0..2859b2161 100644 --- a/test/unit/cmds/logout.test.js +++ b/test/unit/cmds/logout.test.js @@ -41,20 +41,18 @@ test('logout fails when not logged in', async () => { test('logout is actually logging out', async () => { confirmation.mockResolvedValueOnce(true) await logout({}) - expect(assertLoggedIn).toHaveBeenCalledTimes(1) expect(warning).toHaveBeenCalledWith('This will log you out by deleting the CMA token stored on your system.') expect(confirmation).toHaveBeenCalledTimes(1) expect(success).toHaveBeenCalledWith('Successfully logged you out.') expect(log).not.toHaveBeenCalled() expect(setContext).toHaveBeenCalledTimes(1) - expect(setContext.mock.calls[0][0]).toMatchObject({ cmaToken: null }) + expect(setContext.mock.calls[0][0]).toMatchObject({ managementToken: null }) expect(storeRuntimeConfig).toHaveBeenCalledTimes(1) }) test('logout is abortable', async () => { confirmation.mockResolvedValueOnce(false) await logout({}) - expect(assertLoggedIn).toHaveBeenCalledTimes(1) expect(warning).toHaveBeenCalledWith('This will log you out by deleting the CMA token stored on your system.') expect(confirmation).toHaveBeenCalledTimes(1) expect(log).toHaveBeenCalledWith('Log out aborted by user.') diff --git a/test/unit/cmds/space_cmds/accesstoken_cmds/create.test.js b/test/unit/cmds/space_cmds/accesstoken_cmds/create.test.js index a46960011..6a2ec49b4 100644 --- a/test/unit/cmds/space_cmds/accesstoken_cmds/create.test.js +++ b/test/unit/cmds/space_cmds/accesstoken_cmds/create.test.js @@ -21,7 +21,7 @@ const fakeClient = { createManagementClient.mockResolvedValue(fakeClient) getContext.mockResolvedValue({ - cmaToken: 'mockedToken' + managementToken: 'mockedToken' }) afterEach(() => { @@ -36,7 +36,9 @@ test('create new access token', async () => { }) const result = await accessTokenCreate({ ...mockedAccessTokenData, - spaceId: 'some-space-id' + context: { + activeSpaceId: 'some-space-id' + } }) expect(result).toBeTruthy() expect(createManagementClient).toHaveBeenCalledTimes(1) @@ -50,7 +52,9 @@ test('return existing access token', async () => { }) const result = await accessTokenCreate({ ...mockedAccessTokenData, - spaceId: 'some-space-id' + context: { + activeSpaceId: 'some-space-id' + } }) expect(result).toBeTruthy() expect(createManagementClient).toHaveBeenCalledTimes(1) @@ -62,10 +66,12 @@ test('create access token - fails when not logged in', async () => { items: [mockedAccessTokenData] }) getContext.mockResolvedValueOnce({ - cmaToken: null + managementToken: null }) await expect(accessTokenCreate({ - spaceId: 'some-space-id' + context: { + spaceId: 'some-space-id' + } })).rejects.toThrowErrorMatchingSnapshot() expect(createManagementClient).not.toHaveBeenCalled() expect(createApiKeyStub).not.toHaveBeenCalled() @@ -84,7 +90,9 @@ test('create access token - throws error when sth goes wrong', async () => { const errorMessage = 'Unable to create access token because of reasons' getApiKeysStub.mockRejectedValueOnce(new Error(errorMessage)) await expect(accessTokenCreate({ - spaceId: 'some-space-id' + context: { + activeSpaceId: 'some-space-id' + } })).rejects.toThrowError(errorMessage) expect(createManagementClient).toHaveBeenCalledTimes(1) expect(createApiKeyStub).not.toHaveBeenCalled() diff --git a/test/unit/cmds/space_cmds/create.test.js b/test/unit/cmds/space_cmds/create.test.js index e63ddc183..71379228b 100644 --- a/test/unit/cmds/space_cmds/create.test.js +++ b/test/unit/cmds/space_cmds/create.test.js @@ -25,6 +25,14 @@ const getOrganizationsStub = jest.fn().mockResolvedValue({ ] }) +const defaults = { + context: { + managementToken: 'management-token', + activeSpaceId: 'space', + activeEnvironmentId: 'master' + } +} + inquirer.prompt.mockResolvedValue({ organizationId: 'mockedOrgTwo' }) const createSpaceStub = jest.fn().mockResolvedValue({ name: 'Mocked space name', @@ -39,7 +47,7 @@ const fakeClient = { createManagementClient.mockResolvedValue(fakeClient) getContext.mockResolvedValue({ - cmaToken: 'mockedToken' + managementToken: 'mockedToken' }) afterEach(() => { @@ -54,7 +62,7 @@ test('create space with single org user', async () => { const spaceData = { name: 'space name' } - const result = await spaceCreate(spaceData) + const result = await spaceCreate({...defaults, ...spaceData}) expect(result).toBeTruthy() expect(createManagementClient).toHaveBeenCalledTimes(1) expect(fakeClient.createSpace).toHaveBeenCalledTimes(1) @@ -84,7 +92,7 @@ test('create space with multi org user', async () => { } ] }) - const result = await spaceCreate(spaceData) + const result = await spaceCreate({...defaults, ...spaceData}) expect(result).toBeTruthy() expect(createManagementClient).toHaveBeenCalledTimes(1) expect(fakeClient.createSpace).toHaveBeenCalledTimes(1) @@ -99,7 +107,7 @@ test('create space with passed organization id', async () => { name: 'space name', organizationId: 'mockedOrganizationId' } - const result = await spaceCreate(spaceData) + const result = await spaceCreate({...defaults, ...spaceData}) expect(result).toBeTruthy() expect(createManagementClient).toHaveBeenCalledTimes(1) expect(fakeClient.createSpace).toHaveBeenCalledTimes(1) @@ -111,9 +119,9 @@ test('create space with passed organization id', async () => { test('create space - fails when not logged in', async () => { getContext.mockResolvedValueOnce({ - cmaToken: null + managementToken: null }) - await expect(spaceCreate({})).rejects.toThrowErrorMatchingSnapshot() + await expect(spaceCreate({context: {}})).rejects.toThrowErrorMatchingSnapshot() expect(createManagementClient).not.toHaveBeenCalled() expect(inquirer.prompt).not.toHaveBeenCalled() expect(spaceUse).not.toHaveBeenCalled() @@ -122,7 +130,7 @@ test('create space - fails when not logged in', async () => { test('create space - throws error when sth goes wrong', async () => { const errorMessage = 'Unable to create space because of reasons' createSpaceStub.mockRejectedValueOnce(new Error(errorMessage)) - await expect(spaceCreate({})).rejects.toThrowError(errorMessage) + await expect(spaceCreate({context: {managementToken: 'management-token'}})).rejects.toThrowError(errorMessage) expect(fakeClient.createSpace).toHaveBeenCalledTimes(1) expect(inquirer.prompt).not.toHaveBeenCalled() expect(spaceUse).not.toHaveBeenCalled() @@ -133,7 +141,7 @@ test('create space - accepts default locale', async () => { name: 'space name', defaultLocale: 'de-DE' } - const result = await spaceCreate(spaceData) + const result = await spaceCreate({...defaults, ...spaceData}) expect(result).toBeTruthy() expect(fakeClient.createSpace).toHaveBeenCalledTimes(1) expect(fakeClient.createSpace.mock.calls[0][0]).toEqual(spaceData) @@ -147,7 +155,7 @@ test('abort space creation when saying no', async () => { name: 'space name' } confirmation.mockResolvedValueOnce(false) - const result = await spaceCreate(spaceData) + const result = await spaceCreate({...defaults, ...spaceData}) expect(result).toBeFalsy() expect(spaceUse).not.toHaveBeenCalled() }) @@ -157,6 +165,7 @@ test('create space and use it as default for further commands', async () => { name: 'space name' } const result = await spaceCreate({ + ...defaults, ...spaceData, use: true }) diff --git a/test/unit/cmds/space_cmds/environment_cmds/create.test.js b/test/unit/cmds/space_cmds/environment_cmds/create.test.js index d7b8869a3..47b53ddc0 100644 --- a/test/unit/cmds/space_cmds/environment_cmds/create.test.js +++ b/test/unit/cmds/space_cmds/environment_cmds/create.test.js @@ -12,6 +12,14 @@ const environmentData = { } } +const defaults = { + context: { + managementToken: 'management-token', + activeSpaceId: 'someSpaceID' + }, + environmentId: 'test' +} + const createEnvironmentWithIdStub = jest.fn().mockResolvedValue(environmentData) const fakeClient = { @@ -22,7 +30,7 @@ const fakeClient = { createManagementClient.mockResolvedValue(fakeClient) getContext.mockResolvedValue({ - cmaToken: 'mockedToken' + managementToken: 'mockedToken' }) afterEach(() => { @@ -31,16 +39,13 @@ afterEach(() => { }) test('create environment - requires space id', async () => { - await expect(environmentCreate({})).rejects.toThrowErrorMatchingSnapshot() + await expect(environmentCreate({context: {managementToken: 'management-token'}})).rejects.toThrowErrorMatchingSnapshot() expect(createManagementClient).not.toHaveBeenCalled() expect(createEnvironmentWithIdStub).not.toHaveBeenCalled() }) test('create new environment with id', async () => { - const result = await environmentCreate({ - spaceId: 'someSpaceID', - environmentId: 'test' - }) + const result = await environmentCreate(defaults) expect(result).toBeTruthy() expect(createManagementClient).toHaveBeenCalledTimes(1) expect(createEnvironmentWithIdStub).toHaveBeenCalledTimes(1) @@ -50,8 +55,7 @@ test('create new environment with id', async () => { test('create new environment with id and name', async () => { const result = await environmentCreate({ - spaceId: 'someSpaceID', - environmentId: 'test', + ...defaults, name: 'test' }) expect(result).toBeTruthy() @@ -64,8 +68,7 @@ test('create new environment with id and name', async () => { test('create new environment with id and name and source', async () => { const result = await environmentCreate({ - spaceId: 'someSpaceID', - environmentId: 'test', + ...defaults, name: 'test', source: 'srcEnv' }) diff --git a/test/unit/cmds/space_cmds/environment_cmds/delete.test.js b/test/unit/cmds/space_cmds/environment_cmds/delete.test.js index ce00ef407..70aad8d9c 100644 --- a/test/unit/cmds/space_cmds/environment_cmds/delete.test.js +++ b/test/unit/cmds/space_cmds/environment_cmds/delete.test.js @@ -23,7 +23,7 @@ const fakeClient = { createManagementClient.mockResolvedValue(fakeClient) getContext.mockResolvedValue({ - cmaToken: 'mockedToken' + managementToken: 'mockedToken' }) afterEach(() => { @@ -33,7 +33,7 @@ afterEach(() => { deleteEnvironmentStub.mockClear() }) test('delete environment - requires space id', async () => { - await expect(environmentDelete({})).rejects.toThrowErrorMatchingSnapshot() + await expect(environmentDelete({context: {}})).rejects.toThrowErrorMatchingSnapshot() expect(createManagementClient).not.toHaveBeenCalled() expect(getEnvironmentStub).not.toHaveBeenCalled() expect(deleteEnvironmentStub).not.toHaveBeenCalled() @@ -41,7 +41,9 @@ test('delete environment - requires space id', async () => { test('delete environment', async () => { const result = await environmentDelete({ - spaceId: 'someSpaceID', + context: { + activeSpaceId: 'someSpaceID' + }, environmentId: 'someEnvironmentID' }) expect(result).toBeTruthy() diff --git a/test/unit/cmds/space_cmds/environment_cmds/list.test.js b/test/unit/cmds/space_cmds/environment_cmds/list.test.js index dec11e752..82a6f2272 100644 --- a/test/unit/cmds/space_cmds/environment_cmds/list.test.js +++ b/test/unit/cmds/space_cmds/environment_cmds/list.test.js @@ -31,7 +31,7 @@ const fakeClient = { createManagementClient.mockResolvedValue(fakeClient) getContext.mockResolvedValue({ - cmaToken: 'mockedToken' + managementToken: 'mockedToken' }) afterEach(() => { @@ -41,14 +41,16 @@ afterEach(() => { }) test('list environments - requires space id', async () => { - await expect(environmentList({})).rejects.toThrowErrorMatchingSnapshot() + await expect(environmentList({context: {}})).rejects.toThrowErrorMatchingSnapshot() expect(createManagementClient).not.toHaveBeenCalled() expect(getEnvironmentsStub).not.toHaveBeenCalled() }) test('list environments', async () => { await environmentList({ - spaceId: 'someSpaceID' + context: { + activeSpaceId: 'someSpaceID' + } }) expect(createManagementClient).toHaveBeenCalledTimes(1) expect(getEnvironmentsStub).toHaveBeenCalledTimes(1) diff --git a/test/unit/cmds/space_cmds/environment_cmds/use.test.js b/test/unit/cmds/space_cmds/environment_cmds/use.test.js index 711e5f838..236ab7199 100644 --- a/test/unit/cmds/space_cmds/environment_cmds/use.test.js +++ b/test/unit/cmds/space_cmds/environment_cmds/use.test.js @@ -38,7 +38,7 @@ test('login is required', async () => { }) test('active space is required', async () => { - getContext.mockResolvedValue({ cmaToken: 'foo' }) + getContext.mockResolvedValue({ managementToken: 'foo' }) const stubArgv = { environmentId: 'test' } @@ -48,10 +48,14 @@ test('active space is required', async () => { test('it writes the environment id to contentfulrc.json', async () => { getContext.mockResolvedValue({ - cmaToken: 'managementToken', + managementToken: 'managementToken', activeSpaceId: 'spaceId' }) const stubArgv = { + context: { + managementToken: 'managementToken', + activeSpaceId: 'spaceId' + }, environmentId: 'test' } await environmentUse(stubArgv) diff --git a/test/unit/cmds/space_cmds/export.test.js b/test/unit/cmds/space_cmds/export.test.js index 5b352ccf6..3f1d8ac73 100644 --- a/test/unit/cmds/space_cmds/export.test.js +++ b/test/unit/cmds/space_cmds/export.test.js @@ -7,12 +7,16 @@ import contentfulExport from 'contentful-export' jest.mock('../../../../lib/context') jest.mock('contentful-export') -getContext.mockResolvedValue({ cmaToken: 'managementToken' }) +getContext.mockResolvedValue({ managementToken: 'managementToken' }) test('it should pass all args to contentful-export', async () => { const stubArgv = { - spaceId: 'spaceId', - host: 'api.contentful.com', + context: { + activeSpaceId: 'spaceId', + activeEnvironmentId: 'master', + host: 'api.contentful.com', + managementToken: 'managementToken' + }, includeDrafts: false, skipRoles: false, skipContentModel: false, @@ -22,10 +26,16 @@ test('it should pass all args to contentful-export', async () => { saveFile: true, useVerboseRenderer: false, managementApplication: `contentful.cli/${version}`, - managementToken: 'managementToken', managementFeature: 'space-export' } await exportSpace(stubArgv) - expect(contentfulExport.mock.calls[0][0]).toEqual(stubArgv) + const result = { + ...stubArgv, + environmentId: 'master', + managementToken: 'managementToken', + spaceId: 'spaceId', + host: 'api.contentful.com' + } + expect(contentfulExport.mock.calls[0][0]).toEqual(result) expect(contentfulExport).toHaveBeenCalledTimes(1) }) diff --git a/test/unit/cmds/space_cmds/generate_cmds/migration.test.js b/test/unit/cmds/space_cmds/generate_cmds/migration.test.js index fbc87eeca..909edc136 100644 --- a/test/unit/cmds/space_cmds/generate_cmds/migration.test.js +++ b/test/unit/cmds/space_cmds/generate_cmds/migration.test.js @@ -13,7 +13,6 @@ import { } from '../../../../../lib/cmds/space_cmds/generate_cmds/migration' import fs from 'fs' import { createManagementClient } from '../../../../../lib/utils/contentful-clients' -import { getContext } from '../../../../../lib/context' jest.mock('../../../../../lib/utils/contentful-clients') jest.mock('../../../../../lib/context') @@ -210,12 +209,12 @@ test('it generates the filename without content type', async () => { test('it generates the migration and writes to disk', async () => { const writeFileSyncMock = jest.spyOn(fs, 'writeFileSync') - getContext.mockResolvedValue({ - cmaToken: 'mockedToken' - }) await generateMigration({ - spaceId: 'fooSpace', - environmentId: 'fooEnv' + context: { + managementToken: 'managementToken', + activeSpaceId: 'fooSpace', + activeEnvironmentId: 'fooEnv' + } }) const filenameRegex = /^(\w+)-(\w+)-\d+.js$/ diff --git a/test/unit/cmds/space_cmds/import.test.js b/test/unit/cmds/space_cmds/import.test.js index b8c513904..19af07be7 100644 --- a/test/unit/cmds/space_cmds/import.test.js +++ b/test/unit/cmds/space_cmds/import.test.js @@ -7,20 +7,29 @@ import contentfulImport from 'contentful-import' jest.mock('../../../../lib/context') jest.mock('contentful-import') -getContext.mockResolvedValue({ cmaToken: 'managementToken' }) +getContext.mockResolvedValue({ managementToken: 'managementToken' }) test('it should pass all args to contentful-import', async () => { const stubArgv = { + context: { + activeSpaceId: 'spaceId', + managementToken: 'managementToken' + }, skipContentModel: false, skipLocales: false, host: 'api.contentful.com', skipContentPublishing: false, - managementToken: 'managementToken', managementApplication: `contentful.cli/${version}`, - spaceId: 'spaceId', managementFeature: 'space-import' } await importSpace(stubArgv) - expect(contentfulImport.mock.calls[0][0]).toEqual(stubArgv) + const result = { + ...stubArgv, + managementToken: 'managementToken', + spaceId: 'spaceId', + environmentId: undefined, + host: undefined + } + expect(contentfulImport.mock.calls[0][0]).toEqual(result) expect(contentfulImport).toHaveBeenCalledTimes(1) }) diff --git a/test/unit/cmds/space_cmds/migration.test.js b/test/unit/cmds/space_cmds/migration.test.js index c862bfd7f..b9d37e834 100644 --- a/test/unit/cmds/space_cmds/migration.test.js +++ b/test/unit/cmds/space_cmds/migration.test.js @@ -7,16 +7,25 @@ import runMigration from 'contentful-migration/built/bin/cli' jest.mock('../../../../lib/context') jest.mock('contentful-migration/built/bin/cli') -getContext.mockResolvedValue({ cmaToken: 'managementToken' }) +getContext.mockResolvedValue({ managementToken: 'managementToken' }) test('it should pass all args to the migration', async () => { const stubArgv = { - accessToken: 'managementToken', + context: { + managementToken: 'managementToken', + activeEnvironmentId: 'master', + activeSpaceId: 'spaceId' + }, managementApplication: `contentful.cli/${version}`, - spaceId: 'spaceId', managementFeature: 'space-migration' } await migration(stubArgv) - expect(runMigration.mock.calls[0][0]).toEqual(stubArgv) + const result = { + ...stubArgv, + spaceId: 'spaceId', + environmentId: 'master', + accessToken: 'managementToken' + } + expect(runMigration.mock.calls[0][0]).toEqual(result) expect(runMigration).toHaveBeenCalledTimes(1) }) diff --git a/test/unit/cmds/space_cmds/use.test.js b/test/unit/cmds/space_cmds/use.test.js index 267a792d2..8bc832339 100644 --- a/test/unit/cmds/space_cmds/use.test.js +++ b/test/unit/cmds/space_cmds/use.test.js @@ -1,13 +1,11 @@ import { spaceUse } from '../../../../lib/cmds/space_cmds/use' -import { getContext, setContext } from '../../../../lib/context' +import { setContext } from '../../../../lib/context' import { createManagementClient } from '../../../../lib/utils/contentful-clients' jest.mock('../../../../lib/context') jest.mock('../../../../lib/utils/contentful-clients') -getContext.mockResolvedValue({ cmaToken: 'managementToken' }) - const getSpaceStub = jest.fn().mockResolvedValue({ sys: { id: 'test' @@ -21,8 +19,11 @@ createManagementClient.mockResolvedValue(fakeClient) test('it writes the enviroment id to contentfulrc.json', async () => { const stubArgv = { - spaceId: 'test', - managementToken: 'managementToken' + context: { + managementToken: 'managementToken', + activeEnvironmentId: 'master' + }, + spaceId: 'test' } await spaceUse(stubArgv) expect(setContext).toHaveBeenCalledWith({ diff --git a/test/unit/context.test.js b/test/unit/context.test.js index 30ed000aa..3dcd7764a 100644 --- a/test/unit/context.test.js +++ b/test/unit/context.test.js @@ -3,7 +3,7 @@ import { homedir } from 'os' const customConfigPath = resolve(process.cwd(), '.contentfulrc.json') const homeConfigPath = resolve(homedir(), '.contentfulrc.json') -const MOCKED_RC = '{\n "cmaToken": "mocked",\n "activeSpaceId": "mocked"\n}\n' +const MOCKED_RC = '{\n "managementToken": "mocked",\n "activeSpaceId": "mocked"\n}\n' const enoent = new Error() enoent.code = 'ENOENT' diff --git a/test/unit/guide/step-create-space.test.js b/test/unit/guide/step-create-space.test.js index ff3002df7..de67e7260 100644 --- a/test/unit/guide/step-create-space.test.js +++ b/test/unit/guide/step-create-space.test.js @@ -1,15 +1,18 @@ import { AbortedError } from '../../../lib/guide/helpers' import createSpaceStep from '../../../lib/guide/step-create-space' import { confirmation } from '../../../lib/utils/actions' +import { getContext } from '../../../lib/context' import { spaceCreate } from '../../../lib/cmds/space_cmds/create' jest.mock('../../../lib/utils/log') jest.mock('../../../lib/utils/actions') +jest.mock('../../../lib/context') jest.mock('../../../lib/cmds/space_cmds/create') const guideContext = {stepCount: 0, activeGuide: {name: 'test'}} const fakeSpace = {sys: {id: '100abc'}} +getContext.mockResolvedValue({ managementToken: 'managementToken', activeEnvironmentId: 'master' }) confirmation.mockResolvedValue(true) spaceCreate.mockResolvedValue(fakeSpace) @@ -23,7 +26,11 @@ test('creates space on successful user confirmation', async () => { await createSpaceStep(guideContext) expect(confirmation).toHaveBeenCalledTimes(1) expect(spaceCreate).toHaveBeenCalledTimes(1) - expect(spaceCreate).toHaveBeenCalledWith({ name: guideContext.activeGuide.name, feature: 'guide' }) + expect(spaceCreate).toHaveBeenCalledWith({ + context: { activeEnvironmentId: 'master', managementToken: 'managementToken' }, + name: guideContext.activeGuide.name, + feature: 'guide' + }) }) test('guideContext stepCount incremented', async () => { diff --git a/test/unit/guide/step-login.test.js b/test/unit/guide/step-login.test.js index 6de13b7dc..df40928da 100644 --- a/test/unit/guide/step-login.test.js +++ b/test/unit/guide/step-login.test.js @@ -6,21 +6,21 @@ jest.mock('../../../lib/cmds/login') jest.mock('../../../lib/context') jest.mock('../../../lib/utils/log') -getContext.mockResolvedValue({ cmaToken: 'blah' }) +getContext.mockResolvedValue({ managementToken: 'blah' }) afterEach(() => { login.mockClear() getContext.mockClear() }) -test('do not login if cmaToken already exists in context', async () => { +test('do not login if managementToken already exists in context', async () => { await loginStep({}) expect(getContext).toHaveBeenCalledTimes(1) expect(login).not.toHaveBeenCalled() }) test( - 'login and increment stepCount if cmaToken does not exist in context', + 'login and increment stepCount if managementToken does not exist in context', async () => { getContext.mockResolvedValue({}) const stepCount = 0 diff --git a/test/unit/guide/step-seed.test.js b/test/unit/guide/step-seed.test.js index 91f1a9114..e8903791a 100644 --- a/test/unit/guide/step-seed.test.js +++ b/test/unit/guide/step-seed.test.js @@ -2,12 +2,16 @@ import seedStep from '../../../lib/guide/step-seed' import { AbortedError } from '../../../lib/guide/helpers' import { confirmation } from '../../../lib/utils/actions' +import { getContext } from '../../../lib/context' import { spaceSeed } from '../../../lib/cmds/space_cmds/seed' jest.mock('../../../lib/utils/log') jest.mock('../../../lib/utils/actions') +jest.mock('../../../lib/context') jest.mock('../../../lib/cmds/space_cmds/seed') +getContext.mockResolvedValue({ managementToken: 'managementToken', activeEnvironmentId: 'master' }) + const guideContext = { stepCount: 0, spaceId: 'abc124', @@ -27,10 +31,10 @@ test('seeds space on successful user confirmation', async () => { await seedStep(guideContext) expect(confirmation).toHaveBeenCalledTimes(1) expect(spaceSeed).toHaveBeenCalledTimes(1) - const { spaceId, activeGuide: {seed} } = guideContext + const { activeGuide: {seed} } = guideContext expect(spaceSeed).toHaveBeenCalledWith({ + context: { managementToken: 'managementToken', activeEnvironmentId: 'master', activeSpaceId: guideContext.spaceId }, template: seed, - spaceId, yes: true, feature: 'guide' }) diff --git a/test/unit/guide/step-setup.test.js b/test/unit/guide/step-setup.test.js index b0f24456f..c816b53bb 100644 --- a/test/unit/guide/step-setup.test.js +++ b/test/unit/guide/step-setup.test.js @@ -15,7 +15,7 @@ jest.mock('inquirer') prompt.mockResolvedValue({directoryName: 'test', directoryPath: 'test-path'}) accessTokenCreate.mockResolvedValue({accessToken: 'abc123'}) -getContext.mockResolvedValue({cmaToken: 'abc124'}) +getContext.mockResolvedValue({managementToken: 'abc124'}) const setupConfig = jest.fn().mockResolvedValue() const guideContext = { @@ -58,7 +58,7 @@ test('checks for yarn, execa installs, creates cda token', async () => { test('gets context and sets up config', async () => { await setupStep(guideContext) - expect(getContext).toHaveBeenCalledTimes(1) + expect(getContext).toHaveBeenCalledTimes(2) expect(guideContext.activeGuide.setupConfig).toHaveBeenCalledTimes(1) }) diff --git a/test/unit/utils/assertions.test.js b/test/unit/utils/assertions.test.js index 45bbe24b2..a43f9b168 100644 --- a/test/unit/utils/assertions.test.js +++ b/test/unit/utils/assertions.test.js @@ -7,7 +7,7 @@ jest.mock('../../../lib/context') jest.mock('../../../lib/utils/styles') getContext.mockResolvedValue({ - cmaToken: 'mockedToken' + managementToken: 'mockedToken' }) afterEach(() => { @@ -33,7 +33,7 @@ test('assertSpaceIdProvided when provided via args', async () => { test('assertSpaceIdProvided when provided via context', async () => { getContext.mockResolvedValueOnce({ activeSpaceId: 'space id', - cmaToken: 'mockedToken' + managementToken: 'mockedToken' }) await assertSpaceIdProvided() }) diff --git a/test/unit/utils/middlewares.test.js b/test/unit/utils/middlewares.test.js new file mode 100644 index 000000000..11e3836eb --- /dev/null +++ b/test/unit/utils/middlewares.test.js @@ -0,0 +1,79 @@ +import { buildContext } from '../../../lib/utils/middlewares' + +import { getContext } from '../../../lib/context' +import { highlightStyle } from '../../../lib/utils/styles' + +jest.mock('../../../lib/context') +jest.mock('../../../lib/utils/styles') + +getContext.mockResolvedValue({}) + +afterEach(() => { + highlightStyle.mockClear() + getContext.mockClear() +}) + +const defaults = { + context: { + host: 'api.contentful.com', + activeEnvironmentId: 'master' + } +} + +test('useFlagsIfAvailable set defaults', async () => { + const result = await buildContext({}) + expect(result).toEqual(defaults) +}) + +test('useFlagsIfAvailable set activeSpaceId (overwrite context)', async () => { + getContext.mockResolvedValueOnce({ + activeSpaceId: 'spaceId' + }) + const result = await buildContext({spaceId: 'activeSpaceId'}) + expect(result).toEqual({context: {...defaults.context, activeSpaceId: 'activeSpaceId'}}) +}) + +test('useFlagsIfAvailable set activeSpaceId (from context)', async () => { + getContext.mockResolvedValueOnce({ + activeSpaceId: 'spaceId' + }) + const result = await buildContext({}) + expect(result).toEqual({context: {...defaults.context, activeSpaceId: 'spaceId'}}) +}) + +test('useFlagsIfAvailable set managementToken (overwrite context)', async () => { + getContext.mockResolvedValueOnce({ + managementToken: 'managementToken' + }) + const result = await buildContext({managementToken: 'managementToken'}) + expect(result).toEqual({context: {...defaults.context, managementToken: 'managementToken'}}) +}) + +test('useFlagsIfAvailable set managementToken (from context)', async () => { + getContext.mockResolvedValueOnce({ + managementToken: 'managementToken' + }) + const result = await buildContext({}) + expect(result).toEqual({context: {...defaults.context, managementToken: 'managementToken'}}) +}) + +test('useFlagsIfAvailable set activeEnvironmentId (overwrite context)', async () => { + getContext.mockResolvedValueOnce({ + activeEnvironmentId: 'activeEnvironmentId' + }) + const result = await buildContext({environmentId: 'environmentId'}) + expect(result).toEqual({context: {...defaults.context, activeEnvironmentId: 'environmentId'}}) +}) + +test('useFlagsIfAvailable set activeEnvironmentId (from context)', async () => { + getContext.mockResolvedValueOnce({ + activeEnvironmentId: 'activeEnvironmentId' + }) + const result = await buildContext({}) + expect(result).toEqual({context: {...defaults.context, activeEnvironmentId: 'activeEnvironmentId'}}) +}) + +test('useFlagsIfAvailable set host', async () => { + const result = await buildContext({host: 'host'}) + expect(result).toEqual({context: {...defaults.context, host: 'host'}}) +})