Skip to content

Commit

Permalink
fix error with multiple copyAbove tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Roaders committed Jan 24, 2023
1 parent f3bcc78 commit 6bc516b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 31 deletions.
24 changes: 0 additions & 24 deletions src/helpers/insert-code.helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,30 +313,6 @@ describe(`(${insertCode.name}) insert-code.helper`, () => {
expect(result).toEqual(expectedContent);
});

it(`should should throw error if copyBelow and copyAbove are reversed`, async () => {
const fileContent = [beforeInsertionLine, insertBelowToken, insertCodeAboveDefault, afterInsertionLine].join(
'\n',
);

const fileLines = ['randomFirstLine', copyCodeAboveDefault, insertLineOne, copyCodeBelowDefault, insertLineTwo];

mockedFs.setupFunction('readFile', ((_path: string, callback: (err: Error | null, data: Buffer) => void) => {
callback(null, Buffer.from(fileLines.join(EOL)));
}) as any);

let error: Error | undefined;

try {
await insertCode({ fileContent, filePath: `${sampleDirName}/'originalFilePath.ts` }, createOptions());
} catch (e) {
error = e;
}

expect(error?.message).toEqual(
`The copyCodeAbove marker '// ts-command-line-args_write-markdown_copyCodeAbove' was found before the copyCodeBelow marker '// ts-command-line-args_write-markdown_copyCodeBelow'. The copyCodeBelow marked must be before the copyCodeAbove.`,
);
});

it(`should should only insert file content after copyBelow token`, async () => {
const fileContent = [beforeInsertionLine, insertBelowToken, insertCodeAboveDefault, afterInsertionLine].join(
'\n',
Expand Down
10 changes: 3 additions & 7 deletions src/helpers/insert-code.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,9 @@ async function loadLines(
const copyBelowIndex =
copyBelowMarker != null ? contentLines.findIndex(findLine(copyBelowMarker, snippetResult?.[1])) : -1;
const copyAboveIndex =
copyAboveMarker != null ? contentLines.findIndex((line) => line.indexOf(copyAboveMarker) === 0) : -1;

if (copyAboveIndex > -1 && copyBelowIndex > -1 && copyAboveIndex < copyBelowIndex) {
throw new Error(
`The copyCodeAbove marker '${options.copyCodeAbove}' was found before the copyCodeBelow marker '${options.copyCodeBelow}'. The copyCodeBelow marked must be before the copyCodeAbove.`,
);
}
copyAboveMarker != null
? contentLines.findIndex((line, index) => line.indexOf(copyAboveMarker) === 0 && index > copyBelowIndex)
: -1;

if (snippetResult != null && copyBelowIndex < 0) {
throw new Error(
Expand Down

0 comments on commit 6bc516b

Please sign in to comment.