|
| 1 | +import { ExampleConfig, Resource, ResourceSettings, getPty } from '@codifycli/plugin-core'; |
| 2 | +import { OS, ResourceConfig } from '@codifycli/schemas'; |
| 3 | +import * as fs from 'node:fs/promises'; |
| 4 | +import * as path from 'node:path'; |
| 5 | +import { z } from 'zod'; |
| 6 | + |
| 7 | +const schema = z.object({ |
| 8 | + directories: z.array(z.string()).describe('List of directories to run npm install in'), |
| 9 | +}); |
| 10 | + |
| 11 | +export type NpmInstallConfig = z.infer<typeof schema> & ResourceConfig; |
| 12 | + |
| 13 | +const defaultConfig: Partial<NpmInstallConfig> = { |
| 14 | + directories: [], |
| 15 | +}; |
| 16 | + |
| 17 | +const exampleSingleProject: ExampleConfig = { |
| 18 | + title: 'Run npm install in a project directory', |
| 19 | + description: 'Ensure npm dependencies are installed in a specific project directory.', |
| 20 | + configs: [{ |
| 21 | + type: 'npm-install', |
| 22 | + directories: ['~/projects/my-app'], |
| 23 | + }], |
| 24 | +}; |
| 25 | + |
| 26 | +const exampleMultipleProjects: ExampleConfig = { |
| 27 | + title: 'Run npm install in multiple directories', |
| 28 | + description: 'Install npm dependencies across multiple projects in one step.', |
| 29 | + configs: [{ |
| 30 | + type: 'npm-install', |
| 31 | + directories: ['~/projects/frontend', '~/projects/backend', '~/projects/shared'], |
| 32 | + }], |
| 33 | +}; |
| 34 | + |
| 35 | +export class NpmInstallResource extends Resource<NpmInstallConfig> { |
| 36 | + getSettings(): ResourceSettings<NpmInstallConfig> { |
| 37 | + return { |
| 38 | + id: 'npm-install', |
| 39 | + defaultConfig, |
| 40 | + exampleConfigs: { |
| 41 | + example1: exampleSingleProject, |
| 42 | + example2: exampleMultipleProjects, |
| 43 | + }, |
| 44 | + operatingSystems: [OS.Darwin, OS.Linux], |
| 45 | + schema, |
| 46 | + parameterSettings: { |
| 47 | + directories: { |
| 48 | + type: 'array', |
| 49 | + itemType: 'directory', |
| 50 | + canModify: true, |
| 51 | + isElementEqual: (a, b) => path.resolve(a) === path.resolve(b), |
| 52 | + filterInStatelessMode: (desired, current) => |
| 53 | + current.filter((c) => desired.some((d) => path.resolve(d) === path.resolve(c))), |
| 54 | + }, |
| 55 | + }, |
| 56 | + dependencies: ['npm', 'nvm', 'pnpm'], |
| 57 | + importAndDestroy: { |
| 58 | + preventDestroy: true, |
| 59 | + }, |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + async refresh(parameters: Partial<NpmInstallConfig>): Promise<Partial<NpmInstallConfig> | null> { |
| 64 | + const pty = getPty(); |
| 65 | + const { status } = await pty.spawnSafe('which npm'); |
| 66 | + if (status === 'error') { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + if (!parameters.directories || parameters.directories.length === 0) { |
| 71 | + return parameters; |
| 72 | + } |
| 73 | + |
| 74 | + // Return only directories that have node_modules installed |
| 75 | + const installed: string[] = []; |
| 76 | + for (const dir of parameters.directories) { |
| 77 | + const resolved = dir.replace(/^~/, process.env.HOME ?? '~'); |
| 78 | + try { |
| 79 | + await fs.access(path.join(resolved, 'node_modules')); |
| 80 | + installed.push(dir); |
| 81 | + } catch { |
| 82 | + // node_modules doesn't exist — not installed |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return { directories: installed }; |
| 87 | + } |
| 88 | + |
| 89 | + async create(plan: { desiredConfig: NpmInstallConfig }): Promise<void> { |
| 90 | + await this.runInstall(plan.desiredConfig.directories ?? []); |
| 91 | + } |
| 92 | + |
| 93 | + async modify( |
| 94 | + _pc: unknown, |
| 95 | + plan: { desiredConfig: NpmInstallConfig }, |
| 96 | + ): Promise<void> { |
| 97 | + await this.runInstall(plan.desiredConfig.directories ?? []); |
| 98 | + } |
| 99 | + |
| 100 | + async destroy(): Promise<void> { |
| 101 | + // node_modules removal is intentionally left to the user; prevent destroy is set |
| 102 | + } |
| 103 | + |
| 104 | + private async runInstall(directories: string[]): Promise<void> { |
| 105 | + const $ = getPty(); |
| 106 | + for (const dir of directories) { |
| 107 | + const resolved = dir.replace(/^~/, process.env.HOME ?? '~'); |
| 108 | + await $.spawn(`npm install`, { cwd: resolved, interactive: true }); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments