(file: DeepnoteFile, sourceFileStem: string)
| 236 | |
| 237 | /** Splits a multi-notebook DeepnoteFile into one file per notebook, emitting the init notebook (if any) as a standalone entry first. */ |
| 238 | export function splitByNotebooks(file: DeepnoteFile, sourceFileStem: string): NotebookSplitEntry[] { |
| 239 | const notebooks = file.project.notebooks |
| 240 | if (notebooks.length === 0) { |
| 241 | return [] |
| 242 | } |
| 243 | |
| 244 | const initNotebookId = file.project.initNotebookId |
| 245 | const initNotebook = initNotebookId === undefined ? undefined : notebooks.find(nb => nb.id === initNotebookId) |
| 246 | |
| 247 | // A file containing only the init notebook is already in single-notebook form. |
| 248 | if (initNotebook !== undefined && notebooks.length === 1) { |
| 249 | return [] |
| 250 | } |
| 251 | |
| 252 | const used = new Set<string>() |
| 253 | const result: NotebookSplitEntry[] = [] |
| 254 | |
| 255 | if (initNotebook !== undefined) { |
| 256 | const initFilename = allocateUniqueNotebookSplitFilename(sourceFileStem, initNotebook.name, used) |
| 257 | result.push({ |
| 258 | notebook: { id: initNotebook.id, name: initNotebook.name }, |
| 259 | file: { |
| 260 | ...file, |
| 261 | project: { |
| 262 | ...file.project, |
| 263 | notebooks: [initNotebook], |
| 264 | }, |
| 265 | }, |
| 266 | outputFilename: initFilename, |
| 267 | }) |
| 268 | } |
| 269 | |
| 270 | const mainNotebooks = initNotebook === undefined ? notebooks : notebooks.filter(nb => nb.id !== initNotebook.id) |
| 271 | if (mainNotebooks.length === 0) { |
| 272 | // A standalone init entry alone is not useful. |
| 273 | return [] |
| 274 | } |
| 275 | |
| 276 | // Drop an initNotebookId matching no notebook here; left dangling it would make `deepnote run` fail sibling-init resolution. |
| 277 | const hasDanglingInit = initNotebookId !== undefined && initNotebook === undefined |
| 278 | |
| 279 | for (const notebook of mainNotebooks) { |
| 280 | const outputFilename = allocateUniqueNotebookSplitFilename(sourceFileStem, notebook.name, used) |
| 281 | const project = { ...file.project, notebooks: [notebook] } |
| 282 | if (hasDanglingInit) { |
| 283 | delete project.initNotebookId |
| 284 | } |
| 285 | result.push({ |
| 286 | notebook: { id: notebook.id, name: notebook.name }, |
| 287 | file: { ...file, project }, |
| 288 | outputFilename, |
| 289 | }) |
| 290 | } |
| 291 | |
| 292 | return result |
| 293 | } |
no test coverage detected