Skip to content

Commit

Permalink
Merge pull request #990 from contentstack/fix/CS-40552
Browse files Browse the repository at this point in the history
fix: import recursive backup directory issue
  • Loading branch information
aman19K authored Aug 25, 2023
2 parents c1ca3de + bb1ba62 commit 3e74d3d
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 72 deletions.
100 changes: 65 additions & 35 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions packages/contentstack-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"bluebird": "^3.7.2",
"chalk": "^4.1.2",
"debug": "^4.1.0",
"fs-extra": "^11.1.1",
"lodash": "^4.17.20",
"marked": "^4.0.17",
"merge": "^2.1.1",
"mkdirp": "^1.0.4",
"ncp": "^2.0.0",
"promise-limit": "^2.7.0",
"tslib": "^2.4.1",
"winston": "^3.7.2"
Expand All @@ -27,9 +27,9 @@
"@oclif/test": "^1.2.6",
"@types/bluebird": "^3.5.38",
"@types/chai": "^4.2.18",
"@types/fs-extra": "^11.0.1",
"@types/mkdirp": "^1.0.2",
"@types/mocha": "^8.2.2",
"@types/ncp": "^2.0.5",
"@types/node": "^14.14.32",
"@types/sinon": "^10.0.2",
"@types/tar": "^4.0.3",
Expand Down Expand Up @@ -96,4 +96,4 @@
}
},
"repository": "https://github.com/contentstack/cli"
}
}
1 change: 1 addition & 0 deletions packages/contentstack-import/src/import/module-importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ModuleImporter {
}
const backupDir = await backupHandler(this.importConfig);
if (backupDir) {
log(this.importConfig, `Backup directory path - '${backupDir}'`, 'info');
this.importConfig.backupDir = backupDir;
// To support the old config
this.importConfig.data = backupDir;
Expand Down
59 changes: 40 additions & 19 deletions packages/contentstack-import/src/utils/backup-handler.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
import * as path from 'path';
import ncp from 'ncp';
import { copy } from 'fs-extra';
import { cliux } from '@contentstack/cli-utilities';

import { ImportConfig } from '../types';
import { fileHelper } from './index';
import { ImportConfig } from '../types';

export default function setupBackupDir(importConfig: ImportConfig): Promise<string> {
return new Promise(async (resolve, reject) => {
if (importConfig.hasOwnProperty('useBackedupDir')) {
return resolve(importConfig.useBackedupDir);
}

//NOTE: If the backup folder's directory is provided, create it at that location; otherwise, the default path (working directory).
let backupDirPath = path.join(process.cwd(), '_backup_' + Math.floor(Math.random() * 1000));
if (importConfig.createBackupDir) {
if (fileHelper.fileExistsSync(importConfig.createBackupDir)) {
fileHelper.removeDirSync(importConfig.createBackupDir);
const subDir = isSubDirectory(importConfig);
let backupDirPath: string;

if (subDir) {
backupDirPath = path.resolve(importConfig.contentDir, '..', '_backup_' + Math.floor(Math.random() * 1000));
if (importConfig.createBackupDir) {
cliux.print(`Warning!!! Provided createBackupDir is subdirectory.Cannot copy to a subdirectory of itself. New Backup directory path - ${backupDirPath}`, {
color: 'yellow',
});
}
} else {
//NOTE: If the backup folder's directory is provided, create it at that location; otherwise, the default path (working directory).
backupDirPath = path.join(process.cwd(), '_backup_' + Math.floor(Math.random() * 1000));
if (importConfig.createBackupDir) {
if (fileHelper.fileExistsSync(importConfig.createBackupDir)) {
fileHelper.removeDirSync(importConfig.createBackupDir);
}
fileHelper.makeDirectory(importConfig.createBackupDir);
backupDirPath = importConfig.createBackupDir;
}
fileHelper.makeDirectory(importConfig.createBackupDir);
backupDirPath = importConfig.createBackupDir;
}

const limit = importConfig.backupConcurrency || 16;
if (path.isAbsolute(importConfig.contentDir)) {
return ncp(importConfig.contentDir, backupDirPath, { limit }, (error) => {
if (error) {
return reject(error);
}
return resolve(backupDirPath);
});
} else {
ncp(importConfig.contentDir, backupDirPath, (error) => {
if (backupDirPath) {
return copy(importConfig.contentDir, backupDirPath, (error: any) => {
if (error) {
return reject(error);
}
Expand All @@ -38,3 +43,19 @@ export default function setupBackupDir(importConfig: ImportConfig): Promise<stri
}
});
}

/**
* Check whether provided backup directory path is sub directory or not
* @param importConfig
* @returns
*/
function isSubDirectory(importConfig: ImportConfig) {
const parent = importConfig.contentDir;
const child = importConfig.createBackupDir ? importConfig.createBackupDir : process.cwd();
const relative = path.relative(parent, child);
if (relative) {
return relative && !relative.startsWith('..') && !path.isAbsolute(relative);
}
//true if both parent and child have same path
return true;
}
Loading

0 comments on commit 3e74d3d

Please sign in to comment.