forked from angular/angular.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsUtils.js
137 lines (124 loc) · 3.55 KB
/
fsUtils.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
136
137
var path = require('canonical-path');
var fs = require('fs');
var minimatch = require("minimatch");
// Utility wrappers over fs functions
module.exports = {
addSymlink: addSymlink,
removeSymlink: removeSymlink,
removeDirSync: removeDirSync,
lstatSyncExt: lstatSyncExt,
};
// create a new link (linkPath) that points
// to realPath - will also delete the linkPath
// if it is a preexisting folder and not itself a symbolic link before it
// starts.
function addSymlink(realPath, linkPath) {
// check if the linkPath is a folder
// and if so delete it
var stat = lstatSyncExt(linkPath);
if (stat && stat.isSymbolicLink()) {
// todo: check if it points to realPath
return;
}
if (stat && stat.isDirectory()) {
removeDirSync(linkPath);
}
symLinkSyncExt(realPath, linkPath, 'dir');
}
function removeSymlink(linkPath) {
var stat = lstatSyncExt(linkPath);
if (!stat || !stat.isSymbolicLink()) return;
fs.unlinkSync(linkPath);
}
// remove a dir and all of its files - sync
function removeDirSync(dirPath) {
var files;
try {
files = fs.readdirSync(dirPath);
}
catch(e) { return; }
if (files.length > 0) {
for (var i = 0; i < files.length; i++) {
var filePath = path.join(dirPath, files[i]);
var stat = fs.lstatSync(filePath);
if (stat.isFile() || stat.isSymbolicLink()) {
fs.unlinkSync(filePath);
} else {
removeDirSync(filePath);
}
}
}
fs.rmdirSync(dirPath);
}
// same as lstatSync except returns a null for nonExistent file instead of throwing
function lstatSyncExt(filePath) {
try {
var stat = fs.lstatSync(filePath);
} catch (e) {
if(e.code == 'ENOENT') return null;
throw e;
}
return stat;
}
function symLinkSyncExt(realPath, linkPath, type) {
// fs.symlink requires the real path to be fully resolved.
realPath = path.resolve(realPath);
try {
fs.symlinkSync(realPath, linkPath, type);
} catch (e) {
var msg = "Unable to create symlink: " + linkPath;
if (e.code == "EPERM") {
msg += "\nPermissions issue, on windows this function must be run as an administrator."
}
console.log(msg + "\n" + e);
}
}
// Talk to Jay - not currently needed.
//
//function globWithIgnore(basePath, globs, ignoreGlobs, options) {
// options = options || {};
// options.filters = globs.map(function (glob) {
// return minimatch.filter(glob);
// });
// options.ignoreFilters = ignoreGlobs && ignoreGlobs.map(function(glob) {
// return minimatch.filter(glob);
// });
// options.ignoreSymlink = options.ignoreSymlink == null ? true : false;
// return globSyncCore(basePath, options);
//}
//
//function globSyncCore(basePath, options) {
// var results = [];
// var files;
// try {
// files = fs.readdirSync(basePath);
// }
// catch(e) { return results; }
//
// files.forEach(function(file) {
// var filePath = path.join(basePath, file);
// var shouldIgnore = options.ignoreFilters && options.ignoreFilters.some(function(filter) {
// return filter(filePath);
// });
// if (shouldIgnore) return;
//
// var stat = fs.lstatSync(filePath);
//
// if (stat.isDirectory()) {
// if (stat.isSymbolicLink() && options.ignoreSymlink) {
// // do nothing
// } else {
// var partialResults = globSyncCore(filePath, options);
// Array.prototype.push.apply(results, partialResults);
// }
// } else {
// var ok = options.filters.every(function(filter) {
// return filter(filePath);
// });
// if (ok) {
// results.push(filePath);
// }
// }
// });
// return results;
//}