Skip to content

Commit beb5e3a

Browse files
committed
feat: add --verbose flag to install/uninstall scripts
Signed-off-by: leocavalcante <leo@cavalcante.dev>
1 parent bfc2b6d commit beb5e3a

2 files changed

Lines changed: 75 additions & 2 deletions

File tree

postinstall.mjs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ const AGENTS_SOURCE_DIR = getAgentsSourceDir(packageRoot)
2323
/** Check for --dry-run flag in command line arguments */
2424
const DRY_RUN = process.argv.includes("--dry-run")
2525

26+
/** Check for --verbose flag in command line arguments */
27+
const VERBOSE = process.argv.includes("--verbose")
28+
29+
/**
30+
* Logs verbose output if --verbose flag is set.
31+
* @param {string} message - The message to log
32+
*/
33+
function verbose(message) {
34+
if (VERBOSE) {
35+
console.log(`[VERBOSE] ${message}`)
36+
}
37+
}
38+
2639
/** Minimum character count for valid agent files */
2740
const MIN_CONTENT_LENGTH = 100
2841

@@ -172,24 +185,37 @@ function main() {
172185
const prefix = DRY_RUN ? "[DRY-RUN] " : ""
173186
console.log(`${prefix}opencode-plugin-opencoder: Installing agents...`)
174187

188+
verbose(`Package root: ${packageRoot}`)
189+
verbose(`Source directory: ${AGENTS_SOURCE_DIR}`)
190+
verbose(`Target directory: ${AGENTS_TARGET_DIR}`)
191+
verbose(`Dry run: ${DRY_RUN}`)
192+
175193
// Create target directory if it doesn't exist
176194
if (!existsSync(AGENTS_TARGET_DIR)) {
195+
verbose(`Target directory does not exist, creating...`)
177196
if (DRY_RUN) {
178197
console.log(`${prefix}Would create ${AGENTS_TARGET_DIR}`)
179198
} else {
180199
mkdirSync(AGENTS_TARGET_DIR, { recursive: true })
181200
console.log(` Created ${AGENTS_TARGET_DIR}`)
182201
}
202+
} else {
203+
verbose(`Target directory already exists`)
183204
}
184205

185206
// Check if source directory exists
207+
verbose(`Checking source directory exists...`)
186208
if (!existsSync(AGENTS_SOURCE_DIR)) {
187209
console.error(`${prefix} Error: Source agents directory not found at ${AGENTS_SOURCE_DIR}`)
188210
process.exit(1)
189211
}
212+
verbose(`Source directory found`)
190213

191214
// Copy all .md files from agents/ to target
192-
const files = readdirSync(AGENTS_SOURCE_DIR).filter((f) => f.endsWith(".md"))
215+
const allFiles = readdirSync(AGENTS_SOURCE_DIR)
216+
verbose(`Files in source directory: ${allFiles.join(", ") || "(none)"}`)
217+
const files = allFiles.filter((f) => f.endsWith(".md"))
218+
verbose(`Markdown files found: ${files.length}`)
193219

194220
if (files.length === 0) {
195221
console.error(`${prefix} Error: No agent files found in agents/ directory`)
@@ -202,34 +228,45 @@ function main() {
202228
for (const file of files) {
203229
const sourcePath = join(AGENTS_SOURCE_DIR, file)
204230
const targetPath = join(AGENTS_TARGET_DIR, file)
231+
verbose(`Processing: ${file}`)
232+
verbose(` Source path: ${sourcePath}`)
233+
verbose(` Target path: ${targetPath}`)
205234

206235
try {
207236
if (DRY_RUN) {
208237
// In dry-run mode, validate source file but don't copy
238+
verbose(` Validating source file (dry-run mode)...`)
209239
const validation = validateAgentContent(sourcePath)
210240
if (!validation.valid) {
211241
throw new Error(`Invalid agent file content: ${validation.error}`)
212242
}
243+
verbose(` Validation passed`)
213244
successes.push(file)
214245
console.log(`${prefix}Would install: ${file} -> ${targetPath}`)
215246
} else {
247+
verbose(` Copying file...`)
216248
copyFileSync(sourcePath, targetPath)
217249

218250
// Verify the copy succeeded by comparing file sizes
219251
const sourceSize = statSync(sourcePath).size
220252
const targetSize = statSync(targetPath).size
253+
verbose(` Source size: ${sourceSize} bytes`)
254+
verbose(` Target size: ${targetSize} bytes`)
221255

222256
if (sourceSize !== targetSize) {
223257
throw new Error(
224258
`File size mismatch: source=${sourceSize} bytes, target=${targetSize} bytes`,
225259
)
226260
}
261+
verbose(` Size verification passed`)
227262

228263
// Validate content structure
264+
verbose(` Validating content structure...`)
229265
const validation = validateAgentContent(targetPath)
230266
if (!validation.valid) {
231267
throw new Error(`Invalid agent file content: ${validation.error}`)
232268
}
269+
verbose(` Validation passed`)
233270

234271
successes.push(file)
235272
console.log(` Installed: ${file}`)
@@ -243,6 +280,7 @@ function main() {
243280
}
244281

245282
// Print summary
283+
verbose(`Installation summary: ${successes.length} succeeded, ${failures.length} failed`)
246284
console.log("")
247285
if (successes.length > 0 && failures.length === 0) {
248286
console.log(

preuninstall.mjs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ const AGENTS_SOURCE_DIR = getAgentsSourceDir(packageRoot)
2323
/** Check for --dry-run flag in command line arguments */
2424
const DRY_RUN = process.argv.includes("--dry-run")
2525

26+
/** Check for --verbose flag in command line arguments */
27+
const VERBOSE = process.argv.includes("--verbose")
28+
29+
/**
30+
* Logs verbose output if --verbose flag is set.
31+
* @param {string} message - The message to log
32+
*/
33+
function verbose(message) {
34+
if (VERBOSE) {
35+
console.log(`[VERBOSE] ${message}`)
36+
}
37+
}
38+
2639
/**
2740
* Main entry point for the preuninstall script.
2841
*
@@ -45,19 +58,33 @@ function main() {
4558
const prefix = DRY_RUN ? "[DRY-RUN] " : ""
4659
console.log(`${prefix}opencode-plugin-opencoder: Removing agents...`)
4760

61+
verbose(`Package root: ${packageRoot}`)
62+
verbose(`Source directory: ${AGENTS_SOURCE_DIR}`)
63+
verbose(`Target directory: ${AGENTS_TARGET_DIR}`)
64+
verbose(`Dry run: ${DRY_RUN}`)
65+
4866
// Check if target directory exists
67+
verbose(`Checking if target directory exists...`)
4968
if (!existsSync(AGENTS_TARGET_DIR)) {
69+
verbose(`Target directory does not exist`)
5070
console.log(`${prefix} No agents directory found, nothing to remove`)
5171
return
5272
}
73+
verbose(`Target directory exists`)
5374

5475
// Get list of agents we installed (from source directory)
76+
verbose(`Checking if source directory exists...`)
5577
if (!existsSync(AGENTS_SOURCE_DIR)) {
78+
verbose(`Source directory does not exist`)
5679
console.log(`${prefix} Source agents directory not found, skipping cleanup`)
5780
return
5881
}
82+
verbose(`Source directory exists`)
5983

60-
const agentFiles = readdirSync(AGENTS_SOURCE_DIR).filter((f) => f.endsWith(".md"))
84+
const allFiles = readdirSync(AGENTS_SOURCE_DIR)
85+
verbose(`Files in source directory: ${allFiles.join(", ") || "(none)"}`)
86+
const agentFiles = allFiles.filter((f) => f.endsWith(".md"))
87+
verbose(`Markdown files to remove: ${agentFiles.length}`)
6188

6289
if (agentFiles.length === 0) {
6390
console.log(`${prefix} No agent files to remove`)
@@ -68,8 +95,11 @@ function main() {
6895

6996
for (const file of agentFiles) {
7097
const targetPath = join(AGENTS_TARGET_DIR, file)
98+
verbose(`Processing: ${file}`)
99+
verbose(` Target path: ${targetPath}`)
71100

72101
if (existsSync(targetPath)) {
102+
verbose(` File exists, removing...`)
73103
try {
74104
if (DRY_RUN) {
75105
console.log(`${prefix}Would remove: ${targetPath}`)
@@ -79,14 +109,19 @@ function main() {
79109
console.log(` Removed: ${file}`)
80110
removedCount++
81111
}
112+
verbose(` Successfully removed`)
82113
} catch (err) {
83114
const error = err instanceof Error ? err : new Error(String(err))
84115
const message = getErrorMessage(error, file, targetPath)
85116
console.error(`${prefix} Warning: Could not remove ${file}: ${message}`)
117+
verbose(` Error details: ${error.message}`)
86118
}
119+
} else {
120+
verbose(` File does not exist, skipping`)
87121
}
88122
}
89123

124+
verbose(`Removal summary: ${removedCount} files removed`)
90125
if (removedCount > 0) {
91126
console.log(`\n${prefix}opencode-plugin-opencoder: Removed ${removedCount} agent(s)`)
92127
} else {

0 commit comments

Comments
 (0)