Skip to content

Commit 48b567c

Browse files
feat(file): new implementation
Autofix, fix now!
1 parent 97b7f9a commit 48b567c

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/generators/file.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,28 @@ export const file = defineGenerator({
1010
const fullPath = resolvePath(args.src, { url, dir: config.dir });
1111
let contents = await readFile(fullPath, "utf8");
1212

13-
if (args.fromLine) {
14-
if (args.fromLine < 0) {
15-
throw new Error("line index can not be smaller than 1");
16-
}
17-
if (!args.toLine) {
18-
throw new Error("fromLine exists but toLine is not defined");
13+
if (args.lines) {
14+
if (!/^(\d+)?:(\d+)?$/.test(args.lines)) {
15+
throw new Error("invalid lines format");
1916
}
2017

18+
const [_startLine, _endLine] = args.lines.split(":");
19+
2120
const lines = contents.split("\n");
22-
if (args.toLine > lines.length) {
21+
22+
const startLine = _startLine === "" ? 0 : Number(_startLine);
23+
const endLine = _endLine === "" ? lines.length : Number(_endLine);
24+
25+
if (startLine < 1) {
26+
throw new Error("first line's index can not be smaller than 1");
27+
}
28+
if (endLine > lines.length) {
2329
throw new Error(
24-
`line ${args.toLine} exceeds the total line number of the file`,
30+
`line ${endLine} was specified while file ${fullPath} has only ${lines.length} lines`,
2531
);
2632
}
2733

28-
contents = lines.slice(args.fromLine - 1, args.toLine - 1).join("\n");
34+
contents = lines.slice(startLine - 1, endLine - 1).join("\n");
2935
}
3036

3137
if (args.code) {

test/fixture/OUTPUT.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ Programming can be hard. But fear not! With the power of copy-paste, you can con
191191

192192
When your code doesn't work, don't blame yourself. It's clearly the compiler's fault for not understanding your genius. Remember, the more error messages you get, the closer you are to becoming a programming master.
193193

194+
Why waste time solving problems when someone else has already done it for you? Stack Overflow is your best friend, your mentor, and your savior. Just make sure to upvote the answers that save your bacon.
195+
194196
<!-- /automd -->
195197

196198
## `contributors`

0 commit comments

Comments
 (0)