Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208#!/usr/bin/env node
/** Append utility for adding lines to files. This utility appends text lines to a specified file. It validates that the file exists and has write permissions before appending. Usage: task-append "my new line" --to myfile.md task-append "my new line1" --to myfile.md task-append "my new line1\nMy new line2" --to myfile.md task-append "my new line1" "my new line2" --to myfile.md @example ```bash # Append a single line task-append "my new line" --to myfile.md # Append multiple lines as separate arguments task-append "my new line1" "my new line2" --to myfile.md # Append multiline text task-append "my new line1\\nMy new line2" --to myfile.md ``` */
// TODO: deprecate Optiqueimport { object, } from '@optique/core/constructs';// TODO: deprecate Optiqueimport { multiple, } from '@optique/core/modifiers';// TODO: deprecate Optiqueimport { argument, option,} from '@optique/core/primitives';// TODO: deprecate Optiqueimport { string, } from '@optique/core/valueparser';// TODO: deprecate Optiqueimport { runSync, } from '@optique/run';import { constants, } from 'node:fs';import { access, appendFile,} from 'node:fs/promises';
/** Error messages for append operations */const ERROR_MESSAGES = { fileNotFound: function fileNotFound(path: string,): string { return `File not found: ${path}`; }, noWritePermission: function noWritePermission(path: string,): string { return `No write permission for file: ${path}`; }, noTextProvided: 'No text provided to append',} as const;
/** Node filesystem error code for absent paths. */const FILE_NOT_FOUND_ERROR_CODE = 'ENOENT';
/** Validates that a file exists and has write permissions. @param filePath - Absolute or relative path to validate @throws When file does not exist or lacks write permissions @example ```ts await validateFile('./output.md'); ``` */async function validateFile(filePath: string,): Promise<void> { try { await access( filePath, constants.W_OK, ); } catch (error) { if (!(Error.isError(error,))) throw new Error( ERROR_MESSAGES.noWritePermission(filePath,), { cause: error, }, );
if (!('code' in error)) throw new Error( ERROR_MESSAGES.noWritePermission(filePath,), { cause: error, }, );
/** Node filesystem error code attached to the failed permission check. */ const { code, } = error as { readonly code: unknown; }; if (code === FILE_NOT_FOUND_ERROR_CODE) throw new Error( ERROR_MESSAGES.fileNotFound(filePath,), { cause: error, }, );
throw new Error( ERROR_MESSAGES.noWritePermission(filePath,), { cause: error, }, ); }}
/** Options for {@link appendLinesToFile}. @example ```ts const options: AppendLinesToFileOptions = { filePath: './output.md', lines: ['line 1', 'line 2'], }; ``` */type AppendLinesToFileOptions = { /** Absolute or relative path to append to */ readonly filePath: string; /** Lines of text to append */ readonly lines: readonly string[];};
/** Appends lines to a file, joining them with newlines and adding a trailing newline. @param filePath - Absolute or relative path to append to @param lines - Lines of text to append @example ```ts await appendLinesToFile({ filePath: './output.md', lines: ['line 1', 'line 2'] }); ``` */async function appendLinesToFile({ filePath, lines,}: AppendLinesToFileOptions,): Promise<void> { /** Joined payload with a trailing newline so subsequent appends start on a fresh line. */ const content = `${lines.join('\n',)}\n`; await appendFile( filePath, content, );}
//region Parser definition: required --to option and variadic positional text lines
/** TODO: deprecate Optique Optique parser for the task-append CLI */const parser = object({ to: option( '-t', '--to', string(), ), lines: multiple( argument(string(),), ),},);
//endregion Parser definition
/** TODO: deprecate Optique Parsed CLI arguments from process.argv */const args = runSync( parser, { programName: 'task-append', help: 'option', },);
if (args.lines .length === 0) throw new Error(ERROR_MESSAGES.noTextProvided,);
// Validate the target fileawait validateFile(args.to,);
// Append all lines to the fileawait appendLinesToFile({ filePath: args.to, lines: args.lines,},);