-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsymlink-helper.ts
92 lines (85 loc) · 3.13 KB
/
symlink-helper.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
import * as fs from 'fs';
import * as path from 'path';
import * as child_process from 'child_process';
import { Config } from './config';
/**
* You can extend this class in your project
* Set Config.helperClass to use your own
*/
export class SymlinkHelper {
public nestingLevel: number = 0;
constructor(rootDir: string) {
let startsWithSlash = false;
if (rootDir.startsWith('./')) {
startsWithSlash = true;
} else if (rootDir.startsWith('../')) {
startsWithSlash = true;
}
if (!startsWithSlash) {
throw new Error('Directory name has to start with any of: ./, ../');
}
this.nestingLevel = rootDir.split('/').length - 1;
}
/**
* Fetches all symlinks in given directory recursively
* @param dir start by ./
* @returns {Promise<T>}
*/
public findSymlinks(dir: string): Promise<Object> {
return new Promise((resolve, reject) => {
let symlinks = {};
fs.readdir(dir, (err, list) => {
if (err) return reject(err);
let pending = list.length;
if (!pending) return resolve(symlinks);
list.forEach((file) => {
file = dir + '/' + file;
fs.lstat(file, (error, stat) => {
if (stat.isSymbolicLink()) {
symlinks[file] = fs.readlinkSync(file);
}
if (stat && stat.isDirectory()) {
this.findSymlinks(file).then(childLinks => {
symlinks = Object.assign({}, symlinks, childLinks);
if (!--pending) resolve(symlinks);
});
} else {
if (!--pending) resolve(symlinks);
}
});
});
});
});
};
public saveSymlinks(content: any): Promise<true> {
let savedSymlinks = this.getSavedSymlinks();
if (savedSymlinks) {
content = Object.assign({}, savedSymlinks, content);
}
let stringified = JSON.stringify(content, null, '\t');
return new Promise(resolve => {
fs.writeFile(Config.rootDir + Config.symlinksFile, stringified, (err) => {
if (err) return console.error(err);
resolve(true);
});
});
}
public copyFile(source: string, target: string): void {
child_process.execSync('rm ' + target);
child_process.execSync('mkdir -p ' + path.dirname(target));
child_process.execSync('cp -R ' + path.resolve(source) + ' ' + target);
}
public getRelativePath(path: string): string {
for (let i = 0; i < this.nestingLevel; i++) {
path = path.replace('../', '');
}
return path;
}
public getSavedSymlinks(): Object {
let savedSymlinks: any = null;
try {
savedSymlinks = require('../../' + Config.rootDir + Config.symlinksFile);
} catch (e) { }
return savedSymlinks;
}
}