forked from sveltejs/eslint-plugin-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkit-helpers.ts
35 lines (33 loc) · 1.11 KB
/
kit-helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import type { RuleContext } from "../../types"
import fs from "fs"
/**
* return true if it's a Svelte Kit page component.
* @param context
* @returns
*/
export function isKitPageComponent(context: RuleContext): boolean {
// return false if it's not a Svelte Kit page component.
const routes =
context.settings?.kit?.files?.routes?.replace(/^\//, "") ?? "src/routes"
const filePath = context
.getFilename()
.replace(context.getCwd?.() ?? "", "")
.replace(/^\//, "")
return filePath.startsWith(routes)
}
export const hasSvelteKit = (() => {
// Hack: if it runs on browser, it regards as Svelte Kit project.
if (!fs.readFileSync) return true
try {
const packageJson = JSON.parse(fs.readFileSync("./package.json", "utf8"))
// Hack: CI removes `@sveltejs/kit` and it returns false and test failed.
// So always it returns true if it runs on the package.
if (packageJson.name === "eslint-plugin-svelte") return true
return Boolean(
packageJson.dependencies["@sveltejs/kit"] ??
packageJson.devDependencies["@sveltejs/kit"],
)
} catch (_e) {
return false
}
})()