| 94 | export const atBeginning: LineMatcher = () => true; |
| 95 | |
| 96 | export function insertLine(position: 'before' | 'after', search: LineMatcher, content: string | Buffer): FileUpdater { |
| 97 | const match = (content: Buffer | string): boolean => search(content.toString()); |
| 98 | return contents => { |
| 99 | let found = false; |
| 100 | let buffer = Buffer.from([]); |
| 101 | for (const line of eachLine(contents)) { |
| 102 | const bufferLine = typeof line === 'string' ? Buffer.from(line) : line; |
| 103 | if (position === 'after') { |
| 104 | buffer = Buffer.concat([buffer, bufferLine]); |
| 105 | } |
| 106 | if (!found && match(line)) { |
| 107 | found = true; |
| 108 | buffer = Buffer.concat([buffer, typeof content === 'string' ? Buffer.from(content) : content]); |
| 109 | } |
| 110 | if (position === 'before') { |
| 111 | buffer = Buffer.concat([buffer, bufferLine]); |
| 112 | } |
| 113 | } |
| 114 | return Promise.resolve(buffer); |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | export function insertLineBefore(search: LineMatcher, content: string | Buffer): FileUpdater { |
| 119 | return insertLine('before', search, content); |