forked from dsyman2/contentstack-import
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·100 lines (92 loc) · 2.98 KB
/
app.js
File metadata and controls
executable file
·100 lines (92 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*!
* Contentstack Import
* Copyright (c) 2019 Contentstack LLC
* MIT Licensed
*/
var ncp = require('ncp');
var Bluebird = require('bluebird');
var fs = require('fs');
var path = require('path');
var util = require('./lib/util/index');
var login = require('./lib/util/login');
var log = require('./lib/util/log');
var config = require('./config');
config = util.buildAppConfig(config);
util.validateConfig(config);
exports.getConfig = function () {
return config;
};
login(config)
.then(function () {
var migrationBackupDirPath = path.join(process.cwd(), '_backup_' + Math.floor((Math.random() * 1000)));
return createBackup(migrationBackupDirPath).then((basePath) => {
config.data = basePath;
return util.sanitizeStack(config);
})
.then(() => {
var types = config.modules.types;
if (process.argv.length === 3) {
var val = process.argv[2];
if (val && types.indexOf(val) > -1) {
var moduleImport = require('./lib/import/' + val);
return moduleImport.start().then(function () {
log.success(val + ' was imported successfully!');
return;
}).catch(function (error) {
log.error('Failed to import ' + val);
log.error(error);
return;
});
} else {
log.error('Please provide valid module name.');
return 0;
}
} else if (process.argv.length === 2) {
var counter = 0;
return Bluebird.map(types, function () {
var importModule = require('./lib/import/' + types[counter]);
counter++;
return importModule.start();
}, {
concurrency: 1
}).then(function () {
log.success('Import utility executed succesfully!');
return;
}).catch(function (error) {
console.error(error)
log.error('Import utility failed while executing');
log.error(error);
return;
});
} else {
log.error('Only one module can be exported at a time.');
return 0;
}
}).catch(function (error) {
log.error((error.message) ? error.message: error);
process.exit(1);
});
});
function createBackup (backupDirPath) {
return new Promise(function (resolve, reject) {
if (config.hasOwnProperty('useBackedupDir') && fs.existsSync(path.join(__dirname, config.useBackedupDir))) {
return resolve(config.useBackedupDir);
}
ncp.limit = config.backupConcurrency || 16;
if (path.isAbsolute(config.data)) {
return ncp(config.data, backupDirPath, function (error) {
if (error) {
return reject(error);
}
return resolve(backupDirPath);
});
} else {
return ncp(path.join(__dirname, config.data), backupDirPath, function (error) {
if (error) {
return reject(error);
}
return resolve(backupDirPath);
});
}
});
}