-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-links.js
90 lines (71 loc) · 2.54 KB
/
make-links.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
/*jshint node: true */
var fs = require('fs');
var destinationPath;
var sourceFileList;
var sourceFile;
var destinationFile;
var cwd = process.cwd();
var index;
var filename;
function findNodeModulesPath() {
var dirnamePieces;
var nodeModulesPath;
var projectPathDepth;
dirnamePieces = __dirname.split(/[\\\/]/);
projectPathDepth = dirnamePieces.indexOf('frontend-standards');
nodeModulesPath = dirnamePieces.slice(0, projectPathDepth).join('/');
return nodeModulesPath;
}
function findProjectRootPath() {
var nodeModulesPath = findNodeModulesPath();
var nodeModulesPathPieces;
nodeModulesPathPieces = nodeModulesPath.split(/[\\\/]/);
nodeModulesPathPieces.pop();
return nodeModulesPathPieces.join('/');
}
function isFileSymbolicLink(filename) {
var stats;
var isSymbolicLink;
stats = fs.lstatSync(filename);
isSymbolicLink = stats.isSymbolicLink();
return isSymbolicLink;
}
function createLink(sourceFile, destinationFile, filenameWithoutPath) {
// Create symlink.
fs.symlink(sourceFile, destinationFile, 'file', function(error) {
if (error) {
// There was an error.
if (error.code === 'EEXIST') {
// File exists error.
if (!isFileSymbolicLink(destinationFile)) {
// File is not a symbolic link. Do nothing.
console.log('WARNING:', filenameWithoutPath, 'already exists and will not be overwritten.');
} else {
// File is a symbolic link. Erase and overwrite.
fs.unlink(destinationFile, function(error) {
if (!error) {
// No error on unlink. Try to create the link again.
createLink(sourceFile, destinationFile);
} else {
// Unexpected error.
throw new Error(error);
}
});
}
} else {
// Unexpected error.
throw new Error(error);
}
}
});
}
destinationPath = findProjectRootPath();
sourceFileList = fs.readdirSync(cwd + '/linter-configurations');
for (index in sourceFileList) {
if (sourceFileList.hasOwnProperty(index)) {
filename = sourceFileList[index];
sourceFile = cwd + '/linter-configurations/' + filename;
destinationFile = destinationPath + '/' + filename;
createLink(sourceFile, destinationFile, filename);
}
}