Skip to content

Commit

Permalink
Merge pull request #1315 from contentstack/bugfix/CS-44158
Browse files Browse the repository at this point in the history
CS-44158-when adding the path using the inquirer for both import and export removed the quotation marks and version bump
  • Loading branch information
cs-raj authored Mar 5, 2024
2 parents ecb7d0c + a9c69e7 commit 26af493
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/contentstack-clone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 14 additions & 1 deletion packages/contentstack-export/src/utils/export-config-handler.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,7 +16,20 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
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;

Expand Down
8 changes: 5 additions & 3 deletions packages/contentstack-export/src/utils/interactive.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -45,14 +45,16 @@ export const askUsername = async (): Promise<string> => {
};

export const askExportDir = async (): Promise<string> => {
const result = await cliux.inquire<string>({
let result = await cliux.inquire<string>({
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);
}
};
Expand All @@ -63,4 +65,4 @@ export const askAPIKey = async (): Promise<string> => {
message: 'Enter the stack api key',
name: 'apiKey',
});
};
};
14 changes: 12 additions & 2 deletions packages/contentstack-import/src/utils/import-config-handler.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -21,10 +21,20 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
}

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;
Expand Down
6 changes: 4 additions & 2 deletions packages/contentstack-import/src/utils/interactive.ts
Original file line number Diff line number Diff line change
@@ -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<string> => {
const result = await cliux.inquire<string>({
let result = await cliux.inquire<string>({
type: 'input',
message: 'Enter the path for the content',
name: 'dir',
validate: validatePath,
});
result = result.replace(/["']/g, '');
return path.resolve(result);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
18 changes: 17 additions & 1 deletion packages/contentstack-utilities/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\$&');
export const escapeRegExp = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

25 changes: 25 additions & 0 deletions packages/contentstack-utilities/test/unit/helper.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
2 changes: 1 addition & 1 deletion packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 26af493

Please sign in to comment.