-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathinteractive.ts
More file actions
112 lines (96 loc) · 3.19 KB
/
interactive.ts
File metadata and controls
112 lines (96 loc) · 3.19 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
import { cliux, validatePath } from '@contentstack/cli-utilities';
import * as path from 'path';
import first from 'lodash/first';
import split from 'lodash/split';
export const askContentDir = async (): Promise<string> => {
let result = await cliux.inquire<string>({
type: 'input',
message: 'Enter the path for the content',
name: 'dir',
validate: validatePath,
});
result = result.replace(/["']/g, '');
return path.resolve(result);
};
export const askAPIKey = async (): Promise<string> => {
return cliux.inquire<string>({
type: 'input',
message: 'Enter the stack api key',
name: 'apiKey',
validate: (input: string) => {
if (!input || !input.trim()) {
return 'Stack API key cannot be empty. Please enter a valid stack API key.';
}
return true;
},
});
};
export const askEncryptionKey = async (defaultValue: unknown): Promise<string> => {
return await cliux.inquire({
type: 'input',
name: 'name',
default: defaultValue,
validate: (key) => {
if (!key) return "Encryption key can't be empty.";
return true;
},
message: 'Enter Marketplace app configurations encryption key',
});
};
export const askAppName = async (app: any, appSuffix: number): Promise<string> => {
return await cliux.inquire({
type: 'input',
name: 'name',
validate: validateAppName,
default: getAppName(app.name, appSuffix),
message: `${app.name} app already exist. Enter a new name to create an app.?`,
});
};
export const getAppName = (name: string, appSuffix = 1) => {
if (name.length >= 19) name = name.slice(0, 18);
name = `${first(split(name, '◈'))}◈${appSuffix}`;
return name;
};
export const getLocationName = (name: string, appSuffix = 1, existingNames: Set<string>) => {
const maxLength = 50;
const suffixLength = appSuffix.toString().length + 1; // +1 for the '◈' character
let truncatedName = name;
if (name.length + suffixLength > maxLength) {
truncatedName = name.slice(0, maxLength - suffixLength);
}
let newName = `${first(split(truncatedName, '◈'))}◈${appSuffix}`;
// Ensure uniqueness
while (existingNames.has(newName)) {
appSuffix++;
newName = `${first(split(truncatedName, '◈'))}◈${appSuffix}`;
}
// Add the new name to the set of existing names
existingNames.add(newName);
return newName;
};
const validateAppName = (name: string) => {
if (name.length < 3 || name.length > 20) {
return 'The app name should be within 3-20 characters long.';
}
return true;
};
export const selectConfiguration = async (): Promise<string> => {
return await cliux.inquire({
choices: [
'Update it with the new configuration.',
'Do not update the configuration (WARNING!!! If you do not update the configuration, there may be some issues with the content which you import).',
'Exit',
],
type: 'list',
name: 'value',
message: 'Choose the option to proceed',
});
};
export const askBranchSelection = async (branchNames: string[]): Promise<string> => {
return await cliux.inquire({
type: 'list',
name: 'branch',
message: 'Found multiple branches in your export path. Please select one to import:',
choices: branchNames,
});
};