]> git.wincent.com - wincent.git/blob - aspects/dotfiles/index.ts
5acda5698286e24a961a56bc6f47e0997b0eaa33
[wincent.git] / aspects / dotfiles / index.ts
1 import {
2     command,
3     file,
4     resource,
5     template,
6     task,
7     variable,
8     variables,
9 } from '../../src/Fig.js';
10 import stat from '../../src/fs/stat.js';
11 import path from '../../src/path.js';
12
13 variables(({identity}) => ({
14     gitUserEmail: identity === 'wincent' ? 'greg@hurrell.net' : '',
15     gitUserName: identity === 'wincent' ? 'Greg Hurrell' : '',
16     gitHubUsername: identity === 'wincent' ? 'wincent' : '',
17 }));
18
19 task('make directories', async () => {
20     await file({path: '~/.backups', state: 'directory'});
21     await file({path: '~/.config', state: 'directory'});
22     await file({path: '~/.config/karabiner', state: 'directory'});
23     await file({path: '~/.mail', state: 'directory'});
24     await file({path: '~/.zshenv.d', state: 'directory'});
25
26     if (variable('identity') === 'wincent') {
27         await file({path: '~/code', state: 'directory'});
28     }
29 });
30
31 task('copy to ~/backups', async () => {
32     const files = [...variable.paths('files'), ...variable.paths('templates')];
33
34     for (const file of files) {
35         const base = file.basename;
36         const source = path.home.join(base);
37         const target = path.home.join('.backups', base);
38
39         const stats = await stat(source);
40
41         if (stats instanceof Error) {
42             throw stats;
43         } else if (!stats) {
44             continue;
45         } else if (stats.type === 'directory' || stats.type === 'file') {
46             await command('mv', ['-f', source, target], {
47                 creates: target,
48             });
49         }
50     }
51 });
52
53 task('create symlinks', async () => {
54     const files = variable.paths('files');
55
56     for (const src of files) {
57         await file({
58             force: true,
59             path: path.home.join(src.basename),
60             src: path.aspect.join('files', src.basename),
61             state: 'link',
62         });
63     }
64 });
65
66 task('fill templates', async () => {
67     const templates = variable.paths('templates');
68
69     for (const src of templates) {
70         await template({
71             mode: src.endsWith('.sh.erb') ? '0755' : '0644',
72             path: path.home.join(src.basename.strip('.erb')),
73             src: path.aspect.join('templates', src.basename),
74         });
75     }
76 });
77
78 task('create ~/code/.editorconfig', async () => {
79     if (variable('identity') === 'wincent') {
80         await template({
81             path: path.home.join('code/.editorconfig'),
82             src: resource.template('code/.editorconfig'),
83         });
84     }
85 });