-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.js
40 lines (34 loc) · 869 Bytes
/
utils.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
const fs = require('fs').promises;
module.exports = {
async readCode(file) {
const data = await fs.readFile(file, 'utf-8');
return data;
},
writeCode(file, data) {
const target = file.split('.')[0];
fs.writeFile(`${target}.js`, data, 'utf-8');
},
async createDirsRecursive(path) {
await fs.mkdir(path, { recursive: true });
},
getLastPathPart(path) {
return path.split('/').pop();
},
getPathDirs(path, shouldShift = false) {
const splitPath = path.split('/');
splitPath.pop();
if (shouldShift) {
splitPath.shift();
}
return splitPath.join('/');
},
calculateRootPath(path) {
const matches = path.match(/\//g) || [];
const len = matches.length;
let pathString = '';
for (let index = 0; index < len; index += 1) {
pathString += '../';
}
return pathString;
},
};