-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (113 loc) · 4.57 KB
/
index.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
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
const fs = require('fs');
const path = require('path');
class ConvertFileExt {
constructor(dirPath, extName) {
this.extName = extName;
this.level = 0;
this.dirPath = dirPath;
this.destPath = "";
}
async readSyncSubDir(dirPath, folderList = []) {
const arrayList = fs.readdirSync(dirPath);
let directories = [], fileList = [], createdDir = "";
arrayList.forEach(file => {
if (this.isDirectory(dirPath, file))
directories = [...directories, file];
else
fileList = [...fileList, file];
})
if (fileList.length) {
if (this.level === 0) {
this.destPath = await this.makeDestDir(dirPath);
} else {
const basePathlength = this.dirPath.split(path.sep).length;
let splitPath = dirPath.split(path.sep);
const attachString = splitPath.slice(basePathlength).join('/');
createdDir = await this.makeDestDir(this.destPath + '/' + attachString);
}
await this.createFiles(this.level === 0 ? this.destPath : createdDir, dirPath, fileList).then((res) => {
console.log('Done!!!');
});
// await this.createStreamFiles(this.level === 0 ? this.destPath : createdDir, dirPath, fileList).then((res) => {
// console.log('Done!!!');
// });
}
if (directories.length) {
this.level++;
directories.forEach(folder => {
folderList = [...folderList, path.join(dirPath, folder)];
this.readSyncSubDir(path.join(dirPath, folder), folderList);
});
}
}
isDirectory(dirName, file) {
return fs.statSync(path.join(dirName, file)).isDirectory();
}
changeExtension(file) {
let ext = this.extName ? '.' + this.extName : '.js';
return path.extname(file) === ".js" ? file.replace(/\.js$/g, ext) : file;
}
async createFiles(dirName, readPath, fileList) {
for (const file of fileList) {
let reading = await this.readFiles(readPath, file);
let fileExt = this.changeExtension(file);
fs.writeFile(`${dirName}/${fileExt}`, reading, (err) => {
if (err) {
console.log('error writing data=', err);
}
console.log('completed')
})
}
}
// async createStreamFiles(dirName, readPath, fileList) {
// for (const file of fileList) {
// let fileExt = this.changeExtension(file);
// const newPath = path.join(readPath, file);
// this.writerStream(`${dirName}/${fileExt}`, newPath);
// }
// }
readFiles(readPath, regexFile) {
const newPath = path.join(readPath, regexFile);
return new Promise((resolve, reject) => {
fs.readFile(newPath, (err, data) => {
if (err) {
reject(err);
}
resolve(String(data));
})
})
}
readerStream(filename) {
const reader = fs.createReadStream(filename);
let chunks = [];
reader.on('data', (chunk) => { chunks.push(chunk) });
reader.on('close', () => chunks.push(null));
reader.on('error', (err) => { return cb(err) });
return reader;
}
writerStream(writeToFile, readFile) {
const writer = fs.createWriteStream(writeToFile);
let readable = this.readerStream(readFile);
readable.pipe(writer);
writer.on('data', () => { console.log('data written') });
writer.on('error', (err) => { throw err });
writer.on('close', () => console.log('writing done!!'));
}
getRandomPath(dirPath) {
let randomDir = dirPath.slice() + (Math.random() * (10 - 1) + 1).toFixed(2);
return randomDir;
}
makeDestDir(dirPath) {
return new Promise((resolve, reject) => {
const randomDir = this.level ? dirPath : this.getRandomPath(dirPath);
fs.mkdir(randomDir, (err) => {
if (err) {
reject('create directory failed because ' + err);
}
resolve(randomDir);
})
})
}
}
const convertFile = new ConvertFileExt(process.argv[2], process.argv[3]);
convertFile.readSyncSubDir(process.argv[2]).then(res => console.log('All Files are converted'));