-
Notifications
You must be signed in to change notification settings - Fork 0
/
symlink.js
executable file
·175 lines (152 loc) · 5 KB
/
symlink.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// -----------------------------------------------------------
// version 1.0.1
// @author Nathan Walker
// @author Sean Perkins
// -----------------------------------------------------------
"use strict";
var debugging = false;
var fs = require('fs');
var cp = require('child_process');
var path = require('path');
var webAppPath = './src/app';
var webFontsPath = './src/fonts';
var webAssetsPath = './src/assets';
var nativescriptAppPath = './nativescript/src/app/';
var nativescriptFontsPath = './nativescript/src/fonts/';
var nativescriptAssetsPath = './nativescript/src/assets';
// Root SymLink Code for Windows
if (process.argv.length > 2) {
if (process.argv[2] === 'symlink') {
createRootSymLink();
console.log("Created Symlink");
}
return 0;
}
// console.log("Installing NativeScript support files...");
// cp.execSync('npm install', {cwd: 'nativescript'});
console.log("Configuring...");
// remove previous symlinks if they exist
try {
if (fs.existsSync(resolve(nativescriptAppPath))) {
fs.unlinkSync(resolve(nativescriptAppPath));
}
if (fs.existsSync(resolve(nativescriptFontsPath))) {
fs.unlinkSync(resolve(nativescriptFontsPath));
}
if (fs.existsSync(resolve(nativescriptAssetsPath))) {
fs.unlinkSync(resolve(nativescriptAssetsPath));
}
} catch (err) {}
// We need to create a symlink
try {
createSymLink();
} catch (err) {
if (debugging) {
console.log("Symlink error: ", err);
}
// Failed, and doesnt exist which means they weren't running root; so lets try to get root
err.code === 'EEXIST' ? console.log("A symlink already exists.") : AttemptRootSymlink();
}
// Might silent fail on OSX, so we have to see if it exists
// if (!fs.existsSync(resolve(nativescriptComponentsPath))) {
// AttemptRootSymlink();
// }
if (!fs.existsSync(resolve(nativescriptAppPath))) {
console.log("We were unable to create a symlink - from -");
console.log(" ", resolve(webAppPath), " - to - ");
console.log(" ", resolve(nativescriptAppPath));
console.log("If you don't create this symlink, you will have to manually copy the code each time you change it.");
}
return 0;
/**
* This will attempt to run the install script as root to make a symlink
*
*/
function AttemptRootSymlink() {
if (process.platform === 'win32') {
var curPath = resolve("./");
if (debugging) {
console.log("RootSymlink Base path is", curPath);
}
cp.execSync("powershell -Command \"Start-Process 'node' -ArgumentList '" + curPath + "/install.js symlink' -verb runas\"");
} else {
console.log("To automatically create a SymLink between your web app and NativeScript, we need root for a second.");
cp.execSync("sudo " + process.argv[0] + " " + process.argv[1] + " symlink");
}
}
/**
* Create the symlink when running as root
*/
function createRootSymLink() {
var li1 = process.argv[1].lastIndexOf('\\'),
li2 = process.argv[1].lastIndexOf('/');
if (li2 > li1) {
li1 = li2;
}
var AppPath = process.argv[1].substring(0, li1);
var p1 = resolve(AppPath + "/" + nativescriptAppPath);
var p2 = resolve(AppPath + "/" + webAppPath);
if (debugging) {
console.log("Path: ", p1, p2);
}
fs.symlinkSync(p2, p1, 'junction');
}
/**
* Create Symlink
*/
function createSymLink() {
if (debugging) {
console.log("Attempting to Symlink", webAppPath, nativescriptAppPath);
}
fs.symlinkSync(resolve(webAppPath), resolve(nativescriptAppPath), 'junction');
fs.symlinkSync(resolve(webFontsPath), resolve(nativescriptFontsPath), 'junction');
fs.symlinkSync(resolve(webAssetsPath), resolve(nativescriptAssetsPath), 'junction');
}
function splitPath(v) {
var x;
if (v.indexOf('/') !== -1) {
x = v.split('/');
} else {
x = v.split("\\");
}
return x;
}
function resolve(v) {
var cwdPath = splitPath(process.argv[1]);
// Kill the Script name
cwdPath.length = cwdPath.length - 1;
var resolvePath = splitPath(v);
// Eliminate a trailing slash/backslash
if (cwdPath[cwdPath.length - 1] === "") {
cwdPath.pop();
}
if (v[0] === '/' || v[0] === "\\") {
cwdPath = [];
}
for (var i = 0; i < resolvePath.length; i++) {
if (resolvePath[i] === '.' || resolvePath[i] === "") {
continue;
}
if (resolvePath[i] === '..') {
cwdPath.pop();
} else {
cwdPath.push(resolvePath[i]);
}
}
if (process.platform === 'win32') {
var winResult = cwdPath.join("\\");
if (winResult[winResult.length - 1] === "\\") {
winResult = winResult.substring(0, winResult.length - 1);
}
return winResult;
} else {
var result = cwdPath.join('/');
if (result[0] !== '/') {
result = '/' + result;
}
if (result[result.length - 1] === '/') {
result = result.substring(0, result.length - 1);
}
return result;
}
}