|
| 1 | +import {spawn} from 'node:child_process'; |
| 2 | +import {RenderInternals} from '@remotion/renderer'; |
| 3 | +import type { |
| 4 | + GetRemotionSkillsInfoResponse, |
| 5 | + InstallRemotionSkillRequest, |
| 6 | + RemoveRemotionSkillRequest, |
| 7 | +} from '@remotion/studio-shared'; |
| 8 | +import {getPackageManagerSpawnOptions} from '../../helpers/package-manager-spawn-options'; |
| 9 | +import {remotionSkillNames} from '../../remotion-skill-names'; |
| 10 | +import type {ApiHandler} from '../api-types'; |
| 11 | +import {getPackageManager} from '../get-package-manager'; |
| 12 | +import {getRemotionSkillsInfo} from './remotion-skills-info'; |
| 13 | + |
| 14 | +const changingSkillsInProjects = new Set<string>(); |
| 15 | + |
| 16 | +const changeRemotionSkill = async ({ |
| 17 | + action, |
| 18 | + remotionRoot, |
| 19 | + input: {skill}, |
| 20 | + logLevel, |
| 21 | +}: Parameters< |
| 22 | + ApiHandler<InstallRemotionSkillRequest, GetRemotionSkillsInfoResponse> |
| 23 | +>[0] & { |
| 24 | + readonly action: 'install' | 'remove'; |
| 25 | +}): Promise<GetRemotionSkillsInfoResponse> => { |
| 26 | + if (!remotionSkillNames.some((name) => name === skill)) { |
| 27 | + throw new Error(`Unknown Remotion skill: ${JSON.stringify(skill)}`); |
| 28 | + } |
| 29 | + |
| 30 | + if (changingSkillsInProjects.has(remotionRoot)) { |
| 31 | + throw new Error( |
| 32 | + 'A skill is already being installed or removed. Please try again once it finishes.', |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + changingSkillsInProjects.add(remotionRoot); |
| 37 | + try { |
| 38 | + const installedSkill = getRemotionSkillsInfo({remotionRoot}).skills.find( |
| 39 | + ({name}) => name === skill, |
| 40 | + ); |
| 41 | + const removingGlobally = |
| 42 | + action === 'remove' && |
| 43 | + installedSkill?.installedInProject === false && |
| 44 | + installedSkill.installedGlobally; |
| 45 | + if ( |
| 46 | + action === 'remove' && |
| 47 | + !removingGlobally && |
| 48 | + !installedSkill?.installedInProject |
| 49 | + ) { |
| 50 | + throw new Error(`${skill} is not installed.`); |
| 51 | + } |
| 52 | + |
| 53 | + const packageManager = getPackageManager({ |
| 54 | + remotionRoot, |
| 55 | + packageManager: undefined, |
| 56 | + dirUp: 0, |
| 57 | + logLevel, |
| 58 | + }); |
| 59 | + const useBunx = |
| 60 | + packageManager !== 'unknown' && packageManager.manager === 'bun'; |
| 61 | + const executable = useBunx |
| 62 | + ? 'bunx' |
| 63 | + : process.platform === 'win32' |
| 64 | + ? 'npx.cmd' |
| 65 | + : 'npx'; |
| 66 | + const commandArgs = |
| 67 | + action === 'install' |
| 68 | + ? ['add', `remotion-dev/skills@${skill}`, '--yes'] |
| 69 | + : ['remove', ...(removingGlobally ? ['--global'] : []), skill, '--yes']; |
| 70 | + const args = useBunx |
| 71 | + ? ['--silent', 'skills@1.5.26', ...commandArgs] |
| 72 | + : ['--yes', '--loglevel=error', 'skills@1.5.26', ...commandArgs]; |
| 73 | + RenderInternals.Log.info( |
| 74 | + {indent: false, logLevel}, |
| 75 | + RenderInternals.chalk.gray(`╭─ ${executable} ${args.join(' ')}`), |
| 76 | + ); |
| 77 | + const time = Date.now(); |
| 78 | + try { |
| 79 | + await new Promise<void>((resolve, reject) => { |
| 80 | + const child = spawn(executable, args, { |
| 81 | + cwd: remotionRoot, |
| 82 | + env: {...process.env, DISABLE_TELEMETRY: '1'}, |
| 83 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 84 | + ...getPackageManagerSpawnOptions(), |
| 85 | + }); |
| 86 | + let output = ''; |
| 87 | + const onData = (data: Buffer) => { |
| 88 | + output = (output + data.toString()).slice(-8000); |
| 89 | + data |
| 90 | + .toString() |
| 91 | + .trim() |
| 92 | + .split('\n') |
| 93 | + .forEach((line) => |
| 94 | + RenderInternals.Log.info({indent: true, logLevel}, line), |
| 95 | + ); |
| 96 | + }; |
| 97 | + |
| 98 | + child.stdout.on('data', onData); |
| 99 | + child.stderr.on('data', onData); |
| 100 | + child.on('error', reject); |
| 101 | + child.on('close', (code, signal) => { |
| 102 | + if (code === 0) { |
| 103 | + resolve(); |
| 104 | + } else { |
| 105 | + reject( |
| 106 | + new Error( |
| 107 | + `Could not ${action} ${skill} (exit code ${code}, signal ${signal}). ${output.trim()}`, |
| 108 | + ), |
| 109 | + ); |
| 110 | + } |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + const info = getRemotionSkillsInfo({remotionRoot}); |
| 115 | + const updatedSkill = info.skills.find(({name}) => name === skill); |
| 116 | + if (action === 'install' && !updatedSkill?.installedInProject) { |
| 117 | + throw new Error( |
| 118 | + `The installer finished, but ${skill} was not found in the project. Please try again.`, |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + if ( |
| 123 | + action === 'remove' && |
| 124 | + (removingGlobally |
| 125 | + ? updatedSkill?.installedGlobally |
| 126 | + : updatedSkill?.installedInProject) |
| 127 | + ) { |
| 128 | + throw new Error( |
| 129 | + `The remover finished, but ${skill} is still installed ${removingGlobally ? 'globally' : 'in the project'}. Please try again.`, |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + RenderInternals.Log.info( |
| 134 | + {indent: false, logLevel}, |
| 135 | + RenderInternals.chalk.gray('╰─ '), |
| 136 | + `Done in ${Date.now() - time}ms`, |
| 137 | + ); |
| 138 | + return info; |
| 139 | + } catch (error) { |
| 140 | + RenderInternals.Log.info( |
| 141 | + {indent: false, logLevel}, |
| 142 | + RenderInternals.chalk.gray('╰─ '), |
| 143 | + RenderInternals.chalk.red(`Errored in ${Date.now() - time}ms`), |
| 144 | + ); |
| 145 | + throw error; |
| 146 | + } |
| 147 | + } finally { |
| 148 | + changingSkillsInProjects.delete(remotionRoot); |
| 149 | + } |
| 150 | +}; |
| 151 | + |
| 152 | +export const installRemotionSkillHandler: ApiHandler< |
| 153 | + InstallRemotionSkillRequest, |
| 154 | + GetRemotionSkillsInfoResponse |
| 155 | +> = (input) => { |
| 156 | + return changeRemotionSkill({...input, action: 'install'}); |
| 157 | +}; |
| 158 | + |
| 159 | +export const removeRemotionSkillHandler: ApiHandler< |
| 160 | + RemoveRemotionSkillRequest, |
| 161 | + GetRemotionSkillsInfoResponse |
| 162 | +> = (input) => { |
| 163 | + return changeRemotionSkill({...input, action: 'remove'}); |
| 164 | +}; |
0 commit comments