Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DX - 56 - Flags introduction for excluding the global modules #1431

Merged
merged 12 commits into from
Jun 10, 2024
3,076 changes: 531 additions & 2,545 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/contentstack-clone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"@colors/colors": "^1.5.0",
"@contentstack/cli-cm-export": "~1.11.4",
"@contentstack/cli-cm-import": "~1.15.7",
"@contentstack/cli-cm-import": "~1.16.0",
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
"@contentstack/cli-command": "~1.2.18",
"@contentstack/cli-utilities": "~1.6.2",
"async": "^3.2.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-import/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-cm-import",
"description": "Contentstack CLI plugin to import content into stack",
"version": "1.15.7",
"version": "1.16.0",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ export default class ImportCommand extends Command {
'skip-audit': flags.boolean({
description: 'Skips the audit fix.',
}),
'exclude-global-modules': flags.boolean({
description: 'Excluded the Module that are branch independent from the import',
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
}),
'mapper-dir': flags.string({
description: 'path of backup directory for base branch import',
shafeeqd959 marked this conversation as resolved.
Show resolved Hide resolved
}),
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
};

static aliases: string[] = ['cm:import'];
Expand Down
3 changes: 2 additions & 1 deletion packages/contentstack-import/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ const config: DefaultConfig = {
returnResponse: true, // On process completion should return config used in the command
noTerminalOutput: false, // Skip final audit table output on terminal
config: { basePath: '' }, // To overwrite any build-in config. And this config is equal to --config flag.
}
},
globalModules: ['taxonomies', 'environments', 'marketplace-apps', 'webhooks'],
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
};

