-
Notifications
You must be signed in to change notification settings - Fork 16
/
postinstall.js
73 lines (60 loc) · 1.82 KB
/
postinstall.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
const fs = require('fs');
const path = require('path');
move(getMovePaths('webpack.config.js'), log);
move(getMovePaths('.babelrc'), log);
move(getMovePaths('tns'), log);
console.log('listing files');
fs.readdirSync(path.join(__dirname, '..', '..', 'tns')).forEach(file => {
console.log(file);
})
symlinkFromTns('package.json', 'file');
symlinkFromTns('package-lock.json', 'file');
symlinkFromTns('node_modules', 'dir');
symlinkFromTns('app/App_Resources', 'dir');
symlinkFromTns('app/images', 'dir');
fs.unlinkSync('postinstall.js');
function getMovePaths(movingFile) {
return {
oldPath: path.join(__dirname, movingFile),
newPath: path.join(__dirname, '..', '..', movingFile)
}
}
function log(err) {
if (err) {
console.log(err);
}
}
function symlinkFromTns(file, type) {
console.log('linking ', path.join(__dirname, '..', '..', file), path.join(__dirname, '..', '..', 'tns', file));
if (fs.existsSync(path.join(__dirname, '..', '..', 'tns', file))) {
fs.unlinkSync(path.join(__dirname, '..', '..', 'tns', file));
}
fs.symlinkSync(
path.join(__dirname, '..', '..', file),
path.join(__dirname, '..', '..', 'tns', file),
type);
}
// move function copied from: https://stackoverflow.com/questions/8579055/how-i-move-files-on-node-js
function move({ oldPath, newPath }, callback) {
fs.rename(oldPath, newPath, function (err) {
if (err) {
if (err.code === 'EXDEV') {
copy();
} else {
callback(err);
}
return;
}
callback();
});
function copy() {
var readStream = fs.createReadStream(oldPath);
var writeStream = fs.createWriteStream(newPath);
readStream.on('error', callback);
writeStream.on('error', callback);
readStream.on('close', function () {
fs.unlink(oldPath, callback);
});
readStream.pipe(writeStream);
}
}