-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yarn-link-handler.js
77 lines (66 loc) · 2.38 KB
/
yarn-link-handler.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
const { exec } = require('child_process');
const path = require('path');
function runCommand(command, options = {}) {
return new Promise((resolve, reject) => {
exec(command, options, (error, stdout, stderr) => {
if (error) {
reject({ error, stdout, stderr });
return;
}
resolve(stdout.trim());
});
});
}
const unlinkPackage = async (packageName) => {
try {
console.log(`Attempting to unlink '${packageName}'...`);
await runCommand(`yarn unlink "${packageName}"`);
console.log(`Unlinked '${packageName}' successfully.`);
return true;
} catch (unlinkError) {
console.log(`Package '${packageName}' was not previously linked or could not be unlinked.`);
return false;
}
}
const linkPackage = async (packageName) => {
try {
console.log(`Linking package '${packageName}'...`);
await runCommand(`yarn link "${packageName}"`);
console.log(`Linked '${packageName}' successfully.`);
return true;
} catch (linkedError) {
console.error(`Failed to link '${packageName}': ${linkedError}`);
return false;
}
}
const linkChildrenPackage = async (packageName, dir) => {
try {
const dirPath = path.resolve(__dirname, dir);
await runCommand(`yarn link "${packageName}"`, { cwd: dirPath });
console.log(`Linked '${packageName}' in directory: ${dir}`);
} catch (linkedChildrenError) {
console.error(`Failed to link '${packageName}' in directory '${dir}': ${linkedChildrenError}`);
}
}
async function checkAndRelinkPackage(packageName, directories) {
try {
await unlinkPackage(packageName);
const linkedSuccessfully = await linkPackage(packageName);
if (linkedSuccessfully) {
for (const dir of directories) {
// check the index
// if its the last one break
await linkChildrenPackage(packageName, dir);
}
}
return
} catch (error) {
console.error(`An error occurred: ${error}`);
}
}
function linkPackages() {
const packageName = '@devlander/collect-exports-for-bundle';
const directories = ['example']; // Adjust the directories as needed
checkAndRelinkPackage(packageName, directories);
}
linkPackages();