-
Notifications
You must be signed in to change notification settings - Fork 173
/
symbolic-link.js
92 lines (85 loc) · 2.84 KB
/
symbolic-link.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
/**
* This file is used to link cross-dependencies by creating symbolic links as needed.
* The symbolic links list can found in ./symbolic-links.json
*/
const fs = require('fs');
const path = require('path');
// List of symbolic links for the repo
const symbolicLinkList = require('./symbolic-links.json');
/**
* Iterate over the list of symbolic links and create links throughout the repo as needed
*/
async function symbolicLinkAll() {
for (let i = 0; i < symbolicLinkList.length; i++) {
await createSymbolicLinkFromTo(
symbolicLinkList[i].source,
symbolicLinkList[i].destination,
symbolicLinkList[i].scope,
i,
symbolicLinkList.length
);
}
}
// Create symbolic link from source to destination with a given scope
async function createSymbolicLinkFromTo(source, destination, scope, index, numLinks) {
try {
const sourcePath = path.resolve(__dirname, source);
const linkFolderPath = path.resolve(__dirname, destination, 'node_modules', scope);
await createSymbolicLink(sourcePath, linkFolderPath);
console.log(
`\x1b[32m[${index +
1}/${numLinks}]: Symbolic link created successfully.\x1b[0m : \x1b[33m${sourcePath} \x1b[0m => \x1b[35m${linkFolderPath}\x1b[0m`
);
} catch (error) {
console.error('\x1b[31mError creating symbolic link for client package. Cancelling process... \x1b[0m', error);
process.exit(1);
}
}
// Function to create a symbolic link. Deletes destination folder if already exists
function createSymbolicLink(source, target) {
return new Promise((resolve, reject) => {
// Check if the destination path already exists
fs.access(target, fs.constants.F_OK, err => {
if (!err) {
// If the destination path exists, delete it
fs.rm(target, { recursive: true }, err => {
if (err) {
reject(err);
} else {
// Create the symbolic link
fs.symlink(source, target, 'junction', err => {
if (err) {
reject(err);
} else {
resolve();
}
});
}
});
} else {
// Create the symbolic link
fs.symlink(source, target, 'junction', err => {
if (err) {
reject(err);
} else {
resolve();
}
});
}
});
});
}
console.log(
'\x1b[32m\n\n------------- Starting to create symbolic links: \x1b[33m [SOURCE] \x1b[0m=>\x1b[35m [DESTINATION] \x1b[0m -------------->\x1b[0m'
);
symbolicLinkAll().then(() => {
console.log('\x1b[32m-------------> Finished creating symbolic links <----------------\x1b[0m\n');
}, errorHandler);
/**
* Function to handle the error case for promises
* @param {*} error error
*/
function errorHandler(error) {
console.error('Stopping execution of the process due to error:', error);
process.exit(1);
}