-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add postinstall script to generate declarations
- Loading branch information
1 parent
16d09d1
commit d240af5
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { existsSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs"; | ||
import { join, resolve } from "node:path"; | ||
|
||
const srcPath = "./src"; | ||
|
||
const files = readdirSync(srcPath, { recursive: true, withFileTypes: true }).filter(file => file.isFile()).map(({ parentPath, name }) => `${parentPath}/${name}`); | ||
|
||
for (const file of files) { | ||
if (!file.endsWith(".ts") && !file.endsWith(".svelte")) | ||
continue; | ||
|
||
const content = readFileSync(file, "utf-8"); | ||
const lines = content.split("\n"); | ||
|
||
for (const line of lines) { | ||
const importMatch = line.match(/import\s.*from\s*["'](.+)["']/); | ||
if (importMatch) { | ||
const modulePath = resolve(file, "..", importMatch[1]); | ||
|
||
if (existsSync(modulePath) && statSync(modulePath).isDirectory() && !existsSync(join(modulePath, "index.ts"))) { | ||
// generate dts | ||
writeFileSync(join(modulePath, "index.d.ts"), `export default {} as {${readdirSync(modulePath).map(name => `\n ${JSON.stringify(name)}: string;`).join("")}\n}`, "utf-8"); | ||
console.warn(`Generated ${modulePath}/index.d.ts`); | ||
} | ||
} | ||
} | ||
} |