-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakesymlinks.js
165 lines (137 loc) · 3.43 KB
/
makesymlinks.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const {
symlinkSync,
mkdirSync,
renameSync,
existsSync,
readdirSync,
lstatSync,
unlinkSync,
rmdirSync
} = require('fs');
const { sep } = require('path');
const { homedir, platform } = require('os');
const home = homedir();
const createPath = dirArray => dirArray.join(sep);
const makeDirectory = path => {
try {
mkdirSync(path);
console.log(`${path} created`);
} catch (err) {
if (err.code !== 'EEXIST') {
throw err;
}
console.log(`${path} already exists`);
}
};
// The dir of the repository
const dir = createPath([home, 'dev', 'dotfiles']);
const oldDir = [home, 'dotfiles_old'];
// Folder where we will create/link MPV
const mpvFolder =
platform() === 'win32'
? [home, 'AppData', 'Roaming', 'mpv']
: [home, '.config', 'mpv'];
const alacrittyFolder =
platform() === 'win32'
? [home, 'AppData', 'Roaming', 'alacritty']
: [home, '.config', 'alacritty'];
const getShitDoneFolder = [home, '.config'];
const emacsFolder = [home, '.emacs.d'];
// Folders we will create
const folders = {
vim: [
[home, '.vim'],
[home, '.vim', 'undo_files'],
[home, '.vim', 'swap_files'],
[home, '.vim', 'backup_files']
],
mpv: [mpvFolder],
backup: [oldDir],
getShitDone: [getShitDoneFolder],
emacs: [emacsFolder],
alacritty: [alacrittyFolder]
};
const deleteFolderRecursive = path => {
if (existsSync(path)) {
readdirSync(path).forEach(file => {
const curPath = `${path}${sep}${file}`;
if (lstatSync(curPath).isDirectory()) {
// recurse
deleteFolderRecursive(curPath);
} else {
// delete file
unlinkSync(curPath);
}
});
rmdirSync(path);
}
};
deleteFolderRecursive(createPath(oldDir));
const createFolders = folders => {
console.log('Creating folders...\n');
Object.values(folders).forEach(program => {
program.map(createPath).forEach(makeDirectory);
});
console.log('\nFinished creating folders!\n');
};
createFolders(folders);
const moveItem = (path, target) => {
try {
renameSync(path, target);
} catch (err) {
if (err.code === 'EPERM') {
// If I ever have more than one folder I symlink I promise I'll write
// code to delete folders recursively. But in the meantime this will do.
throw new Error(
`Can't move ${path} to ${target}. Try removing both folders and try running this script again`
);
}
if (err.code !== 'ENOENT') {
throw err;
}
console.log(`${path} doesn't exist yet`);
}
};
const createSymlink = ({
name,
path = `${home}${sep}.${name}`,
type = 'file',
dotfile = true
}) => {
moveItem(path, `${createPath(oldDir)}${sep}${dotfile ? '.' : ''}${name}`);
symlinkSync(`${dir}${sep}${name}`, path, type);
console.log(`Created ${path} symlink`);
};
// Files we will symlink
const files = [
{ name: 'vimrc' },
{ name: 'zshrc' },
{ name: 'hyper.js' },
{ name: 'tern-config' },
{ name: 'eslintrc.js' },
{ name: 'tmux.conf' },
{ name: 'bashrc' },
{ name: 'minttyrc' },
{
name: 'mpv',
path: createPath(mpvFolder),
type: 'dir'
},
{
name: '.config',
path: createPath(getShitDoneFolder),
type: 'dir'
},
{
name: '.emacs.d',
path: createPath(emacsFolder),
type: 'dir'
},
{
name: 'alacritty.yml',
path: createPath([...alacrittyFolder, 'alacritty.yml'])
}
];
console.log('Creating symlinks...\n');
files.forEach(createSymlink);
console.log('\nFinished creating symlinks!');