|
| 1 | +import path from 'path' |
| 2 | +import { CliComponents } from '../../components' |
| 3 | +import { declareArgs } from '../../logic/args' |
| 4 | +import { assertValidProjectFolder } from '../../logic/project-validations' |
| 5 | +import { CliError } from '../../logic/error' |
| 6 | +import i18next from 'i18next' |
| 7 | + |
| 8 | +import { Result } from 'arg' |
| 9 | +const GITHUB_API_BASE = 'https://api.github.com/repos/decentraland/documentation/contents/ai-sdk-context' |
| 10 | +interface GitHubFile { |
| 11 | + name: string |
| 12 | + path: string |
| 13 | + type: 'file' | 'dir' |
| 14 | + download_url?: string |
| 15 | + url: string |
| 16 | +} |
| 17 | + |
| 18 | +export interface Options { |
| 19 | + args: Result<typeof args> |
| 20 | + components: Pick<CliComponents, 'logger' | 'fs' | 'fetch'> |
| 21 | +} |
| 22 | + |
| 23 | +export const args = declareArgs({ |
| 24 | + '--help': Boolean, |
| 25 | + '-h': '--help' |
| 26 | +}) |
| 27 | + |
| 28 | +export function help(options: Options) { |
| 29 | + options.components.logger.log(` |
| 30 | + Usage: 'sdk-commands get-context-files [options]' |
| 31 | + Options: |
| 32 | + -h, --help Gets & updates context files from https://github.com/decentraland/documentation/tree/main/ai-sdk-context only on valid scenes |
| 33 | +
|
| 34 | + Example: |
| 35 | + - Get context files: |
| 36 | + $ sdk-commands get-context-files |
| 37 | + `) |
| 38 | +} |
| 39 | + |
| 40 | +async function listFilesFromPath(components: Pick<CliComponents, 'fetch'>, url: string): Promise<GitHubFile[]> { |
| 41 | + const response = await components.fetch.fetch(url) |
| 42 | + |
| 43 | + if (!response.ok) { |
| 44 | + throw new CliError('GET_CONTEXT_FILES_LIST_FAILED', i18next.t('errors.get_context_files.list_failed')) |
| 45 | + } |
| 46 | + |
| 47 | + return (await response.json()) as GitHubFile[] |
| 48 | +} |
| 49 | + |
| 50 | +async function downloadFile(components: Pick<CliComponents, 'fetch'>, url: string): Promise<string> { |
| 51 | + const response = await components.fetch.fetch(url) |
| 52 | + if (!response.ok) { |
| 53 | + throw new CliError('GET_CONTEXT_FILES_DOWNLOAD_FAILED', i18next.t('errors.get_context_files.download_failed')) |
| 54 | + } |
| 55 | + return await response.text() |
| 56 | +} |
| 57 | + |
| 58 | +async function getAllFiles( |
| 59 | + components: Pick<CliComponents, 'fetch'>, |
| 60 | + initialUrl: string = GITHUB_API_BASE |
| 61 | +): Promise<Array<{ url: string; filename: string; path: string }>> { |
| 62 | + const files: Array<{ url: string; filename: string; path: string }> = [] |
| 63 | + const rootFiles = await listFilesFromPath(components, initialUrl) |
| 64 | + for (const file of rootFiles) { |
| 65 | + if (file.type === 'file') { |
| 66 | + if (file.download_url) { |
| 67 | + const filename = file.name |
| 68 | + const fileInfo = { |
| 69 | + url: file.download_url, |
| 70 | + filename: filename, |
| 71 | + path: file.path |
| 72 | + } |
| 73 | + files.push(fileInfo) |
| 74 | + } |
| 75 | + } else if (file.type === 'dir') { |
| 76 | + const subFiles = await getAllFiles(components, file.url) |
| 77 | + files.push(...subFiles) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return files |
| 82 | +} |
| 83 | + |
| 84 | +export async function main(options: Options) { |
| 85 | + const targetDir = process.cwd() |
| 86 | + try { |
| 87 | + await assertValidProjectFolder(options.components, targetDir) |
| 88 | + options.components.logger.log('✓ Valid Scene project') |
| 89 | + } catch (error) { |
| 90 | + options.components.logger.log('Not a valid Scene...') |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + const contextDir = path.join(targetDir, 'dclcontext') |
| 95 | + const contextExists = await options.components.fs.directoryExists(contextDir) |
| 96 | + |
| 97 | + if (contextExists) { |
| 98 | + options.components.logger.log('Context directory exists. Removing old files...') |
| 99 | + await options.components.fs.rm(contextDir, { recursive: true }) |
| 100 | + } |
| 101 | + |
| 102 | + options.components.logger.log('Creating context directory...') |
| 103 | + await options.components.fs.mkdir(contextDir) |
| 104 | + |
| 105 | + const filesToDownload = await getAllFiles(options.components, GITHUB_API_BASE) |
| 106 | + const successfulDownloads: string[] = [] |
| 107 | + const failedDownloads: Array<{ filePath: string; error: string }> = [] |
| 108 | + |
| 109 | + const downloadPromises = filesToDownload.map(async ({ url, filename, path: filePath }) => { |
| 110 | + try { |
| 111 | + const content = await downloadFile(options.components, url) |
| 112 | + const localFilePath = path.join(contextDir, filename) |
| 113 | + await options.components.fs.writeFile(localFilePath, content) |
| 114 | + options.components.logger.log(`✓ Saved ${filePath}`) |
| 115 | + successfulDownloads.push(filePath) |
| 116 | + } catch (error) { |
| 117 | + const errorMessage = error instanceof Error ? error.message : String(error) |
| 118 | + options.components.logger.log(`✗ Failed to download ${filePath}: ${errorMessage}`) |
| 119 | + failedDownloads.push({ filePath, error: errorMessage }) |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + await Promise.all(downloadPromises) |
| 124 | + |
| 125 | + options.components.logger.log( |
| 126 | + `\nDownload complete: ${successfulDownloads.length} successful, ${failedDownloads.length} failed` |
| 127 | + ) |
| 128 | + if (successfulDownloads.length > 0) { |
| 129 | + options.components.logger.log(`Successfully downloaded:`) |
| 130 | + successfulDownloads.forEach((filePath) => { |
| 131 | + options.components.logger.log(` ✓ ${filePath}`) |
| 132 | + }) |
| 133 | + } |
| 134 | + |
| 135 | + if (failedDownloads.length > 0) { |
| 136 | + options.components.logger.log(`Failed downloads:`) |
| 137 | + failedDownloads.forEach(({ filePath, error }) => { |
| 138 | + options.components.logger.log(` ✗ ${filePath}: ${error}`) |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments