-
Notifications
You must be signed in to change notification settings - Fork 2
/
copy-dotfiles.mjs
103 lines (85 loc) · 2.86 KB
/
copy-dotfiles.mjs
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
import fs from 'node:fs/promises';
import path from 'node:path';
import os from 'node:os';
import readline from 'node:readline/promises';
import {stdin as input, stdout as output } from 'node:process';
const home = os.homedir();
const files = await fs.readdir(home, {encoding: 'utf-8'});
const rootDir = process.env.DOTFILES || process.cwd();
const getHomeFiles = async() => {
const groups = {
directories: [],
files: [],
symlinks: [],
};
for (const file of files) {
const filePath = path.join(home, file);
try {
const stat = await fs.lstat(filePath);
let group = stat.isSymbolicLink() && 'symlinks';
if (!group) {
group = stat.isDirectory() ? 'directories' : stat.isFile() ? 'files' : 'unknown';
}
groups[group].push(file);
} catch (err) {
console.log(err);
}
}
const destDir = process.argv[2] || 'bundle';
const dest = path.join(rootDir, destDir, dotfiles.json);
await fs.writeFile(dest, JSON.stringify(groups, null, 2));
console.log('Created dotfiles.json in', destDir);
};
const copyFiles = async() => {
const destDir = process.argv[2] || 'bundle';
const dest = path.join(rootDir, destDir);
const copyFile = path.join(rootDir, 'secrets.copy.mjs');
let files = null;
try {
await fs.access(copyFile);
files = await import('./secrets.copy.mjs');
} catch (err) {
console.log(err);
}
const {toCopy} = files || {}
if (!toCopy) {
const rl = readline.createInterface({input, output});
if (!files) {
console.log(`If you want to include extra files and directories to copy from your home directory, create a file at ${copyFile}, exporting a toCopy array.`);
const answer = await rl.question(`\nWould you like me to halt backing up and set up the file for you (y)?
Or would you rather continue without including extra files (n)?`);
if (answer === 'y') {
console.log('Setting up file…');
await getHomeFiles();
await fs.writeFile(copyFile, 'export const toCopy = [];');
rl.close();
console.log('Aborting the rest of this backup for now.')
process.exit(0);
} else {
console.log('Okay, continuing on our merry way…');
}
} else {
console.log(`The file at ${copyFile} is not exporting a toCopy array.`);
const answer = await rl.question('Do you want to continue anyway? (y/N)');
if (answer !== 'y') {
console.log('Aborting');
rl.close();
process.exit(0);
}
}
rl.close();
return console.log('Not copying any extra files.');
}
for (let c of toCopy) {
const orig = path.join(home, c);
const copy = path.join(dest, c);
try {
await fs.cp(orig, copy, {recursive: true});
console.log('Copied', c);
} catch (err) {
console.log('Could not copy', c);
}
}
console.log('Copied a bunch of files');
};
copyFiles();