|
| 1 | +import { resolve } from 'node:path' |
| 2 | + |
| 3 | +import inquirer from 'inquirer' |
| 4 | +import semver from 'semver' |
| 5 | + |
| 6 | +import type { RunRecipeOptions } from '../../commands/recipes/recipes.js' |
| 7 | +import { chalk, error, log, version } from '../../utils/command-helpers.js' |
| 8 | + |
| 9 | +import { |
| 10 | + applyOverrides, |
| 11 | + downloadFile, |
| 12 | + getExistingContext, |
| 13 | + parseContextFile, |
| 14 | + writeFile, |
| 15 | + FILE_NAME, |
| 16 | + NETLIFY_PROVIDER, |
| 17 | +} from './context.js' |
| 18 | + |
| 19 | +export const description = 'Manage context files for AI tools' |
| 20 | + |
| 21 | +const presets = [ |
| 22 | + { name: 'Cursor rules (.cursor/rules/)', value: '.cursor/rules' }, |
| 23 | + { name: 'Custom location', value: '' }, |
| 24 | +] |
| 25 | + |
| 26 | +const promptForPath = async (): Promise<string> => { |
| 27 | + const { presetPath } = await inquirer.prompt([ |
| 28 | + { |
| 29 | + name: 'presetPath', |
| 30 | + message: 'Where should we put the context files?', |
| 31 | + type: 'list', |
| 32 | + choices: presets, |
| 33 | + }, |
| 34 | + ]) |
| 35 | + |
| 36 | + if (presetPath) { |
| 37 | + return presetPath |
| 38 | + } |
| 39 | + |
| 40 | + const { customPath } = await inquirer.prompt([ |
| 41 | + { |
| 42 | + type: 'input', |
| 43 | + name: 'customPath', |
| 44 | + message: 'Enter the path, relative to the project root, where the context files should be placed', |
| 45 | + default: './ai-context', |
| 46 | + }, |
| 47 | + ]) |
| 48 | + |
| 49 | + if (customPath) { |
| 50 | + return customPath |
| 51 | + } |
| 52 | + |
| 53 | + log('You must select a path.') |
| 54 | + |
| 55 | + return promptForPath() |
| 56 | +} |
| 57 | + |
| 58 | +export const run = async ({ args, command }: RunRecipeOptions) => { |
| 59 | + // Start the download in the background while we wait for the prompts. |
| 60 | + // eslint-disable-next-line promise/prefer-await-to-then |
| 61 | + const download = downloadFile(version).catch(() => null) |
| 62 | + |
| 63 | + const filePath = args[0] || (await promptForPath()) |
| 64 | + const { contents: downloadedFile, minimumCLIVersion } = (await download) ?? {} |
| 65 | + |
| 66 | + if (!downloadedFile) { |
| 67 | + error('An error occurred when pulling the latest context files. Please try again.') |
| 68 | + |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + if (minimumCLIVersion && semver.lt(version, minimumCLIVersion)) { |
| 73 | + error( |
| 74 | + `This command requires version ${minimumCLIVersion} or above of the Netlify CLI. Refer to ${chalk.underline( |
| 75 | + 'https://ntl.fyi/update-cli', |
| 76 | + )} for information on how to update.`, |
| 77 | + ) |
| 78 | + |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + const absoluteFilePath = resolve(command?.workingDir ?? '', filePath, FILE_NAME) |
| 83 | + const existing = await getExistingContext(absoluteFilePath) |
| 84 | + const remote = parseContextFile(downloadedFile) |
| 85 | + |
| 86 | + let { contents } = remote |
| 87 | + |
| 88 | + // Does a file already exist at this path? |
| 89 | + if (existing) { |
| 90 | + // If it's a file we've created, let's check the version and bail if we're |
| 91 | + // already on the latest, otherwise rewrite it with the latest version. |
| 92 | + if (existing.provider?.toLowerCase() === NETLIFY_PROVIDER) { |
| 93 | + if (remote?.version === existing.version) { |
| 94 | + log( |
| 95 | + `You're all up to date! ${chalk.underline( |
| 96 | + absoluteFilePath, |
| 97 | + )} contains the latest version of the context files.`, |
| 98 | + ) |
| 99 | + |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + // We must preserve any overrides found in the existing file. |
| 104 | + contents = applyOverrides(remote.contents, existing.overrides?.innerContents) |
| 105 | + } else { |
| 106 | + // If this is not a file we've created, we can offer to overwrite it and |
| 107 | + // preserve the existing contents by moving it to the overrides slot. |
| 108 | + const { confirm } = await inquirer.prompt({ |
| 109 | + type: 'confirm', |
| 110 | + name: 'confirm', |
| 111 | + message: `A context file already exists at ${chalk.underline( |
| 112 | + absoluteFilePath, |
| 113 | + )}. It has not been created by the Netlify CLI, but we can update it while preserving its existing content. Can we proceed?`, |
| 114 | + default: true, |
| 115 | + }) |
| 116 | + |
| 117 | + if (!confirm) { |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + // Whatever exists in the file goes in the overrides block. |
| 122 | + contents = applyOverrides(remote.contents, existing.contents) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + await writeFile(absoluteFilePath, contents) |
| 127 | + |
| 128 | + log(`${existing ? 'Updated' : 'Created'} context files at ${chalk.underline(absoluteFilePath)}`) |
| 129 | +} |
2 commit comments
github-actions[bot] commentedon Feb 13, 2025
📊 Benchmark results
github-actions[bot] commentedon Feb 13, 2025
📊 Benchmark results