export default config;
4 changes: 4 additions & 0 deletions packages/contentstack-import/src/import/module-importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class ModuleImporter {
async importAllModules(): Promise<any> {
// use the algorithm to determine the parallel and sequential execution of modules
for (let moduleName of this.importConfig.modules.types) {
if (this.importConfig.globalModules.includes(moduleName) && this.importConfig['exclude-global-modules']) {
log(this.importConfig, `Skipping the import for '${moduleName}' ...`,'warn');
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
await this.importByModuleByName(moduleName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,18 @@ export default class ContentTypesImport extends BaseClass {
this.cTsSuccessPath = path.join(sanitizePath(this.cTsMapperPath), 'success.json');
this.gFsFolderPath = path.resolve(sanitizePath(this.importConfig.data), sanitizePath(this.gFsConfig.dirName));
this.gFsMapperFolderPath = path.join(sanitizePath(importConfig.data), 'mapper', 'global_fields', 'success.json');
this.gFsPendingPath = path.join(sanitizePath(importConfig.data), 'mapper', 'global_fields', 'pending_global_fields.js');
this.marketplaceAppMapperPath = path.join(sanitizePath(this.importConfig.data), 'mapper', 'marketplace_apps', 'uid-mapping.json');
this.gFsPendingPath = path.join(
sanitizePath(importConfig.data),
'mapper',
'global_fields',
'pending_global_fields.js',
);
this.marketplaceAppMapperPath = path.join(
sanitizePath(this.importConfig.data),
'mapper',
'marketplace_apps',
'uid-mapping.json',
);
this.ignoredFilesInContentTypesFolder = new Map([
['__master.json', 'true'],
['__priority.json', 'true'],
Expand Down
1 change: 1 addition & 0 deletions packages/contentstack-import/src/types/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,5 @@ export default interface DefaultConfig {
branch?: string;
} & Record<string, any>; // To overwrite any build-in config. And this config is equal to --config flag.
};
globalModules: string[];
}
2 changes: 2 additions & 0 deletions packages/contentstack-import/src/types/import-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export default interface ImportConfig extends DefaultConfig, ExternalConfig {
skipExisting?: boolean;
skipAudit?: boolean;
stackName?: string;
'mapper-dir'?: string;
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
'exclude-global-modules': false;
}

type branch = {
Expand Down
31 changes: 29 additions & 2 deletions packages/contentstack-import/src/utils/backup-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import { ImportConfig } from '../types';

export default async function backupHandler(importConfig: ImportConfig): Promise<string> {
if (importConfig.hasOwnProperty('useBackedupDir')) {
// await copyMapperFolder(importConfig, importConfig.useBackedupDir);
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
return importConfig.useBackedupDir;
}

let backupDirPath: string;
const subDir = isSubDirectory(importConfig);

if (subDir) {
backupDirPath = path.resolve(sanitizePath(importConfig.contentDir), '..', '_backup_' + Math.floor(Math.random() * 1000));
backupDirPath = path.resolve(
sanitizePath(importConfig.contentDir),
'..',
'_backup_' + Math.floor(Math.random() * 1000),
);
if (importConfig.createBackupDir) {
cliux.print(
`Warning!!! Provided backup directory path is a sub directory of the content directory, Cannot copy to a sub directory. Hence new backup directory created - ${backupDirPath}`,
Expand All @@ -37,7 +42,7 @@ export default async function backupHandler(importConfig: ImportConfig): Promise

if (backupDirPath) {
cliux.print('Copying content to the backup directory...');

await copyMapperFolder(importConfig, backupDirPath);
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
return new Promise((resolve, reject) => {
return copy(importConfig.contentDir, backupDirPath, (error: any) => {
if (error) {
Expand Down Expand Up @@ -67,3 +72,25 @@ function isSubDirectory(importConfig: ImportConfig) {
// true if both parent and child have same path
return true;
}

/**
*
*/

async function copyMapperFolder(importConfig: ImportConfig, backupDirPath: string) {
try {
const mapperDir = importConfig['mapper-dir'];
if (!mapperDir) {
return;
}
const globalModules = ['taxonomies', 'environments', 'marketplace_apps', 'webhooks'];
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
const copyPromises = globalModules.map((globalModule) =>
copy(`${mapperDir}/mapper/${globalModule}`, `${backupDirPath}/mapper/${globalModule}`),
);
await Promise.all(copyPromises);
return backupDirPath;
} catch (error) {
trace(error, 'error', true);
throw error;
}
}
19 changes: 12 additions & 7 deletions packages/contentstack-import/src/utils/import-config-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ 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(
`\nPlease add a directory path without any of the special characters: (*,&,{,},[,],$,%,<,>,?,!)`,
{
color: 'yellow',
},
);
cliux.print(`\nPlease add a directory path without any of the special characters: (*,&,{,},[,],$,%,<,>,?,!)`, {
color: 'yellow',
});
config.contentDir = await askContentDir();
}
config.contentDir = config.contentDir.replace(/['"]/g, '');
Expand Down Expand Up @@ -81,7 +78,7 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {

if (importCmdFlags['branch']) {
config.branchName = importCmdFlags['branch'];
config.branchDir = path.join(sanitizePath(config.contentDir), sanitizePath (config.branchName));
config.branchDir = path.join(sanitizePath(config.contentDir), sanitizePath(config.branchName));
}
if (importCmdFlags['module']) {
config.moduleName = importCmdFlags['module'];
Expand All @@ -98,6 +95,14 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
config.replaceExisting = importCmdFlags['replace-existing'];
config.skipExisting = importCmdFlags['skip-existing'];

if (importCmdFlags['mapper-dir']) {
config['mapper-dir'] = importCmdFlags['mapper-dir'];
}

if (importCmdFlags['exclude-global-modules']) {
config['exclude-global-modules'] = importCmdFlags['exclude-global-modules'];
}

return config;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-seed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-cm-import": "~1.15.7",
"@contentstack/cli-cm-import": "~1.16.0",
"@contentstack/cli-command": "~1.2.18",
"@contentstack/cli-utilities": "~1.6.2",
"inquirer": "8.2.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli",
"description": "Command-line tool (CLI) to interact with Contentstack",
"version": "1.19.0",
"version": "1.20.0",
"author": "Contentstack",
"bin": {
"csdx": "./bin/run.js"
Expand Down Expand Up @@ -30,7 +30,7 @@
"@contentstack/cli-cm-export": "~1.11.4",
"@contentstack/cli-cm-clone": "~1.10.5",
"@contentstack/cli-cm-export-to-csv": "~1.7.1",
"@contentstack/cli-cm-import": "~1.15.7",
"@contentstack/cli-cm-import": "~1.16.0",
"@contentstack/cli-cm-migrate-rte": "~1.4.17",
"@contentstack/cli-cm-seed": "~1.7.5",
"@contentstack/cli-command": "~1.2.18",
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

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

Loading