|
| 1 | +import { CreatePlan, DestroyPlan, RefreshContext, Resource, ResourceSettings, getPty } from 'codify-plugin-lib'; |
| 2 | +import { ResourceConfig } from 'codify-schemas'; |
| 3 | +import fs from 'node:fs/promises'; |
| 4 | +import os from 'node:os'; |
| 5 | +import path from 'node:path'; |
| 6 | + |
| 7 | +import { codifySpawn } from '../../../utils/codify-spawn.js'; |
| 8 | +import { FileUtils } from '../../../utils/file-utils.js'; |
| 9 | +import schema from './pnpm-schema.json'; |
| 10 | + |
| 11 | +export interface PnpmConfig extends ResourceConfig { |
| 12 | + version?: string; |
| 13 | +} |
| 14 | + |
| 15 | +export class Pnpm extends Resource<PnpmConfig> { |
| 16 | + getSettings(): ResourceSettings<PnpmConfig> { |
| 17 | + return { |
| 18 | + id: 'pnpm', |
| 19 | + schema, |
| 20 | + parameterSettings: { |
| 21 | + version: { type: 'version' } |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + async refresh(parameters: Partial<PnpmConfig>, context: RefreshContext<PnpmConfig>): Promise<Partial<PnpmConfig> | Partial<PnpmConfig>[] | null> { |
| 27 | + const pty = getPty(); |
| 28 | + |
| 29 | + const { status } = await pty.spawnSafe('which pnpm'); |
| 30 | + if (status === 'error') { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + // Return a specific version if it's required from the user. |
| 35 | + if (parameters.version) { |
| 36 | + const { data } = await pty.spawn('pnpm --version'); |
| 37 | + return { version: data } |
| 38 | + } |
| 39 | + |
| 40 | + return parameters; |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + async create(plan: CreatePlan<PnpmConfig>): Promise<void> { |
| 45 | + const specificVersion = plan.desiredConfig.version; |
| 46 | + |
| 47 | + specificVersion |
| 48 | + ? await codifySpawn(`curl -fsSL https://get.pnpm.io/install.sh | env PNPM_VERSION=${specificVersion} sh -`) |
| 49 | + : await codifySpawn('curl -fsSL https://get.pnpm.io/install.sh | sh -') |
| 50 | + } |
| 51 | + |
| 52 | + async destroy(plan: DestroyPlan<PnpmConfig>): Promise<void> { |
| 53 | + const { data: pnpmLocation } = await codifySpawn('which pnpm'); |
| 54 | + if (pnpmLocation.trim().toLowerCase() !== path.join(os.homedir(), 'Library', 'pnpm', 'pnpm').trim().toLowerCase()) { |
| 55 | + throw new Error('pnpm was installed outside of Codify. Please uninstall manually and re-run Codify'); |
| 56 | + } |
| 57 | + |
| 58 | + const { data: pnpmHome } = await codifySpawn('echo $PNPM_HOME', { throws: false }); |
| 59 | + if (!pnpmHome) { |
| 60 | + throw new Error('$PNPM_HOME variable is not set. Unable to determine how to uninstall pnpm. Please uninstall manually and re-run Codify.') |
| 61 | + } |
| 62 | + |
| 63 | + await fs.rm(pnpmHome, { recursive: true, force: true }); |
| 64 | + console.log('Successfully uninstalled pnpm'); |
| 65 | + |
| 66 | + await FileUtils.removeLineFromZshrc('# pnpm') |
| 67 | + await FileUtils.removeLineFromZshrc(`export PNPM_HOME="${os.homedir()}/Library/pnpm"`) |
| 68 | + await FileUtils.removeFromFile(path.join(os.homedir(), '.zshrc'), |
| 69 | +`case ":$PATH:" in |
| 70 | + *":$PNPM_HOME:"*) ;; |
| 71 | + *) export PATH="$PNPM_HOME:$PATH" ;; |
| 72 | +esac`) |
| 73 | + await FileUtils.removeLineFromZshrc('# pnpm end') |
| 74 | + } |
| 75 | +} |
0 commit comments