-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathfileUtils.ts
138 lines (122 loc) · 4.12 KB
/
fileUtils.ts
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
import {
addOrUpdateFile,
FileState,
renameFile,
} from 'preview/store/file.slice';
import { LibraryConfigFile } from 'preview/store/libraryConfigFiles.slice';
import { Dispatch, SetStateAction } from 'react';
import { useDispatch } from 'react-redux';
export const isFileModifiable = (fileName: string) =>
!['README.md', 'description.md', '.gitlab-ci.yml'].includes(fileName);
export const isFileDeletable = (fileName: string) =>
!['.gitlab-ci.yml'].includes(fileName);
export const defaultFiles = [
{ name: 'description.md', type: 'description' },
{ name: 'README.md', type: 'description' },
{ name: '.gitlab-ci.yml', type: 'config' },
];
export const getExtension = (filename: string): string => {
const parts = filename.split('.');
return parts.length > 1 ? parts.pop()! : '';
};
export const validateFiles = (
files: FileState[],
libraryFiles: LibraryConfigFile[],
setErrorMessage: Dispatch<SetStateAction<string>>,
): boolean => {
const emptyFiles = files
.filter((file) => file.isNew && file.content === '')
.map((file) => file.name);
const emptyLibraryFiles = libraryFiles.filter(
(file) => file.isNew && file.isModified && file.fileContent === '',
);
if (emptyFiles.length > 0 || emptyLibraryFiles.length > 0) {
setErrorMessage(
`The following files have empty content: ${
emptyFiles.length > 0 ? emptyFiles.join(', ') : ''
}${emptyFiles.length > 0 && emptyLibraryFiles.length > 0 ? ', ' : ''}${
emptyLibraryFiles.length > 0
? emptyLibraryFiles
.map((file) => `${file.fileName} (${file.assetPath})`)
.join(', ')
: ''
}.\n Edit them in order to create the new digital twin.`,
);
return true;
}
return false;
};
export const addDefaultFiles = (
defaultFilesNames: { name: string; type: string }[],
files: FileState[],
dispatch: ReturnType<typeof useDispatch>,
) => {
defaultFilesNames.forEach((file) => {
if (!files.some((existingFile) => existingFile.name === file.name)) {
dispatch(
addOrUpdateFile({
name: file.name,
content: '',
isNew: true,
isModified: false,
type: file.type,
}),
);
}
});
};
export const handleChangeFileName = (
files: FileState[],
modifiedFileName: string,
fileName: string,
setFileName: Dispatch<SetStateAction<string>>,
setFileType: Dispatch<SetStateAction<string>>,
setErrorChangeMessage: Dispatch<SetStateAction<string>>,
setOpenChangeFileNameDialog: Dispatch<SetStateAction<boolean>>,
dispatch: ReturnType<typeof useDispatch>,
) => {
const fileExists = files.some(
(fileStore: { name: string }) => fileStore.name === modifiedFileName,
);
if (fileExists) {
setErrorChangeMessage('A file with this name already exists.');
return;
}
if (modifiedFileName === '') {
setErrorChangeMessage("File name can't be empty.");
return;
}
setErrorChangeMessage('');
dispatch(renameFile({ oldName: fileName, newName: modifiedFileName }));
setFileName(modifiedFileName);
const extension = getExtension(modifiedFileName);
setFileType(extension);
setOpenChangeFileNameDialog(false);
};
export const getFileTypeFromExtension = (fileName: string): string => {
const extension = fileName.split('.').pop()?.toLowerCase();
if (extension === 'md') return 'description';
if (extension === 'json' || extension === 'yaml' || extension === 'yml')
return 'config';
return 'lifecycle';
};
export const getFilteredFileNames = (type: string, files: FileState[]) =>
files
.filter(
(file) => file.isNew && getFileTypeFromExtension(file.name) === type,
)
.map((file) => file.name);
export const updateFileState = (
fileName: string,
fileContent: string,
setFileName: Dispatch<SetStateAction<string>>,
setFileContent: Dispatch<SetStateAction<string>>,
setFileType: Dispatch<SetStateAction<string>>,
setFilePrivacy: Dispatch<SetStateAction<string>>,
isPrivate?: boolean,
) => {
setFileName(fileName);
setFileContent(fileContent);
setFileType(fileName.split('.').pop()!);
setFilePrivacy(isPrivate === undefined || isPrivate ? 'private' : 'common');
};