-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfileUpdate.js
More file actions
133 lines (118 loc) · 4.11 KB
/
Copy pathfileUpdate.js
File metadata and controls
133 lines (118 loc) · 4.11 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
const fs = require('fs');
const path = require('path');
const { cliux, messageHandler } = require('@contentstack/cli-utilities');
const isEmpty = (value) => value === null || value === undefined ||
(typeof value === 'object' && Object.keys(value).length === 0) ||
(typeof value === 'string' && value.trim().length === 0);
const envFilePath = path.resolve(path.join('upload-api', '.env'));
const ensureDirectoryExists = (filePath) => {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
console.log('📂 Created missing directory:', dir);
}
};
const readEnvFile = () => {
const vars = {};
if (fs.existsSync(envFilePath)) {
const content = fs.readFileSync(envFilePath, 'utf8');
for (const line of content.split('\n')) {
const trimmed = line.trim();
if (trimmed && !trimmed.startsWith('#')) {
const eqIndex = trimmed.indexOf('=');
if (eqIndex > 0) {
vars[trimmed.substring(0, eqIndex)] = trimmed.substring(eqIndex + 1);
}
}
}
}
return vars;
};
const writeEnvFile = (newVars) => {
ensureDirectoryExists(envFilePath);
const existing = readEnvFile();
const merged = { ...existing, ...newVars };
const lines = Object.entries(merged).map(([k, v]) => `${k}=${v}`);
fs.writeFileSync(envFilePath, lines.join('\n') + '\n', 'utf8');
};
const inquireRequireFieldValidation = (input) => {
if (isEmpty(input)) {
return messageHandler.parse('Please enter the path');
}
if (!fs.existsSync(input)) {
return messageHandler.parse('The specified path does not exist. Please enter a valid path.');
}
return true;
};
const XMLMigration = async () => {
const typeOfcms = await cliux.inquire({
choices: ['sitecore', 'contentful', 'wordpress', 'aem', 'drupal'],
type: 'list',
name: 'value',
message: 'Choose the option to proceed with your legacy CMS:'
});
if (typeof typeOfcms !== 'string') {
console.log('⚠️ Error: Expected a string for typeOfcms but got an object.');
return;
}
const envVars = { CMS_TYPE: typeOfcms };
if (typeOfcms === 'drupal') {
console.log('\nDrupal uses a MySQL database connection. Please provide your database details:');
envVars.MYSQL_HOST = await cliux.inquire({
type: 'input',
message: 'Enter MySQL Host',
name: 'mysqlHost',
validate: (input) => isEmpty(input) ? 'Please enter the MySQL host' : true
});
envVars.MYSQL_USER = await cliux.inquire({
type: 'input',
message: 'Enter MySQL User',
name: 'mysqlUser',
validate: (input) => isEmpty(input) ? 'Please enter the MySQL user' : true
});
envVars.MYSQL_PASSWORD = await cliux.inquire({
type: 'password',
message: 'Enter MySQL Password (can be empty)',
name: 'mysqlPassword'
});
envVars.MYSQL_DATABASE = await cliux.inquire({
type: 'input',
message: 'Enter MySQL Database Name',
name: 'mysqlDatabase',
validate: (input) => isEmpty(input) ? 'Please enter the MySQL database name' : true
});
const portInput = await cliux.inquire({
type: 'input',
message: 'Enter MySQL Port [3306]',
name: 'mysqlPort'
});
envVars.MYSQL_PORT = portInput || '3306';
envVars.DRUPAL_ASSETS_BASE_URL = await cliux.inquire({
type: 'input',
message: 'Enter Drupal Assets Base URL (e.g. https://example.com)',
name: 'assetsBaseUrl'
});
envVars.DRUPAL_ASSETS_PUBLIC_PATH = await cliux.inquire({
type: 'input',
message: 'Enter Drupal Assets Public Path (e.g. sites/default/files)',
name: 'assetsPublicPath'
});
envVars.CMS_LOCAL_PATH = 'sql';
} else {
const localPath = await cliux.inquire({
type: 'input',
message: 'Enter file path',
name: 'filePath',
validate: inquireRequireFieldValidation
});
if (typeof localPath === 'string') {
envVars.CMS_LOCAL_PATH = localPath;
} else {
console.log('⚠️ Error: Expected a string for localPath but got an object.');
return;
}
}
writeEnvFile(envVars);
console.log(`✅ Configuration written to ${envFilePath}`);
};
XMLMigration();