|
| 1 | +import { |
| 2 | + CreatePlan, |
| 3 | + DestroyPlan, |
| 4 | + ExampleConfig, |
| 5 | + ModifyPlan, |
| 6 | + ParameterChange, |
| 7 | + Resource, |
| 8 | + ResourceSettings, |
| 9 | + z, |
| 10 | +} from '@codifycli/plugin-core'; |
| 11 | +import { OS } from '@codifycli/schemas'; |
| 12 | +import fs from 'node:fs/promises'; |
| 13 | +import path from 'node:path'; |
| 14 | + |
| 15 | +export const schema = z.object({ |
| 16 | + path: z.string().describe('The location of the symlink to create.'), |
| 17 | + target: z.string().describe('The file or directory the symlink should point to.'), |
| 18 | +}) |
| 19 | + .describe('Manages a single symbolic link, creating it at `path` and pointing it at `target`.'); |
| 20 | + |
| 21 | +export type SymlinkConfig = z.infer<typeof schema>; |
| 22 | + |
| 23 | +const defaultConfig: Partial<SymlinkConfig> = { |
| 24 | + path: '<Replace me here!>', |
| 25 | + target: '<Replace me here!>', |
| 26 | +} |
| 27 | + |
| 28 | +const exampleDotfile: ExampleConfig = { |
| 29 | + title: 'Symlink a dotfile from a dotfiles repo', |
| 30 | + description: 'Point a config file at the copy tracked in a version-controlled dotfiles repository.', |
| 31 | + configs: [{ |
| 32 | + type: 'symlink', |
| 33 | + path: '~/.vimrc', |
| 34 | + target: '~/dotfiles/vimrc', |
| 35 | + }] |
| 36 | +} |
| 37 | + |
| 38 | +const exampleDirectory: ExampleConfig = { |
| 39 | + title: 'Symlink an application into /Applications', |
| 40 | + description: 'Expose an application installed in a custom location under the standard /Applications directory.', |
| 41 | + configs: [{ |
| 42 | + type: 'symlink', |
| 43 | + path: '/Applications/MyApp.app', |
| 44 | + target: '~/Applications/MyApp.app', |
| 45 | + }] |
| 46 | +} |
| 47 | + |
| 48 | +export class SymlinkResource extends Resource<SymlinkConfig> { |
| 49 | + getSettings(): ResourceSettings<SymlinkConfig> { |
| 50 | + return { |
| 51 | + id: 'symlink', |
| 52 | + defaultConfig, |
| 53 | + exampleConfigs: { |
| 54 | + example1: exampleDotfile, |
| 55 | + example2: exampleDirectory, |
| 56 | + }, |
| 57 | + operatingSystems: [OS.Darwin, OS.Linux], |
| 58 | + schema, |
| 59 | + parameterSettings: { |
| 60 | + path: { type: 'directory' }, |
| 61 | + target: { type: 'directory', canModify: true }, |
| 62 | + }, |
| 63 | + importAndDestroy: { |
| 64 | + preventImport: true, |
| 65 | + }, |
| 66 | + allowMultiple: { |
| 67 | + identifyingParameters: ['path'], |
| 68 | + }, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + override async refresh(parameters: Partial<SymlinkConfig>): Promise<Partial<SymlinkConfig> | null> { |
| 73 | + const { path: linkPath } = parameters; |
| 74 | + if (!linkPath) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + let stats; |
| 79 | + try { |
| 80 | + stats = await fs.lstat(linkPath); |
| 81 | + } catch { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + if (!stats.isSymbolicLink()) { |
| 86 | + throw new Error(`A file or directory already exists at ${linkPath} and is not a symlink. Please remove it manually and re-run Codify.`); |
| 87 | + } |
| 88 | + |
| 89 | + const target = await fs.readlink(linkPath); |
| 90 | + |
| 91 | + return { |
| 92 | + path: linkPath, |
| 93 | + target, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + override async create(plan: CreatePlan<SymlinkConfig>): Promise<void> { |
| 98 | + const { path: linkPath, target } = plan.desiredConfig; |
| 99 | + |
| 100 | + const parentDir = path.dirname(linkPath); |
| 101 | + await fs.mkdir(parentDir, { recursive: true }); |
| 102 | + |
| 103 | + await fs.symlink(target, linkPath); |
| 104 | + } |
| 105 | + |
| 106 | + override async modify(pc: ParameterChange<SymlinkConfig>, plan: ModifyPlan<SymlinkConfig>): Promise<void> { |
| 107 | + if (pc.name !== 'target') { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const { path: linkPath } = plan.currentConfig; |
| 112 | + |
| 113 | + await fs.unlink(linkPath); |
| 114 | + await fs.symlink(plan.desiredConfig.target, linkPath); |
| 115 | + } |
| 116 | + |
| 117 | + override async destroy(plan: DestroyPlan<SymlinkConfig>): Promise<void> { |
| 118 | + const { path: linkPath } = plan.currentConfig; |
| 119 | + |
| 120 | + const stats = await fs.lstat(linkPath); |
| 121 | + if (!stats.isSymbolicLink()) { |
| 122 | + throw new Error(`Refusing to remove ${linkPath} because it is not a symlink. Please remove it manually and re-run Codify.`); |
| 123 | + } |
| 124 | + |
| 125 | + await fs.unlink(linkPath); |
| 126 | + } |
| 127 | +} |
0 commit comments