Skip to content

Commit

Permalink
feat: provide line and column information when crawling files
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Jun 3, 2021
1 parent 9e4d278 commit 8e9d00a
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/core/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "path";
import * as dicojson from "./dicojson";
import globby from "globby";
import { SOURCES_EXTRACT_REGEX } from "../const";
import { ProjectKey } from "../types";

export const findSources = async (sources?: string[]): Promise<string[]> => {
if (!sources) {
Expand All @@ -15,12 +16,28 @@ export const findSources = async (sources?: string[]): Promise<string[]> => {
return paths;
};

export const crawlFile = (relativePath: string): string[] => {
export const crawlFile = (relativePath: string): ProjectKey[] => {
const absolutePath = path.join(process.cwd(), relativePath);

const blob = fs.readFileSync(absolutePath, "utf8");

const matches = blob.match(SOURCES_EXTRACT_REGEX);
const matches: ProjectKey[] = [];

const regex = new RegExp(SOURCES_EXTRACT_REGEX);
let line = 1;
let index = 0;
let match;
while ((match = regex.exec(blob))) {
const section = blob.slice(index, match.index).split("\n");
line += section.length - 1;
index = match.index;
matches.push({
key: match[0],
file: relativePath,
line,
column: (section.pop() || "").length + 1
});
}

return matches ? matches : [];
return matches;
};

0 comments on commit 8e9d00a

Please sign in to comment.