-
Notifications
You must be signed in to change notification settings - Fork 3
/
postinstall.js
50 lines (38 loc) · 1.13 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
const fs = require('fs');
const path = require('path');
const pkg = require('./package');
// Helpers
const reset = '\x1b[0m';
const yellow = '\x1b[33m';
const blue = '\x1b[34m';
const log = (message) => console.log(`${blue}${message}${reset}`);
const modulesDir = 'node_modules';
function fileExists(path) {
try {
fs.accessSync(path);
return true;
} catch (e) {
return false;
}
}
// Main
log(`${pkg.name}@${pkg.version}`);
try {
const lIndex = __dirname.lastIndexOf(path.sep + modulesDir + path.sep);
if (lIndex === -1) {
throw new Error('- Could not find node_modules directory in __dirname');
}
const base = path.resolve(__dirname.slice(0, lIndex));
const baseLink = path.resolve(base, modulesDir, '$');
if (fileExists(baseLink)) {
if (base === fs.realpathSync(baseLink)) {
log('- $ symlink already points to base\n');
process.exit();
}
throw new Error(`- File already exists: ${baseLink}`);
}
fs.symlinkSync(base, baseLink, 'junction');
log(`- Created $ symlink to ${base}\n`);
} catch (error) {
console.warn(`${yellow}${error.message}\n- Not creating $ symlink${reset}\n`);
}