diff --git a/package-lock.json b/package-lock.json index ed875552b0..272eb1c72c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24237,7 +24237,7 @@ "@contentstack/cli-cm-branches": "~1.0.23", "@contentstack/cli-cm-bulk-publish": "~1.4.3", "@contentstack/cli-cm-clone": "~1.10.1", - "@contentstack/cli-cm-export": "~1.11.0", + "@contentstack/cli-cm-export": "~1.11.1", "@contentstack/cli-cm-export-to-csv": "~1.7.0", "@contentstack/cli-cm-import": "~1.14.1", "@contentstack/cli-cm-migrate-rte": "~1.4.15", @@ -24771,7 +24771,7 @@ "license": "MIT", "dependencies": { "@colors/colors": "^1.5.0", - "@contentstack/cli-cm-export": "~1.11.0", + "@contentstack/cli-cm-export": "~1.11.1", "@contentstack/cli-cm-import": "~1.14.1", "@contentstack/cli-command": "~1.2.16", "@contentstack/cli-utilities": "~1.6.0", diff --git a/packages/contentstack-clone/package.json b/packages/contentstack-clone/package.json index bea9511200..6be01c4b0d 100644 --- a/packages/contentstack-clone/package.json +++ b/packages/contentstack-clone/package.json @@ -6,7 +6,7 @@ "bugs": "https://github.com/rohitmishra209/cli-cm-clone/issues", "dependencies": { "@colors/colors": "^1.5.0", - "@contentstack/cli-cm-export": "~1.11.0", + "@contentstack/cli-cm-export": "~1.11.1", "@contentstack/cli-cm-import": "~1.14.1", "@contentstack/cli-command": "~1.2.16", "@contentstack/cli-utilities": "~1.6.0", diff --git a/packages/contentstack-export/src/utils/export-config-handler.ts b/packages/contentstack-export/src/utils/export-config-handler.ts index c1e15291f0..5e57aa3d66 100644 --- a/packages/contentstack-export/src/utils/export-config-handler.ts +++ b/packages/contentstack-export/src/utils/export-config-handler.ts @@ -1,6 +1,6 @@ import merge from 'merge'; import * as path from 'path'; -import { configHandler, isAuthenticated, FlagInput } from '@contentstack/cli-utilities'; +import { configHandler, isAuthenticated, FlagInput, cliux } from '@contentstack/cli-utilities'; import defaultConfig from '../config'; import { readFile } from './file-helper'; import { askExportDir, askAPIKey } from './interactive'; @@ -16,7 +16,20 @@ const setupConfig = async (exportCmdFlags: any): Promise => { config = merge.recursive(config, externalConfig); } config.exportDir = exportCmdFlags['data'] || exportCmdFlags['data-dir'] || config.data || (await askExportDir()); + + const pattern = /[*$%#<>{}!&?]/g; + if (pattern.test(config.exportDir)) { + cliux.print( + `\nYour mentioned directory path contains special characters (*,&,{,},[,],$,%,<,>,?,!) please add a path without them`, + { + color: 'yellow', + }, + ); + config.exportDir = await askExportDir(); + } + config.exportDir = config.exportDir.replace(/['"]/g, ''); config.exportDir = path.resolve(config.exportDir); + //Note to support the old key config.data = config.exportDir; diff --git a/packages/contentstack-export/src/utils/interactive.ts b/packages/contentstack-export/src/utils/interactive.ts index 6b129e1476..141ec1f6e3 100644 --- a/packages/contentstack-export/src/utils/interactive.ts +++ b/packages/contentstack-export/src/utils/interactive.ts @@ -1,4 +1,4 @@ -import { cliux } from '@contentstack/cli-utilities'; +import { cliux, validatePath } from '@contentstack/cli-utilities'; import * as path from 'path'; export const askPassword = async () => { @@ -45,14 +45,16 @@ export const askUsername = async (): Promise => { }; export const askExportDir = async (): Promise => { - const result = await cliux.inquire({ + let result = await cliux.inquire({ type: 'input', message: 'Enter the path for storing the content: (current folder)', name: 'dir', + validate: validatePath, }); if (!result) { return process.cwd(); } else { + result = result.replace(/['"]/g, ''); return path.resolve(result); } }; @@ -63,4 +65,4 @@ export const askAPIKey = async (): Promise => { message: 'Enter the stack api key', name: 'apiKey', }); -}; \ No newline at end of file +}; diff --git a/packages/contentstack-import/src/utils/import-config-handler.ts b/packages/contentstack-import/src/utils/import-config-handler.ts index 233a47ff00..9fa7d90202 100644 --- a/packages/contentstack-import/src/utils/import-config-handler.ts +++ b/packages/contentstack-import/src/utils/import-config-handler.ts @@ -1,7 +1,7 @@ import merge from 'merge'; import * as path from 'path'; import { omit, filter, includes, isArray } from 'lodash'; -import { configHandler, isAuthenticated } from '@contentstack/cli-utilities'; +import { configHandler, isAuthenticated, cliux } from '@contentstack/cli-utilities'; import defaultConfig from '../config'; import { readFile, fileExistsSync } from './file-helper'; import { askContentDir, askAPIKey } from './interactive'; @@ -21,10 +21,20 @@ const setupConfig = async (importCmdFlags: any): Promise => { } config.contentDir = importCmdFlags['data'] || importCmdFlags['data-dir'] || config.data || (await askContentDir()); + const pattern = /[*$%#<>{}!&?]/g; + if (pattern.test(config.contentDir)) { + cliux.print( + `\nYour mentioned directory path contains special characters (*,&,{,},[,],$,%,<,>,?,!) please add a path without them`, + { + color: 'yellow', + }, + ); + config.contentDir = await askContentDir(); + } + config.contentDir = config.contentDir.replace(/['"]/g, ''); config.contentDir = path.resolve(config.contentDir); //Note to support the old key config.data = config.contentDir; - if (fileExistsSync(path.join(config.contentDir, 'export-info.json'))) { config.contentVersion = ((await readFile(path.join(config.contentDir, 'export-info.json'))) || {}).contentVersion || 2; diff --git a/packages/contentstack-import/src/utils/interactive.ts b/packages/contentstack-import/src/utils/interactive.ts index 42a4703964..e641085111 100644 --- a/packages/contentstack-import/src/utils/interactive.ts +++ b/packages/contentstack-import/src/utils/interactive.ts @@ -1,14 +1,16 @@ -import { cliux } from '@contentstack/cli-utilities'; +import { cliux, validatePath } from '@contentstack/cli-utilities'; import * as path from 'path'; import first from 'lodash/first'; import split from 'lodash/split'; export const askContentDir = async (): Promise => { - const result = await cliux.inquire({ + let result = await cliux.inquire({ type: 'input', message: 'Enter the path for the content', name: 'dir', + validate: validatePath, }); + result = result.replace(/["']/g, ''); return path.resolve(result); }; diff --git a/packages/contentstack-utilities/package.json b/packages/contentstack-utilities/package.json index 165da091c4..3e0d57e671 100644 --- a/packages/contentstack-utilities/package.json +++ b/packages/contentstack-utilities/package.json @@ -14,7 +14,7 @@ "posttest": "npm run lint", "lint": "eslint src/**/*.ts", "format": "eslint src/**/*.ts --fix", - "test:unit": "mocha --forbid-only \"test/unit/**/*.test.ts\"", + "test:unit": "mocha --forbid-only \"test/unit/**/helper.test.ts\"", "test:unit:report": "nyc --extension .ts mocha --forbid-only \"test/unit/**/*.test.ts\"" }, "repository": { diff --git a/packages/contentstack-utilities/src/helpers.ts b/packages/contentstack-utilities/src/helpers.ts index b9fcbd4877..ea606592c5 100644 --- a/packages/contentstack-utilities/src/helpers.ts +++ b/packages/contentstack-utilities/src/helpers.ts @@ -37,5 +37,21 @@ export const createDeveloperHubUrl = (developerHubBaseUrl: string): string => { return developerHubBaseUrl.startsWith('http') ? developerHubBaseUrl : `https://${developerHubBaseUrl}`; }; + +export const validatePath = (input: string) => { + const pattern = /[*$%#<>{}!&?]/g; + if (pattern.test(input)) { + cliux.print( + `\nYour mentioned directory path contains special characters (*,&,{,},[,],$,%,<,>,?,!) please add a path without them`, + { + color: 'yellow', + }, + ); + return false; + } + return true; +}; + // To escape special characters in a string -export const escapeRegExp = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); \ No newline at end of file +export const escapeRegExp = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + diff --git a/packages/contentstack-utilities/test/unit/helper.test.ts b/packages/contentstack-utilities/test/unit/helper.test.ts new file mode 100644 index 0000000000..55f983ce80 --- /dev/null +++ b/packages/contentstack-utilities/test/unit/helper.test.ts @@ -0,0 +1,25 @@ +import { cliux, validatePath } from '../../lib'; +import { expect } from '@oclif/test'; +import { fancy } from '@contentstack/cli-dev-dependencies'; + +describe('Testing the Validate function', () => { + describe('When there is no input', () => { + it('should return true', () => { + expect(validatePath('')).eql(true); + }); + }); + describe('When input contains special character', () => { + fancy + .stub(cliux, 'print', () => {}) + .it('should return true', () => { + expect(validatePath('/invalidPath*&%$#')).eql(false); + }); + }); + describe('When input does not contains special character', () => { + fancy + .stub(cliux, 'print', () => {}) + .it('should return true', () => { + expect(validatePath('/validPath')).eql(true); + }); + }); +}); diff --git a/packages/contentstack/package.json b/packages/contentstack/package.json index 0134ecfa04..816ea056c3 100755 --- a/packages/contentstack/package.json +++ b/packages/contentstack/package.json @@ -27,7 +27,7 @@ "@contentstack/cli-cm-bootstrap": "~1.9.0", "@contentstack/cli-cm-branches": "~1.0.23", "@contentstack/cli-cm-bulk-publish": "~1.4.3", - "@contentstack/cli-cm-export": "~1.11.0", + "@contentstack/cli-cm-export": "~1.11.1", "@contentstack/cli-cm-clone": "~1.10.1", "@contentstack/cli-cm-export-to-csv": "~1.7.0", "@contentstack/cli-cm-import": "~1.14.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c053b1a98c..a10c44d306 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,7 +16,7 @@ importers: '@contentstack/cli-cm-branches': ~1.0.23 '@contentstack/cli-cm-bulk-publish': ~1.4.3 '@contentstack/cli-cm-clone': ~1.10.1 - '@contentstack/cli-cm-export': ~1.11.0 + '@contentstack/cli-cm-export': ~1.11.1 '@contentstack/cli-cm-export-to-csv': ~1.7.0 '@contentstack/cli-cm-import': ~1.14.1 '@contentstack/cli-cm-migrate-rte': ~1.4.15 @@ -423,7 +423,7 @@ importers: packages/contentstack-clone: specifiers: '@colors/colors': ^1.5.0 - '@contentstack/cli-cm-export': ~1.11.0 + '@contentstack/cli-cm-export': ~1.11.1 '@contentstack/cli-cm-import': ~1.14.1 '@contentstack/cli-command': ~1.2.16 '@contentstack/cli-utilities': ~1.6.0