(args: Record<string, unknown>)
| 151 | } |
| 152 | |
| 153 | async function handleConvertTo(args: Record<string, unknown>) { |
| 154 | const parsedArgs = convertToArgsSchema.safeParse(args) |
| 155 | if (!parsedArgs.success) { |
| 156 | return toError(`Invalid arguments for deepnote_convert_to: ${formatFirstIssue(parsedArgs.error)}`) |
| 157 | } |
| 158 | const { inputPath, outputPath, projectName } = parsedArgs.data |
| 159 | let format = parsedArgs.data.format ?? 'auto' |
| 160 | |
| 161 | const absoluteInput = path.resolve(inputPath) |
| 162 | |
| 163 | let inputStat: Awaited<ReturnType<typeof fs.stat>> |
| 164 | try { |
| 165 | inputStat = await fs.stat(absoluteInput) |
| 166 | } catch (error) { |
| 167 | const message = error instanceof Error ? error.message : String(error) |
| 168 | return toError(`Invalid inputPath: ${message}`) |
| 169 | } |
| 170 | |
| 171 | if (!inputStat.isFile() && !inputStat.isDirectory()) { |
| 172 | return toError('inputPath must point to a file or directory') |
| 173 | } |
| 174 | |
| 175 | const inputIsDirectory = inputStat.isDirectory() |
| 176 | const inputIsFile = inputStat.isFile() |
| 177 | |
| 178 | if (format === 'auto' && inputIsDirectory) { |
| 179 | return toError('Auto-detect format is only supported for file inputs') |
| 180 | } |
| 181 | |
| 182 | if (format === 'auto' && inputIsFile) { |
| 183 | const content = await fs.readFile(absoluteInput, 'utf-8') |
| 184 | const detectedFormat = detectFormat(absoluteInput, content) |
| 185 | if (detectedFormat === 'deepnote') { |
| 186 | return toError('Input is already a .deepnote file; use deepnote_convert_from to export to other formats') |
| 187 | } |
| 188 | format = detectedFormat |
| 189 | } |
| 190 | |
| 191 | if (!isSourceNotebookFormat(format)) { |
| 192 | return toError(`Unknown format: ${format}`) |
| 193 | } |
| 194 | |
| 195 | try { |
| 196 | if (inputIsDirectory) { |
| 197 | const outputDir = resolveConvertToOutputDir(outputPath, absoluteInput) |
| 198 | const finalProjectName = projectName || path.basename(absoluteInput) |
| 199 | |
| 200 | let result: Awaited<ReturnType<typeof convertDirectoryToDeepnote>> |
| 201 | try { |
| 202 | result = await convertDirectoryToDeepnote({ |
| 203 | inputDir: absoluteInput, |
| 204 | format, |
| 205 | outputDir, |
| 206 | projectName: finalProjectName, |
| 207 | }) |
| 208 | } catch (error) { |
| 209 | const message = error instanceof Error ? error.message : String(error) |
| 210 | return toError(message) |
no test coverage detected