diff --git a/src/commands/import.ts b/src/commands/import.ts index f84c85897..acde2e2e3 100644 --- a/src/commands/import.ts +++ b/src/commands/import.ts @@ -4,6 +4,7 @@ import { deploy as toolsDeploy } from '../tools'; import log from '../logger'; import { setupContext } from '../context'; import { ImportParams } from '../args'; +import { Assets, Config } from '../types'; export default async function importCMD(params: ImportParams) { const { @@ -48,8 +49,47 @@ export default async function importCMD(params: ImportParams) { const config = configFactory(); config.setProvider((key) => nconf.get(key)); - //@ts-ignore because context and assets still need to be typed TODO: type assets and type context + findUnreplacedKeywords(context.assets); + await toolsDeploy(context.assets, context.mgmtClient, config); log.info('Import Successful'); } + +export const findUnreplacedKeywords = (assets: Assets) => { + const recursiveFindUnreplacedKeywords = (target): string[] => { + let unreplaced: string[] = []; + if (target === undefined || target === null) return []; + if (Array.isArray(target)) { + target.forEach((child) => { + unreplaced.push(...recursiveFindUnreplacedKeywords(child)); + }); + } else if (typeof target === 'object') { + Object.values(target).forEach((child) => { + unreplaced.push(...recursiveFindUnreplacedKeywords(child)); + }); + } + + if (typeof target === 'string') { + const arrayMatches = target.match(/(?<=@@).*(?=@@)/g); + if (arrayMatches !== null) { + return arrayMatches; + } + const keywordMatches = target.match(/(?<=##).*(?=##)/g); + if (keywordMatches !== null) { + return keywordMatches; + } + } + + return unreplaced; + }; + + const unreplacedKeywords = recursiveFindUnreplacedKeywords(assets); + + if (unreplacedKeywords.length > 0) { + throw `Unreplaced keywords found: ${unreplacedKeywords.join( + ', ' + )}. Either correct these values or add to AUTH0_KEYWORD_REPLACE_MAPPINGS configuration.`; + } + return; +}; diff --git a/test/commands/import.test.ts b/test/commands/import.test.ts new file mode 100644 index 000000000..327b7d699 --- /dev/null +++ b/test/commands/import.test.ts @@ -0,0 +1,69 @@ +import { expect } from 'chai'; +import { findUnreplacedKeywords } from '../../src/commands/import'; + +describe('#findUnreplacedKeywords function', () => { + it('should not throw if no unreplaced keywords', () => { + const fn = () => + findUnreplacedKeywords({ + actions: [ + { + foo: 'foo', + bar: 'bar', + }, + { + foo: ['foo1', 'foo2'], + bar: 'bar', + }, + { + foo: 'foo', + bar: 'bar', + }, + ], + tenant: { + foo: 'foo', + bar: { + some: { + nested: { property: 'bar baz' }, + }, + }, + }, + //@ts-ignore because we're detecting this + databases: ' database value ', // + }); + + expect(fn).to.not.throw(); + }); + it('should throw if unreplaced keywords detected', () => { + const fn = () => + findUnreplacedKeywords({ + actions: [ + { + foo: 'foo', + bar: 'bar', + }, + { + foo: ['##KEYWORD1##', '##KEYWORD2##'], + bar: 'bar', + }, + { + foo: 'foo', + bar: 'bar', + }, + ], + tenant: { + foo: 'foo', + bar: { + some: { + nested: { property: 'bar ##KEYWORD3##' }, + }, + }, + }, + //@ts-ignore because we're detecting this + databases: ' @@KEYWORD4@@ ', // + }); + + expect(fn).to.throw( + 'Unreplaced keywords found: KEYWORD1, KEYWORD2, KEYWORD3, KEYWORD4. Either correct these values or add to AUTH0_KEYWORD_REPLACE_MAPPINGS configuration.' + ); + }); +});