mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-09-01 00:07:06 +00:00
add type declarations (automatically generated)
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
import { Project, SyntaxKind } from "npm:ts-morph";
|
||||
|
||||
function processFile(filePath: string) {
|
||||
function processFile(filePath: string,origin: string, repoHash: string): string {
|
||||
const project = new Project();
|
||||
const sourceFile = project.addSourceFileAtPath(filePath);
|
||||
|
||||
// 0. insert a commit hash comment at the top of the file
|
||||
sourceFile.insertText(0, `// REPO: ${origin} Commit hash: ${repoHash}\n`);
|
||||
|
||||
// 1. Collect all 'any' type nodes in the file
|
||||
const anyTypeNodes = sourceFile.getDescendantsOfKind(SyntaxKind.AnyKeyword);
|
||||
const sourceText = sourceFile.getFullText();
|
||||
const lineBreak = sourceText.includes("\r\n") ? "\r\n" : "\n";
|
||||
const lines = sourceText.split(/\r?\n/);
|
||||
const targetLines = new Set<number>();
|
||||
let updated = false;
|
||||
|
||||
// 2. Collect the line numbers that contain 'any'
|
||||
anyTypeNodes.forEach((anyNode: any) => {
|
||||
@@ -26,16 +29,19 @@ function processFile(filePath: string) {
|
||||
if (line.includes("eslint-disable-line @typescript-eslint/no-explicit-any")) {
|
||||
continue;
|
||||
}
|
||||
lines[lineIndex] = `${line} // eslint-disable-line @typescript-eslint/no-explicit-any`;
|
||||
lines[lineIndex] = `${line} // eslint-disable-line @typescript-eslint/no-explicit-any -- Only type declaration`;
|
||||
updated = true;
|
||||
}
|
||||
|
||||
const updatedSourceText = lines.join(lineBreak);
|
||||
|
||||
if (updated) {
|
||||
console.log(`Processed file: ${filePath}`);
|
||||
}
|
||||
// Output the result
|
||||
return updatedSourceText;
|
||||
}
|
||||
|
||||
const targetDir = `./_types/`;
|
||||
const targetDir = `./_types`;
|
||||
|
||||
async function processDir(dirPath: string) {
|
||||
for await (const entry of Deno.readDir(dirPath)) {
|
||||
@@ -45,14 +51,26 @@ async function processDir(dirPath: string) {
|
||||
if (entry.isFile && entry.name.endsWith(".d.ts")) {
|
||||
const filePath = `${dirPath}/${entry.name}`;
|
||||
console.log(`Processing: ${filePath}`);
|
||||
const updatedContent = processFile(filePath);
|
||||
const updatedContent = processFile(filePath, repoRemoteOriginStr, gitCommitHashStr);
|
||||
// Write the file. To revert, regenerate it with npm run lib:build:types.
|
||||
await Deno.writeTextFile(filePath, updatedContent);
|
||||
|
||||
// console.log(`Updated content for ${filePath}:\n${updatedContent}\n`);
|
||||
console.log(`Processed: ${filePath}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const subDir = "./src/lib/";
|
||||
const repoRemoteOrigins = new Deno.Command("git", {
|
||||
args: ["remote", "get-url", "origin"],
|
||||
cwd: subDir,
|
||||
stdout: "piped",
|
||||
}).outputSync().stdout;
|
||||
const repoRemoteOriginStr = new TextDecoder().decode(repoRemoteOrigins).trim();
|
||||
console.log(`STAMP: Git remote origin: ${repoRemoteOriginStr}`);
|
||||
const gitCommitHashSub = new Deno.Command("git", {
|
||||
args: ["rev-parse", "--short", "HEAD"],
|
||||
cwd: subDir,
|
||||
stdout: "piped",
|
||||
}).outputSync().stdout;
|
||||
const gitCommitHashStr = new TextDecoder().decode(gitCommitHashSub).trim();
|
||||
console.log(`STAMP: Git commit hash: ${gitCommitHashStr}`);
|
||||
await processDir(targetDir);
|
||||
|
||||
Reference in New Issue
Block a user