Skip to content

Commit 9c32cba

Browse files
committed
added file uploade
1 parent 5a62a84 commit 9c32cba

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

fileUpdate.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { cliux, messageHandler } = require('@contentstack/cli-utilities');
4+
const isEmpty = (value) => value === null || value === undefined ||
5+
(typeof value === 'object' && Object.keys(value).length === 0) ||
6+
(typeof value === 'string' && value.trim().length === 0);;
7+
const config = {
8+
plan: {
9+
dropdown: { optionLimit: 100 }
10+
},
11+
cmsType: null,
12+
isLocalPath: true,
13+
awsData: {
14+
awsRegion: 'us-east-2',
15+
awsAccessKeyId: '',
16+
awsSecretAccessKey: '',
17+
awsSessionToken: '',
18+
bucketName: 'migartion-test',
19+
buketKey: 'project/package 45.zip'
20+
},
21+
localPath: null
22+
};
23+
24+
const configFilePath = path.resolve(path?.join?.('upload-api', 'src', 'config', 'index.ts'));
25+
26+
const ensureDirectoryExists = (filePath) => {
27+
const dir = path.dirname(filePath);
28+
if (!fs.existsSync(dir)) {
29+
fs.mkdirSync(dir, { recursive: true });
30+
console.log('📂 Created missing directory:', dir);
31+
}
32+
};
33+
34+
const inquireRequireFieldValidation = (input) => {
35+
if (isEmpty(input)) {
36+
return messageHandler.parse('Please enter the path');
37+
}
38+
if (!fs.existsSync(input)) {
39+
return messageHandler.parse('The specified path does not exist. Please enter a valid path.');
40+
}
41+
return true;
42+
};
43+
44+
const typeSwitcher = async (type) => {
45+
switch (type) {
46+
case 'Aws S3': {
47+
const awsData = {
48+
awsRegion: await cliux.inquire({
49+
type: 'input',
50+
message: 'Enter AWS Region',
51+
name: 'awsRegion',
52+
validate: inquireRequireFieldValidation
53+
}),
54+
awsAccessKeyId: await cliux.inquire({
55+
type: 'input',
56+
message: 'Enter AWS Access Key Id',
57+
name: 'awsAccessKeyId',
58+
validate: inquireRequireFieldValidation
59+
}),
60+
awsSecretAccessKey: await cliux.inquire({
61+
type: 'input',
62+
message: 'Enter AWS Secret Access Key',
63+
name: 'awsSecretAccessKey',
64+
validate: inquireRequireFieldValidation
65+
}),
66+
};
67+
const isSessionToken = await cliux.inquire({
68+
choices: ['yes', 'no'],
69+
type: 'list',
70+
name: 'isSessionToken',
71+
message: 'Do you have a Session Token?'
72+
});
73+
if (isSessionToken === 'yes') {
74+
awsData.awsSessionToken = await cliux.inquire({
75+
type: 'input',
76+
message: 'Enter AWS Session Token',
77+
name: 'awsSessionToken',
78+
validate: inquireRequireFieldValidation
79+
});
80+
}
81+
return awsData;
82+
}
83+
case 'Locale Path': {
84+
return await cliux.inquire({
85+
type: 'input',
86+
message: 'Enter file path',
87+
name: 'filePath',
88+
validate: inquireRequireFieldValidation
89+
});
90+
}
91+
default:
92+
console.log('⚠️ Invalid type provided');
93+
return;
94+
}
95+
};
96+
97+
const XMLMigration = async () => {
98+
const typeOfcms = await cliux.inquire({
99+
choices: ['sitecore', 'contentful'],
100+
type: 'list',
101+
name: 'value',
102+
message: 'Choose the option to proceed'
103+
});
104+
105+
const data = await typeSwitcher('Locale Path');
106+
if (typeof typeOfcms === 'string') {
107+
config.cmsType = typeOfcms;
108+
} else {
109+
console.log('⚠️ Error: Expected a string for typeOfcms but got an object.');
110+
}
111+
if (typeof data === 'string') {
112+
config.localPath = data;
113+
} else {
114+
console.log('⚠️ Error: Expected a string for localPath but got an object.');
115+
}
116+
ensureDirectoryExists(configFilePath);
117+
fs.writeFileSync(configFilePath, `export default ${JSON.stringify(config, null, 2)};`, 'utf8');
118+
};
119+
120+
XMLMigration();

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"api": "cd ./api && npm run dev",
1010
"upload": "cd ./upload-api && npm start",
1111
"ui": "cd ./ui && npm start",
12+
"setup:file": "npm i && node fileUpdate.js",
1213
"create:env": "node index.js",
1314
"setup:mac": "bash setup.sh"
1415
},
@@ -37,5 +38,8 @@
3738
"validate-branch-name": {
3839
"pattern": "^(feature|bugfix|hotfix)/[a-z0-9-]{5,30}$",
3940
"errorMsg": "Please add valid branch name!"
41+
},
42+
"dependencies": {
43+
"@contentstack/cli-utilities": "^1.8.4"
4044
}
4145
}

setup.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ sudo chown -R $(id -u):$(id -g) "$HOME/.npm"
4848
echo "Creating .env file..."
4949
npm run create:env
5050

51+
echo "Updating config file..."
52+
npm run setup:file
53+
5154
# Start services in new terminals
5255
echo "Starting services in new terminals..."
5356

0 commit comments

Comments
 (0)