-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathinstall.ts
More file actions
83 lines (70 loc) · 3.39 KB
/
install.ts
File metadata and controls
83 lines (70 loc) · 3.39 KB
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
import assert from 'assert';
import fs from 'fs-extra';
import path from 'path';
import getDotaPath from './getDotaPath';
import config from './addon.config';
(async () => {
if (process.platform !== 'win32') {
console.log('This script runs on windows only, Addon Linking is skipped.');
return;
}
const dotaPath = await getDotaPath();
if (dotaPath === undefined) {
console.log('No Dota 2 installation found. Addon linking is skipped.');
return;
}
for (const directoryName of ['game', 'content']) {
const sourcePath = path.resolve(__dirname, '..', directoryName);
assert(fs.existsSync(sourcePath), `Could not find '${sourcePath}'`);
const targetRoot = path.join(dotaPath, directoryName, 'dota_addons');
assert(fs.existsSync(targetRoot), `Could not find '${targetRoot}'`);
const targetPath = path.join(dotaPath, directoryName, 'dota_addons', config.addon_name);
if (fs.existsSync(targetPath)) {
const isCorrect = fs.lstatSync(sourcePath).isSymbolicLink() && fs.realpathSync(sourcePath) === targetPath;
if (isCorrect) {
console.log(`Skipping '${sourcePath}' since it is already linked`);
continue;
} else {
console.log(`'${targetPath}' is already linked to another directory, repairing...`);
const backupPath = `${sourcePath}.backup.${Date.now()}`;
let backupCreated = false;
try {
fs.chmodSync(targetPath, '0755');
fs.moveSync(sourcePath, backupPath);
backupCreated = true;
console.log(`Created backup at '${backupPath}'`);
await fs.remove(targetPath);
console.log('Removed old target path');
fs.moveSync(backupPath, targetPath);
console.log('Moved backup to target path');
fs.symlinkSync(targetPath, sourcePath, 'junction');
console.log(`Repaired broken link ${sourcePath} <==> ${targetPath}`);
} catch (error) {
console.error('Failed to repair link:', error);
if (backupCreated && fs.existsSync(backupPath)) {
try {
if (!fs.existsSync(sourcePath)) {
fs.moveSync(backupPath, sourcePath);
console.log(`Restored '${sourcePath}' from backup`);
} else {
await fs.remove(backupPath);
console.log(`Cleaned up backup at '${backupPath}'`);
}
} catch (restoreError) {
console.error(`CRITICAL: Failed to restore backup from '${backupPath}':`, restoreError);
console.error(`Please manually restore your data from '${backupPath}' to '${sourcePath}'`);
}
}
throw error;
}
}
} else {
fs.moveSync(sourcePath, targetPath);
fs.symlinkSync(targetPath, sourcePath, 'junction');
console.log(`Linked ${sourcePath} <==> ${targetPath}`);
}
}
})().catch((error: Error) => {
console.error(error);
process.exit(1);
});