Open History to see the last commit
1import { exec } from 'node:child_process'
2import { existsSync } from 'node:fs'
3import { join } from 'node:path'
4import { devtoolsEventClient } from '@tanstack/devtools-client'
5import chalk from 'chalk'
6import { readPackageJson, tryParseJson } from './utils'
7import { injectPluginIntoFile } from './inject-plugin'
8import type { OutdatedDeps } from '@tanstack/devtools-client'
9β
10/**
11 * Gets the outdated command for the detected package manager
12 */
13const getOutdatedCommand = (packageManager: string): string => {
14 switch (packageManager) {
15 case 'yarn':
16 return 'yarn outdated --json'
17 case 'pnpm':
18 return 'pnpm outdated --format json'
19 case 'bun':
20 return 'bun outdated --json'
21 case 'npm':
22 default:
23 return 'npm outdated --json'
24 }
25}
26β
27/**
28 * Adds a plugin to the devtools configuration file
29 */
30export const addPluginToDevtools = (
31 devtoolsFileId: string | null,
32 packageName: string,
33 pluginName: string,
34 pluginImport?: { importName: string; type: 'jsx' | 'function' },
35): { success: boolean; error?: string } => {
36 // Check if we found the devtools file
37 if (!devtoolsFileId) {
38 const error = 'Devtools file not found'
39 console.log(
40 chalk.yellowBright(
41 `[@tanstack/devtools-vite] Could not add plugin. ${error}.`,
42 ),
43 )
44 return { success: false, error }
45 }
46β
47 // Inject the plugin into the file
48 const result = injectPluginIntoFile(devtoolsFileId, {
49 packageName,
50 pluginName,
51 pluginImport,
52 })
53β
54 if (result.success) {
55 console.log(
56 chalk.greenBright(
57 `[@tanstack/devtools-vite] Successfully added ${packageName} to devtools!`,
58 ),
59 )
60 } else {
61 console.log(
62 chalk.yellowBright(
63 `[@tanstack/devtools-vite] Could not add plugin: ${result.error}`,
64 ),
65 )
66 }
67β
68 return result
69}
70/**
71 * Gets the install command for the detected package manager
72 */
73const getInstallCommand = (
74 packageManager: string,
75 packageName: string,
76): string => {
77 switch (packageManager) {
78 case 'yarn':
79 return `yarn add -D ${packageName}`
80 case 'pnpm':
81 return `pnpm add -D ${packageName}`
82 case 'bun':
83 return `bun add -D ${packageName}`
84 case 'npm':
85 default:
86 return `npm install -D ${packageName}`
87 }
88}
89β
90export const installPackage = async (
91 packageName: string,
92): Promise<{
93 success: boolean
94 error?: string
95}> => {
96 return new Promise((resolve) => {
97 const packageManager = detectPackageManager()
98 const installCommand = getInstallCommand(packageManager, packageName)
99β
100 console.log(
101 chalk.blueBright(
102 `[@tanstack/devtools-vite] Installing ${packageName}...`,
103 ),
104 )
105β
106 exec(installCommand, async (installError) => {
107 if (installError) {
108 console.error(
109 chalk.redBright(
110 `[@tanstack/devtools-vite] Failed to install ${packageName}:`,
111 ),
112 installError.message,
113 )
114 resolve({
115 success: false,
116 error: installError.message,
117 })
118 return
119 }
120β
121 console.log(
122 chalk.greenBright(
123 `[@tanstack/devtools-vite] Successfully installed ${packageName}`,
124 ),
125 )
126β
127 // Read the updated package.json and emit the event
128 const updatedPackageJson = await readPackageJson()
129 devtoolsEventClient.emit('package-json-updated', {
130 packageJson: updatedPackageJson,
131 })
132β
133 resolve({ success: true })
134 })
135 })
136}
137β
138/**
139 * Detects the package manager used in the project by checking for lock files
140 */
141const detectPackageManager = (): 'npm' | 'yarn' | 'pnpm' | 'bun' => {
142 const cwd = process.cwd()
143β
144 // Check for lock files in order of specificity
145 if (existsSync(join(cwd, 'bun.lockb')) || existsSync(join(cwd, 'bun.lock'))) {
146 return 'bun'
147 }
148 if (existsSync(join(cwd, 'pnpm-lock.yaml'))) {
149 return 'pnpm'
150 }
151 if (existsSync(join(cwd, 'yarn.lock'))) {
152 return 'yarn'
153 }
154 if (existsSync(join(cwd, 'package-lock.json'))) {
155 return 'npm'
156 }
157β
158 // Default to pnpm if no lock file is found
159 return 'pnpm'
160}
161β
162export const emitOutdatedDeps = async () => {
163 return await new Promise<OutdatedDeps | null>((resolve) => {
164 const packageManager = detectPackageManager()
165 const outdatedCommand = getOutdatedCommand(packageManager)
166β
167 exec(outdatedCommand, (_, stdout) => {
168 // outdated commands exit with code 1 if there are outdated packages, but still output valid JSON
169 if (stdout) {
170 const newOutdatedDeps = tryParseJson<OutdatedDeps>(stdout)
171 if (!newOutdatedDeps) {
172 return
173 }
174 devtoolsEventClient.emit('outdated-deps-read', {
175 outdatedDeps: newOutdatedDeps,
176 })
177 resolve(newOutdatedDeps)
178 }
179 })
180 })
181}