(uri: string, workspaceRoot?: string)
| 113 | * Read a resource's contents |
| 114 | */ |
| 115 | export async function readResource(uri: string, workspaceRoot?: string): Promise<ResourceContents[]> { |
| 116 | const parsed = parseDeepnoteUri(uri) |
| 117 | |
| 118 | if (parsed.type === 'examples') { |
| 119 | // Return list of example notebooks |
| 120 | const currentDir = path.dirname(fileURLToPath(import.meta.url)) |
| 121 | const examplesDir = path.resolve(currentDir, '../../../examples') |
| 122 | const files = await findDeepnoteFiles(examplesDir, 1) |
| 123 | |
| 124 | const examples = await Promise.all( |
| 125 | files.map(async file => { |
| 126 | try { |
| 127 | const content = await fs.readFile(file, 'utf-8') |
| 128 | const deepnote = deserializeDeepnoteFile(content) |
| 129 | return { |
| 130 | name: path.basename(file, DEEPNOTE_EXTENSION), |
| 131 | path: file, |
| 132 | projectName: deepnote.project.name, |
| 133 | notebooks: deepnote.project.notebooks.length, |
| 134 | blocks: deepnote.project.notebooks.reduce((sum, n) => sum + n.blocks.length, 0), |
| 135 | } |
| 136 | } catch { |
| 137 | return { |
| 138 | name: path.basename(file, DEEPNOTE_EXTENSION), |
| 139 | path: file, |
| 140 | error: 'Could not parse', |
| 141 | } |
| 142 | } |
| 143 | }) |
| 144 | ) |
| 145 | |
| 146 | return [ |
| 147 | { |
| 148 | uri, |
| 149 | mimeType: 'application/json', |
| 150 | text: JSON.stringify({ examples }, null, 2), |
| 151 | }, |
| 152 | ] |
| 153 | } |
| 154 | |
| 155 | if (parsed.type === 'workspace') { |
| 156 | if (!workspaceRoot) { |
| 157 | return [ |
| 158 | { |
| 159 | uri, |
| 160 | mimeType: 'application/json', |
| 161 | text: JSON.stringify({ error: 'No workspace root configured' }), |
| 162 | }, |
| 163 | ] |
| 164 | } |
| 165 | |
| 166 | const files = await findDeepnoteFiles(workspaceRoot) |
| 167 | const notebooks = await Promise.all( |
| 168 | files.map(async file => { |
| 169 | try { |
| 170 | const content = await fs.readFile(file, 'utf-8') |
| 171 | const deepnote = deserializeDeepnoteFile(content) |
| 172 | return { |
no test coverage detected