|
| 1 | +import { |
| 2 | + CreatePlan, |
| 3 | + DestroyPlan, |
| 4 | + ExampleConfig, |
| 5 | + ModifyPlan, |
| 6 | + ParameterChange, |
| 7 | + Resource, |
| 8 | + ResourceSettings, |
| 9 | + Utils, |
| 10 | +} from '@codifycli/plugin-core'; |
| 11 | +import { OS, StringIndexedObject } from '@codifycli/schemas'; |
| 12 | +import fs from 'node:fs/promises'; |
| 13 | + |
| 14 | +import { FileUtils } from '../../../utils/file-utils.js'; |
| 15 | +import Schema from './env-var-schema.json'; |
| 16 | + |
| 17 | +export interface EnvVarConfig extends StringIndexedObject { |
| 18 | + variable: string; |
| 19 | + value: string; |
| 20 | +} |
| 21 | + |
| 22 | +const ENV_DECLARATION_REGEX = /^\s*export\s+([A-Z_a-z][A-Z0-9_a-z]*)\s*=\s*(["']?)(.+?)\2\s*(?:#.*)?$/gm; |
| 23 | + |
| 24 | +const defaultConfig: Partial<EnvVarConfig> = { |
| 25 | + variable: '<Replace me here!>', |
| 26 | + value: '<Replace me here!>', |
| 27 | +} |
| 28 | + |
| 29 | +const examplePnpmHome: ExampleConfig = { |
| 30 | + title: 'Set PNPM_HOME', |
| 31 | + description: 'Declare the PNPM_HOME environment variable so pnpm global binaries are available in a new shell.', |
| 32 | + configs: [{ |
| 33 | + type: 'env-var', |
| 34 | + variable: 'PNPM_HOME', |
| 35 | + value: '$HOME/Library/pnpm', |
| 36 | + }] |
| 37 | +} |
| 38 | + |
| 39 | +const exampleAsdfDataDir: ExampleConfig = { |
| 40 | + title: 'Set ASDF_DATA_DIR', |
| 41 | + description: 'Override the default asdf data directory to a custom location.', |
| 42 | + configs: [{ |
| 43 | + type: 'env-var', |
| 44 | + variable: 'ASDF_DATA_DIR', |
| 45 | + value: '$HOME/.asdf', |
| 46 | + }] |
| 47 | +} |
| 48 | + |
| 49 | +export class EnvVarResource extends Resource<EnvVarConfig> { |
| 50 | + private readonly filePaths = Utils.getShellRcFiles(); |
| 51 | + |
| 52 | + getSettings(): ResourceSettings<EnvVarConfig> { |
| 53 | + return { |
| 54 | + id: 'env-var', |
| 55 | + defaultConfig, |
| 56 | + exampleConfigs: { |
| 57 | + example1: examplePnpmHome, |
| 58 | + example2: exampleAsdfDataDir, |
| 59 | + }, |
| 60 | + operatingSystems: [OS.Darwin, OS.Linux], |
| 61 | + schema: Schema, |
| 62 | + parameterSettings: { |
| 63 | + value: { canModify: true, isSensitive: true }, |
| 64 | + }, |
| 65 | + importAndDestroy: { |
| 66 | + preventImport: true, |
| 67 | + }, |
| 68 | + allowMultiple: { |
| 69 | + identifyingParameters: ['variable'], |
| 70 | + }, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + override async refresh(parameters: Partial<EnvVarConfig>): Promise<Partial<EnvVarConfig> | null> { |
| 75 | + for (const filePath of this.filePaths) { |
| 76 | + if (!(await FileUtils.fileExists(filePath))) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + const contents = await fs.readFile(filePath, 'utf8'); |
| 81 | + const declarations = this.findAllDeclarations(contents); |
| 82 | + const found = declarations.find((d) => d.variable === parameters.variable); |
| 83 | + |
| 84 | + if (found) { |
| 85 | + return found; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + override async create(plan: CreatePlan<EnvVarConfig>): Promise<void> { |
| 93 | + const shellRcPath = Utils.getPrimaryShellRc(); |
| 94 | + if (!(await FileUtils.fileExists(shellRcPath))) { |
| 95 | + await fs.writeFile(shellRcPath, '', { encoding: 'utf8' }); |
| 96 | + } |
| 97 | + |
| 98 | + await FileUtils.addToStartupFile(this.declarationString(plan.desiredConfig.variable, plan.desiredConfig.value)); |
| 99 | + } |
| 100 | + |
| 101 | + override async modify(pc: ParameterChange<EnvVarConfig>, plan: ModifyPlan<EnvVarConfig>): Promise<void> { |
| 102 | + if (pc.name !== 'value') { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const { variable, value } = plan.currentConfig; |
| 107 | + const found = await this.findDeclaration(variable, value); |
| 108 | + if (!found) { |
| 109 | + throw new Error(`Unable to find env var declaration: ${variable}. Please remove it manually and re-run Codify.`); |
| 110 | + } |
| 111 | + |
| 112 | + const lines = found.contents.trimEnd().split(/\n/); |
| 113 | + const lineIndex = lines.findIndex((l) => l.trim() === this.declarationString(variable, value)); |
| 114 | + if (lineIndex === -1) { |
| 115 | + throw new Error(`Unable to find line for ${variable} in ${found.path}. Please remove it manually and re-run Codify.`); |
| 116 | + } |
| 117 | + |
| 118 | + lines.splice(lineIndex, 1, this.declarationString(plan.desiredConfig.variable, plan.desiredConfig.value)); |
| 119 | + await fs.writeFile(found.path, lines.join('\n'), 'utf8'); |
| 120 | + } |
| 121 | + |
| 122 | + override async destroy(plan: DestroyPlan<EnvVarConfig>): Promise<void> { |
| 123 | + const { variable, value } = plan.currentConfig; |
| 124 | + const found = await this.findDeclaration(variable, value); |
| 125 | + if (!found) { |
| 126 | + throw new Error(`Unable to find env var declaration: ${variable}. Please remove it manually and re-run Codify.`); |
| 127 | + } |
| 128 | + |
| 129 | + await FileUtils.removeLineFromFile(found.path, this.declarationString(variable, value)); |
| 130 | + } |
| 131 | + |
| 132 | + private async findDeclaration(variable: string, value: string): Promise<{ path: string; contents: string } | null> { |
| 133 | + const declaration = this.declarationString(variable, value); |
| 134 | + |
| 135 | + for (const filePath of this.filePaths) { |
| 136 | + if (!(await FileUtils.fileExists(filePath))) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + const contents = await fs.readFile(filePath, 'utf8'); |
| 141 | + if (contents.includes(declaration)) { |
| 142 | + return { path: filePath, contents }; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + findAllDeclarations(contents: string): Array<{ variable: string; value: string }> { |
| 150 | + const results: Array<{ variable: string; value: string }> = []; |
| 151 | + const matches = contents.matchAll(ENV_DECLARATION_REGEX); |
| 152 | + |
| 153 | + for (const match of matches) { |
| 154 | + const [, variable, , value] = match; |
| 155 | + if (variable === 'PATH') { |
| 156 | + continue; |
| 157 | + } |
| 158 | + results.push({ variable, value }); |
| 159 | + } |
| 160 | + |
| 161 | + return results; |
| 162 | + } |
| 163 | + |
| 164 | + private declarationString(variable: string, value: string): string { |
| 165 | + return `export ${variable}="${value}"`; |
| 166 | + } |
| 167 | +} |
0 commit comments