This repository was archived by the owner on Jan 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelper.js
More file actions
executable file
·144 lines (128 loc) · 4.9 KB
/
helper.js
File metadata and controls
executable file
·144 lines (128 loc) · 4.9 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*!
* contentstack-cli
* copyright (c) Built.io Contentstack
* MIT Licensed
*/
'use strict';
/*!
* Module dependencies
*/
var prompt = require('prompt'),
ncp = require('ncp').ncp,
_ = require('lodash'),
mkdirp = require('mkdirp'),
path = require('path'),
fs = require('fs');
/*
* Application variable
* */
function mkdirAllSync(path, permission) {
mkdirp.sync(path, permission);
};
function createDirectories(language) {
if (!fs.existsSync(language.contentPath)) mkdirAllSync(language.contentPath, "0755");
if (!fs.existsSync(language.assetsPath)) mkdirAllSync(language.assetsPath, "0755");
if (!fs.existsSync(path.join(language.assetsPath, '__cms_assets.json'))) fs.writeFileSync(path.join(__path, '__cms_assets.json'), '{}');
};
var helper = exports;
exports.url = 'https://contentstack.built.io/';
exports.title = 'Built.io Contentstack';
exports.confirm = function(config, lang, backup, callback) {
try {
var languages = config.get('languages'),
storagePath = config.get('path.storage'),
language;
if(languages && lang) {
language = _.findIndex(languages, {"code": lang});
if(~language) {
language = languages[language];
helper.emptyDirectory(language.contentPath, function (empty) {
if (empty) {
createDirectories(language);
callback();
} else {
if(backup === undefined) {
prompt.message = prompt.delimiter = "";
prompt.get([{
name: 'confirm',
description: 'Do you want to create a backup of the existing content? (Yes/No):',
message: 'Please provide confirmation.',
required: true,
conform: function (value) {
value = value.toLowerCase();
return ( value == "yes" || value == "no" );
},
before: function (value) {
return value.toLowerCase();
}
}], function (err, result) {
if(err) throw err;
var ok = (result.confirm == "yes") ? true : false;
process.stdin.destroy();
if (ok) {
helper.createBackupDir(storagePath, lang, callback);
} else {
callback();
}
});
} else if (backup === false) {
callback();
} else {
helper.createBackupDir(storagePath, lang, callback);
}
}
});
} else {
throw new Error("Language code is incorrectly specified or is not present in the configuration file.");
}
} else {
throw new Error("Language code is incorrectly specified or is not present in the configuration file.");
}
} catch (e) {
console.error("Error in Confirm : ", e.message);
callback(e);
}
};
exports.createBackupDir = function (storagePath, lang, callback) {
console.log("Creating backup.....");
var d = new Date();
ncp(path.join(storagePath, lang), path.join(storagePath, lang, "..", d.getTime() + "_" + lang + "_backup"), function (err) {
if (err) {
console.error("Backup creation for path[%s] failed. Error: ", __path, err);
}
console.log("Backup created successfully.");
callback();
});
};
exports.emptyDirectory = function (path, fn) {
fs.readdir(path, function (err, files) {
if (err && 'ENOTDIR' == err.code) {
fn(new Error('The name "'+path+'" is already used in this location. Please use a different name.'))
}else if (err && 'ENOENT' != err.code) {
throw err;
}else{
fn(null, !files || !files.length);
}
});
};
exports.mkdir = function (path, fn) {
mkdirp(path, "0755", function (err) {
if (err) {
throw err;
}
console.log(' \u001b[36mcreated\u001b[0m : ' + path);
fn && fn();
});
};
exports.write = function (path, str) {
fs.writeFile(path, str);
console.log(' \x1b[36mcreated\x1b[0m : ' + path);
};
exports.abort = function (str) {
console.error('\u001b[31m'+str+'\u001b[0m');
process.exit(1);
};
exports.merge = function(source, destination) {
for(var key in destination) source[key] = destination[key];
return source;
};