-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-manager.js
91 lines (71 loc) · 1.94 KB
/
file-manager.js
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
'use strict'
const fs = require('fs');
const path = require('path');
const runtimes = {'python': '.py', 'node': '.js'}
class FileManager {
static validatePrefix(prefixPath) {
if(prefixPath == null || prefixPath.length == 0) {
throw new Error('Property error: Prefix is empty');
}
if(prefixPath[prefixPath.length-1] != '/') {
return prefixPath + '/';
}
return prefixPath
}
static getWorkDir() {
return this.validatePrefix(process.cwd());
}
static normalizePath(normalizablePath) {
return path.normalize(normalizablePath);
}
static createFolders(prefixPath) {
let folders = prefixPath.split('/');
let path = ''
for(let folder of folders) {
path += folder + '/'
if(!fs.existsSync(path)) {
fs.mkdirSync(path)
}
}
}
static deleteFolders(path, removablePath) {
let folders = removablePath.split('/');
for(let i = folders.length; i > 0 ; i--) {
let folder = folders.slice(0, i).join('/')
fs.rmdirSync(path + folder);
}
}
static moveFilesToPath(pathFrom, pathTo, runtime) {
let fileTypes = FileManager.getFileTypes(runtime);
let files = []
fs.readdirSync(pathFrom).forEach(file => {
for(let type of fileTypes) {
if(file.indexOf(type) > -1) {
files.push(file);
}
}
});
for(let file of files) {
fs.renameSync(pathFrom + '/' + file, pathTo + '/' + file);
}
}
static moveFilesBack(pathFrom, pathTo) {
let files = []
fs.readdirSync(pathFrom).forEach(file => {
files.push(file);
});
for(let file of files) {
fs.renameSync(pathFrom + '/' + file, pathTo + '/' + file);
}
}
static getFileTypes(runtime) {
let fileTypes = []
for(let runtimeType of Object.keys(runtimes)) {
if(runtime.indexOf(runtimeType) > -1) {
fileTypes.push(runtimes[runtimeType]);
}
}
return fileTypes;
}
}
module.exports = FileManager;