-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfileUpdate.js
More file actions
120 lines (113 loc) · 3.46 KB
/
fileUpdate.js
File metadata and controls
120 lines (113 loc) · 3.46 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
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 config = {
plan: {
dropdown: { optionLimit: 100 }
},
cmsType: null,
isLocalPath: true,
awsData: {
awsRegion: 'us-east-2',
awsAccessKeyId: '',
awsSecretAccessKey: '',
awsSessionToken: '',
bucketName: '',
bucketKey: ''
},
localPath: null
};
const configFilePath = path.resolve(path?.join?.('upload-api', 'src', 'config', 'index.ts'));
const ensureDirectoryExists = (filePath) => {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
console.log('📂 Created missing directory:', dir);
}
};
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 typeSwitcher = async (type) => {
switch (type) {
case 'Aws S3': {
const awsData = {
awsRegion: await cliux.inquire({
type: 'input',
message: 'Enter AWS Region',
name: 'awsRegion',
validate: inquireRequireFieldValidation
}),
awsAccessKeyId: await cliux.inquire({
type: 'input',
message: 'Enter AWS Access Key Id',
name: 'awsAccessKeyId',
validate: inquireRequireFieldValidation
}),
awsSecretAccessKey: await cliux.inquire({
type: 'input',
message: 'Enter AWS Secret Access Key',
name: 'awsSecretAccessKey',
validate: inquireRequireFieldValidation
}),
};
const isSessionToken = await cliux.inquire({
choices: ['yes', 'no'],
type: 'list',
name: 'isSessionToken',
message: 'Do you have a Session Token?'
});
if (isSessionToken === 'yes') {
awsData.awsSessionToken = await cliux.inquire({
type: 'input',
message: 'Enter AWS Session Token',
name: 'awsSessionToken',
validate: inquireRequireFieldValidation
});
}
return awsData;
}
case 'Locale Path': {
return await cliux.inquire({
type: 'input',
message: 'Enter file path',
name: 'filePath',
validate: inquireRequireFieldValidation
});
}
default:
console.log('⚠️ Invalid type provided');
return;
}
};
const XMLMigration = async () => {
const typeOfcms = await cliux.inquire({
choices: ['sitecore', 'contentful', 'wordpress', 'aem'],
type: 'list',
name: 'value',
message: 'Choose the option to proceed with your legacy CMS:'
});
const data = await typeSwitcher('Locale Path');
if (typeof typeOfcms === 'string') {
config.cmsType = typeOfcms;
} else {
console.log('⚠️ Error: Expected a string for typeOfcms but got an object.');
}
if (typeof data === 'string') {
config.localPath = data;
} else {
console.log('⚠️ Error: Expected a string for localPath but got an object.');
}
ensureDirectoryExists(configFilePath);
fs.writeFileSync(configFilePath, `export default ${JSON.stringify(config, null, 2)};`, 'utf8');
};
XMLMigration();