|
| 1 | +import { Subscription } from 'rxjs' |
| 2 | +import * as sourcegraph from 'sourcegraph' |
| 3 | +import { evaluateAndCreateCampaignSpec } from '@sourcegraph/campaigns-client' |
| 4 | +import slugify from 'slugify' |
| 5 | +import { getCurrentUser } from './util' |
| 6 | + |
| 7 | +// TODO(sqs) SECURITY(sqs): sanitize for real |
| 8 | +const escapedMarkdownCode = (text: string): string => '`' + text.replace(/`/g, '\\`') + '`' |
| 9 | + |
| 10 | +export const registerFindReplaceAction = (): Subscription => { |
| 11 | + const subscription = new Subscription() |
| 12 | + subscription.add( |
| 13 | + sourcegraph.commands.registerCommand('start-find-replace', async () => { |
| 14 | + |
| 15 | + // To create campaigns, a namespace is used, which can be the current user's username. |
| 16 | + const currentUser = await getCurrentUser(); |
| 17 | + const namespaceName = currentUser?.username |
| 18 | + console.log('currentUser', currentUser) |
| 19 | + |
| 20 | + const match = await sourcegraph.app.activeWindow!.showInputBox({ |
| 21 | + prompt: 'Find all matches of:', |
| 22 | + }) |
| 23 | + if (!match) { |
| 24 | + return |
| 25 | + } |
| 26 | + const replacement = await sourcegraph.app.activeWindow!.showInputBox({ |
| 27 | + prompt: 'Replace with:', |
| 28 | + }) |
| 29 | + if (replacement === undefined) { |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + const name = `replace-${slugify(match)}-with-${slugify(replacement)}` |
| 34 | + const description = `Replace ${escapedMarkdownCode(match)} with ${escapedMarkdownCode(replacement)}` |
| 35 | + |
| 36 | + let percentage = 0 |
| 37 | + const { applyURL, diffStat } = await sourcegraph.app.activeWindow!.withProgress( |
| 38 | + { title: '**Find-replace**' }, |
| 39 | + async reporter => |
| 40 | + evaluateAndCreateCampaignSpec(namespaceName, { |
| 41 | + name, |
| 42 | + on: [ |
| 43 | + { |
| 44 | + repositoriesMatchingQuery: match, |
| 45 | + }, |
| 46 | + ], |
| 47 | + description, |
| 48 | + steps: [ |
| 49 | + { |
| 50 | + fileFilter: path => |
| 51 | + path.endsWith('.yaml') || path.endsWith('.yml') || path.endsWith('.md'), // TODO(sqs) |
| 52 | + editFile: (path, text) => { |
| 53 | + if (!text.includes(match)) { |
| 54 | + return null |
| 55 | + } |
| 56 | + try { |
| 57 | + console.log('editFile running on', path) |
| 58 | + } catch (error) { |
| 59 | + console.error('Caught', error) |
| 60 | + } |
| 61 | + |
| 62 | + percentage += (100 - percentage) / 100 |
| 63 | + reporter.next({ message: `Computing changes in ${path}`, percentage }) |
| 64 | + return text.split(match).join(replacement) |
| 65 | + }, |
| 66 | + }, |
| 67 | + ], |
| 68 | + changesetTemplate: { |
| 69 | + title: description, |
| 70 | + branch: `campaign/${name}`, |
| 71 | + commit: { |
| 72 | + message: description, |
| 73 | + author: { |
| 74 | + name: currentUser.username, |
| 75 | + email: currentUser.email |
| 76 | + }, |
| 77 | + }, |
| 78 | + published: false, |
| 79 | + }, |
| 80 | + }) |
| 81 | + ) |
| 82 | + await sourcegraph.app.activeWindow!.showNotification( |
| 83 | + `[**Find-replace changes**](${applyURL}) are ready to preview and apply.<br/><br/><small>${diffStat.added} additions, ${diffStat.changed} changes, ${diffStat.deleted} deletions</small>`, |
| 84 | + sourcegraph.NotificationType.Success |
| 85 | + ) |
| 86 | + |
| 87 | + // const relativeURL = new URL(applyURL) |
| 88 | + // await sourcegraph.commands.executeCommand('open', `${relativeURL.pathname}${relativeURL.search}`) |
| 89 | + }) |
| 90 | + ) |
| 91 | + return subscription |
| 92 | +} |
0 commit comments