-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileUtils.js
More file actions
191 lines (147 loc) · 4.5 KB
/
Copy pathFileUtils.js
File metadata and controls
191 lines (147 loc) · 4.5 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
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
'use babel';
/* eslint-disable no-shadow */
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
import ignore from 'ignore';
import { isEmpty } from 'lodash';
import { Store } from './Store';
import { STORE_KEYS } from '../constants/store';
import { EXCLUDED_NAMES, CRYPTO, IGNORES } from '../constants/files';
import { PLUGIN } from '../constants/common';
class FileUtils {
projectPaths = [];
ignoreFilters = new Map();
constructor() {
this.init();
this.initIgnore();
}
init() {
this.projectPaths = atom.project.getPaths();
}
getProjectPaths() {
this.init();
return this.projectPaths;
}
getMainProjectPath() {
this.init();
return this.projectPaths[0];
}
getIgnoreContent(ignoreFile) {
const result = [];
try {
const buffer = fs.readFileSync(ignoreFile);
const lines = buffer.toString().split('\n').filter(line => !!line);
result.push(...lines);
} catch (err) {
// nothing
}
return result;
}
initIgnore() {
this.projectPaths.forEach(path => {
const rootFilter = ignore();
rootFilter.add(EXCLUDED_NAMES);
const gitContent = this.getIgnoreContent(`${path}${PLUGIN.pathSeparator}.gitignore`);
const dcContent = this.getIgnoreContent(`${path}${PLUGIN.pathSeparator}.dcignore`);
rootFilter.add(gitContent);
rootFilter.add(dcContent);
this.ignoreFilters.set(path, rootFilter);
});
}
updateIgnore(folderPath) {
const isDir = fs.lstatSync(folderPath).isDirectory();
if (!isDir) {
return;
}
const gitContent = this.getIgnoreContent(`${folderPath}${PLUGIN.pathSeparator}.gitignore`);
const dcContent = this.getIgnoreContent(`${folderPath}${PLUGIN.pathSeparator}.dcignore`);
const rules = [...gitContent, ...dcContent];
if (!isEmpty(rules)) {
const localFilter = ignore();
localFilter.add(rules);
this.ignoreFilters.set(folderPath, localFilter);
}
}
getUnconfirmedProjectFolders() {
this.init();
const confirmedFolders = Store.get(STORE_KEYS.confirmedFolders);
const projectPaths = this.getProjectPaths();
return projectPaths.filter(projectFolder => !confirmedFolders.includes(projectFolder));
}
isIgnored(filePath) {
if (filePath.includes(IGNORES.git) || filePath.includes(IGNORES.idea)) {
return true;
}
// check ignore filters
for (const entry of this.ignoreFilters.entries()) {
const [folderPath, filter] = entry;
if (!filePath.includes(folderPath)) {
// eslint-disable-next-line no-continue
continue;
}
const relativePath = filePath.replace(`${folderPath}${PLUGIN.pathSeparator}`, '');
let ignored = false;
try {
ignored = filter.ignores(relativePath);
} catch (err) {
// nothing
}
if (ignored) {
return true;
}
}
const isAllowed = this.isAllowedFile(filePath);
if (!isAllowed) {
return true;
}
return false;
}
isAllowedFile(filePath) {
const extname = path.extname(filePath);
if (!extname) {
return true; // probably it is folder
}
const { extensions, configFiles } = Store.get(STORE_KEYS.allowedFiles);
const basename = path.basename(filePath);
const isAllowedConfigFile = configFiles.includes(basename);
const isAllowedExtension = extensions.includes(extname);
if (!(isAllowedConfigFile || isAllowedExtension)) {
return false;
}
// checking for file size
const fileSize = fs.lstatSync(filePath).size;
return (fileSize <= PLUGIN.maxPayload);
}
createFileHash(filePath) {
const fileContent = this.readFileContent(filePath);
return crypto
.createHash(CRYPTO.algorithm)
.update(fileContent)
.digest(CRYPTO.hashEncode);
}
readFileContent(filePath) {
return fs.readFileSync(filePath, { encoding: CRYPTO.fileEncode });
}
getLocalFolderName(folder) {
const projectPath = this.projectPaths[0];
if (!projectPath) {
return folder;
}
return folder.replace(projectPath, '');
}
getLocalFileName(filePath) {
return path.basename(filePath) || filePath;
}
isIgnoreFile(filePath) {
const fileName = path.basename(filePath);
const isGitIgnore = (fileName === IGNORES.gitIgnore);
const isDCIgnore = (fileName === IGNORES.dcIgnore);
return (isGitIgnore || isDCIgnore);
}
getDirname(filePath) {
return path.dirname(filePath);
}
}
const FileUtilsInstance = new FileUtils();
export { FileUtilsInstance as FileUtils };