|
| 1 | +#!/usr/bin/env tsx |
| 2 | +import { |
| 3 | + chmodSync, |
| 4 | + existsSync, |
| 5 | + mkdirSync, |
| 6 | + readFileSync, |
| 7 | + renameSync, |
| 8 | + rmSync, |
| 9 | + statSync, |
| 10 | + writeFileSync |
| 11 | +} from 'node:fs' |
| 12 | +import { homedir } from 'node:os' |
| 13 | +import { dirname, join } from 'node:path' |
| 14 | +import { pathToFileURL } from 'node:url' |
| 15 | +import { parseArgs } from 'node:util' |
| 16 | + |
| 17 | +import { applyEdits, modify, parse } from 'jsonc-parser' |
| 18 | +import type { FormattingOptions, ParseError } from 'jsonc-parser' |
| 19 | + |
| 20 | +type SettingValue = boolean | string |
| 21 | +type Tool = 'amp' | 'claude' | 'codex' |
| 22 | +type Outcome = |
| 23 | + | 'already configured' |
| 24 | + | 'error' |
| 25 | + | 'updated' |
| 26 | + | 'workspace setting required' |
| 27 | + |
| 28 | +interface Setting { |
| 29 | + path: string[] |
| 30 | + value: SettingValue |
| 31 | +} |
| 32 | + |
| 33 | +interface UpdateResult { |
| 34 | + tool: Tool |
| 35 | + outcome: Outcome |
| 36 | + detail?: string |
| 37 | +} |
| 38 | + |
| 39 | +const CLAUDE_SETTINGS: Setting[] = [ |
| 40 | + { path: ['attribution', 'commit'], value: '' }, |
| 41 | + { path: ['attribution', 'pr'], value: '' }, |
| 42 | + { path: ['attribution', 'sessionUrl'], value: false } |
| 43 | +] |
| 44 | + |
| 45 | +const AMP_SETTINGS: Setting[] = [ |
| 46 | + { path: ['amp.git.commit.ampThread.enabled'], value: false }, |
| 47 | + { path: ['amp.git.commit.coauthor.enabled'], value: false } |
| 48 | +] |
| 49 | + |
| 50 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 51 | + return typeof value === 'object' && value !== null && !Array.isArray(value) |
| 52 | +} |
| 53 | + |
| 54 | +function readPath(value: unknown, path: string[]): unknown { |
| 55 | + let current = value |
| 56 | + for (const key of path) { |
| 57 | + if (!isRecord(current)) return undefined |
| 58 | + current = current[key] |
| 59 | + } |
| 60 | + return current |
| 61 | +} |
| 62 | + |
| 63 | +function formattingOptions(content: string): FormattingOptions { |
| 64 | + return { |
| 65 | + insertSpaces: true, |
| 66 | + tabSize: 2, |
| 67 | + eol: content.includes('\r\n') ? '\r\n' : '\n' |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function parseSettings(content: string): Record<string, unknown> { |
| 72 | + const errors: ParseError[] = [] |
| 73 | + const parsed: unknown = parse(content, errors, { |
| 74 | + allowTrailingComma: true, |
| 75 | + disallowComments: false |
| 76 | + }) |
| 77 | + if (errors.length > 0 || !isRecord(parsed)) { |
| 78 | + throw new Error('settings file is not a valid JSON object') |
| 79 | + } |
| 80 | + return parsed |
| 81 | +} |
| 82 | + |
| 83 | +function writeSettings(path: string, content: string) { |
| 84 | + mkdirSync(dirname(path), { recursive: true }) |
| 85 | + const temporaryPath = `${path}.${process.pid}.tmp` |
| 86 | + const mode = existsSync(path) ? statSync(path).mode : undefined |
| 87 | + |
| 88 | + try { |
| 89 | + writeFileSync(temporaryPath, content, { mode }) |
| 90 | + renameSync(temporaryPath, path) |
| 91 | + if (mode !== undefined) chmodSync(path, mode) |
| 92 | + } finally { |
| 93 | + rmSync(temporaryPath, { force: true }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function updateSettingsFile(path: string, settings: Setting[]): Outcome { |
| 98 | + let content = existsSync(path) ? readFileSync(path, 'utf8') : '{}\n' |
| 99 | + const parsed = parseSettings(content) |
| 100 | + if (settings.every(({ path, value }) => readPath(parsed, path) === value)) { |
| 101 | + return 'already configured' |
| 102 | + } |
| 103 | + |
| 104 | + const options = formattingOptions(content) |
| 105 | + for (const setting of settings) { |
| 106 | + content = applyEdits( |
| 107 | + content, |
| 108 | + modify(content, setting.path, setting.value, { |
| 109 | + formattingOptions: options |
| 110 | + }) |
| 111 | + ) |
| 112 | + } |
| 113 | + parseSettings(content) |
| 114 | + writeSettings(path, content) |
| 115 | + return 'updated' |
| 116 | +} |
| 117 | + |
| 118 | +function ampSettingsPath(home: string): string { |
| 119 | + const directory = join(home, '.config', 'amp') |
| 120 | + const jsonPath = join(directory, 'settings.json') |
| 121 | + const jsoncPath = join(directory, 'settings.jsonc') |
| 122 | + if (existsSync(jsonPath) && existsSync(jsoncPath)) { |
| 123 | + throw new Error('both settings.json and settings.jsonc exist') |
| 124 | + } |
| 125 | + return existsSync(jsoncPath) ? jsoncPath : jsonPath |
| 126 | +} |
| 127 | + |
| 128 | +function updateTool(tool: Tool, update: () => Outcome): UpdateResult { |
| 129 | + try { |
| 130 | + return { tool, outcome: update() } |
| 131 | + } catch (error) { |
| 132 | + return { |
| 133 | + tool, |
| 134 | + outcome: 'error', |
| 135 | + detail: error instanceof Error ? error.message : 'unknown error' |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +export function updateAttributionSettings(home: string): UpdateResult[] { |
| 141 | + return [ |
| 142 | + updateTool('claude', () => |
| 143 | + updateSettingsFile( |
| 144 | + join(home, '.claude', 'settings.json'), |
| 145 | + CLAUDE_SETTINGS |
| 146 | + ) |
| 147 | + ), |
| 148 | + updateTool('amp', () => |
| 149 | + updateSettingsFile(ampSettingsPath(home), AMP_SETTINGS) |
| 150 | + ), |
| 151 | + { tool: 'codex', outcome: 'workspace setting required' } |
| 152 | + ] |
| 153 | +} |
| 154 | + |
| 155 | +export function formatResults(results: UpdateResult[]): string { |
| 156 | + return results |
| 157 | + .map(({ tool, outcome, detail }) => |
| 158 | + detail ? `${tool}: ${outcome} (${detail})` : `${tool}: ${outcome}` |
| 159 | + ) |
| 160 | + .join('\n') |
| 161 | +} |
| 162 | + |
| 163 | +function main() { |
| 164 | + const { values } = parseArgs({ |
| 165 | + options: { home: { type: 'string', default: homedir() } } |
| 166 | + }) |
| 167 | + const results = updateAttributionSettings(values.home) |
| 168 | + process.stdout.write(`${formatResults(results)}\n`) |
| 169 | + if (results.some(({ outcome }) => outcome === 'error')) process.exitCode = 1 |
| 170 | +} |
| 171 | + |
| 172 | +if (import.meta.url === pathToFileURL(process.argv[1]).href) main() |
0 commit comments