forked from play-co/native-android
-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkSymlinks
99 lines (83 loc) · 2.42 KB
/
checkSymlinks
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
#!/usr/bin/env node
// first entry is the source (relative to the destination)
// second entry is the destination
var path = require('path');
var fs = require('fs');
var isWindows = require('os').platform() == 'win32';
var base = path.join(path.dirname(__filename), './');
exports.check = function (cb) {
var cwd = process.cwd();
process.chdir(__dirname);
var links = require('./links.json');
// Linking to newly created project, the name should be changed
links[0][1] = links[0][1].replace('AndroidSeed', process.argv[2]);
links[0][1] = links[0][1].replace('scheme', process.argv[3]);
var i = 0;
var nextLink = function () {
var link = links[i];
if (!link) {
process.chdir(cwd);
cb && cb();
} else {
++i;
var dest = path.join(base, link[1]);
//if we are on windows then the src path needs to be provided as a absolute path
//in this case the link sources are provided relative to the directory that the destination of the link resides
if (isWindows) {
var src = path.join(dest,'\\..\\' + link[0]);
} else {
var src = link[0];
}
createLink(src, dest, nextLink);
}
}
nextLink();
};
function createLink (src, dest, next) {
console.log('creating link to', dest);
var cwd = process.cwd();
process.chdir(__dirname);
try {
makeDirsSync(path.dirname(dest));
} catch (e) {
return next(e);
}
try {
fs.unlinkSync(dest);
} catch (e) {
//do nothing with this error
}
fs.symlink(src, dest, 'junction', next);
}
// based on https://gist.github.com/1053383
function makeDirsSync (dir) {
// normalize and resolve path to an absolute one:
// (path.resolve automatically uses the current directory if needed)
dir = path.resolve(path.normalize(dir));
// try to create this directory:
try {
// XXX hardcoding recommended file mode of 511 (0777 in octal)
// (note that octal numbers are disallowed in ES5 strict mode)
fs.mkdirSync(dir, 511);
} catch (e) {
// and if we fail, base action based on why we failed:
switch (e.code) {
// base case: if the path already exists, we're good to go.
// TODO account for this path being a file, not a dir?
case 'EEXIST':
return;
// recursive case: some directory in the path doesn't exist, so
// make this path's parent directory.
case 'ENOENT':
fs.mkdirSync(path.dirname(dir));
fs.mkdirSync(dir);
break;
default:
throw e;
}
}
}
// Command line invocation.
if (require.main === module) {
exports.check();
}