-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
files.ts
199 lines (167 loc) · 7.33 KB
/
files.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import { getInfoAboutPath, PathInfo } from '../../../fileInfo/getInfoAboutPath';
import { DirectoriesInfo } from '../../../fileInfo/getSiblingInfo';
import { getFilesContentAsTemplate } from '../../../fileSystem/getFilesContentAsTemplate';
import { getRelativePath } from '../../../fileSystem/getRelativePath';
import { generateFileAction, setDataAction } from '../../../reactViews/apps/chooseFiles/actions/action';
import { compareTree, createTemplateInTree, filterTree } from '../../../symbolInformation/compareTree';
import { getFileSymbols } from '../../../symbolInformation/getFileSymbols';
import { joinSymbolToString, treeSymbolToFlattArray } from '../../../symbolInformation/joinSymbolToString';
import { compareVariableTemplate } from '../../../variableTemplate/compareVariableTemplate';
import { renderVariableTemplate } from '../../../variableTemplate/renderVariableTemplate';
import { WebViewReact } from '../webView/webViewReact';
export type WebViewInfoAboutFiles = {
content: string;
filePath: string;
filePathTemplate: string;
relativePath: string;
fromTemplate: boolean;
generated: boolean;
};
export class Files {
private webView: WebViewReact;
constructor(webView: WebViewReact){
this.webView = webView;
}
async showWebView(
createdItemUri: vscode.Uri,
filesWithContent:WebViewInfoAboutFiles[],
infoAboutSiblingDirectories: DirectoriesInfo,
siblingTemplatePathFiles: string[],
onHandleEvent: ((filePath: string, content: string) => void)
): Promise<vscode.WebviewPanel> {
let chooseFilesPanel = await this.webView.showWebView('Choose files to create', 'reactAppChooseFiles.js');
const countSiblingDirectories = Object.keys(infoAboutSiblingDirectories).length;
const allSiblingHave: WebViewInfoAboutFiles[] = [];
const notAllSiblingHave: WebViewInfoAboutFiles[] = [];
const fromTemplate: WebViewInfoAboutFiles[] = [];
filesWithContent.forEach(async(fileInfo) => {
if (fileInfo.fromTemplate) {
fromTemplate.push(fileInfo);
} else if (this.countSameTemplates(siblingTemplatePathFiles, fileInfo.filePathTemplate) === countSiblingDirectories) {
allSiblingHave.push(fileInfo);
} else {
notAllSiblingHave.push(fileInfo);
}
});
chooseFilesPanel.webview.postMessage(setDataAction({
createdFolderName: path.basename(createdItemUri.fsPath),
allSiblingHave,
notAllSiblingHave,
fromTemplate
}));
chooseFilesPanel.webview.onDidReceiveMessage((action) => {
if (action.type === generateFileAction.type) {
const { filePath, content } = action.payload;
onHandleEvent(filePath, content);
}
});
return chooseFilesPanel;
}
generateFileContentByTemplate(createdItemUri: vscode.Uri, savedTemplate: string[]): string{
const relativePath = getRelativePath(createdItemUri.fsPath);
const infoAboutNewFile = getInfoAboutPath(relativePath);
const content = savedTemplate.map(line =>
renderVariableTemplate(line, [infoAboutNewFile])
).join('\n');
return content;
}
async getContentBySibling(createdItemUri: vscode.Uri): Promise<string> {
const relativePath = getRelativePath(createdItemUri.fsPath);
const infoAboutNewFile = getInfoAboutPath(relativePath);
const parentDir = path.dirname(createdItemUri.fsPath);
const fileToSkip = path.basename(createdItemUri.fsPath);
const contents = fs.readdirSync(parentDir)
.filter(siblingFile => fs.lstatSync(path.join(parentDir, siblingFile)).isFile() && siblingFile !== fileToSkip);
if (contents.length < 2) {
return '';
}
const files = contents.map(siblingFile => {
const filePath = path.join(parentDir, siblingFile);
const fileUri = vscode.Uri.file(filePath);
const infoAboutFilePath = getInfoAboutPath(getRelativePath(filePath));
return getFileSymbols(fileUri).then(data => createTemplateInTree([data], [infoAboutFilePath])[0]);
});
const filesContent = await Promise.all(files)
.then(data => compareTree(data))
.then(data => filterTree(data, 0.75))
.then(data => treeSymbolToFlattArray(data));
return joinSymbolToString(filesContent, [infoAboutNewFile]);
}
createFileContent(templateStringPath:string, directories: DirectoriesInfo, variables: PathInfo[]): string {
const contents: Array<string[]> = getFilesContentAsTemplate(directories, templateStringPath);
if (contents.length === 0) {
return '';
}
const [baseFile, ...otherFiles] = contents;
const lineToGenerate: string[] = [];
baseFile.forEach(line => {
if(this.allFilesIncludeThisLine(otherFiles, line)){
lineToGenerate.push(renderVariableTemplate(line, variables));
}
});
return lineToGenerate.join('');
}
allFilesIncludeThisLine(files: Array<string[]>, line: string): boolean {
for (let file of files) {
if(!this.includesThisTemplate(file, line)){
return false;
}
}
return true;
}
includesThisTemplate (templates: string [], templateToFind: string) {
for (let fileLine of templates) {
if (compareVariableTemplate(fileLine, templateToFind)) {
return true;
}
}
return false;
}
countSameTemplates (templates: string [], templateToFind: string) {
let counter = 0;
for (let template of templates) {
if (compareVariableTemplate(template, templateToFind)) {
counter++;
}
}
return counter;
}
deleteSameTemplates(templates: string[]){
return templates.reduce((uniqueTemplates, template) => {
if(!this.includesThisTemplate(uniqueTemplates, template)){
uniqueTemplates.push(template);
}
return uniqueTemplates;
}, [] as string[]);
}
isDirectory(uri: vscode.Uri, outputChannel?: vscode.OutputChannel): boolean {
try {
return fs.lstatSync(uri.fsPath).isDirectory();
} catch {
outputChannel && outputChannel.appendLine(`We have problem with check file ${uri.fsPath}`);
return false;
}
}
isEmptyDirectory(uri: vscode.Uri, outputChannel: vscode.OutputChannel): boolean {
try {
return this.isDirectory(uri) && !fs.readdirSync(uri.fsPath).length;
} catch {
outputChannel.appendLine(`We have problem with check file ${uri.fsPath}`);
return false;
}
}
isFile(uri: vscode.Uri): boolean {
return fs.lstatSync(uri.fsPath).isFile();
}
isEmptyFile(uri: vscode.Uri, outputChannel: vscode.OutputChannel): boolean {
try {
return this.isFile(uri) && fs.readFileSync(uri.fsPath).toString().length === 0;
} catch {
outputChannel.appendLine(`We have problem with check file ${uri.fsPath}`);
return false;
}
}
}